List of usage examples for com.badlogic.gdx.maps.tiled TiledMapTileLayer setCell
public void setCell(int x, int y, Cell cell)
From source file:MyGdxGame.java
License:Apache License
private void updateKoala(float deltaTime) { if (deltaTime == 0) return;/* w w w .j a v a 2 s . c om*/ koala.stateTime += deltaTime; // check input and apply to velocity & state if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.5f, 1)) && koala.grounded) { koala.velocity.y += Koala.JUMP_VELOCITY; koala.state = Koala.State.Jumping; koala.grounded = false; } if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) { koala.velocity.x = -Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = false; } if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) { koala.velocity.x = Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = true; } // apply gravity if we are falling koala.velocity.add(0, GRAVITY); // clamp the velocity to the maximum, x-axis only if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) { koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY; } // clamp the velocity to 0 if it's < 1, and set the state to standign if (Math.abs(koala.velocity.x) < 1) { koala.velocity.x = 0; if (koala.grounded) koala.state = Koala.State.Standing; } // multiply by delta time so we know how far we go // in this frame koala.velocity.scl(deltaTime); // perform collision detection & response, on each axis, separately // if the koala is moving right, check the tiles to the right of it's // right bounding box edge, otherwise check the ones to the left Rectangle koalaRect = rectPool.obtain(); koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT); int startX, startY, endX, endY; if (koala.velocity.x > 0) { startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x); } else { startX = endX = (int) (koala.position.x + koala.velocity.x); } startY = (int) (koala.position.y); endY = (int) (koala.position.y + Koala.HEIGHT); getTiles(startX, startY, endX, endY, tiles); koalaRect.x += koala.velocity.x; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { koala.velocity.x = 0; break; } } koalaRect.x = koala.position.x; // if the koala is moving upwards, check the tiles to the top of it's // top bounding box edge, otherwise check the ones to the bottom if (koala.velocity.y > 0) { startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y); } else { startY = endY = (int) (koala.position.y + koala.velocity.y); } startX = (int) (koala.position.x); endX = (int) (koala.position.x + Koala.WIDTH); getTiles(startX, startY, endX, endY, tiles); koalaRect.y += koala.velocity.y; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { // we actually reset the koala y-position here // so it is just below/above the tile we collided with // this removes bouncing :) if (koala.velocity.y > 0) { koala.position.y = tile.y - Koala.HEIGHT; // we hit a block jumping upwards, let's destroy it! TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls"); layer.setCell((int) tile.x, (int) tile.y, null); } else { koala.position.y = tile.y + tile.height; // if we hit the ground, mark us as grounded so we can jump koala.grounded = true; } koala.velocity.y = 0; break; } } rectPool.free(koalaRect); // unscale the velocity by the inverse delta time and set // the latest position koala.position.add(koala.velocity); koala.velocity.scl(1 / deltaTime); // Apply damping to the velocity on the x-axis so we don't // walk infinitely once a key was pressed koala.velocity.x *= Koala.DAMPING; }
From source file:ca.hiphiparray.amazingmaze.MapFactory.java
License:Open Source License
/** * Return a map generated with the {@link MapFactory}'s parameters. * * @return a tiled map.// w w w. j a va 2 s . c o m */ public TiledMap generateMap() { TiledMap map = new TiledMap(); map.getTileSets().addTileSet(assets.tiles); TiledMapTileLayer backgroundLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE, MazeScreen.TILE_SIZE); backgroundLayer.setName(BACKGROUND_LAYER); for (int c = 0; c < backgroundLayer.getWidth(); c++) { for (int r = 0; r < backgroundLayer.getHeight(); r++) { Cell cell = new Cell(); cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BACKGROUND))); backgroundLayer.setCell(c, r, cell); } } map.getLayers().add(backgroundLayer); final int gateSpace = 2; final int extraRoom = 3; List<Integer> splits = generateWireLocations(); TiledMapTileLayer objectLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE, MazeScreen.TILE_SIZE); objectLayer.setName(OBJECT_LAYER); TiledMapTileLayer wireLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE, MazeScreen.TILE_SIZE); wireLayer.setName(WIRE_LAYER); for (int col : splits) { // Place the middle barriers and the unknown wires. boolean upperOutput = random.nextBoolean(); Circuit upperGate = new Circuit(upperOutput, random); Circuit lowerGate = new Circuit(!upperOutput, random); Point highLocation = new Point(col, height - gateSpace); Point lowLocation = new Point(col, gateSpace - 1); if (Circuit.evaluateGate(upperGate.getGate(), upperGate.isInputA(), upperGate.isInputB())) { gateOn.add(upperGate); } else { gateOn.add(lowerGate); } placeUpperCircuit(objectLayer, upperGate, highLocation); placeLowerCircuit(objectLayer, lowerGate, lowLocation); int barrierLoc = randomInt(gateSpace + extraRoom, height - (gateSpace + extraRoom)); Cell cell = new Cell(); cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BARRIER))); objectLayer.setCell(col, barrierLoc, cell); for (int r = barrierLoc - 1; r >= gateSpace; r--) { // Place the lower wires. WireCell wire = new WireCell(!upperOutput); wire.setTile(assets.tiles .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.UNKNOWN))); wireLayer.setCell(col, r, wire); } for (int r = barrierLoc + 1; r < height - gateSpace; r++) { // Place the upper wires. WireCell wire = new WireCell(upperOutput); wire.setTile(assets.tiles .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, TileIDs.UNKNOWN))); wireLayer.setCell(col, r, wire); } } for (int c = 0; c < width; c++) { if (!splits.contains(c)) { Cell cell = new Cell(); cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BARRIER))); objectLayer.setCell(c, gateSpace, cell); cell = new Cell(); cell.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.BARRIER))); objectLayer.setCell(c, height - gateSpace - 1, cell); } } map.getLayers().add(objectLayer); map.getLayers().add(wireLayer); TiledMapTileLayer powerUpLayer = new TiledMapTileLayer(width, height, MazeScreen.TILE_SIZE, MazeScreen.TILE_SIZE); powerUpLayer.setName(ITEM_LAYER); for (int c = 1; c < width; c++) { if (!splits.contains(c)) { if (random.nextDouble() <= 0.25) { placeFish(powerUpLayer, c, gateSpace); c++; } else if (random.nextDouble() <= 0.1) { placeCheese(powerUpLayer, c, gateSpace); c++; } } } map.getLayers().add(powerUpLayer); return map; }
From source file:ca.hiphiparray.amazingmaze.MapFactory.java
License:Open Source License
/** * Place a piece of cheese on the map.//w w w .ja va 2 s .c o m * * @param layer the layer to add the cheese to. * @param col the column to place the cheese on. * @param gateSpace how much space to leave for gates. */ private void placeCheese(TiledMapTileLayer layer, int col, int gateSpace) { int row = randomInt(gateSpace + 1, height - gateSpace - 1); Cell cheese = new Cell(); cheese.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.CHEESE))); layer.setCell(col, row, cheese); }
From source file:ca.hiphiparray.amazingmaze.MapFactory.java
License:Open Source License
/** * Place a fish on the map./*from ww w. j a v a 2 s.co m*/ * * @param layer the layer to add the fish to. * @param col the column to place the fish on. * @param gateSpace how much space to leave for gates. */ private void placeFish(TiledMapTileLayer layer, int col, int gateSpace) { FishCell fish; double r = random.nextDouble(); if (r <= 0.2) { fish = new FishCell( assets.tiles.getTile(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.FISH, TileIDs.BLUE)), FishColour.BLUE); } else if (r <= 0.4) { fish = new FishCell( assets.tiles.getTile(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.FISH, TileIDs.PURPLE)), FishColour.PURPLE); } else if (r <= 0.6) { fish = new FishCell( assets.tiles.getTile(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.FISH, TileIDs.GREEN)), FishColour.GREEN); } else if (r <= 0.8) { fish = new FishCell( assets.tiles.getTile(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.FISH, TileIDs.RED)), FishColour.RED); } else { fish = new FishCell( assets.tiles.getTile(TileIDs.computeID(TileIDs.POWERUP_RANGE, TileIDs.FISH, TileIDs.ORANGE)), FishColour.ORANGE); } int row = randomInt(gateSpace + 1, height - gateSpace - 1); layer.setCell(col, row, fish); }
From source file:ca.hiphiparray.amazingmaze.MapFactory.java
License:Open Source License
/** * Place the given circuit on the given layer. * It is placed at the top, facing down. * * @param layer the layer to place the circuit on. * @param circuit the circuit to use./*from w w w . j a v a2 s . co m*/ * @param location the location of the gate being placed. */ private void placeUpperCircuit(TiledMapTileLayer layer, Circuit circuit, Point location) { Cell gate = new Cell(); gate.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.GATE_RANGE, Circuit.getID(circuit.getGate()), TileIDs.UNKNOWN, TileIDs.DOWN_GATE))); layer.setCell(location.x, location.y, gate); gateLocations.add(new Point(location)); Cell inputAStart = new Cell(); int inputAPowerID = circuit.isInputA() ? TileIDs.ON : TileIDs.OFF; inputAStart.setTile( assets.tiles.getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, inputAPowerID))); layer.setCell(location.x - 1, location.y + 1, inputAStart); Cell inputATurn = new Cell(); inputATurn.setTile(assets.tiles .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, inputAPowerID, TileIDs.UP_RIGHT))); layer.setCell(location.x - 1, location.y, inputATurn); Cell inputBStart = new Cell(); int inputBPowerID = circuit.isInputB() ? TileIDs.ON : TileIDs.OFF; inputBStart.setTile( assets.tiles.getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, inputBPowerID))); layer.setCell(location.x + 1, location.y + 1, inputBStart); Cell inputBTurn = new Cell(); inputBTurn.setTile(assets.tiles .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, inputBPowerID, TileIDs.UP_LEFT))); layer.setCell(location.x + 1, location.y, inputBTurn); }
From source file:ca.hiphiparray.amazingmaze.MapFactory.java
License:Open Source License
/** * Place the given circuit on the given layer. * It is placed at the bottom, facing up. * * @param layer the layer to place the circuit on. * @param circuit the circuit to use.// w w w .j av a 2 s . c o m * @param location the location of the gate being placed. */ private void placeLowerCircuit(TiledMapTileLayer layer, Circuit circuit, Point location) { Cell gate = new Cell(); gate.setTile(assets.tiles.getTile(TileIDs.computeID(TileIDs.GATE_RANGE, Circuit.getID(circuit.getGate()), TileIDs.UNKNOWN, TileIDs.UP_GATE))); layer.setCell(location.x, location.y, gate); gateLocations.add(new Point(location)); Cell inputAStart = new Cell(); int inputAPowerID = circuit.isInputA() ? TileIDs.ON : TileIDs.OFF; inputAStart.setTile( assets.tiles.getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, inputAPowerID))); layer.setCell(location.x - 1, location.y - 1, inputAStart); Cell inputATurn = new Cell(); inputATurn.setTile(assets.tiles .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, inputAPowerID, TileIDs.DOWN_RIGHT))); layer.setCell(location.x - 1, location.y, inputATurn); Cell inputBStart = new Cell(); int inputBPowerID = circuit.isInputB() ? TileIDs.ON : TileIDs.OFF; inputBStart.setTile( assets.tiles.getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.VERTICAL, inputBPowerID))); layer.setCell(location.x + 1, location.y - 1, inputBStart); Cell inputBTurn = new Cell(); inputBTurn.setTile(assets.tiles .getTile(TileIDs.computeID(TileIDs.WIRE_RANGE, TileIDs.TURN, inputBPowerID, TileIDs.DOWN_LEFT))); layer.setCell(location.x + 1, location.y, inputBTurn); }
From source file:ca.hiphiparray.amazingmaze.Player.java
License:Open Source License
/** Handle the player collecting fish. */ private void collectFish() { Rectangle thisBox = getBoundingRectangle(); for (int i = 0; i < maze.fishBoxes.size; i++) { if (thisBox.overlaps(maze.fishBoxes.get(i))) { TiledMapTileLayer layer = (TiledMapTileLayer) maze.map.getLayers().get(MapFactory.ITEM_LAYER); int x = (int) maze.fishBoxes.get(i).x; int y = (int) maze.fishBoxes.get(i).y; FishColour colour = ((FishCell) layer.getCell(x, y)).getColour(); layer.setCell(x, y, null); maze.fishBoxes.removeIndex(i); switch (colour) { case BLUE: blueCollected++;/*from w w w.j a v a 2 s . c o m*/ break; case PURPLE: purpleCollected++; break; case GREEN: greenCollected++; break; case RED: redCollected++; break; case ORANGE: orangeCollected++; break; } break; } } }
From source file:ca.hiphiparray.amazingmaze.Player.java
License:Open Source License
/** Handle the player collecting cheese. */ private void collectCheese() { Rectangle thisBox = getBoundingRectangle(); for (int i = 0; i < maze.cheeseBoxes.size; i++) { if (thisBox.overlaps(maze.cheeseBoxes.get(i))) { TiledMapTileLayer layer = (TiledMapTileLayer) maze.map.getLayers().get(MapFactory.ITEM_LAYER); int x = (int) maze.cheeseBoxes.get(i).x; int y = (int) maze.cheeseBoxes.get(i).y; layer.setCell(x, y, null); maze.cheeseBoxes.removeIndex(i); lives++;// w w w .ja v a 2 s .com maze.updateLives(-1); break; } } }
From source file:com.badlogic.gdx.tests.bench.TiledMapBench.java
License:Apache License
@Override public void create() { float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); camera = new OrthographicCamera(); camera.setToOrtho(false, (w / h) * 320, 320); camera.update();/*from w w w. ja v a2s.co m*/ cameraController = new OrthoCamController(camera); Gdx.input.setInputProcessor(cameraController); font = new BitmapFont(); batch = new SpriteBatch(); { tiles = new Texture(Gdx.files.internal("data/maps/tiled/tiles.png")); TextureRegion[][] splitTiles = TextureRegion.split(tiles, 32, 32); map = new TiledMap(); MapLayers layers = map.getLayers(); for (int l = 0; l < 20; l++) { TiledMapTileLayer layer = new TiledMapTileLayer(150, 100, 32, 32); for (int x = 0; x < 150; x++) { for (int y = 0; y < 100; y++) { int ty = (int) (Math.random() * splitTiles.length); int tx = (int) (Math.random() * splitTiles[ty].length); Cell cell = new Cell(); cell.setTile(new StaticTiledMapTile(splitTiles[ty][tx])); layer.setCell(x, y, cell); } } layers.add(layer); } } renderer = new OrthogonalTiledMapRenderer(map); }
From source file:com.badlogic.gdx.tests.superkoalio.SuperKoalio.java
License:Apache License
private void updateKoala(float deltaTime) { if (deltaTime == 0) return;//from w ww . j a v a 2 s . c o m koala.stateTime += deltaTime; // check input and apply to velocity & state if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.75f, 1)) && koala.grounded) { koala.velocity.y += Koala.JUMP_VELOCITY; koala.state = Koala.State.Jumping; koala.grounded = false; } if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) { koala.velocity.x = -Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = false; } if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) { koala.velocity.x = Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = true; } // apply gravity if we are falling koala.velocity.add(0, GRAVITY); // clamp the velocity to the maximum, x-axis only if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) { koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY; } // clamp the velocity to 0 if it's < 1, and set the state to standign if (Math.abs(koala.velocity.x) < 1) { koala.velocity.x = 0; if (koala.grounded) koala.state = Koala.State.Standing; } // multiply by delta time so we know how far we go // in this frame koala.velocity.scl(deltaTime); // perform collision detection & response, on each axis, separately // if the koala is moving right, check the tiles to the right of it's // right bounding box edge, otherwise check the ones to the left Rectangle koalaRect = rectPool.obtain(); koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT); int startX, startY, endX, endY; if (koala.velocity.x > 0) { startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x); } else { startX = endX = (int) (koala.position.x + koala.velocity.x); } startY = (int) (koala.position.y); endY = (int) (koala.position.y + Koala.HEIGHT); getTiles(startX, startY, endX, endY, tiles); koalaRect.x += koala.velocity.x; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { koala.velocity.x = 0; break; } } koalaRect.x = koala.position.x; // if the koala is moving upwards, check the tiles to the top of it's // top bounding box edge, otherwise check the ones to the bottom if (koala.velocity.y > 0) { startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y); } else { startY = endY = (int) (koala.position.y + koala.velocity.y); } startX = (int) (koala.position.x); endX = (int) (koala.position.x + Koala.WIDTH); getTiles(startX, startY, endX, endY, tiles); koalaRect.y += koala.velocity.y; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { // we actually reset the koala y-position here // so it is just below/above the tile we collided with // this removes bouncing :) if (koala.velocity.y > 0) { koala.position.y = tile.y - Koala.HEIGHT; // we hit a block jumping upwards, let's destroy it! TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); layer.setCell((int) tile.x, (int) tile.y, null); } else { koala.position.y = tile.y + tile.height; // if we hit the ground, mark us as grounded so we can jump koala.grounded = true; } koala.velocity.y = 0; break; } } rectPool.free(koalaRect); // unscale the velocity by the inverse delta time and set // the latest position koala.position.add(koala.velocity); koala.velocity.scl(1 / deltaTime); // Apply damping to the velocity on the x-axis so we don't // walk infinitely once a key was pressed koala.velocity.x *= Koala.DAMPING; }