From 74ba61ee53e7bd2525a07c4bd7fce58eae80ce23 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 25 Jan 2020 17:26:04 +0400 Subject: [PATCH] Add EntityFactory, player can now shoot bullets Add DEBUG Constant + rendering of FPS when true --- core/src/com/me/asteroids/Constants.java | 2 + core/src/com/me/asteroids/EntityFactory.java | 73 +++++++++++++++++++ .../com/me/asteroids/screens/GameScreen.java | 51 +++++-------- .../asteroids/systems/PlayerInputSystem.java | 6 ++ 4 files changed, 100 insertions(+), 32 deletions(-) create mode 100644 core/src/com/me/asteroids/EntityFactory.java diff --git a/core/src/com/me/asteroids/Constants.java b/core/src/com/me/asteroids/Constants.java index 06c99e3..1bd5a2f 100644 --- a/core/src/com/me/asteroids/Constants.java +++ b/core/src/com/me/asteroids/Constants.java @@ -2,6 +2,8 @@ package com.me.asteroids; public class Constants { + public static final boolean DEBUG = false; + public static final int WIDTH = 800; public static final int HEIGHT = 600; diff --git a/core/src/com/me/asteroids/EntityFactory.java b/core/src/com/me/asteroids/EntityFactory.java new file mode 100644 index 0000000..32860a9 --- /dev/null +++ b/core/src/com/me/asteroids/EntityFactory.java @@ -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; + } + +} diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index c3de16d..598b380 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -1,8 +1,11 @@ package com.me.asteroids.screens; -import com.badlogic.gdx.math.Polygon; -import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.Gdx; +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.EntityFactory; import com.me.asteroids.Graphics; import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.ModelComponent; @@ -23,12 +26,19 @@ public class GameScreen extends Screen { Graphics graphics; + SpriteBatch batch; + BitmapFont font; + public GameScreen(Graphics graphics) { this.graphics = graphics; } @Override public void setup() { + batch = new SpriteBatch(); + font = new BitmapFont(); + font.setColor(Color.RED); + engine = new Engine(); engine.registerComponentClass(PlayerComponent.class); @@ -44,36 +54,7 @@ public class GameScreen extends Screen { 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()); - + Entity player = EntityFactory.createPlayer(engine); player.activate(); } @@ -81,6 +62,12 @@ public class GameScreen extends Screen { public void update(float dt) { graphics.reset(); 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 diff --git a/core/src/com/me/asteroids/systems/PlayerInputSystem.java b/core/src/com/me/asteroids/systems/PlayerInputSystem.java index 39b3daf..af2f3d8 100644 --- a/core/src/com/me/asteroids/systems/PlayerInputSystem.java +++ b/core/src/com/me/asteroids/systems/PlayerInputSystem.java @@ -3,6 +3,7 @@ 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.EntityFactory; import com.me.asteroids.Utils; import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.PlayerComponent; @@ -60,6 +61,11 @@ public class PlayerInputSystem extends EntitySystem { acceleration.set(0, 0); } } + + if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) { + Entity bullet = EntityFactory.createBullet(engine, entity); + bullet.activate(); + } } }