Add EntityFactory, player can now shoot bullets
Add DEBUG Constant + rendering of FPS when true
This commit is contained in:
parent
087b53564f
commit
74ba61ee53
@ -2,6 +2,8 @@ package com.me.asteroids;
|
|||||||
|
|
||||||
public class Constants {
|
public class Constants {
|
||||||
|
|
||||||
|
public static final boolean DEBUG = false;
|
||||||
|
|
||||||
public static final int WIDTH = 800;
|
public static final int WIDTH = 800;
|
||||||
public static final int HEIGHT = 600;
|
public static final int HEIGHT = 600;
|
||||||
|
|
||||||
|
73
core/src/com/me/asteroids/EntityFactory.java
Normal file
73
core/src/com/me/asteroids/EntityFactory.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package com.me.asteroids;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.math.Polygon;
|
||||||
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.me.asteroids.components.AccelerationComponent;
|
||||||
|
import com.me.asteroids.components.ModelComponent;
|
||||||
|
import com.me.asteroids.components.PlayerComponent;
|
||||||
|
import com.me.asteroids.components.PositionComponent;
|
||||||
|
import com.me.asteroids.components.VelocityComponent;
|
||||||
|
import com.me.common.ecs.Engine;
|
||||||
|
import com.me.common.ecs.Entity;
|
||||||
|
|
||||||
|
public class EntityFactory {
|
||||||
|
|
||||||
|
public static Entity createPlayer(Engine engine) {
|
||||||
|
PositionComponent position = new PositionComponent();
|
||||||
|
position.position = new Vector2(Constants.HALF_WIDTH, Constants.HALF_HEIGHT);
|
||||||
|
position.rotation = 90;
|
||||||
|
|
||||||
|
VelocityComponent velocity = new VelocityComponent();
|
||||||
|
velocity.velocity = new Vector2(0f, 0f);
|
||||||
|
velocity.maxVelocity = 400f;
|
||||||
|
|
||||||
|
ModelComponent model = new ModelComponent();
|
||||||
|
model.model = new Polygon(new float[] {
|
||||||
|
0f, 4f, // tip
|
||||||
|
-2.5f, -4f, // bottom left
|
||||||
|
-1f, -2.5f, // indent
|
||||||
|
1f, -2.5f, // indent
|
||||||
|
2.5f, -4f, // bottom right
|
||||||
|
});
|
||||||
|
model.model.scale(5);
|
||||||
|
|
||||||
|
AccelerationComponent accel = new AccelerationComponent();
|
||||||
|
accel.acceleration = new Vector2(0,1f);
|
||||||
|
|
||||||
|
Entity player = engine.createEntity();
|
||||||
|
player.addComponent(position);
|
||||||
|
player.addComponent(velocity);
|
||||||
|
player.addComponent(model);
|
||||||
|
player.addComponent(accel);
|
||||||
|
player.addComponent(new PlayerComponent());
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Entity createBullet(Engine engine, Entity player) {
|
||||||
|
PositionComponent position = new PositionComponent();
|
||||||
|
float[] modelVertices = player.getComponent(ModelComponent.class).model.getTransformedVertices();
|
||||||
|
position.position = new Vector2(modelVertices[0], modelVertices[1]);
|
||||||
|
position.rotation = player.getComponent(PositionComponent.class).rotation;
|
||||||
|
|
||||||
|
VelocityComponent velocity = new VelocityComponent();
|
||||||
|
velocity.velocity = Utils.setUnitVectorAngle(new Vector2(), position.rotation).scl(500);
|
||||||
|
|
||||||
|
ModelComponent model = new ModelComponent();
|
||||||
|
model.model = new Polygon(new float[] {
|
||||||
|
-1f, 2f,
|
||||||
|
1f, 2f,
|
||||||
|
1f, -2f,
|
||||||
|
-1f, -2f,
|
||||||
|
});
|
||||||
|
model.model.setRotation(position.rotation);
|
||||||
|
model.model.setPosition(position.position.x, position.position.x);
|
||||||
|
|
||||||
|
|
||||||
|
Entity bullet = engine.createEntity();
|
||||||
|
bullet.addComponent(position);
|
||||||
|
bullet.addComponent(velocity);
|
||||||
|
bullet.addComponent(model);
|
||||||
|
return bullet;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,11 @@
|
|||||||
package com.me.asteroids.screens;
|
package com.me.asteroids.screens;
|
||||||
|
|
||||||
import com.badlogic.gdx.math.Polygon;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.me.asteroids.Constants;
|
import com.me.asteroids.Constants;
|
||||||
|
import com.me.asteroids.EntityFactory;
|
||||||
import com.me.asteroids.Graphics;
|
import com.me.asteroids.Graphics;
|
||||||
import com.me.asteroids.components.AccelerationComponent;
|
import com.me.asteroids.components.AccelerationComponent;
|
||||||
import com.me.asteroids.components.ModelComponent;
|
import com.me.asteroids.components.ModelComponent;
|
||||||
@ -23,12 +26,19 @@ public class GameScreen extends Screen {
|
|||||||
|
|
||||||
Graphics graphics;
|
Graphics graphics;
|
||||||
|
|
||||||
|
SpriteBatch batch;
|
||||||
|
BitmapFont font;
|
||||||
|
|
||||||
public GameScreen(Graphics graphics) {
|
public GameScreen(Graphics graphics) {
|
||||||
this.graphics = graphics;
|
this.graphics = graphics;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setup() {
|
public void setup() {
|
||||||
|
batch = new SpriteBatch();
|
||||||
|
font = new BitmapFont();
|
||||||
|
font.setColor(Color.RED);
|
||||||
|
|
||||||
engine = new Engine();
|
engine = new Engine();
|
||||||
|
|
||||||
engine.registerComponentClass(PlayerComponent.class);
|
engine.registerComponentClass(PlayerComponent.class);
|
||||||
@ -44,36 +54,7 @@ public class GameScreen extends Screen {
|
|||||||
|
|
||||||
engine.ready();
|
engine.ready();
|
||||||
|
|
||||||
Entity player = engine.createEntity();
|
Entity player = EntityFactory.createPlayer(engine);
|
||||||
|
|
||||||
PositionComponent position = new PositionComponent();
|
|
||||||
position.position = new Vector2(Constants.HALF_WIDTH, Constants.HALF_HEIGHT);
|
|
||||||
position.rotation = 90;
|
|
||||||
player.addComponent(position);
|
|
||||||
|
|
||||||
VelocityComponent velocity = new VelocityComponent();
|
|
||||||
velocity.velocity = new Vector2(0f, 0f);
|
|
||||||
velocity.maxVelocity = 400f;
|
|
||||||
player.addComponent(velocity);
|
|
||||||
|
|
||||||
ModelComponent shape = new ModelComponent();
|
|
||||||
float[] vertices = new float[] {
|
|
||||||
0f, 4f, // tip
|
|
||||||
-2.5f, -4f, // bottom left
|
|
||||||
-1f, -2.5f, // indent
|
|
||||||
1f, -2.5f, // indent
|
|
||||||
2.5f, -4f, // bottom right
|
|
||||||
};
|
|
||||||
shape.model = new Polygon(vertices);
|
|
||||||
shape.model.scale(5);
|
|
||||||
player.addComponent(shape);
|
|
||||||
|
|
||||||
AccelerationComponent accel = new AccelerationComponent();
|
|
||||||
accel.acceleration = new Vector2(0,1f);
|
|
||||||
player.addComponent(accel);
|
|
||||||
|
|
||||||
player.addComponent(new PlayerComponent());
|
|
||||||
|
|
||||||
player.activate();
|
player.activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +62,12 @@ public class GameScreen extends Screen {
|
|||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
graphics.reset();
|
graphics.reset();
|
||||||
engine.update(dt);
|
engine.update(dt);
|
||||||
|
|
||||||
|
if (Constants.DEBUG) {
|
||||||
|
batch.begin();
|
||||||
|
font.draw(batch, String.format("FPS: %d, Entities: %d", Gdx.graphics.getFramesPerSecond(), engine.getEntityCount()), 50, 50);
|
||||||
|
batch.end();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -3,6 +3,7 @@ package com.me.asteroids.systems;
|
|||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.Input;
|
import com.badlogic.gdx.Input;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.me.asteroids.EntityFactory;
|
||||||
import com.me.asteroids.Utils;
|
import com.me.asteroids.Utils;
|
||||||
import com.me.asteroids.components.AccelerationComponent;
|
import com.me.asteroids.components.AccelerationComponent;
|
||||||
import com.me.asteroids.components.PlayerComponent;
|
import com.me.asteroids.components.PlayerComponent;
|
||||||
@ -60,6 +61,11 @@ public class PlayerInputSystem extends EntitySystem {
|
|||||||
acceleration.set(0, 0);
|
acceleration.set(0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
|
||||||
|
Entity bullet = EntityFactory.createBullet(engine, entity);
|
||||||
|
bullet.activate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user