Search in sources :

Example 1 with BoundingBoxComponent

use of com.almasb.fxgl.entity.components.BoundingBoxComponent in project FXGL by AlmasB.

the class BoundingBoxComponentTest method testSerialization.

@Test
public void testSerialization() {
    bbox.addHitBox(new HitBox("BOX", BoundingShape.box(30, 40)));
    // write
    Bundle bundle = new Bundle("BBOXTest");
    bbox.write(bundle);
    Entity e2 = new Entity();
    // read
    BoundingBoxComponent bbox2 = e2.getBoundingBoxComponent();
    bbox2.read(bundle);
    assertThat(bbox2.getWidth(), is(30.0));
    assertThat(bbox2.getHeight(), is(40.0));
    List<HitBox> boxes = bbox2.hitBoxesProperty();
    assertThat(boxes.size(), is(1));
    assertThat(boxes.get(0).getName(), is("BOX"));
    assertThat(boxes.get(0).getWidth(), is(30.0));
    assertThat(boxes.get(0).getHeight(), is(40.0));
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) BoundingBoxComponent(com.almasb.fxgl.entity.components.BoundingBoxComponent) Bundle(com.almasb.fxgl.core.serialization.Bundle) Test(org.junit.jupiter.api.Test)

Example 2 with BoundingBoxComponent

use of com.almasb.fxgl.entity.components.BoundingBoxComponent in project FXGL by AlmasB.

the class PhysicsWorld method createFixtures.

private void createFixtures(Entity e) {
    BoundingBoxComponent bbox = e.getBoundingBoxComponent();
    PhysicsComponent physics = e.getComponent(PhysicsComponent.class);
    FixtureDef fd = physics.fixtureDef;
    for (HitBox box : bbox.hitBoxesProperty()) {
        Shape b2Shape = createShape(box, e);
        // we use definitions from user, but override shape
        fd.setShape(b2Shape);
        Fixture fixture = physics.body.createFixture(fd);
        fixture.setHitBox(box);
    }
}
Also used : BoundingBoxComponent(com.almasb.fxgl.entity.components.BoundingBoxComponent) Shape(com.almasb.fxgl.physics.box2d.collision.shapes.Shape)

Aggregations

BoundingBoxComponent (com.almasb.fxgl.entity.components.BoundingBoxComponent)2 Bundle (com.almasb.fxgl.core.serialization.Bundle)1 Entity (com.almasb.fxgl.entity.Entity)1 HitBox (com.almasb.fxgl.physics.HitBox)1 Shape (com.almasb.fxgl.physics.box2d.collision.shapes.Shape)1 Test (org.junit.jupiter.api.Test)1