Last active
November 18, 2016 13:58
-
-
Save zacharycarter/16c927f4c95835e4db167ad698f27a03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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