Example usage for com.badlogic.gdx.physics.box2d Body getUserData

List of usage examples for com.badlogic.gdx.physics.box2d Body getUserData

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Body getUserData.

Prototype

public Object getUserData() 

Source Link

Document

Get the user data

Usage

From source file:com.agateau.pixelwheels.bonus.ClosestRacerFinder.java

License:Open Source License

public Racer find(World world, Vector2 origin, float angle) {
    Body body = mBodyFinder.find(world, origin, angle);
    if (body == null) {
        return null;
    } else {//from   w ww.  j a  v  a 2s. c om
        return (Racer) body.getUserData();
    }
}

From source file:com.anythingmachine.tiledMaps.TiledMapHelper.java

License:Apache License

public void destroyMap() {
    Array<Body> bodies = new Array<Body>();
    GamePlayManager.world.getBodies(bodies);
    Iterator it = bodies.iterator();
    while (it.hasNext()) {
        Body b = (Body) it.next();
        Object dat = b.getUserData();
        if (dat != null && dat instanceof Entity) {
            Entity e = (Entity) dat;
            if (e.type == EntityType.PLATFORM || e.type == EntityType.WALL || e.type == EntityType.LEVELWALL
                    || e.type == EntityType.STAIRS || e.type == EntityType.AINODE
                    || e.type == EntityType.DOOR) {
                // System.out.println(e.type);
                GamePlayManager.world.destroyBody(b);
            }/*  w  w w .jav a 2s .  c o  m*/
        }
        it.remove();
    }
}

From source file:com.dongbat.game.util.networkUtil.SaveLoadUtil.java

public static void load(World w, WorldState state) {
    CustomWorld world = (CustomWorld) w;
    WorldState s = WorldStateUtil.copy(state);
    getWorldProgress(world).setFrame(s.getCurrentFrame());

    world.setIgnoringSystem(true);/*from  w w w . ja  v  a 2 s  .  c  o m*/

    IntBag allEntities = getAllEntities(world);
    for (int i = 0; i < allEntities.size(); i++) {
        int id = allEntities.get(i);
        world.deleteEntity(id);
    }

    world.process();

    for (EntityState entityState : s.getEntityStates()) {
        Entity e = EntityUtil.createEntity(world);
        setUuid(e, entityState.getUuid());
        EntityEdit edit = e.edit();
        for (Component component : entityState.getComponents()) {
            if (component instanceof Physics) {
                edit.add(new Physics());
            } else {
                edit.add(component);
            }
        }
    }

    //    String physicsWorldData = (String) s.getData().get("physicsWorld");
    //    com.badlogic.gdx.physics.box2d.World physicsWorld = Box2dJsonUtil.toWorld(physicsWorldData);
    Box2dState box2dState = (Box2dState) s.getWorldData().get("physicsWorld");
    com.badlogic.gdx.physics.box2d.World physicsWorld = Box2dStateUtil.toWorld(box2dState);
    com.badlogic.gdx.physics.box2d.World oldWorld = PhysicsUtil.getPhysicsWorld(world);
    PhysicsUtil.setPhysicsWorld(world, physicsWorld);
    oldWorld.dispose();

    world.process();

    Array<Body> bodies = new Array<Body>();
    physicsWorld.getBodies(bodies);
    for (Body body : bodies) {
        if (body.getUserData() == null) {
            continue;
        }
        UUID id = (UUID) body.getUserData();
        Entity e = getEntityByUuid(world, id);

        EntityUtil.getComponent(world, e, Physics.class).setBody(body);

    }

    world.process();
    world.setIgnoringSystem(false);

}

From source file:com.dongbat.game.util.networkUtil.WorldStateUtil.java

public static void load(World w, WorldState state) {
    CustomWorld world = (CustomWorld) w;
    WorldState s = WorldStateUtil.copy(state);
    getWorldProgress(world).setFrame(s.getCurrentFrame());
    long seed = (Long) s.getWorldData().get("seed");
    getRandom(world).setSeed(seed);/*from  w w  w . j  a v a  2  s  . c o  m*/

    world.setIgnoringSystem(true);

    IntBag allEntities = getAllEntities(world);
    for (int i = 0; i < allEntities.size(); i++) {
        int id = allEntities.get(i);
        world.deleteEntity(id);
    }

    world.process();

    for (EntityState entityState : s.getEntityStates()) {
        Entity e = EntityUtil.createEntity(world);
        setUuid(e, entityState.getUuid());
        EntityEdit edit = e.edit();
        for (Component component : entityState.getComponents()) {
            if (component instanceof Physics) {
                edit.add(new Physics());
            } else {
                edit.add(component);
            }
        }
    }

    //    String physicsWorldData = (String) s.getData().get("physicsWorld");
    //    com.badlogic.gdx.physics.box2d.World physicsWorld = Box2dJsonUtil.toWorld(physicsWorldData);
    Box2dState box2dState = (Box2dState) s.getWorldData().get("physicsWorld");
    com.badlogic.gdx.physics.box2d.World physicsWorld = Box2dStateUtil.toWorld(box2dState);
    com.badlogic.gdx.physics.box2d.World oldWorld = PhysicsUtil.getPhysicsWorld(world);
    PhysicsUtil.setPhysicsWorld(world, physicsWorld);
    oldWorld.dispose();

    world.process();

    Array<Body> bodies = new Array<Body>();
    physicsWorld.getBodies(bodies);
    for (Body body : bodies) {
        if (body.getUserData() == null) {
            continue;
        }
        UUID id = (UUID) body.getUserData();
        Entity e = getEntityByUuid(world, id);

        EntityUtil.getComponent(world, e, Physics.class).setBody(body);

    }
    world.getSystem(Box2dSystem.class).setUninitialized(true);
    world.process();
    world.setIgnoringSystem(false);

}

