diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index d27b6b3..41aedd8 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -12,6 +12,7 @@ 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.asteroids.systems.ScreenWrapSystem; import com.me.common.Screen; import com.me.common.ecs.Engine; import com.me.common.ecs.Entity; @@ -38,6 +39,7 @@ public class GameScreen extends Screen { engine.registerSystem(new PlayerInputSystem()); engine.registerSystem(new MovementSystem()); + engine.registerSystem(new ScreenWrapSystem()); engine.registerSystem(new ModelRenderSystem(graphics)); engine.ready(); diff --git a/core/src/com/me/asteroids/systems/ModelRenderSystem.java b/core/src/com/me/asteroids/systems/ModelRenderSystem.java index b09d1a6..a38de76 100644 --- a/core/src/com/me/asteroids/systems/ModelRenderSystem.java +++ b/core/src/com/me/asteroids/systems/ModelRenderSystem.java @@ -3,10 +3,8 @@ 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; @@ -15,7 +13,7 @@ public class ModelRenderSystem extends EntitySystem { private ShapeRenderer renderer; public ModelRenderSystem(Graphics graphics) { - super(ModelComponent.class, PositionComponent.class); + super(ModelComponent.class); this.renderer = graphics.getShapeRenderer(); } @@ -28,15 +26,8 @@ public class ModelRenderSystem extends EntitySystem { @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()); } diff --git a/core/src/com/me/asteroids/systems/MovementSystem.java b/core/src/com/me/asteroids/systems/MovementSystem.java index d472165..75b5fb9 100644 --- a/core/src/com/me/asteroids/systems/MovementSystem.java +++ b/core/src/com/me/asteroids/systems/MovementSystem.java @@ -1,7 +1,9 @@ package com.me.asteroids.systems; +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.PositionComponent; import com.me.asteroids.components.VelocityComponent; import com.me.common.ecs.Entity; @@ -33,6 +35,13 @@ public class MovementSystem extends EntitySystem { velocity.clamp(0, maxVelocity); position.add(tmp.set(velocity).scl(dt)); } + + ModelComponent modelComponent = entity.getComponent(ModelComponent.class); + if (modelComponent != null) { + Polygon model = modelComponent.model; + model.setPosition(position.x, position.y); + model.setRotation(positionComponent.rotation - 90); + } } } diff --git a/core/src/com/me/asteroids/systems/ScreenWrapSystem.java b/core/src/com/me/asteroids/systems/ScreenWrapSystem.java new file mode 100644 index 0000000..345438a --- /dev/null +++ b/core/src/com/me/asteroids/systems/ScreenWrapSystem.java @@ -0,0 +1,49 @@ +package com.me.asteroids.systems; + +import com.badlogic.gdx.math.Polygon; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; +import com.me.asteroids.Constants; +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 ScreenWrapSystem extends EntitySystem { + + public ScreenWrapSystem() { + super(PositionComponent.class, ModelComponent.class); + } + + @Override + public void processEntity(Entity entity, float dt) { + PositionComponent positionComponent = entity.getComponent(PositionComponent.class); + ModelComponent modelComponent = entity.getComponent(ModelComponent.class); + + Vector2 position = positionComponent.position; + Polygon model = modelComponent.model; + Rectangle aabb = model.getBoundingRectangle(); + + // Check top/bottom edges + float minY = aabb.y; + float maxY = minY + aabb.height; + if (minY > Constants.HEIGHT) { + position.y = position.y - maxY; + return; + } else if (maxY < 0) { + position.y = Constants.HEIGHT + (position.y - minY); + return; + } + + // Check right/left edges + float minX = aabb.x; + float maxX = minX + aabb.width; + if (maxX < 0) { + position.x = Constants.WIDTH + (position.x - minX); + } else if (minX > Constants.WIDTH) { + position.x = position.x - maxX; + + } + } + +}