Delete bullets when they leave the screen

This commit is contained in:
Matt Low 2020-01-25 17:55:23 +04:00
parent b9148c028d
commit 28dbff3d16
5 changed files with 58 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package com.me.asteroids;
import com.badlogic.gdx.math.Polygon; 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.BulletComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PlayerComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
@ -67,6 +68,7 @@ public class EntityFactory {
bullet.addComponent(position); bullet.addComponent(position);
bullet.addComponent(velocity); bullet.addComponent(velocity);
bullet.addComponent(model); bullet.addComponent(model);
bullet.addComponent(new BulletComponent());
return bullet; return bullet;
} }

View File

@ -0,0 +1,6 @@
package com.me.asteroids.components;
import com.me.common.ecs.Component;
public class BulletComponent implements Component {
}

View File

@ -0,0 +1,18 @@
package com.me.asteroids.events;
import com.me.common.ecs.Entity;
import com.me.common.ecs.event.Event;
/**
* Called when an entity is about to wrap around the screen.
* If cancelled, don't perform the wrapping.
*/
public class ScreenWrapEvent extends Event {
public Entity entity;
public ScreenWrapEvent(Entity entity) {
this.entity = entity;
}
}

View File

@ -8,10 +8,12 @@ import com.me.asteroids.Constants;
import com.me.asteroids.EntityFactory; import com.me.asteroids.EntityFactory;
import com.me.asteroids.Graphics; import com.me.asteroids.Graphics;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.BulletComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PlayerComponent;
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.asteroids.events.ScreenWrapEvent;
import com.me.asteroids.systems.CollisionSystem; import com.me.asteroids.systems.CollisionSystem;
import com.me.asteroids.systems.ModelRenderSystem; import com.me.asteroids.systems.ModelRenderSystem;
import com.me.asteroids.systems.MovementSystem; import com.me.asteroids.systems.MovementSystem;
@ -20,8 +22,10 @@ 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;
import com.me.common.ecs.event.EventHandler;
import com.me.common.ecs.event.Listener;
public class GameScreen extends Screen { public class GameScreen extends Screen implements Listener {
Engine engine; Engine engine;
@ -43,6 +47,7 @@ public class GameScreen extends Screen {
engine = new Engine(); engine = new Engine();
engine.registerComponentClass(PlayerComponent.class); engine.registerComponentClass(PlayerComponent.class);
engine.registerComponentClass(BulletComponent.class);
engine.registerComponentClass(PositionComponent.class); engine.registerComponentClass(PositionComponent.class);
engine.registerComponentClass(VelocityComponent.class); engine.registerComponentClass(VelocityComponent.class);
engine.registerComponentClass(AccelerationComponent.class); engine.registerComponentClass(AccelerationComponent.class);
@ -54,6 +59,8 @@ public class GameScreen extends Screen {
engine.registerSystem(new ScreenWrapSystem(engine)); engine.registerSystem(new ScreenWrapSystem(engine));
engine.registerSystem(new ModelRenderSystem(engine, graphics)); engine.registerSystem(new ModelRenderSystem(engine, graphics));
engine.registerListener(this);
engine.ready(); engine.ready();
Entity player = EntityFactory.createPlayer(engine); Entity player = EntityFactory.createPlayer(engine);
@ -77,4 +84,14 @@ public class GameScreen extends Screen {
} }
@EventHandler
public void onScreenWrap(ScreenWrapEvent event) {
if (event.entity.hasComponent(BulletComponent.class)) {
// Remove bullets when they leave the screen
event.setCancelled(true);
event.entity.remove();
}
}
} }

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.Constants; import com.me.asteroids.Constants;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.events.ScreenWrapEvent;
import com.me.common.ecs.ComponentMapper; import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
@ -21,6 +22,15 @@ public class ScreenWrapSystem extends EntitySystem {
modelMapper = engine.getComponentMapper(ModelComponent.class); modelMapper = engine.getComponentMapper(ModelComponent.class);
} }
private void updatePosition(Entity entity, Vector2 position, float newX, float newY) {
ScreenWrapEvent event = new ScreenWrapEvent(entity);
engine.callEvent(event);
if (!event.isCancelled()) {
position.set(newX, newY);
}
}
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
PositionComponent positionComponent = positionMapper.get(entity); PositionComponent positionComponent = positionMapper.get(entity);
@ -33,10 +43,10 @@ public class ScreenWrapSystem extends EntitySystem {
float minY = aabb.y; float minY = aabb.y;
float maxY = minY + aabb.height; float maxY = minY + aabb.height;
if (minY > Constants.HEIGHT) { if (minY > Constants.HEIGHT) {
position.y = (position.y - maxY) + (minY - Constants.HEIGHT); updatePosition(entity, position, position.x, (position.y - maxY) + (minY - Constants.HEIGHT));
return; return;
} else if (maxY < 0) { } else if (maxY < 0) {
position.y = Constants.HEIGHT + (position.y - minY) + maxY; updatePosition(entity, position, position.x, Constants.HEIGHT + (position.y - minY) + maxY);
return; return;
} }
@ -44,9 +54,9 @@ public class ScreenWrapSystem extends EntitySystem {
float minX = aabb.x; float minX = aabb.x;
float maxX = minX + aabb.width; float maxX = minX + aabb.width;
if (maxX < 0) { if (maxX < 0) {
position.x = Constants.WIDTH + (position.x - minX) + maxX; updatePosition(entity, position, Constants.WIDTH + (position.x - minX) + maxX, position.y);
} else if (minX > Constants.WIDTH) { } else if (minX > Constants.WIDTH) {
position.x = (position.x - maxX) + (minX - Constants.WIDTH); updatePosition(entity, position, (position.x - maxX) + (minX - Constants.WIDTH), position.y);
} }
} }