public boolean isTileCollidable(int column, int row) { // System.out.println(column + "," + row + " testing"); Cell tileCell = metaLayer.getCell(column, row); if (tileCell != null) { TiledMapTile tile = tileCell.getTile(); MapProperties properties = tile.getProperties(); if (properties.containsKey("collidable")) { Object value = properties.get("collidable"); if (value.toString().compareTo("1") == 0) { return true; } } } return false; }
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); } } }
public void Create() { // set tilemap file name m_MapFileName = "map/test.tmx"; // set up the tilemap m_TiledMap = new TmxMapLoader().load(m_MapFileName); m_TiledMapRenderer = new OrthogonalTiledMapRenderer(m_TiledMap, 1f); // set up the box2d physics m_PhysicsWorld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0, 0), true); // attach contact listener to physics world m_PhysicsWorld.setContactListener(new GameContactListener(this)); // set up box2dlights m_RayHandler = new RayHandler(m_PhysicsWorld); m_RayHandler.setAmbientLight(0.0f); // add tilemap collision boxes to physics world for (int i = 0; i < m_TiledMap.getLayers().getCount(); i++) { if (m_TiledMap.getLayers().get(i) instanceof TiledMapTileLayer) { TiledMapTileLayer layer = (TiledMapTileLayer) m_TiledMap.getLayers().get(i); for (int j = 0; j < layer.getWidth(); j++) { for (int k = 0; k < layer.getHeight(); k++) { Cell cell = layer.getCell(j, k); if (cell != null) { TiledMapTile tile = cell.getTile(); if (tile != null) { MapProperties props = tile.getProperties(); if (props.containsKey("blocked")) { float worldX = j * 64 + 32; float worldY = k * 64 + 32; BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.position.set(new Vector2(worldX * WORLD_TO_BOX, worldY * WORLD_TO_BOX)); Body newBody = m_PhysicsWorld.createBody(bodyDef); PolygonShape boxShape = new PolygonShape(); boxShape.setAsBox(32 * WORLD_TO_BOX, 32 * WORLD_TO_BOX); // Create a fixture definition to apply our shape to FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = boxShape; newBody.createFixture(fixtureDef); boxShape.dispose(); } if (props.containsKey("rotate")) { // flip some random tiles to break pattern if (k * j % 3 == 0) layer.getCell(j, k).setFlipVertically(true); if (k * j % 2 == 0) layer.getCell(j, k).setFlipHorizontally(true); } } } } } } } // set up artemis entity system m_ArtemisWorld = new com.artemis.World(); // add the passive systems m_SpineRenderSystem = new SpineRenderSystem(m_GameScreen); m_ArtemisWorld.setSystem(m_SpineRenderSystem, true); // add the systems m_ArtemisWorld.setSystem(new PlayerInputSystem(this)); m_ArtemisWorld.setSystem(new CameraSystem(m_GameScreen.m_Camera)); m_ArtemisWorld.setSystem(new PhysicsSystem()); m_ArtemisWorld.setSystem(new LifecycleSystem(this)); m_ArtemisWorld.setSystem(new SteeringSystem()); // init the world m_ArtemisWorld.initialize(); EntityHelper.LoadObjects( m_GameScreen, m_ArtemisWorld, m_TiledMap, m_PhysicsWorld, m_RayHandler); // add player to entity list m_PlayerEntity = new PlayerEntity(); m_PlayerEntity.AddToWorld(this); checkpointPos = new Vector2(0, 0); checkpointPos.x = m_PlayerEntity.m_Entity.getComponent(PositionComponent.class).m_X; checkpointPos.y = m_PlayerEntity.m_Entity.getComponent(PositionComponent.class).m_Y; // hud items are special cases since they needs to draw on top of border m_VialOne = new SpineComponent("map/vial", 5, 0.75f, -25); m_VialTwo = new SpineComponent("map/vial", 5, 0.65f, -5); m_VialThree = new SpineComponent("map/vial", 5, 0.75f, -50); m_WatchSpine = new SpineComponent("map/watch", 4, 0.75f, 0); if (m_GameScreen.m_Settings.m_WatchFound) { ActivateWatch(); BodyComponent bodyComponent = m_PlayerEntity.m_Entity.getComponent(BodyComponent.class); bodyComponent.m_Body.setTransform(621 * WORLD_TO_BOX, 5961 * WORLD_TO_BOX, 0); } if (m_GameScreen.m_Settings.m_WeaponFound) { ActivateSword(); BodyComponent bodyComponent = m_PlayerEntity.m_Entity.getComponent(BodyComponent.class); bodyComponent.m_Body.setTransform(1965 * WORLD_TO_BOX, 4842 * WORLD_TO_BOX, 0); } if (m_GameScreen.m_Settings.m_WeaponLightFound) { ActivateSwordLight(); BodyComponent bodyComponent = m_PlayerEntity.m_Entity.getComponent(BodyComponent.class); bodyComponent.m_Body.setTransform(6390 * WORLD_TO_BOX, 6462 * WORLD_TO_BOX, 0); } }
@Override public void renderTileLayer(TiledMapTileLayer layer) { final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity()); final int layerWidth = layer.getWidth(); final int layerHeight = layer.getHeight(); final float layerTileWidth = layer.getTileWidth() * unitScale; final float layerTileHeight = layer.getTileHeight() * unitScale; final float layerTileWidth25 = layerTileWidth * 0.25f; final float layerTileWidth50 = layerTileWidth * 0.50f; final float layerTileWidth75 = layerTileWidth * 0.75f; final float layerTileHeight50 = layerTileHeight * 0.50f; final float layerTileHeight150 = layerTileHeight * 1.50f; final int col1 = Math.max(0, (int) (((viewBounds.x - layerTileWidth25) / layerTileWidth75))); final int col2 = Math.min( layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth75) / layerTileWidth75)); final int row1 = Math.max(0, (int) ((viewBounds.y / layerTileHeight150))); final int row2 = Math.min( layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight150) / layerTileHeight)); final float[] vertices = this.vertices; for (int row = row1; row < row2; row++) { for (int col = col1; col < col2; col++) { float x = layerTileWidth75 * col; float y = (col % 2 == (yDown ? 0 : 1) ? 0 : layerTileHeight50) + (layerTileHeight * row); final TiledMapTileLayer.Cell cell = layer.getCell(col, row); if (cell == null) { x += layerTileWidth; continue; } final TiledMapTile tile = cell.getTile(); if (tile != null) { if (tile instanceof AnimatedTiledMapTile) continue; final boolean flipX = cell.getFlipHorizontally(); final boolean flipY = cell.getFlipVertically(); final int rotations = cell.getRotation(); TextureRegion region = tile.getTextureRegion(); float x1 = x; float y1 = y; float x2 = x1 + region.getRegionWidth() * unitScale; float y2 = y1 + region.getRegionHeight() * unitScale; float u1 = region.getU(); float v1 = region.getV2(); float u2 = region.getU2(); float v2 = region.getV(); vertices[X1] = x1; vertices[Y1] = y1; vertices[C1] = color; vertices[U1] = u1; vertices[V1] = v1; vertices[X2] = x1; vertices[Y2] = y2; vertices[C2] = color; vertices[U2] = u1; vertices[V2] = v2; vertices[X3] = x2; vertices[Y3] = y2; vertices[C3] = color; vertices[U3] = u2; vertices[V3] = v2; vertices[X4] = x2; vertices[Y4] = y1; vertices[C4] = color; vertices[U4] = u2; vertices[V4] = v1; if (flipX) { float temp = vertices[U1]; vertices[U1] = vertices[U3]; vertices[U3] = temp; temp = vertices[U2]; vertices[U2] = vertices[U4]; vertices[U4] = temp; } if (flipY) { float temp = vertices[V1]; vertices[V1] = vertices[V3]; vertices[V3] = temp; temp = vertices[V2]; vertices[V2] = vertices[V4]; vertices[V4] = temp; } if (rotations == 2) { float tempU = vertices[U1]; vertices[U1] = vertices[U3]; vertices[U3] = tempU; tempU = vertices[U2]; vertices[U2] = vertices[U4]; vertices[U4] = tempU; float tempV = vertices[V1]; vertices[V1] = vertices[V3]; vertices[V3] = tempV; tempV = vertices[V2]; vertices[V2] = vertices[V4]; vertices[V4] = tempV; break; } spriteBatch.draw(region.getTexture(), vertices, 0, 20); } } } }
@Override public void renderTileLayer(TiledMapTileLayer layer) { final Color batchColor = batch.getColor(); final float color = Color.toFloatBits( batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity()); final int layerWidth = layer.getWidth(); final int layerHeight = layer.getHeight(); final float layerTileWidth = layer.getTileWidth() * unitScale; final float layerTileHeight = layer.getTileHeight() * unitScale; final float layerTileWidth50 = layerTileWidth * 0.50f; final float layerTileHeight50 = layerTileHeight * 0.50f; final int minX = Math.max(0, (int) (((viewBounds.x - layerTileWidth50) / layerTileWidth))); final int maxX = Math.min( layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth + layerTileWidth50) / layerTileWidth)); final int minY = Math.max(0, (int) (((viewBounds.y - layerTileHeight) / layerTileHeight))); final int maxY = Math.min( layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight50)); for (int y = maxY - 1; y >= minY; y--) { float offsetX = (y % 2 == 1) ? layerTileWidth50 : 0; for (int x = maxX - 1; x >= minX; x--) { final TiledMapTileLayer.Cell cell = layer.getCell(x, y); if (cell == null) continue; final TiledMapTile tile = cell.getTile(); if (tile != null) { final boolean flipX = cell.getFlipHorizontally(); final boolean flipY = cell.getFlipVertically(); final int rotations = cell.getRotation(); TextureRegion region = tile.getTextureRegion(); float x1 = x * layerTileWidth - offsetX + tile.getOffsetX() * unitScale; float y1 = y * layerTileHeight50 + tile.getOffsetY() * unitScale; float x2 = x1 + region.getRegionWidth() * unitScale; float y2 = y1 + region.getRegionHeight() * unitScale; float u1 = region.getU(); float v1 = region.getV2(); float u2 = region.getU2(); float v2 = region.getV(); vertices[X1] = x1; vertices[Y1] = y1; vertices[C1] = color; vertices[U1] = u1; vertices[V1] = v1; vertices[X2] = x1; vertices[Y2] = y2; vertices[C2] = color; vertices[U2] = u1; vertices[V2] = v2; vertices[X3] = x2; vertices[Y3] = y2; vertices[C3] = color; vertices[U3] = u2; vertices[V3] = v2; vertices[X4] = x2; vertices[Y4] = y1; vertices[C4] = color; vertices[U4] = u2; vertices[V4] = v1; if (flipX) { float temp = vertices[U1]; vertices[U1] = vertices[U3]; vertices[U3] = temp; temp = vertices[U2]; vertices[U2] = vertices[U4]; vertices[U4] = temp; } if (flipY) { float temp = vertices[V1]; vertices[V1] = vertices[V3]; vertices[V3] = temp; temp = vertices[V2]; vertices[V2] = vertices[V4]; vertices[V4] = temp; } if (rotations != 0) { switch (rotations) { case Cell.ROTATE_90: { float tempV = vertices[V1]; vertices[V1] = vertices[V2]; vertices[V2] = vertices[V3]; vertices[V3] = vertices[V4]; vertices[V4] = tempV; float tempU = vertices[U1]; vertices[U1] = vertices[U2]; vertices[U2] = vertices[U3]; vertices[U3] = vertices[U4]; vertices[U4] = tempU; break; } case Cell.ROTATE_180: { float tempU = vertices[U1]; vertices[U1] = vertices[U3]; vertices[U3] = tempU; tempU = vertices[U2]; vertices[U2] = vertices[U4]; vertices[U4] = tempU; float tempV = vertices[V1]; vertices[V1] = vertices[V3]; vertices[V3] = tempV; tempV = vertices[V2]; vertices[V2] = vertices[V4]; vertices[V4] = tempV; break; } case Cell.ROTATE_270: { float tempV = vertices[V1]; vertices[V1] = vertices[V4]; vertices[V4] = vertices[V3]; vertices[V3] = vertices[V2]; vertices[V2] = tempV; float tempU = vertices[U1]; vertices[U1] = vertices[U4]; vertices[U4] = vertices[U3]; vertices[U3] = vertices[U2]; vertices[U2] = tempU; break; } } } batch.draw(region.getTexture(), vertices, 0, 20); } } } }