コード例 #1
0
ファイル: Tilemap.java プロジェクト: ZeroPage/CJG
  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);
  }
コード例 #2
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);
        }
      }
    }
  }
コード例 #3
0
ファイル: Round.java プロジェクト: TeamPochard/Assessment-3
  /**
   * 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)));
    }
  }
コード例 #4
0
  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);
    }
  }
コード例 #5
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;
        }
      }
    }
  }
コード例 #6
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);
    }
  }
コード例 #7
0
  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());
    }
  }
コード例 #8
0
ファイル: Map.java プロジェクト: Super8BitFight/LoS
  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();
  }
コード例 #9
0
 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;
 }
コード例 #10
0
ファイル: MapController.java プロジェクト: neosam/ld30game
 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);
 }
コード例 #11
0
ファイル: Level.java プロジェクト: russjr08/RunAway-Java
  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");
  }
コード例 #12
0
 @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();
 }
コード例 #13
0
 @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();
 }
コード例 #14
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;
      }
    }
  }
コード例 #15
0
ファイル: PlayScreen2.java プロジェクト: Clairehuei/rockman
  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");
  }
コード例 #16
0
ファイル: MapController.java プロジェクト: neosam/ld30game
 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;
 }
コード例 #17
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();
  }
コード例 #18
0
 @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();
 }
コード例 #19
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();
 }
コード例 #20
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);
      }
    }
  }
コード例 #21
0
 public TiledMapTileLayer getMetaLayer() {
   return (TiledMapTileLayer) map.getLayers().get("MetaLayer");
 }
コード例 #22
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);
        }
      }
    }
  }
コード例 #23
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));

      }
    }
  }
コード例 #24
0
  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);
  }
コード例 #25
0
ファイル: MapHandler.java プロジェクト: AdamAwan/CaveHorrors
 public void loadGameMap() {
   tiledMap = new TmxMapLoader().load("maps/testMap.tmx");
   renderer = new OrthogonalTiledMapRenderer(tiledMap, unitScale);
   wallLayer = (TiledMapTileLayer) tiledMap.getLayers().get("Walls");
 }
コード例 #26
0
 public TiledMapTileLayer getTileLayer() {
   // return (TiledMapTileLayer) map.getLayers().get(0);
   return (TiledMapTileLayer) map.getLayers().get("MetaLayer");
 }
コード例 #27
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));
 }
コード例 #28
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);
    }
  }