Set up ECS on GameScreen: register components and systems, add player

This commit is contained in:
Matt Low 2020-01-24 13:20:37 +04:00
parent 6fb664adaf
commit d7c55d72e3

View File

@ -1,10 +1,25 @@
package com.me.asteroids.screens;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.Constants;
import com.me.asteroids.Graphics;
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.asteroids.systems.ModelRenderSystem;
import com.me.asteroids.systems.MovementSystem;
import com.me.asteroids.systems.PlayerInputSystem;
import com.me.common.Screen;
import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity;
public class GameScreen extends Screen {
Engine engine;
Graphics graphics;
public GameScreen(Graphics graphics) {
@ -13,12 +28,57 @@ public class GameScreen extends Screen {
@Override
public void setup() {
engine = new Engine();
engine.registerComponentClass(PlayerComponent.class);
engine.registerComponentClass(PositionComponent.class);
engine.registerComponentClass(VelocityComponent.class);
engine.registerComponentClass(AccelerationComponent.class);
engine.registerComponentClass(ModelComponent.class);
engine.registerSystem(new PlayerInputSystem());
engine.registerSystem(new MovementSystem());
engine.registerSystem(new ModelRenderSystem(graphics));
engine.ready();
Entity player = engine.createEntity();
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();
}
@Override
public void update(float dt) {
graphics.reset();
engine.update(dt);
}
@Override