Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Last active November 18, 2016 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacharycarter/16c927f4c95835e4db167ad698f27a03 to your computer and use it in GitHub Desktop.
Save zacharycarter/16c927f4c95835e4db167ad698f27a03 to your computer and use it in GitHub Desktop.
@Subscribe
void handleEvent(WeaponFiredEvent event) {
Vector2 position = positionComponentMapper.get(playerSystem.getPlayer().getId()).getPosition();
Entity bullet = EntityFactory.createBullet(world, position.x, 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-24, positionComponent.getPosition().y+12);
bodyDef.fixedRotation = true;
// Create a body in the world using our definition
Body body = physicsSystem.getPhysics().createBody(bodyDef);
physicsComponent.setBody(body);
body.setBullet(true);
body.setGravityScale(0);
body.setLinearDamping(0);
CircleShape shape = new CircleShape();
shape.setRadius(1);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.friction = 0;
physicsComponent.setFixture(physicsComponent.getBody().createFixture(fixtureDef));
body.applyLinearImpulse(new Vector2(-10000000f, 0), body.getWorldCenter(), false);
// Shape is the only disposable of the lot, so get rid of it
shape.dispose();
}
@Override
protected void initialize() {
physics = new World(new Vector2(0, 0), true);
physics.setContactListener(new CollisionHandler());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment