コード例 #1
0
ファイル: MapController.java プロジェクト: neosam/ld30game
  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);
    }
  }
コード例 #2
0
ファイル: Tilemap.java プロジェクト: ZeroPage/CJG
  void checkNutrition(int x, int y) {
    MapProperties curPro = mapLayer.getCell(x, y).getTile().getProperties();

    if (curPro.containsKey("blocked")) {
      int curNutri = Integer.parseInt((String) curPro.get("nutrient"));
      System.out.println(curNutri);
      if (curNutri < 3) {
        mapLayer.setCell(x, y, blockLayer.getCell(curNutri + 5, 0));
      }
    }
  }
コード例 #3
0
  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);
        }
      }
    }
  }
コード例 #4
0
ファイル: Player.java プロジェクト: NickBeers/Happy-Potter
 private boolean isCellBlocked(float x, float y) {
   Vector2 newCoords = UtilityMethods.screenToMapCoordinate(new Vector2(x, y));
   // System.out.println("Player: x" + sprite.getX() + " y " + sprite.getY());
   // System.out.println("Tile: x" + newCoords.x + " y " + newCoords.y);
   // sprite.setPosition(newCoords.x, newCoords.y);
   Cell cell =
       collisionLayer.getCell(
           (int) (newCoords.x / collisionLayer.getTileWidth()),
           (int) (newCoords.y / collisionLayer.getTileHeight()));
   // System.out.println(cell);
   return cell != null
       && cell.getTile() != null
       && cell.getTile().getProperties().containsKey("blocked");
 }
コード例 #5
0
ファイル: Tilemap.java プロジェクト: ZeroPage/CJG
  boolean digging(int x, int y) {
    MapProperties curPro = mapLayer.getCell(x, y).getTile().getProperties();

    if (curPro.containsKey("ground")) {
      return false;
    } else {
      if (curPro.containsKey("blocked")) {
        if (checkDigged(x - 1, y)
            || checkDigged(x + 1, y)
            || checkDigged(x, y - 1)
            || checkDigged(x, y + 1)) {
          mapLayer.setCell(x, y, blockLayer.getCell(3, 0));
          return true;
        } else return false;
      } else return false;
    }
  }
コード例 #6
0
ファイル: LD33.java プロジェクト: RafaelGiordanno/LD33
  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;
      }
    }
  }
コード例 #7
0
ファイル: MapHandler.java プロジェクト: AdamAwan/CaveHorrors
 public void setupBox2D(WorldHandler worldHandler) {
   for (int x = 0; x < mapSize; x++) {
     for (int y = 0; y < mapSize; y++) {
       if (wallLayer.getCell(x, y) != null) {
         makeWallCell(x, y, worldHandler);
       }
     }
   }
 }
コード例 #8
0
  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;
        }
      }
    }
  }
コード例 #9
0
  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();
  }
コード例 #10
0
ファイル: MapController.java プロジェクト: neosam/ld30game
 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();
 }
コード例 #11
0
ファイル: Tilemap.java プロジェクト: ZeroPage/CJG
  boolean checkDigged(int x, int y) {
    MapProperties curPro = mapLayer.getCell(x, y).getTile().getProperties();

    if (x < 0 || x > 29 || y < 0 || y > 26) {
      System.out.println("out of map");
      return false;
    }

    if (curPro.containsKey("unblocked")) return true;
    else {
      System.out.println(x + ", " + y + " is blocked");
      return false;
    }
  }
コード例 #12
0
  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);
      }
    }
  }
コード例 #13
0
 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;
 }
コード例 #14
0
  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);
    }
  }
