Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Created November 19, 2016 02:32
Show Gist options
  • Save zacharycarter/30fcae41404e4dd5428cbf67fcfa6dbb to your computer and use it in GitHub Desktop.
Save zacharycarter/30fcae41404e4dd5428cbf67fcfa6dbb to your computer and use it in GitHub Desktop.
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));
}
}
}
package com.carterza.system.physics;
import com.artemis.Aspect;
import com.artemis.BaseSystem;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.annotations.Wire;
import com.artemis.systems.IteratingSystem;
import com.badlogic.gdx.Gdx;
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.component.TurnComponent;
import com.carterza.physics.CollisionHandler;
import com.carterza.strategy.LogicRenderMarker;
import net.mostlyoriginal.api.system.core.PassiveSystem;
@Wire
public class PhysicsSystem extends IteratingSystem implements LogicRenderMarker {
World physics;
ComponentMapper<PositionComponent> positionComponentMapper;
ComponentMapper<PhysicsComponent> physicsComponentMapper;
/**
* Creates a new EntityProcessingSystem.
*
* @param aspect the aspect to match entities
*/
public PhysicsSystem() {
super(Aspect.all(PhysicsComponent.class, PositionComponent.class));
}
@Override
protected void initialize() {
physics = new World(new Vector2(0, 0), true);
physics.setContactListener(new CollisionHandler());
}
public World getPhysics() {
return this.physics;
}
@Override
protected void process(int entityId) {
Entity e = world.getEntity(entityId);
PositionComponent positionComponent = positionComponentMapper.get(entityId);
physicsComponentMapper.create(entityId);
PhysicsComponent physicsComponent = physicsComponentMapper.get(entityId);
if(physicsComponent.getBody() == null) {
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+12)/24, (positionComponent.getPosition().y+12)/24);
bodyDef.position.set((positionComponent.getPosition().x + .5f), (positionComponent.getPosition().y + .5f));
bodyDef.fixedRotation = true;
// Create a body in the world using our definition
physicsComponent.setBody(physics.createBody(bodyDef));
PolygonShape shape = new PolygonShape();
shape.setAsBox(.5f, .5f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
physicsComponent.setFixture(physicsComponent.getBody().createFixture(fixtureDef));
// Shape is the only disposable of the lot, so get rid of it
shape.dispose();
}
physics.step(Gdx.graphics.getDeltaTime(), 6, 2);
positionComponent.setPosition(new Vector2((physicsComponent.getBody().getPosition().x), (physicsComponent.getBody().getPosition().y)));
}
@Override
protected void dispose() {
this.physics.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment