From d7c55d72e34c4ffa6a78ddc80409b8fd5a3fc57c Mon Sep 17 00:00:00 2001 From: Matt Low Date: Fri, 24 Jan 2020 13:20:37 +0400 Subject: [PATCH] Set up ECS on GameScreen: register components and systems, add player --- .../com/me/asteroids/screens/GameScreen.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index 59f040c..d27b6b3 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -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