public void LoadNewMethod(World world) { // load boxr Parameters params = new Parameters(); params.textureMinFilter = TextureFilter.Linear; params.textureMagFilter = TextureFilter.Linear; TiledMap tileMap = new TmxMapLoader().load("Maps/level" + GameStats.Level + ".tmx"); for (TiledMapTileSet tileSet : tileMap.getTileSets()) { Iterator<TiledMapTile> it = tileSet.iterator(); while (it.hasNext()) { it.next() .getTextureRegion() .getTexture() .setFilter(TextureFilter.Linear, TextureFilter.Linear); } } // LoadTilesWithBody(tileMap, world); // LoadTilesWithoutBody(tileMap, world); LoadGameTiles(tileMap, world); renderer = new OrthogonalTiledMapRenderer(tileMap, 1 / 100f); // tileMap.dispose(); }
Tilemap() { tiledMap = new TmxMapLoader().load("temp.tmx"); mapLayer = (TiledMapTileLayer) tiledMap.getLayers().get(0); tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap); blockTile = new TmxMapLoader().load("block.tmx"); blockLayer = (TiledMapTileLayer) blockTile.getLayers().get(0); }
public Vector2 getTriggerPoint(String name) { final MapObject triggerObject = tiledMap.getLayers().get("trigger").getObjects().get(name); if (triggerObject == null) { return null; } final int tileWidth = (Integer) tiledMap.getProperties().get("tilewidth"); final int tileHeight = (Integer) tiledMap.getProperties().get("tileheight"); return new Vector2( (Float) triggerObject.getProperties().get("x") / tileWidth, (Float) triggerObject.getProperties().get("y") / tileHeight); }
public static void convertMap( World box2dWorld, TiledMap map, float PPM, float playfieldOffsetX, float playfieldOffsetY, int defaultID) { TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0); for (int y = 0; y <= layer.getHeight() - 1; y++) { for (int x = 0; x <= layer.getWidth() - 1; x++) { Cell cell = layer.getCell(x, y); if (cell != null) { PhysicsBodyFactory.addStaticTileBodyAndFixture( defaultID, box2dWorld, playfieldOffsetX + ((x + 1) * layer.getTileWidth() / PPM), playfieldOffsetY + ((y + 1) * layer.getTileHeight() / PPM), layer.getTileWidth() / PPM, layer.getTileHeight() / PPM, 0, 0); } } } }
/** * Randomly selects and returns a set of predefined obstacles from the map. * * @return the map layer containing the obstacles */ private TiledMapTileLayer chooseObstacles() { int count = 0; // First count how many obstacle layers we have. while (map.getLayers().get(String.format("Obstacles%d", count)) != null) { count++; } // Choose a random layer or return null if there are no layers. if (count == 0) { return null; } else { return (TiledMapTileLayer) map.getLayers().get(String.format("Obstacles%d", MathUtils.random(0, count - 1))); } }
private void createCoins() { coins = new Array<Coin>(); MapLayer layer = tileMap.getLayers().get("Coins"); BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); for (MapObject mo : layer.getObjects()) { bdef.type = BodyType.StaticBody; float x = mo.getProperties().get("x", Float.class) / PPM; float y = mo.getProperties().get("y", Float.class) / PPM; bdef.position.set(x, y); CircleShape cShape = new CircleShape(); cShape.setRadius(8 / PPM); fdef.shape = cShape; fdef.isSensor = true; fdef.filter.categoryBits = B2DVars.BIT_COINS; fdef.filter.maskBits = B2DVars.BIT_PLAYER; Body body = world.createBody(bdef); body.createFixture(fdef).setUserData("coin"); Coin c = new Coin(body); coins.add(c); body.setUserData(c); } }
public void LoadTilesWithoutBody(TiledMap tileMap, World world) { TiledMapTileLayer layer = (TiledMapTileLayer) tileMap.getLayers().get("Empty"); float tileSize = layer.getTileHeight(); for (int row = 0; row < layer.getHeight(); row++) { for (int col = 0; col < layer.getWidth(); col++) { Cell cell = layer.getCell(col, row); if (cell == null || cell.getTile() == null) continue; // create float x = col * tileSize / B2D.PPM; float y = row * tileSize / B2D.PPM; int tileType = cell.getTile().getId(); tileType--; if (tileType == 15) { GameStats.GameSpawnPosition = new Vector2(x, y); Cell c = new Cell(); layer.setCell(col, row, c); } if (tileType <= 14) { // e element normal int id = tileType; GameTileRenderer.tlz[col][row] = id; } } } }
public void applyPhysics(World world) { final Iterator<MapLayer> layerIterator = tiledMap.getLayers().iterator(); while (layerIterator.hasNext()) { final MapLayer mapLayer = layerIterator.next(); if (mapLayer instanceof TiledMapTileLayer) { final TiledMapTileLayer tiledLayer = (TiledMapTileLayer) mapLayer; final int width = tiledLayer.getWidth(); final int height = tiledLayer.getHeight(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { final TiledMapTileLayer.Cell cell = tiledLayer.getCell(x, y); if (cell == null) { continue; } final boolean dirt = cell.getTile().getProperties().containsKey("dirt"); final boolean noWall = cell.getTile().getProperties().containsKey("nowall"); if (!noWall) { addBody(world, x + offset.x, y + offset.y, dirt); } } } } } for (String portalName : portals.keySet()) { final Vector2 portalPosition = portals.get(portalName); addPortalBody(world, portalName, portalPosition); } final Vector2 finish = getTriggerPoint("finish"); if (finish != null) { addFinish(world, finish); } }
@Override public void dispose() { batch.dispose(); ui.dispose(); fontLabel.dispose(); stage.dispose(); skin.dispose(); tiledMap.dispose(); }
public void getCollidingRects() { MapObjects objects = tiledMap.getLayers().get("phyics").getObjects(); collideRects = new Array<Rectangle>(); for (MapObject object : objects) { RectangleMapObject rectangleObject = (RectangleMapObject) object; collideRects.add(rectangleObject.getRectangle()); } }
Map(MapFactory.MapType mapType, String fullMapPath) { json = new Json(); mapEntities = new Array<Entity>(10); currentMapType = mapType; playerStart = new Vector2(0, 0); playerStartPositionRect = new Vector2(0, 0); closestPlayerStartPosition = new Vector2(0, 0); convertedUnits = new Vector2(0, 0); if (fullMapPath == null || fullMapPath.isEmpty()) { Gdx.app.debug(TAG, "Map is invalid"); return; } Utility.loadMapAsset(fullMapPath); if (Utility.isAssetLoaded(fullMapPath)) { currentMap = Utility.getMapAsset(fullMapPath); } else { Gdx.app.debug(TAG, "Map not loaded"); return; } collisionLayer = currentMap.getLayers().get(COLLISION_LAYER); if (collisionLayer == null) { Gdx.app.debug(TAG, "No collision layer!"); } portalLayer = currentMap.getLayers().get(PORTAL_LAYER); if (portalLayer == null) { Gdx.app.debug(TAG, "No portal layer!"); } spawnsLayer = currentMap.getLayers().get(SPAWNS_LAYER); if (spawnsLayer == null) { Gdx.app.debug(TAG, "No spawn layer!"); } else { setClosestStartPosition(playerStart); } npcStartPositions = getNPCStartPositions(); specialNPCStartPositions = getSpecialNPCStartPositions(); }
public Map<String, Vector2> getTriggerStartWith(String name) { final HashMap<String, Vector2> triggerObjects = new HashMap<String, Vector2>(); final Iterator<MapObject> mapObjectIterator = tiledMap.getLayers().get("trigger").getObjects().iterator(); final int tileWidth = (Integer) tiledMap.getProperties().get("tilewidth"); final int tileHeight = (Integer) tiledMap.getProperties().get("tileheight"); while (mapObjectIterator.hasNext()) { final MapObject mapObject = mapObjectIterator.next(); if (mapObject != null && mapObject.getName() != null && mapObject.getName().startsWith(name)) { triggerObjects.put( mapObject.getName(), new Vector2( (Float) mapObject.getProperties().get("x") / tileWidth, (Float) mapObject.getProperties().get("y") / tileHeight)); } } return triggerObjects; }
public LevelRenderer( final TiledMap map, final AssetManager assets, final Batch batch, final float unitScale) { collectibles = new Array<CollectibleRenderer>(3); Texture background = assets.get( map.getProperties() .get(BACKGROUND_PROPERTY, Assets.DEFAULT_BACKGROUND.fileName, String.class), Texture.class); renderer = new BackgroundTiledMapRenderer(map, unitScale, batch, background); }
@Override public void create() { batch = new SpriteBatch(); world = new World(new Vector2(0, -9.8f), true); ninjaRabbit = EntityFactory.createNinjaRabbit(world); InputProcessor inputProcessor = new InputMultiplexer( new NinjaRabbitInputProcessor(ninjaRabbit), new InputAdapter() { @Override public boolean scrolled(final int amount) { viewport.setUnitsPerPixel( viewport.getUnitsPerPixel() + viewport.getUnitsPerPixel() / amount / 8f); viewport.update(viewport.getScreenWidth(), viewport.getScreenHeight()); return true; } }); Gdx.input.setInputProcessor(inputProcessor); TiledMap tileMap = new TmxMapLoader().load(LEVEL_MAP_FILE); Box2DMapObjectParser objectParser = new Box2DMapObjectParser(1 / PPM); objectParser.load(world, tileMap); tileMapRenderer = new OrthogonalTiledMapRenderer(tileMap, 1 / PPM); viewport = new ScreenViewport(); viewport.setUnitsPerPixel(1 / PPM); viewport.setCamera( new BoundedCamera( 0.0f, tileMap.getProperties().get("width", Integer.class).floatValue() * tileMap.getProperties().get("tilewidth", Integer.class).floatValue() / PPM)); b2dRenderer = new Box2DDebugRenderer(); }
@Override public void render(int[] layers) { beginRender(); for (int layerIdx : layers) { MapLayer layer = map.getLayers().get(layerIdx); if (layer.isVisible()) { if (layer instanceof TiledMapTileLayer) { renderTileLayer((TiledMapTileLayer) layer); } else { for (MapObject object : layer.getObjects()) { renderObject(object); } } } } endRender(); }
@Override public void render() { AnimatedTiledMapTile.updateAnimationBaseTime(); spriteBatch.begin(); for (MapLayer layer : map.getLayers()) { if (layer.isVisible()) { if (layer instanceof TiledMapTileLayer) { renderTileLayer((TiledMapTileLayer) layer); } else { for (MapObject object : layer.getObjects()) { renderObject(object); } } } } spriteBatch.end(); }
private void loadCollisionBoxes() { MapLayer collisionLayer = map.getLayers().get("collisions"); Game.debug("Inflating Collision Rectangles / Tile Data"); for (RectangleMapObject object : collisionLayer.getObjects().getByType(RectangleMapObject.class)) { if (object.getProperties().containsKey("spawnpoint")) { spawnpoint = new Vector2(object.getRectangle().x, object.getRectangle().y); Game.debug("Set Spawn Point to: " + spawnpoint.toString()); } if (!object.getProperties().containsKey("collision")) continue; Game.debug("Added Collision Rectangle at: " + object.getRectangle().toString()); collisions.add(object.getRectangle()); } Game.debug("Added " + collisions.size() + " Collision Rectangles"); }
private void _loadSpecialTilesRoutine(TiledMap map) { // System.out.println("_loadSpecialTilesRoutine"); Iterator<TiledMapTile> tiles = map.getTileSets().getTileSet(0).iterator(); // System.out.println(map.getTileSets().getTileSet(0).size()); while (tiles.hasNext()) { TiledMapTile tile = tiles.next(); if (tile.getProperties().containsKey("debug")) { // System.out.println("debug tile found!!"); debugCell = new Cell(); debugCell.setTile(tile); } else if (tile.getProperties().containsKey("collidable")) { // System.out.println("collidable tile found!!"); collidableCell = new Cell(); collidableCell.setTile(tile); } } }
private void initCollidableBlocks() { layer = (TiledMapTileLayer) map.getLayers().get("Collidable"); for (int row = 0; row < layer.getHeight(); row++) { for (int col = 0; col < layer.getWidth(); col++) { Cell cell = layer.getCell(col, row); // Checamos para ver se existe algum tile aqui if (cell == null || cell.getTile() == null) { collidableArea[col][row] = false; continue; } // adicionamos essa posição à collidableBlocks; collidableArea[col][row] = true; } } }
public void init() { // camera = new OrthographicCamera(); // viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera); float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); camera = new OrthographicCamera(); viewport = new FitViewport(SCENE_WIDTH, SCENE_HEIGHT, camera); // 設置鏡頭大小 camera.setToOrtho(false, w, h); // y軸向上 camera.update(); batch = new SpriteBatch(); deltaTime = 0.0f; xPosition = 0; // Load atlases and textures // hero1Atlas = new TextureAtlas(Gdx.files.internal("hero/shana/RunningRight.pack")); hero1Atlas = new TextureAtlas(Gdx.files.internal("hero/shana/heroShana.pack")); TextureRegion[] frameWalkingRight = new TextureRegion[8]; frameWalkingRight[0] = hero1Atlas.findRegion("RunningRight1"); frameWalkingRight[1] = hero1Atlas.findRegion("RunningRight2"); frameWalkingRight[2] = hero1Atlas.findRegion("RunningRight3"); frameWalkingRight[3] = hero1Atlas.findRegion("RunningRight4"); frameWalkingRight[4] = hero1Atlas.findRegion("RunningRight5"); frameWalkingRight[5] = hero1Atlas.findRegion("RunningRight6"); frameWalkingRight[6] = hero1Atlas.findRegion("RunningRight7"); frameWalkingRight[7] = hero1Atlas.findRegion("RunningRight8"); // Load animations // animation = new Animation(FRAME_DURATION, hero1Atlas.getRegions(), // Animation.PlayMode.LOOP); animation = new Animation(FRAME_DURATION, frameWalkingRight); HUDBatch = new SpriteBatch(); font1 = new BitmapFont(); font1.setColor(Color.YELLOW); tiledMap = new TmxMapLoader().load("map/newmap2.tmx"); tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap); foregroundLayer = (TiledMapTileLayer) tiledMap.getLayers().get("foreground"); }
private void updateCamera() { direction.set(0.0f, 0.0f); int mouseX = Gdx.input.getX(); int mouseY = Gdx.input.getY(); int width = Gdx.graphics.getWidth(); int height = Gdx.graphics.getHeight(); if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || (Gdx.input.isTouched() && mouseX < width * 0.75f)) { direction.x = -1; } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || (Gdx.input.isTouched() && mouseX > width * 0.75f)) { direction.x = 1; } if (Gdx.input.isKeyPressed(Input.Keys.UP) || (Gdx.input.isTouched() && mouseY < height * 0.75f)) { direction.y = 1; } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) || (Gdx.input.isTouched() && mouseY > height * 0.75f)) { direction.y = -1; } direction.nor().scl(CAMERA_SPEED * Gdx.graphics.getDeltaTime()); camera.position.x += direction.x; camera.position.y += direction.y; TiledMapTileLayer layer = (TiledMapTileLayer) tiledMap.getLayers().get(0); float cameraMinX = viewport.getWorldWidth() * 0.5f; float cameraMinY = viewport.getWorldHeight() * 0.5f; float cameraMaxX = layer.getWidth() * layer.getTileWidth() + (playerWidth - cameraMinX); float cameraMaxY = layer.getHeight() * layer.getTileHeight() - cameraMinY; camera.position.x = MathUtils.clamp(sprite.getX(), cameraMinX, cameraMaxX); camera.position.y = MathUtils.clamp(sprite.getY(), cameraMinY, cameraMaxY); camera.update(); }
@Override public void render(GamePlayManager main) { boolean drewBG = false; boolean drewPlayer = false; beginRender(); for (MapLayer layer : map.getLayers()) { if (!layer.getName().equals("AINODEMAP")) { if (!drewBG && layer.getProperties().containsKey("bg")) { drewBG = true; main.drawBackGround(spriteBatch); } if (!drewPlayer && layer.getProperties().containsKey("player")) { main.drawPlayerLayer(spriteBatch); drewPlayer = true; } if (layer.isVisible()) { renderTileLayer((TiledMapTileLayer) layer); } } } endRender(); }
private boolean _loadLayersRoutine(TiledMap map) throws STiledLevelException { tile_width = map.getProperties().get("tilewidth", Integer.class); tile_height = map.getProperties().get("tileheight", Integer.class); map_width = map.getProperties().get("width", Integer.class); map_height = map.getProperties().get("height", Integer.class); decorativeLayer = (TiledMapTileLayer) map.getLayers().get("DecorativeLayer"); if (decorativeLayer == null) { throw new STiledLevelException( "Missing DecorativeLayer. Add DecorativeLayer to your tiled map"); } metaLayer = (TiledMapTileLayer) map.getLayers().get("MetaLayer"); if (metaLayer == null) { throw new STiledLevelException("Missing MetaLayer. Add MetaLayer to your tiled map"); } objectLayer = map.getLayers().get("ObjectLayer"); if (objectLayer == null) { throw new STiledLevelException("Missing ObjectLayer. Add ObjectLayer to your tiled map"); } return true; }
public void draw(Batch batch) { batch.begin(); final Iterator<MapLayer> layerIterator = tiledMap.getLayers().iterator(); while (layerIterator.hasNext()) { final MapLayer mapLayer = layerIterator.next(); if (mapLayer instanceof TiledMapTileLayer) { final TiledMapTileLayer tiledLayer = (TiledMapTileLayer) mapLayer; final int width = tiledLayer.getWidth(); final int height = tiledLayer.getHeight(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { final TiledMapTileLayer.Cell cell = tiledLayer.getCell(x, y); if (cell == null) { continue; } final TextureRegion textureRegion = cell.getTile().getTextureRegion(); batch.draw(textureRegion, x + offset.x, y + offset.y, 1, 1); } } } } batch.end(); }
private void generatePlatforms(String LayerName) { TiledMapTileLayer layer = (TiledMapTileLayer) tileMap.getLayers().get(LayerName); tileSize = layer.getTileHeight(); BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); // iterate over tiles and build box2d objects and chaining them together for (int row = 0; row < layer.getHeight(); row++) { for (int col = 0; col < layer.getWidth(); col++) { Cell cell = layer.getCell(col, row); if (cell == null) continue; if (cell.getTile() == null) continue; bdef.type = BodyType.StaticBody; bdef.position.set((col + 0.5f) * tileSize / PPM, (row + 0.5f) * tileSize / PPM); ChainShape cs = new ChainShape(); Vector2[] v = new Vector2[3]; v[0] = new Vector2(-tileSize / 2 / PPM, -tileSize / 2 / PPM); v[1] = new Vector2(-tileSize / 2 / PPM, +tileSize / 2 / PPM); v[2] = new Vector2(+tileSize / 2 / PPM, +tileSize / 2 / PPM); cs.createChain(v); fdef.friction = 0; fdef.shape = cs; fdef.filter.categoryBits = B2DVars.BIT_BLOCKS; fdef.filter.maskBits = B2DVars.BIT_PLAYER; fdef.isSensor = false; world.createBody(bdef).createFixture(fdef); } } }
public TiledMapTileLayer.Cell getCell() { TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(2); return layer.getCell( (int) (body.getPosition().x * StarWars.PPM / 16), (int) (body.getPosition().y * StarWars.PPM / 16)); }
public void LoadTilesWithBody(TiledMap tileMap, World world) { TiledMapTileLayer layer = (TiledMapTileLayer) tileMap.getLayers().get("Body"); BodyDef def = new BodyDef(); FixtureDef fdef = new FixtureDef(); def.type = BodyType.StaticBody; FixtureDef sensorDef = new FixtureDef(); sensorDef.isSensor = true; int coinID = 0; int starID = 0; float tileSize = layer.getTileHeight(); GameTileRenderer.width = layer.getWidth(); GameTileRenderer.height = layer.getHeight(); GameTileRenderer.tlz = new int[GameTileRenderer.width][GameTileRenderer.height]; for (int[] row : GameTileRenderer.tlz) Arrays.fill(row, -1); for (int row = 0; row < layer.getHeight(); row++) { for (int col = 0; col < layer.getWidth(); col++) { Cell cell = layer.getCell(col, row); if (cell == null || cell.getTile() == null) continue; // create def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); float x = col * tileSize / B2D.PPM; float y = row * tileSize / B2D.PPM; int tileType = cell.getTile().getId(); tileType--; if (tileType <= 14) { // e element normal int id = tileType; GameTileRenderer.tlz[col][row] = id; ChainShape shape = new ChainShape(); Vector2[] v = new Vector2[5]; if (tileType < 14) { v[0] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[1] = new Vector2(-tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[2] = new Vector2(tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[3] = new Vector2(tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[4] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); } else { v[0] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[1] = new Vector2(-tileSize / 2 / B2D.PPM, 0 / B2D.PPM); v[2] = new Vector2(tileSize / 2 / B2D.PPM, 0 / B2D.PPM); v[3] = new Vector2(tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[4] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); } shape.createChain(v); fdef.friction = 0; fdef.shape = shape; if (tileType < 14) world.createBody(def).createFixture(fdef).setUserData("tile"); else world.createBody(def).createFixture(fdef).setUserData("die"); } if (tileType == 16) { // coin Cell c = new Cell(); layer.setCell(col, row, c); if (GameStats.AddTheCoin(coinID)) { GameWorld.cr.coins.add(new Vector2(x, y)); def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); ChainShape shape = new ChainShape(); Vector2[] v = new Vector2[5]; v[0] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[1] = new Vector2(-tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[2] = new Vector2(tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[3] = new Vector2(tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[4] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); shape.createChain(v); sensorDef.shape = shape; TileData tmp = new TileData(coinID, "coin"); coinID++; world.createBody(def).createFixture(sensorDef).setUserData(tmp); } else { GameWorld.cr.coins.add(null); coinID++; } } if (tileType == 17) { Cell c = new Cell(); layer.setCell(col, row, c); GameWorld.cr.stars.add(new Vector2(x, y)); def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); ChainShape shape = new ChainShape(); Vector2[] v = new Vector2[5]; v[0] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[1] = new Vector2(-tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[2] = new Vector2(tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[3] = new Vector2(tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[4] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); shape.createChain(v); sensorDef.shape = shape; TileData tmp = new TileData(starID, "star"); starID++; world.createBody(def).createFixture(sensorDef).setUserData(tmp); } if (tileType == 18) { Cell c = new Cell(); layer.setCell(col, row, c); GameMap.flagposition = new Vector2(x, y); def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); ChainShape shape = new ChainShape(); Vector2[] v = new Vector2[5]; v[0] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[1] = new Vector2(-tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[2] = new Vector2(tileSize / 2 / B2D.PPM, tileSize / 2 / B2D.PPM); v[3] = new Vector2(tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); v[4] = new Vector2(-tileSize / 2 / B2D.PPM, -tileSize / 2 / B2D.PPM); shape.createChain(v); sensorDef.shape = shape; world.createBody(def).createFixture(sensorDef).setUserData("finish"); } // tiles.add(new Vector2(x, y)); } } }
public void LoadGameTiles(TiledMap tileMap, World world) { TiledMapTileLayer layer = (TiledMapTileLayer) tileMap.getLayers().get("Tiles"); BodyDef def = new BodyDef(); FixtureDef fdef = new FixtureDef(); def.type = BodyType.StaticBody; FixtureDef sensorDef = new FixtureDef(); sensorDef.isSensor = true; int coinID = 0; int starID = 0; float tileSize = layer.getTileHeight(); GameTileRenderer.width = layer.getWidth(); GameTileRenderer.height = layer.getHeight(); GameTileRenderer.tlz = new int[GameTileRenderer.width][GameTileRenderer.height]; for (int[] row : GameTileRenderer.tlz) Arrays.fill(row, -1); for (int row = 0; row < layer.getHeight(); row++) { for (int col = 0; col < layer.getWidth(); col++) { Cell cell = layer.getCell(col, row); if (cell == null || cell.getTile() == null) continue; // create def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); float x = col * tileSize / B2D.PPM; float y = row * tileSize / B2D.PPM; int tileType = cell.getTile().getId(); tileType--; if (tileType == 11) { GameStats.GameSpawnPosition = new Vector2(x, y); Cell c = new Cell(); layer.setCell(col, row, c); } if (tileType == 12) { Cell c = new Cell(); layer.setCell(col, row, c); GameMap.flagposition = new Vector2(x, y); def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); sensorDef.shape = createShape(tileSize); world.createBody(def).createFixture(sensorDef).setUserData("finish"); } if (tileType == 8) { // e spike int id = tileType; GameTileRenderer.tlz[col][row] = id; fdef.friction = 0; fdef.shape = createSpikeShape(tileSize); world.createBody(def).createFixture(fdef).setUserData("die"); } if (tileType == 0 || tileType == 4) { // e element normal int id = tileType; GameTileRenderer.tlz[col][row] = id; fdef.friction = 0; fdef.shape = createShape(tileSize); world.createBody(def).createFixture(fdef).setUserData("tile"); } if ((tileType >= 1 && tileType <= 3) || (tileType >= 5 && tileType <= 7)) { // e bara int id = tileType; GameTileRenderer.tlz[col][row] = id; fdef.friction = 0; fdef.shape = createShape(tileSize); world.createBody(def).createFixture(fdef).setUserData("rail"); } if (tileType == 10) { // coin Cell c = new Cell(); layer.setCell(col, row, c); if (GameStats.AddTheCoin(coinID)) { GameWorld.cr.coins.add(new Vector2(x, y)); def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); sensorDef.shape = createShape(tileSize); TileData tmp = new TileData(coinID, "coin"); coinID++; world.createBody(def).createFixture(sensorDef).setUserData(tmp); } else { GameWorld.cr.coins.add(null); coinID++; } } if (tileType == 9) { // coin Cell c = new Cell(); layer.setCell(col, row, c); GameWorld.cr.stars.add(new Vector2(x, y)); def.position.set((col + 0.5f) * tileSize / B2D.PPM, (row + 0.5f) * tileSize / B2D.PPM); sensorDef.shape = createShape(tileSize); TileData tmp = new TileData(starID, "star"); starID++; world.createBody(def).createFixture(sensorDef).setUserData(tmp); } } } }
public void load(World world, TiledMap map) throws Exception { // Set platform box2D data if (map.getLayers().get("Platform") != null && map.getLayers().get("Platform").getObjects().getCount() != 0) { map.getLayers().get("Platform").getProperties().put("type", Constants.BOX2D_OBJECT); map.getLayers() .get("Platform") .getProperties() .put("categoryBits", Constants.PLATFORM_CATEGORY); map.getLayers().get("Platform").getProperties().put("maskBits", Constants.PLATFORM_MASK); map.getLayers() .get("Platform") .getProperties() .put("userData", Constants.BOX2D_PLATFORM_USERDATA); } else { throw new Exception( "Error: Map have no platforms (The object layer of platforms has to be named as 'Platform')"); } // Set spikes' box2D data if (map.getLayers().get("Spike") != null && map.getLayers().get("Spike").getObjects().getCount() != 0) { map.getLayers().get("Spike").getProperties().put("type", Constants.BOX2D_OBJECT); map.getLayers().get("Spike").getProperties().put("categoryBits", Constants.PLATFORM_CATEGORY); map.getLayers().get("Spike").getProperties().put("userData", Constants.BOX2D_SPIKE_USERDATA); map.getLayers().get("Spike").getProperties().put("maskBits", Constants.SPIKE_MASK); } // Set moving platform's box2D data if (map.getLayers().get("MovingPlatform") != null && map.getLayers().get("MovingPlatform").getObjects().getCount() != 0) { int i = 0; for (MapObject mo : map.getLayers().get("MovingPlatform").getObjects()) { mo.setName("mplatform" + i); mo.getProperties().put("type", Constants.BOX2D_OBJECT); mo.getProperties().put("bodyType", Constants.BOX2D_KINEMATIC_BODY_TYPE); mo.getProperties().put("userData", Constants.BOX2D_PLATFORM_USERDATA); mo.getProperties().put("categoryBits", Constants.PLATFORM_CATEGORY); mo.getProperties().put("maskBits", Constants.PLATFORM_MASK); i++; } } // Check if spawnpoint exists if (map.getLayers().get("Points") == null || map.getLayers().get("Points").getObjects().get("Spawnpoint") == null) { throw new Exception( "Error: Map have no spawnpoint (Check that you have object layer called Points and element called 'Spawnpoint')"); } // Check if goal exists in map and set box2D data if (map.getLayers().get("Points").getObjects().get("Goal") != null) { map.getLayers() .get("Points") .getObjects() .get("Goal") .getProperties() .put("type", Constants.BOX2D_OBJECT); map.getLayers() .get("Points") .getObjects() .get("Goal") .getProperties() .put("categoryBits", Constants.GOAL_CATEGORY); map.getLayers() .get("Points") .getObjects() .get("Goal") .getProperties() .put("userData", Constants.BOX2D_GOAL_USERDATA); map.getLayers().get("Points").getProperties().put("maskBits", Constants.GOAL_MASK); } else { throw new Exception( "Error: Map have no goal (Check that you have object layer called Points and element called 'Goal')"); } super.load(world, map); }
public void loadGameMap() { tiledMap = new TmxMapLoader().load("maps/testMap.tmx"); renderer = new OrthogonalTiledMapRenderer(tiledMap, unitScale); wallLayer = (TiledMapTileLayer) tiledMap.getLayers().get("Walls"); }