Add screen wrapping system

Move model position/rotation updating to movement system
This commit is contained in:
Matt Low 2020-01-24 17:22:00 +04:00
parent d7c55d72e3
commit 3d9b62d455
4 changed files with 61 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import com.me.asteroids.components.VelocityComponent;
import com.me.asteroids.systems.ModelRenderSystem; import com.me.asteroids.systems.ModelRenderSystem;
import com.me.asteroids.systems.MovementSystem; import com.me.asteroids.systems.MovementSystem;
import com.me.asteroids.systems.PlayerInputSystem; import com.me.asteroids.systems.PlayerInputSystem;
import com.me.asteroids.systems.ScreenWrapSystem;
import com.me.common.Screen; import com.me.common.Screen;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
@ -38,6 +39,7 @@ public class GameScreen extends Screen {
engine.registerSystem(new PlayerInputSystem()); engine.registerSystem(new PlayerInputSystem());
engine.registerSystem(new MovementSystem()); engine.registerSystem(new MovementSystem());
engine.registerSystem(new ScreenWrapSystem());
engine.registerSystem(new ModelRenderSystem(graphics)); engine.registerSystem(new ModelRenderSystem(graphics));
engine.ready(); engine.ready();

View File

@ -3,10 +3,8 @@ package com.me.asteroids.systems;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Polygon; import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.Graphics; import com.me.asteroids.Graphics;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PositionComponent;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
@ -15,7 +13,7 @@ public class ModelRenderSystem extends EntitySystem {
private ShapeRenderer renderer; private ShapeRenderer renderer;
public ModelRenderSystem(Graphics graphics) { public ModelRenderSystem(Graphics graphics) {
super(ModelComponent.class, PositionComponent.class); super(ModelComponent.class);
this.renderer = graphics.getShapeRenderer(); this.renderer = graphics.getShapeRenderer();
} }
@ -28,15 +26,8 @@ public class ModelRenderSystem extends EntitySystem {
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
ModelComponent modelComponent = entity.getComponent(ModelComponent.class); ModelComponent modelComponent = entity.getComponent(ModelComponent.class);
PositionComponent positionComponent = entity.getComponent(PositionComponent.class);
Polygon model = modelComponent.model; 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()); renderer.polygon(model.getTransformedVertices());
} }

View File

@ -1,7 +1,9 @@
package com.me.asteroids.systems; package com.me.asteroids.systems;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent; import com.me.asteroids.components.VelocityComponent;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
@ -33,6 +35,13 @@ public class MovementSystem extends EntitySystem {
velocity.clamp(0, maxVelocity); velocity.clamp(0, maxVelocity);
position.add(tmp.set(velocity).scl(dt)); 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);
}
} }
} }

View File

@ -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;
}
}
}