/** * Creates a pivoted platform from the given object, at the given world. * * @param world The world were the platform will be. * @param object The object used to initialize the platform. * @return The Platform Model of the created platform. */ static PlatformModel makePlatform(World world, RectangleMapObject object) { PlatformModel platform = new PlatformModel(world, object); Boolean pivoted = object.getProperties().get("pivoted", boolean.class); if (pivoted != null && pivoted == true) { Rectangle rect = object.getRectangle(); RectangleMapObject anchorRect = new RectangleMapObject( rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2, 1, 1 ); Body anchor = makeRectGround(world, anchorRect); RevoluteJointDef jointDef = new RevoluteJointDef(); jointDef.initialize(anchor, platform.getBody(), platform.getBody().getWorldCenter()); world.createJoint(jointDef); } return platform; }
/** * Creates rectangular ground from the given object, at the given world. * * @param world The world were the ground will be. * @param object The object used to initialize the ground. * @return The body of the created ground. */ static Body makeRectGround(World world, RectangleMapObject object) { Rectangle rect = object.getRectangle(); // Body and Fixture variables BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2)); Body body = world.createBody(bdef); shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER); fdef.shape = shape; fdef.filter.categoryBits = GROUND_BIT; body.createFixture(fdef); return body; }
/** * Creates a PowerUp from the given object, at the given world. * * @param world The world were the PowerUp will be. * @param object The object used to initialize the PowerUp. * @return The created PowerUp. */ static PowerUp makePowerUp(World world, RectangleMapObject object) { String classProperty = object.getProperties().get("class", String.class); if (classProperty == null) { System.err.println("PowerUp has no class set!"); return null; } switch (classProperty) { case "gravity": return new GravityPowerUp(world, object); case "velocity": return new SpeedPowerUp(world, object); case "jump": return new JumpPowerUp(world, object); default: return new RandomPowerUp(world, object); } }
/** * Box's Constructor. * Creates a Box in the given with world, from the given object. * * @param world Physics world where the box will be in. * @param object Object used to create the box. */ public BoxModel(World world, RectangleMapObject object) { super(world, object.getRectangle().getCenter(new Vector2()).scl(PIXEL_TO_METER), ModelType.BOX, ANGULAR_DAMP, LINEAR_DAMP); // Fetch density from properties Float property = object.getProperties().get("density", float.class); if (property != null) density = property; // Create Fixture's Shape Shape shape = createPolygonShape(new float[]{ 0, 0, 64, 0, 64, 64, 0, 64 }, new Vector2(64, 64)); createFixture(new FixtureProperties(shape, density, FRICTION, RESTITUTION, GROUND_BIT, (short) (BALL_BIT | GROUND_BIT | FLUID_BIT))); }
/** * Inititalisiert den NPC. * Lädt alle Grafiken und Animationen. */ @Override public void onInit() { super.onInit(); stoneAtlas = new TextureAtlas(Gdx.files.internal("data/graphics/packed/stone.atlas")); bigStone = stoneAtlas.findRegion("stone_big"); if (rawObject instanceof RectangleMapObject) { RectangleMapObject rectObject = (RectangleMapObject) rawObject; Rectangle rect = rectObject.getRectangle(); position = new Vector2(rect.getX() + rect.getWidth() / 2f, rect.getY()); startPosition = new Vector2(rect.getX(), rect.getY()); rectShape = Physics.createRectangle(rect.getWidth(), rect.getHeight(), new Vector2(rect.getWidth() / 2f, rect.getHeight() / 2f)); if (activate) body = createEntityBody(startPosition, rectShape, BodyDef.BodyType.KinematicBody); } else { Gdx.app.log("WARNING", "Stone Barrier " + objectId + " must have an RectangleMapObject!"); worldObjectManager.removeObject(this); } }
public Box(World world, MapObject object){ super(Assets.getAtlas().findRegion("boxCrate_double")); Rectangle rect = ((RectangleMapObject) object).getRectangle(); bdef.type = BodyDef.BodyType.DynamicBody; bdef.position.set((rect.getX()+rect.getWidth()/2)/ QuackHack.PPM, (rect.getY() + rect.getHeight()/2)/QuackHack.PPM); // I don't follow the math body = world.createBody(bdef); shape.setAsBox(rect.getWidth() / 2 / QuackHack.PPM, rect.getHeight() / 2 / QuackHack.PPM); fdef.shape = shape; fdef.friction = 0.4f; fdef.density = 0.1f; body.createFixture(fdef); boxRegion = new TextureRegion(getTexture(), 0, 0, 128, 128); setBounds(0,0, 128 / QuackHack.PPM, 128 / QuackHack.PPM); setRegion(boxRegion); setOrigin(getHeight()/2, getWidth()/2); }
private void processFreeBodies(Array<MapObject> bodies) { for (MapObject object : bodies) { FixtureBodyDefinition fixtureBodyDefinition; if (object instanceof RectangleMapObject) { fixtureBodyDefinition = TiledUtils.createRectangleFixtureBodyDef((RectangleMapObject)object); } else if (object instanceof CircleMapObject) { fixtureBodyDefinition = TiledUtils.createCircleFixtureBodyDef((CircleMapObject)object); } else if (object instanceof EllipseMapObject) { fixtureBodyDefinition = TiledUtils.createEllipseFixtureBodyDef((EllipseMapObject)object); } else if (object instanceof PolylineMapObject || object instanceof PolygonMapObject) { fixtureBodyDefinition = TiledUtils.createPolyFixtureBodyDef(object); } else { throw new InvalidConfigException(filename, "Unknown MapObject type"); } freeBodyDefinitions.add(fixtureBodyDefinition); } }
public static FixtureDef getFixtureDefFromBodySkeleton(MapObject object) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = 1; fixtureDef.friction = 0; fixtureDef.restitution = 0; Shape shape = null; if (object instanceof TextureMapObject) { shape = getTextureMapShape(object); } else if (object instanceof RectangleMapObject) { shape = getRectangleShape(object); } else if (object instanceof CircleMapObject) { shape = getCircleShape(object); } else if (object instanceof EllipseMapObject) { shape = getEllipseShape(object); } else if (object instanceof PolygonMapObject) { shape = getPolygonShape(object); } else if (object instanceof PolylineMapObject) { shape = getPolylineShape(object); } fixtureDef.shape = shape; return fixtureDef; }
private void parseCollisionLayer() { if (collisionLayer == null) { Gdx.app.debug(TAG, "No collision layer!"); return; } for (MapObject object : collisionLayer.getObjects()) { if (object instanceof RectangleMapObject) { Rectangle rect = ((RectangleMapObject) object).getRectangle(); rect.set( // update Tiled editor real values with our game world values rect.x * GameWorld.UNIT_SCALE, // scale x rect.y * GameWorld.UNIT_SCALE, // scale y rect.width * GameWorld.UNIT_SCALE, // scale width rect.height * GameWorld.UNIT_SCALE // scale height ); } } }
private void parseSpawnLayer() { if (spawnsLayer == null) { Gdx.app.debug(TAG, "No spawn layer!"); return; } for (MapObject object : spawnsLayer.getObjects()) { if (object instanceof RectangleMapObject) { entities.add(Entity.getEntity( // params EntityType.valueOf(object.getName()), // entity type ((RectangleMapObject) object).getRectangle().x * GameWorld.UNIT_SCALE, // start x ((RectangleMapObject) object).getRectangle().y * GameWorld.UNIT_SCALE // start y )); } } }
private Vector2 getPositionFromMapObject(MapObject mapObject) { if (mapObject instanceof PolygonMapObject) { Polygon polygon = ((PolygonMapObject) mapObject).getPolygon(); return new Vector2(polygon.getX(), polygon.getY()); } else if (mapObject instanceof RectangleMapObject) { Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle(); return new Vector2(rectangle.getX(), rectangle.getY()); } else if (mapObject instanceof EllipseMapObject) { Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse(); return new Vector2(ellipse.x, ellipse.y); } else if (mapObject instanceof CircleMapObject) { Circle circle = ((CircleMapObject) mapObject).getCircle(); return new Vector2(circle.x, circle.y); } throw new GdxRuntimeException("Only Polygons, Rectangles, Ellipses and Circles are supported!"); }
public InteractiveTileObject(PlayScreen screen, MapObject object) { this.screen = screen; this.world = screen.getWorld(); this.map = screen.getMap(); this.object = object; this.bounds = ((RectangleMapObject) object).getRectangle(); BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MarioBros.PPM, (bounds.getY() + bounds.getHeight() / 2) / MarioBros.PPM); body = world.createBody(bdef); shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM); fdef.shape = shape; fixture = body.createFixture(fdef); }
/** * * @param world * @param rectangleObject * @param density * @param friction * @param restitution */ private void createRectangle(World world, RectangleMapObject rectangleObject, float density, float friction, float restitution){ Rectangle rect = rectangleObject.getRectangle(); PolygonShape shape = new PolygonShape(); shape.setAsBox(rect.width / SupaBox.PPM / 2f, rect.height / SupaBox.PPM / 2f); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.position.set(new Vector2((rect.x + rect.width / 2f) / SupaBox.PPM, (rect.y + rect.height / 2f) / SupaBox.PPM)); Body body = world.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.restitution = restitution; body.createFixture(fixtureDef); shape.dispose(); }
/** * Carga los enemigos del nivel actual */ private static void loadEnemies() { Enemy enemy = null; // Carga los objetos m�viles del nivel actual for (MapObject object : LevelManager.map.getLayers().get("objects").getObjects()) { if (object instanceof RectangleMapObject) { RectangleMapObject rectangleObject = (RectangleMapObject) object; if (rectangleObject.getProperties().containsKey(TiledMapManager.ENEMY)) { Rectangle rect = rectangleObject.getRectangle(); enemy = new Enemy(); enemy.position.set(rect.x, rect.y); LevelManager.enemies.add(enemy); } } } }
/** * Carga las plataformas m�viles de la pantalla actual */ public static void loadPlatforms() { Platform platform = null; // Carga los objetos m�viles del nivel actual for (MapObject object : LevelManager.map.getLayers().get("objects").getObjects()) { if (object instanceof RectangleMapObject) { RectangleMapObject rectangleObject = (RectangleMapObject) object; if (rectangleObject.getProperties().containsKey(TiledMapManager.MOBILE)) { Rectangle rect = rectangleObject.getRectangle(); Direction direction = null; if (Boolean.valueOf((String) rectangleObject.getProperties().get("right_direction"))) direction = Direction.RIGHT; else direction = Direction.LEFT; platform = new Platform(rect.x, rect.y, TiledMapManager.PLATFORM_WIDTH, TiledMapManager.PLATFORM_HEIGHT, Integer.valueOf((String) rectangleObject.getProperties().get("offset")), direction); LevelManager.platforms.add(platform); } } } }
public static void createCoins(Map map, MapLayer layer, Box2DWorld world) { Matrix4 transformMat4 = getTransformationMatrix(map); Vector3 coinPos = new Vector3(); for(MapObject object : layer.getObjects()) { if(object instanceof RectangleMapObject) { RectangleMapObject rectangleObj = (RectangleMapObject)object; // Get coin position from map object and transform it by transformation matrix coinPos.set(rectangleObj.getRectangle().getX() + rectangleObj.getRectangle().width / 2, rectangleObj.getRectangle().getY() + rectangleObj.getRectangle().height / 2, 0); coinPos.mul(transformMat4); // Create new Coin Coin newCoin = new Coin(coinPos.x, coinPos.y, world); // Add Coin entity to EntityManager map.getEntMan().addCoin(newCoin); } } }
public Level() { super("Game"); cam = new OrthographicCamera(); // Setup camera viewport cam.setToOrtho(false, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2); cam.update(); batch = new SpriteBatch(); // Load map map = new TmxMapLoader().load("maps/Lvl1.tmx"); mapRenderer = new OrthogonalTiledMapRenderer(map); // Load walls as rectangles in an array MapObjects wallobjects = map.getLayers().get("Walls").getObjects(); for(int i = 0; i < wallobjects.getCount(); i++) { RectangleMapObject obj = (RectangleMapObject) wallobjects.get(i); Rectangle rect = obj.getRectangle(); walls.add(new Rectangle(rect.x, rect.y, 16, 16)); } Texture[] tex = new Texture[]{new Texture("sprites/Player_Side_Left.png"),new Texture("sprites/Player_Side_Right.png"), new Texture("sprites/Player_Behind_1.png"), new Texture("sprites/Player_Behind_2.png"),new Texture("sprites/Player_Forward1.png"),new Texture("sprites/Player.png")}; player = new Player("sprites/Player.png", 120, 120, walls, tex); wall = new Texture("tiles/Wall.png"); }
private void setPlayerStartInformation(MapObjects mapObjects) { if (mapObjects == null || mapObjects.getCount() != 1) { throw new GdxRuntimeException("Invalid map: Only one player start position allowed!"); } Rectangle playerBox = ((RectangleMapObject) mapObjects.get("startpoint")).getRectangle(); mPlayerCell = new int[2]; mPlayerCell[0] = (int) Math.floor(playerBox.getX()); mPlayerCell[1] = (int) Math.floor(playerBox.getY()); String healthProperty = (String) mapObjects.get("startpoint").getProperties().get("health"); if (healthProperty != null) { mPlayerStartHealth = Integer.parseInt(healthProperty); } else { mPlayerStartHealth = 10; } // Remove fog on start position if (mFogLayer != null) { removeFog(mPlayerCell[0], mPlayerCell[1]); } }
/** * Generic Power Up's constructor. * Creates a power up from the given object, with the given type, into the given world. * * @param world The world the power up will be in. * @param object The object to create the power up with. * @param type Type of Power Up. */ public PowerUp(World world, RectangleMapObject object, ModelType type) { super(world, object.getRectangle().getCenter(new Vector2()).scl(PIXEL_TO_METER), type); getBody().setType(BodyDef.BodyType.StaticBody); Shape shape = createPolygonShape(new float[]{ 14f, 1f, 18f, 1f, 25f, 7f, 25f, 23f, 18f, 31f, 14f, 31f, 7f, 23f, 7f, 7f }, new Vector2(32, 32)); FixtureProperties fixtureProperties = new FixtureProperties(shape, HITTABLE_BIT, BALL_BIT); fixtureProperties.setSensor(); createFixture(fixtureProperties); }
/** * Platform Model's constructor. * Creates a platform model from the given object, into the given world. * * @param world The world the platform model will be in. * @param object The object to create the platform model with. */ public PlatformModel(World world, RectangleMapObject object) { super(ModelType.PLATFORM, object.getRectangle()); Rectangle rect = object.getRectangle(); // Body and Fixture variables BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.DynamicBody; bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2)); this.body = world.createBody(bdef); shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER); fdef.shape = shape; fdef.filter.categoryBits = GROUND_BIT; fdef.isSensor = false; fdef.friction = 0.1f; fdef.restitution = 0f; // Fetch density from properties Float property = object.getProperties().get("density", float.class); if (property != null) fdef.density = property; else fdef.density = DEFAULT_DENSITY; body.createFixture(fdef); }
private void addSpawner(RectangleMapObject spawnerElement) { MapProperties properties = spawnerElement.getProperties(); String monsterIdentifier = (String) properties.get("MonsterType"); @SuppressWarnings("unchecked") Class<? extends Monster> monsterType = (Class<? extends Monster>) ObjectsIdentifier .getObjectType(monsterIdentifier); float spawnInterval = (float) properties.get("spawnInterval"); int maximumMonsterAmount = (int) properties.get("MaximumMonsterAmount"); IntegerRectangle spawnArea = new IntegerRectangle(spawnerElement.getRectangle()); MonsterSpawnerUnit spawnerUnit = new MonsterSpawnerUnit(monsterType, spawnArea, maximumMonsterAmount, spawnInterval); monsterSpawner.addSpawner(spawnerUnit); }
public static FixtureBodyDefinition createRectangleFixtureBodyDef(RectangleMapObject object) { BodyDef bodyDef = new BodyDef(); Rectangle rectangle = object.getRectangle(); bodyDef.position.x = rectangle.x + (rectangle.width / 2); bodyDef.position.y = rectangle.y + (rectangle.height / 2); bodyDef.position.scl(MainCamera.getInstance().getTileMapScale()); bodyDef.type = getBodyType(object); FixtureDef fixtureDef = getFixtureDefFromBodySkeleton(object); return new FixtureBodyDefinition(fixtureDef, bodyDef); }
private static Shape getRectangleShape(MapObject object) { Rectangle rectangle = ((RectangleMapObject)object).getRectangle(); PolygonShape shape = new PolygonShape(); float scale = MainCamera.getInstance().getTileMapScale(); shape.setAsBox((rectangle.width / 2) * scale, (rectangle.height / 2) * scale); return shape; }
private void parsePortalLayer(MapLayer layer) { if (layer == null) { Gdx.app.debug(TAG, "No portal layer!"); return; } for (MapObject object : layer.getObjects()) { if (object instanceof RectangleMapObject) { final Rectangle rect = ((RectangleMapObject) object).getRectangle(); if (START_LOCATION.equals(object.getName())) { rect.getPosition(startLocation); startLocation.scl(GameWorld.UNIT_SCALE); } else { portals.add(new Portal( rect.set( // update Tiled editor real values with our game world values rect.x * GameWorld.UNIT_SCALE, // scale x rect.y * GameWorld.UNIT_SCALE, // scale y rect.width * GameWorld.UNIT_SCALE, // scale width rect.height * GameWorld.UNIT_SCALE), // scale height, MapType.valueOf(object.getName()), // target map (int) (Integer.parseInt(object.getProperties().get("targetX", String.class)) * tileSize.x), // target tile index x (int) (Integer.parseInt(object.getProperties().get("targetY", String.class)) * tileSize.y)) // target tile index y ); } } } }
protected boolean isLocationPathable(Map map, Rectangle collisionBox) { if (!map.getBoundingBox().contains(collisionBox)) { return false; } for (MapObject object : map.getCollisionLayer().getObjects()) { if (object instanceof RectangleMapObject) { if (collisionBox.overlaps(((RectangleMapObject) object).getRectangle())) { return false; } } } return true; }
private Polygon getPolygonFromMapObject(MapObject mapObject) { if (mapObject instanceof PolygonMapObject) { return ((PolygonMapObject) mapObject).getPolygon(); } else if (mapObject instanceof RectangleMapObject) { return MathUtil.polygonFromRectangle(((RectangleMapObject) mapObject).getRectangle()); } throw new GdxRuntimeException("Only Polygons and Rectangles are supported!"); }
public void load(World world) { int mapWidth = map.getProperties().get("width", Integer.class); int mapHeight = map.getProperties().get("height", Integer.class); int tileWidth = map.getProperties().get("tilewidth", Integer.class); int tileHeight = map.getProperties().get("tileheight", Integer.class); BodyDef worldBodyDef = new BodyDef(); worldBodyDef.type = BodyDef.BodyType.StaticBody; worldBodyDef.position.set(0, 0); body = world.createBody(worldBodyDef); for (MapObject mapObject : map.getLayers().get("Collision").getObjects()) { if(mapObject instanceof RectangleMapObject) { // Everything here is in world units float objectX = mapObject.getProperties().get("x", Float.class) / tileWidth; float objectY = mapObject.getProperties().get("y", Float.class) / tileWidth; float objectWidth = mapObject.getProperties().get("width", Float.class) / tileWidth; float objectHeight = mapObject.getProperties().get("height", Float.class) / tileWidth; FixtureDef fixtureDef = new FixtureDef(); PolygonShape shape = new PolygonShape(); shape.setAsBox(objectWidth/2, objectHeight/2, new Vector2(objectX + objectWidth/2, objectY + objectHeight/2), 0); fixtureDef.shape = shape; body.createFixture(fixtureDef); shape.dispose(); } } }
public void createWorldObject(MapObject object) { if (object instanceof RectangleMapObject) { Rectangle rectangle = ((RectangleMapObject) object).getRectangle(); Body body = new Body(rectangle); world.bodies.add(body); if (rectangle.x < 20) { rectangle = new Rectangle(rectangle); rectangle.x += WorldRenderer.VIEWPORT_WIDTH; body = new Body(rectangle); world.bodies.add(body); } if (rectangle.x + rectangle.width > WorldRenderer.VIEWPORT_WIDTH - 20) { rectangle = new Rectangle(rectangle); rectangle.x -= WorldRenderer.VIEWPORT_WIDTH; body = new Body(rectangle); world.bodies.add(body); } if (rectangle.y < 20) { rectangle = new Rectangle(rectangle); rectangle.y += WorldRenderer.VIEWPORT_HEIGHT; body = new Body(rectangle); world.bodies.add(body); } if (rectangle.y > WorldRenderer.VIEWPORT_HEIGHT - 20) { rectangle = new Rectangle(rectangle); rectangle.y -= WorldRenderer.VIEWPORT_WIDTH; body = new Body(rectangle); world.bodies.add(body); } } }
/** * * @param world * @param map */ public void createBodies(ArrayList<Entity> entities, World world, TiledMap map){ MapObjects objects; objects = map.getLayers().get("ground").getObjects(); for(MapObject object : objects){ if(object instanceof RectangleMapObject){ createRectangle(world, (RectangleMapObject) object, 0.5f, 0.4f, 0.6f); } else if(object instanceof PolygonMapObject){ createPolygon(world, (PolygonMapObject) object, 0.5f, 0.4f, 0.6f); } else if(object instanceof PolylineMapObject){ createPolyline(world, (PolylineMapObject) object, 0.5f, 0.4f, 0.6f); } else if(object instanceof EllipseMapObject){ createEllipse(world, (EllipseMapObject) object, 0.5f, 0.4f, 0.6f); } else{ Gdx.app.error("Error", "Invalid map object"); } } /* objects = map.getLayers().get("dynamic").getObjects(); for(MapObject object : objects){ RectangleMapObject entity = (RectangleMapObject) object; Rectangle position = entity.getRectangle(); entities.add(new Box(world, new Vector2(position.x / SupaBox.PPM, position.y / SupaBox.PPM), new Vector2(0f, 0f), 1f, 1f)); } */ }
private Shape getRectangle(RectangleMapObject rectangleObject) { Rectangle rectangle = rectangleObject.getRectangle(); PolygonShape polygon = new PolygonShape(); Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) / GameWorld.units, (rectangle.y + rectangle.height * 0.5f ) / GameWorld.units); polygon.setAsBox(rectangle.width * 0.5f / GameWorld.units, rectangle.height * 0.5f / GameWorld.units, size, 0.0f); return polygon; }
private Shape getRectangle(RectangleMapObject rectangleObject) { Rectangle rectangle = rectangleObject.getRectangle(); PolygonShape polygon = new PolygonShape(); Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) / units, (rectangle.y + rectangle.height * 0.5f ) / units); polygon.setAsBox(rectangle.width * 0.5f / units, rectangle.height * 0.5f / units, size, 0.0f); return polygon; }
public void populatePhysicsWorld(World physicsWorld) { for (MapObject collisionObject : collisionObjects) { if (collisionObject instanceof RectangleMapObject) { RectangleMapObject rectangleCollisionObject = (RectangleMapObject) collisionObject; Rectangle collisionRectangle = rectangleCollisionObject.getRectangle(); CollisionBody collisionBody = new CollisionBody(collisionRectangle); collisionBody.addToPhysicsWorld(physicsWorld); } } }
private void buildRoom() { for (MapObject o : deviceLayer.getObjects()) { if (!(o instanceof RectangleMapObject)) { continue; } RectangleMapObject r = (RectangleMapObject) o; // all objects on the device layer will be rectangles int x = Integer.parseInt((String) r.getProperties().get("x")); int y = Integer.parseInt((String) r.getProperties().get("y")); DeviceTile tile = new DeviceTile(x, y); deviceTileMap.put(tile.isoPos, tile); } }
private Rectangle getRectangle(RectangleMapObject object) { Rectangle rectangle = new Rectangle(((RectangleMapObject) object).getRectangle()); rectangle.x *= unitScale; rectangle.y *= unitScale; rectangle.width *= unitScale; rectangle.height *= unitScale; return rectangle; }
public Water createWater(MapObject object) { Water water = null; MapProperties properties = object.getProperties(); BodyDef def = new BodyDef(); def.type = BodyDef.BodyType.StaticBody; def.position.set(getProperty(properties, "x", def.position.x) * unitScale, getProperty(properties, "y", def.position.y) * unitScale); Body box = physics.createBody(def); if (object instanceof RectangleMapObject && !properties.get(Box2DMapObjectParser.Aliases.type).equals(Box2DMapObjectParser.Aliases.typeModelObject)) { PolygonShape shape = new PolygonShape(); Rectangle rectangle = getRectangle((RectangleMapObject) object); shape.setAsBox(rectangle.width / 2, rectangle.height / 2, new Vector2(rectangle.x - box.getPosition().x + rectangle.width / 2, rectangle.y - box.getPosition().y + rectangle.height / 2), box.getAngle()); FixtureDef fixDef = new FixtureDef(); fixDef.shape = shape; fixDef.friction = 1f; fixDef.isSensor = true; fixDef.density = getProperty(properties, Box2DMapObjectParser.Aliases.density, 2); fixDef.filter.categoryBits = GRUPO.FLUID.getCategory(); fixDef.filter.maskBits = Box2DPhysicsObject.MASK_FLUID; Fixture fixBox = box.createFixture(fixDef); shape.dispose(); water = new Water(object.getName(), box); water.setWidthBodyA(rectangle.width); water.setHeightBodyA(rectangle.height); box.setUserData(water); fixBox.setUserData(water); } else { throw new IllegalArgumentException("type of " + object + " is \"" + properties.get(Box2DMapObjectParser.Aliases.type) + "\" instead of \"" + Box2DMapObjectParser.Aliases.typeModelObject + "\""); } return water; }
public MovingPlatform createMovingPlatform(MapObject object) { MovingPlatform movingPlatform = null; MapProperties properties = object.getProperties(); BodyDef def = new BodyDef(); def.type = BodyDef.BodyType.KinematicBody; def.position.set(getProperty(properties, "x", def.position.x) * unitScale, getProperty(properties, "y", def.position.y) * unitScale); Body box = physics.createBody(def); if (object instanceof RectangleMapObject && !properties.get(Box2DMapObjectParser.Aliases.type).equals(Box2DMapObjectParser.Aliases.typeModelObject)) { PolygonShape shape = new PolygonShape(); Rectangle rectangle = getRectangle((RectangleMapObject) object); shape.setAsBox(rectangle.width / 2, rectangle.height / 2, new Vector2(rectangle.x - box.getPosition().x + rectangle.width / 2, rectangle.y - box.getPosition().y + rectangle.height / 2), box.getAngle()); FixtureDef fixDef = new FixtureDef(); fixDef.shape = shape; fixDef.filter.categoryBits = GRUPO.MOVING_PLATFORM.getCategory(); fixDef.filter.maskBits = Box2DPhysicsObject.MASK_MOVING_PLATFORM; Fixture fixBox = box.createFixture(fixDef); shape.dispose(); box.setBullet(true); String name = object.getName(); movingPlatform = new MovingPlatform(name, GRUPO.MOVING_PLATFORM, box, Float.parseFloat(properties.get(Box2DMapObjectParser.Aliases.movingPlatformDistX, String.class)) , Float.parseFloat(properties.get(Box2DMapObjectParser.Aliases.movingPlatformDistY, String.class)), Float.parseFloat(properties.get(Box2DMapObjectParser.Aliases.movingPlatformSpeed, String.class))); movingPlatform.setWidthBodyA(rectangle.width); movingPlatform.setHeightBodyA(rectangle.height); box.setUserData(movingPlatform); fixBox.setUserData(movingPlatform); } else { throw new IllegalArgumentException("type of " + object + " is \"" + properties.get(Box2DMapObjectParser.Aliases.type) + "\" instead of \"" + Box2DMapObjectParser.Aliases.typeModelObject + "\""); } return movingPlatform; }
private Shape getRectangle(RectangleMapObject rectangleObject) { Rectangle rectangle = rectangleObject.getRectangle(); PolygonShape polygon = new PolygonShape(); Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) * m_units, (rectangle.y + rectangle.height * 0.5f) * m_units); polygon.setAsBox(rectangle.width * 0.5f * m_units, rectangle.height * 0.5f * m_units, size, 0.0f); return polygon; }
private void insertMapObjectsIntoCollisionMap(PixelCollisionMap<GameObject> collisionMap, TiledMap worldMap) { MapObjects objects = worldMap.getLayers().get("CollisionLayer").getObjects(); Array<RectangleMapObject> collision = objects.getByType(RectangleMapObject.class); collision.forEach((rectangle) -> collisionMap.insertStaticObject(rectangle.getRectangle())); }
private void loadCollision(TiledMap map) { MapObjects objects = map.getLayers().get("CollisionLayer").getObjects(); Array<RectangleMapObject> collision = objects.getByType(RectangleMapObject.class); collision.forEach((rectangle) -> collisionMap.insert(new MapCollisionUnknownObject(rectangle.getRectangle()))); }
private void loadSpawners(TiledMap map) { MapObjects objects = map.getLayers().get("SpawnAreasLayer").getObjects(); Array<RectangleMapObject> spawnerInfos = objects.getByType(RectangleMapObject.class); spawnerInfos.forEach(spawnerElement -> addSpawner(spawnerElement)); }