use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class PhysicsWorld method beginContact.
@Override
public void beginContact(Contact contact) {
Entity e1 = (Entity) contact.getFixtureA().getBody().getUserData();
Entity e2 = (Entity) contact.getFixtureB().getBody().getUserData();
// check sensors
if (contact.getFixtureA().isSensor()) {
e1.getComponent(PhysicsComponent.class).groundedList.add(e2);
return;
} else if (contact.getFixtureB().isSensor()) {
e2.getComponent(PhysicsComponent.class).groundedList.add(e1);
return;
}
if (!areCollidable(e1, e2))
return;
CollisionHandler handler = getHandler(e1, e2);
if (handler != null) {
CollisionPair pair = getPair(e1, e2);
// no collision registered, so add the pair
if (pair == null) {
pair = Pools.obtain(CollisionPair.class);
pair.init(e1, e2, handler);
// add pair to list of collisions so we still use it
collisions.add(pair);
HitBox boxA = (HitBox) contact.getFixtureA().getUserData();
HitBox boxB = (HitBox) contact.getFixtureB().getUserData();
handler.onHitBoxTrigger(pair.getA(), pair.getB(), e1 == pair.getA() ? boxA : boxB, e2 == pair.getB() ? boxB : boxA);
pair.collisionBegin();
}
}
}
use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class PhysicsWorld method endContact.
@Override
public void endContact(Contact contact) {
Entity e1 = (Entity) contact.getFixtureA().getBody().getUserData();
Entity e2 = (Entity) contact.getFixtureB().getBody().getUserData();
// check sensors
if (contact.getFixtureA().isSensor()) {
e1.getComponent(PhysicsComponent.class).groundedList.remove(e2);
return;
} else if (contact.getFixtureB().isSensor()) {
e2.getComponent(PhysicsComponent.class).groundedList.remove(e1);
return;
}
if (!areCollidable(e1, e2))
return;
CollisionHandler handler = getHandler(e1, e2);
if (handler != null) {
int pairIndex = getPairIndex(e1, e2);
// collision registered, so remove it and put pair back to pool
if (pairIndex != -1) {
CollisionPair pair = collisions.get(pairIndex);
collisions.removeIndex(pairIndex);
pair.collisionEnd();
Pools.free(pair);
}
}
}
use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class PhysicsWorld method checkCollisions.
/**
* Perform collision detection for all entities that have
* setCollidable(true) and if at least one entity is not PhysicsEntity.
* Subsequently fire collision handlers for all entities that have
* setCollidable(true).
*/
private void checkCollisions() {
for (Entity e : entities) {
if (isCollidable(e)) {
collidables.add(e);
}
}
for (int i = 0; i < collidables.size(); i++) {
Entity e1 = collidables.get(i);
for (int j = i + 1; j < collidables.size(); j++) {
Entity e2 = collidables.get(j);
CollisionHandler handler = getHandler(e1, e2);
// if no handler registered, no need to check for this pair
if (handler == null)
continue;
// if no need for manual check, let jbox handle it
if (!needManualCheck(e1, e2)) {
continue;
}
// check if colliding
CollisionResult result = e1.getBoundingBoxComponent().checkCollision(e2.getBoundingBoxComponent());
if (result.hasCollided()) {
collisionBeginFor(handler, e1, e2, result.getBoxA(), result.getBoxB());
// put result back to pool only if collided
Pools.free(result);
} else {
collisionEndFor(e1, e2);
}
}
}
collidables.clear();
}
use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class InterpolatorSample2 method initGame.
@Override
protected void initGame() {
int i = 0;
for (Interpolators interpolator : Interpolators.values()) {
Text t = getUIFactory().newText(interpolator.toString() + ":");
t.setFill(Color.BLACK);
Pane p = new Pane(t);
p.setTranslateY(i * 50 + 25);
Line line = new Line(0, i * 50, getWidth(), i * 50);
line.setStroke(Color.RED);
getGameScene().addUINodes(p, line);
Texture texture = DSLKt.texture("bird.png").toAnimatedTexture(2, Duration.seconds(0.5));
Entity bird = Entities.builder().at(100, i * 50).viewFromNode(texture).buildAndAttach();
Entities.animationBuilder().interpolator(interpolator.EASE_OUT()).duration(Duration.seconds(2)).repeatInfinitely().translate(bird).from(new Point2D(100, i * 50)).to(new Point2D(400, i * 50)).buildAndPlay();
i++;
}
}
use of com.almasb.fxgl.entity.Entity in project FXGL by AlmasB.
the class PhysicsSample method initPhysics.
@Override
protected void initPhysics() {
// 3. get physics world and register a collision handler
// between Type.PLAYER and Type.ENEMY
PhysicsWorld physics = getPhysicsWorld();
physics.addCollisionHandler(new CollisionHandler(Type.PLAYER, Type.ENEMY) {
@Override
protected void onHitBoxTrigger(Entity player, Entity enemy, HitBox playerBox, HitBox enemyBox) {
System.out.println(playerBox.getName() + " X " + enemyBox.getName());
}
// the order of entities is determined by
// the order of their types passed into constructor
@Override
protected void onCollisionBegin(Entity player, Entity enemy) {
System.out.println("On Collision Begin");
}
@Override
protected void onCollision(Entity player, Entity enemy) {
System.out.println("On Collision");
}
@Override
protected void onCollisionEnd(Entity player, Entity enemy) {
System.out.println("On Collision End");
}
});
}
Aggregations