|
package com.carterza.system.logic; |
|
|
|
import com.artemis.Aspect; |
|
import com.artemis.BaseSystem; |
|
import com.artemis.ComponentMapper; |
|
import com.artemis.Entity; |
|
import com.artemis.systems.IteratingSystem; |
|
import com.badlogic.gdx.math.Vector2; |
|
import com.badlogic.gdx.physics.box2d.*; |
|
import com.carterza.component.PhysicsComponent; |
|
import com.carterza.component.PositionComponent; |
|
import com.carterza.entity.EntityFactory; |
|
import com.carterza.event.WeaponFiredEvent; |
|
import com.carterza.system.physics.PhysicsSystem; |
|
import com.carterza.system.player.PlayerSystem; |
|
import net.mostlyoriginal.api.event.common.Subscribe; |
|
|
|
import java.util.ArrayList; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
/** |
|
* Created by zachcarter on 11/11/16. |
|
*/ |
|
public class BulletSystem extends BaseSystem { |
|
|
|
private PlayerSystem playerSystem; |
|
private PhysicsSystem physicsSystem; |
|
private ComponentMapper<PositionComponent> positionComponentMapper; |
|
private ComponentMapper<PhysicsComponent> physicsComponentMapper; |
|
private List<Entity> bullets; |
|
|
|
/** |
|
* Creates a new EntityProcessingSystem. |
|
* |
|
* @param aspect the aspect to match entities |
|
*/ |
|
public BulletSystem() { |
|
bullets = new ArrayList<>(); |
|
} |
|
|
|
@Subscribe |
|
void handleEvent(WeaponFiredEvent event) { |
|
Vector2 position = positionComponentMapper.get(playerSystem.getPlayer().getId()).getPosition(); |
|
Entity bullet = EntityFactory.createBullet(world, position.x-1, position.y); |
|
bullets.add(bullet); |
|
PhysicsComponent physicsComponent = physicsComponentMapper.get(bullet.getId()); |
|
PositionComponent positionComponent = positionComponentMapper.get(bullet.getId()); |
|
|
|
BodyDef bodyDef = new BodyDef(); |
|
bodyDef.type = BodyDef.BodyType.DynamicBody; |
|
|
|
// Set our body to the same position as our sprite |
|
bodyDef.position.set(positionComponent.getPosition().x-1, positionComponent.getPosition().y); |
|
bodyDef.fixedRotation = true; |
|
// Create a body in the world using our definition |
|
Body body = physicsSystem.getPhysics().createBody(bodyDef); |
|
physicsComponent.setBody(body); |
|
body.setBullet(true); |
|
|
|
|
|
CircleShape shape = new CircleShape(); |
|
shape.setRadius(.1f); |
|
|
|
|
|
FixtureDef fixtureDef = new FixtureDef(); |
|
fixtureDef.shape = shape; |
|
fixtureDef.density = .1f; |
|
fixtureDef.friction = 0; |
|
|
|
physicsComponent.setFixture(physicsComponent.getBody().createFixture(fixtureDef)); |
|
|
|
//body.applyForce(new Vector2(-.1f, 0), body.getWorldCenter(), false); |
|
|
|
// Shape is the only disposable of the lot, so get rid of it |
|
shape.dispose(); |
|
} |
|
|
|
@Override |
|
protected void processSystem() { |
|
Iterator<Entity> bulletIterator = bullets.iterator(); |
|
while(bulletIterator.hasNext()) { |
|
Entity bullet = bulletIterator.next(); |
|
PhysicsComponent physicsComponent = physicsComponentMapper.get(bullet.getId()); |
|
Map<String, Boolean> userData = (Map<String, Boolean>) physicsComponent.getBody().getUserData(); |
|
if(userData != null) { |
|
if(userData.get("deleteFlag")) physicsSystem.getPhysics().destroyBody(physicsComponent.getBody()); |
|
} |
|
physicsComponent.getBody().setLinearVelocity(new Vector2(-.1f, 0)); |
|
} |
|
} |
|
} |