From 6fb664adaf2320436e53b53258f9d07ae57aed5d Mon Sep 17 00:00:00 2001 From: Matt Low Date: Fri, 24 Jan 2020 13:16:21 +0400 Subject: [PATCH] Add player/model components + input/render systems --- .../asteroids/components/ModelComponent.java | 10 ++++ .../asteroids/components/PlayerComponent.java | 7 +++ .../asteroids/systems/ModelRenderSystem.java | 48 ++++++++++++++++ .../asteroids/systems/PlayerInputSystem.java | 55 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 core/src/com/me/asteroids/components/ModelComponent.java create mode 100644 core/src/com/me/asteroids/components/PlayerComponent.java create mode 100644 core/src/com/me/asteroids/systems/ModelRenderSystem.java create mode 100644 core/src/com/me/asteroids/systems/PlayerInputSystem.java diff --git a/core/src/com/me/asteroids/components/ModelComponent.java b/core/src/com/me/asteroids/components/ModelComponent.java new file mode 100644 index 0000000..57e4c69 --- /dev/null +++ b/core/src/com/me/asteroids/components/ModelComponent.java @@ -0,0 +1,10 @@ +package com.me.asteroids.components; + +import com.badlogic.gdx.math.Polygon; +import com.me.common.ecs.Component; + +public class ModelComponent extends Component { + + public Polygon model; + +} diff --git a/core/src/com/me/asteroids/components/PlayerComponent.java b/core/src/com/me/asteroids/components/PlayerComponent.java new file mode 100644 index 0000000..6d1ee62 --- /dev/null +++ b/core/src/com/me/asteroids/components/PlayerComponent.java @@ -0,0 +1,7 @@ +package com.me.asteroids.components; + +import com.me.common.ecs.Component; + +public class PlayerComponent extends Component { + // TODO: implement engine feature for tagging entities (as player, for e.g.) +} diff --git a/core/src/com/me/asteroids/systems/ModelRenderSystem.java b/core/src/com/me/asteroids/systems/ModelRenderSystem.java new file mode 100644 index 0000000..b09d1a6 --- /dev/null +++ b/core/src/com/me/asteroids/systems/ModelRenderSystem.java @@ -0,0 +1,48 @@ +package com.me.asteroids.systems; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Polygon; +import com.badlogic.gdx.math.Vector2; +import com.me.asteroids.Graphics; +import com.me.asteroids.components.ModelComponent; +import com.me.asteroids.components.PositionComponent; +import com.me.common.ecs.Entity; +import com.me.common.ecs.EntitySystem; + +public class ModelRenderSystem extends EntitySystem { + + private ShapeRenderer renderer; + + public ModelRenderSystem(Graphics graphics) { + super(ModelComponent.class, PositionComponent.class); + this.renderer = graphics.getShapeRenderer(); + } + + @Override + public void preProcessing() { + renderer.setColor(Color.WHITE); + renderer.begin(ShapeRenderer.ShapeType.Line); + } + + @Override + public void processEntity(Entity entity, float dt) { + ModelComponent modelComponent = entity.getComponent(ModelComponent.class); + PositionComponent positionComponent = entity.getComponent(PositionComponent.class); + + Polygon model = modelComponent.model; + Vector2 position = positionComponent.position; + float rotation = positionComponent.rotation; + + model.setPosition(position.x, position.y); + model.setRotation(rotation - 90); + + renderer.polygon(model.getTransformedVertices()); + } + + @Override + public void postProcessing() { + renderer.end(); + } + +} diff --git a/core/src/com/me/asteroids/systems/PlayerInputSystem.java b/core/src/com/me/asteroids/systems/PlayerInputSystem.java new file mode 100644 index 0000000..dc597aa --- /dev/null +++ b/core/src/com/me/asteroids/systems/PlayerInputSystem.java @@ -0,0 +1,55 @@ +package com.me.asteroids.systems; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.math.Vector2; +import com.me.asteroids.Utils; +import com.me.asteroids.components.AccelerationComponent; +import com.me.asteroids.components.PlayerComponent; +import com.me.asteroids.components.PositionComponent; +import com.me.asteroids.components.VelocityComponent; +import com.me.common.ecs.Entity; +import com.me.common.ecs.EntitySystem; + +public class PlayerInputSystem extends EntitySystem { + + public Vector2 tmp = new Vector2(0, 1); + + public PlayerInputSystem() { + super( + PositionComponent.class, + VelocityComponent.class, + AccelerationComponent.class, + PlayerComponent.class + ); + } + + @Override + public void processEntity(Entity entity, float dt) { + PositionComponent positionComponent = entity.getComponent(PositionComponent.class); + VelocityComponent velocityComponent = entity.getComponent(VelocityComponent.class); + AccelerationComponent accelComponent = entity.getComponent(AccelerationComponent.class); + + if (Gdx.input.isKeyPressed(Input.Keys.D)) { + positionComponent.rotation = Utils.rotate(positionComponent.rotation, -5); + } + + if (Gdx.input.isKeyPressed(Input.Keys.A)) { + positionComponent.rotation = Utils.rotate(positionComponent.rotation, 5); + } + + Vector2 acceleration = accelComponent.acceleration; + Vector2 velocity = velocityComponent.velocity; + + if (Gdx.input.isKeyPressed(Input.Keys.W)) { + acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(500)); + } else { + if (!velocity.isZero(1f)) { + acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-100)); + } else { + acceleration.set(0, 0); + } + } + } + +}