Search in sources :

Example 1 with BlockShape

use of org.terasology.engine.world.block.shapes.BlockShape in project Terasology by MovingBlocks.

the class BlockManagerImpl method loadFamily.

private Optional<BlockFamily> loadFamily(BlockUri uri) {
    Optional<BlockFamilyDefinition> familyDef = assetManager.getAsset(uri.getBlockFamilyDefinitionUrn(), BlockFamilyDefinition.class);
    if (familyDef.isPresent() && familyDef.get().isLoadable()) {
        if (familyDef.get().isFreeform()) {
            ResourceUrn shapeUrn;
            if (uri.getShapeUrn().isPresent()) {
                shapeUrn = uri.getShapeUrn().get();
            } else {
                shapeUrn = CUBE_SHAPE_URN;
            }
            Optional<BlockShape> shape = assetManager.getAsset(shapeUrn, BlockShape.class);
            if (shape.isPresent()) {
                return Optional.of(familyDef.get().createFamily(shape.get(), blockBuilder));
            }
        } else if (!familyDef.get().isFreeform()) {
            return Optional.of(familyDef.get().createFamily(blockBuilder));
        }
    } else {
        logger.error("Family not available: {}", uri);
    }
    return Optional.empty();
}
Also used : BlockShape(org.terasology.engine.world.block.shapes.BlockShape) BlockFamilyDefinition(org.terasology.engine.world.block.loader.BlockFamilyDefinition) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn)

Example 2 with BlockShape

use of org.terasology.engine.world.block.shapes.BlockShape in project Terasology by MovingBlocks.

the class BlockFamilyLibrary method createFamily.

/**
 * Create a family based on the type and instantiate from the the family definition of the block and builder
 *
 * @param blockFamily
 * @param blockFamilyDefinition
 * @param blockBuilderHelper
 * @param shape
 * @return new BlockFamily
 */
public static BlockFamily createFamily(Class<? extends AbstractBlockFamily> blockFamily, BlockFamilyDefinition blockFamilyDefinition, BlockShape shape, BlockBuilderHelper blockBuilderHelper) {
    try {
        SimpleClassFactory simpleClassFactory = new SimpleClassFactory(new ParameterProvider() {

            @Override
            public <T> Optional<T> get(Class<T> type) {
                if (type.isAssignableFrom(BlockBuilderHelper.class)) {
                    return Optional.ofNullable((T) blockBuilderHelper);
                } else if (type.isAssignableFrom(BlockFamilyDefinition.class)) {
                    return Optional.ofNullable((T) blockFamilyDefinition);
                } else if (type.isAssignableFrom(BlockShape.class)) {
                    return Optional.ofNullable((T) shape);
                }
                return Optional.empty();
            }
        });
        BlockFamily result = simpleClassFactory.instantiateClass(blockFamily).get();
        InjectionHelper.inject(result);
        if (result.getURI() == null) {
            throw new Exception("Family Is missng a BlockUri");
        }
        return result;
    } catch (Exception e) {
        logger.error("Failed to load blockFamily {}", blockFamily, e);
    }
    return null;
}
Also used : ParameterProvider(org.terasology.gestalt.util.reflection.ParameterProvider) Optional(java.util.Optional) BlockShape(org.terasology.engine.world.block.shapes.BlockShape) BlockBuilderHelper(org.terasology.engine.world.block.BlockBuilderHelper) SimpleClassFactory(org.terasology.gestalt.util.reflection.SimpleClassFactory)

Example 3 with BlockShape

use of org.terasology.engine.world.block.shapes.BlockShape in project Terasology by MovingBlocks.

the class BlockShapeTest method testConvexHull.

@Test
public void testConvexHull() {
    BlockShape blockShape = assetManager.getAsset("engine:halfSlope", BlockShape.class).get();
    CollisionShape shape = blockShape.getCollisionShape(Rotation.rotate(Yaw.CLOCKWISE_90));
    assertTrue(shape instanceof ConvexHullShape);
    Vector3f[] test = new Vector3f[] { new Vector3f(0.49999997f, 0.0f, 0.49999997f), new Vector3f(-0.49999997f, -0.49999997f, 0.49999997f), new Vector3f(0.49999997f, -0.49999997f, 0.49999997f), new Vector3f(0.49999997f, 0.0f, -0.49999997f), new Vector3f(0.49999997f, -0.49999997f, -0.49999997f), new Vector3f(-0.49999997f, -0.49999997f, -0.49999997f), new Vector3f(0.49999997f, -0.49999997f, 0.49999997f), new Vector3f(0.49999997f, -0.49999997f, -0.49999997f), new Vector3f(0.49999997f, 0.0f, -0.49999997f), new Vector3f(0.49999997f, 0.0f, 0.49999997f), new Vector3f(0.49999997f, -0.49999997f, 0.49999997f), new Vector3f(-0.49999997f, -0.49999997f, 0.49999997f), new Vector3f(-0.49999997f, -0.49999997f, -0.49999997f), new Vector3f(0.49999997f, -0.49999997f, -0.49999997f), new Vector3f(0.49999997f, 0.0f, -0.49999997f), new Vector3f(-0.49999997f, -0.49999997f, -0.49999997f), new Vector3f(-0.49999997f, -0.49999997f, 0.49999997f), new Vector3f(0.49999997f, 0.0f, 0.49999997f) };
    BulletConvexHullShape bulletConvexHullShape = (BulletConvexHullShape) shape;
    // TODO: Test fails because native library is not loaded
    for (int x = 0; x < ((btConvexHullShape) bulletConvexHullShape.underlyingShape).getNumPoints(); x++) {
        fuzzVectorTest(test[x], ((btConvexHullShape) bulletConvexHullShape.underlyingShape).getScaledPoint(x));
    }
}
Also used : CollisionShape(org.terasology.engine.physics.shapes.CollisionShape) BlockShape(org.terasology.engine.world.block.shapes.BlockShape) Vector3f(org.joml.Vector3f) ConvexHullShape(org.terasology.engine.physics.shapes.ConvexHullShape) com.badlogic.gdx.physics.bullet.collision.btConvexHullShape(com.badlogic.gdx.physics.bullet.collision.btConvexHullShape) BulletConvexHullShape(org.terasology.engine.physics.bullet.shapes.BulletConvexHullShape) BulletConvexHullShape(org.terasology.engine.physics.bullet.shapes.BulletConvexHullShape) Test(org.junit.jupiter.api.Test)

Aggregations

BlockShape (org.terasology.engine.world.block.shapes.BlockShape)3 com.badlogic.gdx.physics.bullet.collision.btConvexHullShape (com.badlogic.gdx.physics.bullet.collision.btConvexHullShape)1 Optional (java.util.Optional)1 Vector3f (org.joml.Vector3f)1 Test (org.junit.jupiter.api.Test)1 BulletConvexHullShape (org.terasology.engine.physics.bullet.shapes.BulletConvexHullShape)1 CollisionShape (org.terasology.engine.physics.shapes.CollisionShape)1 ConvexHullShape (org.terasology.engine.physics.shapes.ConvexHullShape)1 BlockBuilderHelper (org.terasology.engine.world.block.BlockBuilderHelper)1 BlockFamilyDefinition (org.terasology.engine.world.block.loader.BlockFamilyDefinition)1 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)1 ParameterProvider (org.terasology.gestalt.util.reflection.ParameterProvider)1 SimpleClassFactory (org.terasology.gestalt.util.reflection.SimpleClassFactory)1