コード例 #15
0
  // Start Game
  public void init() {
    // map size
    level = new Level();

    MapProperties prop = level.map.getProperties();
    int mapWidth = prop.get("width", Integer.class);
    int mapHeight = prop.get("height", Integer.class);
    System.out.println("mapWidth: " + mapWidth + ", " + "mapHeight: " + mapHeight);

    tilePixelWidth = prop.get("tilewidth", Integer.class);
    tilePixelHeight = prop.get("tileheight", Integer.class);

    System.out.println(
        "tilePixelWidth: " + tilePixelWidth + ", " + "tilePixelHeight: " + tilePixelHeight);

    mapPixelWidth = mapWidth * tilePixelWidth;
    mapPixelHeight = mapHeight * tilePixelHeight;

    System.out.println(
        "mapPixelWidth: " + mapPixelWidth + ", " + "mapPixelHeight: " + mapPixelHeight);

    // set bounding boxes for tilemap sprites
    // stones
    TiledMapTileLayer layer = (TiledMapTileLayer) level.map.getLayers().get("stones");
    System.out.println("Layer: " + layer);

    freeCells = new boolean[layer.getWidth()][layer.getHeight()];
    mapCellWidth = layer.getWidth();
    mapCellHeight = layer.getHeight();
    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        freeCells[x][y] = true;
        if (layer.getCell(x, y) != null) {
          // Spawn Walls
          WallGameObject wall =
              new WallGameObject(x * tilePixelWidth + 25, y * tilePixelWidth + 25, 100, 100);
          this.objs.addObject(wall);

          // safe blocked cell coordinates for random player position
          freeCells[x][y] = false;
        }
      }
    }

    // bushs
    layer = (TiledMapTileLayer) level.map.getLayers().get("bushs");

    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        if (layer.getCell(x, y) != null) {
          // Spawn Bush
          BushGameObject bush =
              new BushGameObject(x * tilePixelWidth + 25, y * tilePixelWidth + 25, 90, 90);
          this.objs.addObject(bush);
        }
      }
    }

    // checkpoints
    layer = (TiledMapTileLayer) level.map.getLayers().get("checkpoint");

    int ci = 0;
    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        if (layer.getCell(x, y) != null) {
          // Spawn Checkpoint
          CheckpointGameObject checkpoint =
              new CheckpointGameObject(x * tilePixelWidth, y * tilePixelWidth, 125, 125);
          checkpoint.client = this.client;
          checkpoint.checkpointID = ci;
          this.objs.addObject(checkpoint);
          checkpointsNeeded++;
          ci++;
        }
      }
    }

    this.client.start();

    // For consistency, the classes to be sent over the network are registered by the same method
    // for both the client and server
    TurirunNetwork.register(client);

    client.addListener(
        new ThreadedListener(
            new Listener() {
              public void connected(Connection connection) {}

              public void received(Connection connection, Object object) {
                WorldController.events.add(object);
              }

              public void disconnected(Connection connection) {}
            }));

    try {
      // Block for max. 3000ms // 172.18.12.25
      client.connect(3000, this.game.host, this.game.port, TurirunNetwork.udpPort);
      // Server communication after connection can go here, or in Listener#connected()
    } catch (IOException e) {
      Gdx.app.error("Could not connect to server", e.getMessage());

      // Create local player as fallback
      TouriCharacterObject playerObj = new TouriCharacterObject(10, 10);
      playerObj.setNick(game.nickname);
      objs.addObject(playerObj);
      controller.setPlayerObj(playerObj);
    }

    Register register = new Register();

    register.nick = this.game.nickname;
    register.type = 0;

    client.sendTCP(register);
  }
コード例 #16
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);
        }
      }
    }
  }
コード例 #17
0
  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));

      }
    }
  }
コード例 #18
0
  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);
        }
      }
    }
  }
コード例 #19
0
 public void setTileCollidable(int column, int row) {
   metaLayer.setCell(column, row, collidableCell);
 }
コード例 #20
0
 public void removeMetaCellForIndex(int column, int row) {
   if (metaLayer.getCell(column, row) != null) {
     metaLayer.setCell(column, row, null);
   }
 }
コード例 #21
0
 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));
 }
コード例 #22
0
  @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);
        }
      }
    }
  }
コード例 #23
0
 public TiledMapTile getTile(float x, float y) {
   Vector2 tileIndex = getTileIndex(x, y);
   Cell cell = decorativeLayer.getCell((int) tileIndex.x, (int) tileIndex.y);
   return cell.getTile();
 }