From source file:com.dongbat.game.util.WorldQueryUtil.java

/**
 * Find any entity in radius//w  w  w.jav a  2 s.c o  m
 *
 * @param world artemis world
 * @param location location to find
 * @param radius radius to find
 * @return Array of nearest Entity in radius
 */
public static Array<Entity> findAnyInRadius(final com.artemis.World world, final Vector2 location,
        final float radius) {
    final Array<Entity> entities = new Array<Entity>();

    QueryCallback callback = new QueryCallback() {

        @Override
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData());
            float distanceSq = new Vector2(body.getPosition()).sub(location).len2();
            if (distanceSq <= radius * radius) {
                entities.add(entity);
            }
            return true;
        }
    };
    Vector2 lowerLeft = new Vector2(location).sub(radius, radius);
    Vector2 upperRight = new Vector2(location).add(radius, radius);
    PhysicsUtil.getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x,
            upperRight.y);

    return entities;
}

From source file:com.dongbat.game.util.WorldQueryUtil.java

/**
 * Find food in radius, centre is an point on map
 *
 * @param world artemis world//from  w w w.  j ava2s  .  c o m
 * @param location centre of circle
 * @param radius radius to find
 * @return Food entity list
 */
public static Array<Entity> findFoodInRadius(final com.artemis.World world, final Vector2 location,
        final float radius) {
    final Array<Entity> food = new Array<Entity>();

    QueryCallback callback = new QueryCallback() {

        @Override
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData());
            if (entity != null) {
                if (isFood(world, entity.getId())) {
                    float distanceSq = new Vector2(body.getPosition()).sub(location).len2();
                    if (distanceSq <= radius * radius) {
                        food.add(entity);
                    }
                }
            }
            return true;
        }
    };
    Vector2 lowerLeft = new Vector2(location).sub(radius, radius);
    Vector2 upperRight = new Vector2(location).add(radius, radius);
    getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y);

    return food;
}

From source file:com.dongbat.game.util.WorldQueryUtil.java

public static Array<Entity> findColdFoodInRadius(final com.artemis.World world, final Vector2 location,
        final float radius) {
    final Array<Entity> food = new Array<Entity>();

    QueryCallback callback = new QueryCallback() {

        @Override//  ww w  .  j  a  v a  2s  . c o m
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData());
            if (entity != null) {
                if (isFood(world, entity.getId())) {
                    float distanceSq = new Vector2(body.getPosition()).sub(location).len2();
                    if (distanceSq <= radius * radius) {
                        Food f = EntityUtil.getComponent(world, entity, Food.class);
                        if (!f.isToxic()) {
                            food.add(entity);
                        }
                    }
                }
            }
            return true;
        }
    };
    Vector2 lowerLeft = new Vector2(location).sub(radius, radius);
    Vector2 upperRight = new Vector2(location).add(radius, radius);
    getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y);

    return food;
}

From source file:com.dongbat.game.util.WorldQueryUtil.java

/**
 * Find enemy in radius, centre is an point on map
 *
 * @param world artemis world/*w w  w .j  av a  2 s  .co m*/
 * @param location centre of circle
 * @param radius radius to find
 * @return Enemy entity list
 */
public static Array<Entity> findEnemyInRadius(final com.artemis.World world, final Vector2 location,
        final float radius) {
    // TODO: to be fixed
    final Array<Entity> enemy = new Array<Entity>();

    QueryCallback callback = new QueryCallback() {

        @Override
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData());
            boolean isEnemy = false;
            if (isEnemy) {
                float distanceSq = new Vector2(body.getPosition()).sub(location).len2();
                if (distanceSq <= radius * radius) {
                    enemy.add(entity);
                }
            }
            return true;
        }
    };

    Vector2 lowerLeft = new Vector2(location).sub(radius, radius);
    Vector2 upperRight = new Vector2(location).add(radius, radius);
    getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y);

    return enemy;
}

From source file:com.dongbat.game.util.WorldQueryUtil.java

/**
 * Find player in radius from a location
 *
 * @param world artemis world//from   ww  w .  ja v a  2s  .  c o m
 * @param location location that you want to find
 * @param radius radius to find
 * @return Player entity list
 */
public static Array<Entity> findPlayerNonAiInRadius(final com.artemis.World world, final Vector2 location,
        final float radius) {
    final Array<Entity> playerAndAIList = new Array<Entity>();

    QueryCallback callback = new QueryCallback() {

        @Override
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData());
            if (isPlayerNonAiUnit(world, entity.getId())) {
                float distanceSq = new Vector2(body.getPosition()).sub(location).len2();
                if (distanceSq <= radius * radius) {
                    playerAndAIList.add(entity);
                }
            }
            return true;
        }
    };
    Vector2 lowerLeft = new Vector2(location).sub(radius, radius);
    Vector2 upperRight = new Vector2(location).add(radius, radius);
    getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y);

    return playerAndAIList;
}

From source file:com.dongbat.game.util.WorldQueryUtil.java

public static Array<Entity> findQueenInRadius(final com.artemis.World world, final Vector2 location,
        final float radius) {
    final Array<Entity> queenList = new Array<Entity>();

    QueryCallback callback = new QueryCallback() {

        @Override/*w ww .j a v  a 2s. c  o  m*/
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData());
            if (isQueen(world, entity.getId())) {
                float distanceSq = new Vector2(body.getPosition()).sub(location).len2();
                if (distanceSq <= radius * radius) {
                    queenList.add(entity);
                }
                return false;
            }
            return true;
        }
    };
    Vector2 lowerLeft = new Vector2(location).sub(radius, radius);
    Vector2 upperRight = new Vector2(location).add(radius, radius);
    getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y);

    return queenList;
}