Beispiel #1
0
  private Hashtable<String, Vector2> getSpecialNPCStartPositions() {
    Hashtable<String, Vector2> specialNPCStartPositions = new Hashtable<String, Vector2>();

    for (MapObject object : spawnsLayer.getObjects()) {
      String objectName = object.getName();

      if (objectName == null || objectName.isEmpty()) {
        continue;
      }

      // This is meant for all the special spawn locations, a catch all, so ignore known ones
      if (objectName.equalsIgnoreCase(NPC_START) || objectName.equalsIgnoreCase(PLAYER_START)) {
        continue;
      }

      // Get center of rectangle
      float x = ((RectangleMapObject) object).getRectangle().getX();
      float y = ((RectangleMapObject) object).getRectangle().getY();

      // scale by the unit to convert from map coordinates
      x *= UNIT_SCALE;
      y *= UNIT_SCALE;

      specialNPCStartPositions.put(objectName, new Vector2(x, y));
    }
    return specialNPCStartPositions;
  }
Beispiel #2
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);
    }
  }
 private void _loadObjectsRoutine(MapLayer objectLayer) {
   if (getTileLayer() != null) {
     Iterator<MapObject> objects = objectLayer.getObjects().iterator();
     while (objects.hasNext()) {
       MapObject object = objects.next();
       Integer position_x = object.getProperties().get("x", Float.class).intValue();
       Integer position_y = object.getProperties().get("y", Float.class).intValue();
       listener.objectFound(object, position_x, position_y);
     }
   }
 }
Beispiel #4
0
 public Vector2 getTriggerPoint(String name) {
   final MapObject triggerObject = tiledMap.getLayers().get("trigger").getObjects().get(name);
   if (triggerObject == null) {
     return null;
   }
   final int tileWidth = (Integer) tiledMap.getProperties().get("tilewidth");
   final int tileHeight = (Integer) tiledMap.getProperties().get("tileheight");
   return new Vector2(
       (Float) triggerObject.getProperties().get("x") / tileWidth,
       (Float) triggerObject.getProperties().get("y") / tileHeight);
 }
Beispiel #5
0
  private void setClosestStartPosition(final Vector2 position) {
    Gdx.app.debug(
        TAG,
        "setClosestStartPosition INPUT: ("
            + position.x
            + ","
            + position.y
            + ") "
            + currentMapType.toString());

    // Get last known position on this map
    playerStartPositionRect.set(0, 0);
    closestPlayerStartPosition.set(0, 0);
    float shortestDistance = 0f;

    // Go through all player start positions and choose closest to last known position
    for (MapObject object : spawnsLayer.getObjects()) {
      String objectName = object.getName();

      if (objectName == null || objectName.isEmpty()) {
        continue;
      }

      if (objectName.equalsIgnoreCase(PLAYER_START)) {
        ((RectangleMapObject) object).getRectangle().getPosition(playerStartPositionRect);
        float distance = position.dst2(playerStartPositionRect);

        Gdx.app.debug(TAG, "DISTANCE: " + distance + " for " + currentMapType.toString());

        if (distance < shortestDistance || shortestDistance == 0) {
          closestPlayerStartPosition.set(playerStartPositionRect);
          shortestDistance = distance;
          Gdx.app.debug(
              TAG,
              "closest START is: ("
                  + closestPlayerStartPosition.x
                  + ","
                  + closestPlayerStartPosition.y
                  + ") "
                  + currentMapType.toString());
        }
      }
    }
    playerStart = closestPlayerStartPosition.cpy();
  }
Beispiel #6
0
 public Map<String, Vector2> getTriggerStartWith(String name) {
   final HashMap<String, Vector2> triggerObjects = new HashMap<String, Vector2>();
   final Iterator<MapObject> mapObjectIterator =
       tiledMap.getLayers().get("trigger").getObjects().iterator();
   final int tileWidth = (Integer) tiledMap.getProperties().get("tilewidth");
   final int tileHeight = (Integer) tiledMap.getProperties().get("tileheight");
   while (mapObjectIterator.hasNext()) {
     final MapObject mapObject = mapObjectIterator.next();
     if (mapObject != null
         && mapObject.getName() != null
         && mapObject.getName().startsWith(name)) {
       triggerObjects.put(
           mapObject.getName(),
           new Vector2(
               (Float) mapObject.getProperties().get("x") / tileWidth,
               (Float) mapObject.getProperties().get("y") / tileHeight));
     }
   }
   return triggerObjects;
 }
 public static void mapColl() {
   MapLayer l = Play.currentMap.getLayers().get("Collision");
   for (MapObject m : l.getObjects()) {
     BodyDef bdef = new BodyDef();
     bdef.type = BodyType.StaticBody;
     Body b = Play.world.createBody(bdef);
     b.setTransform(
         new Vector2(
             (float) m.getProperties().get("x") + ((float) m.getProperties().get("width") / 2),
             (float) m.getProperties().get("y") + ((float) m.getProperties().get("height") / 2)),
         0);
     PolygonShape shape = new PolygonShape();
     shape.setAsBox(
         (float) m.getProperties().get("width") / 2, (float) m.getProperties().get("height") / 2);
     FixtureDef fdef = new FixtureDef();
     fdef.shape = shape;
     Fixture f = b.createFixture(fdef);
     f.setUserData("Collision");
     shape.dispose();
   }
 }
Beispiel #8
0
  private Array<Vector2> getNPCStartPositions() {
    Array<Vector2> npcStartPositions = new Array<Vector2>();

    for (MapObject object : spawnsLayer.getObjects()) {
      String objectName = object.getName();

      if (objectName == null || objectName.isEmpty()) {
        continue;
      }

      if (objectName.equalsIgnoreCase(NPC_START)) {
        // Get center of rectangle
        float x = ((RectangleMapObject) object).getRectangle().getX();
        float y = ((RectangleMapObject) object).getRectangle().getY();

        // scale by the unit to convert from map coordinates
        x *= UNIT_SCALE;
        y *= UNIT_SCALE;

        npcStartPositions.add(new Vector2(x, y));
      }
    }
    return npcStartPositions;
  }
  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);
  }
  protected void loadObject(MapLayer layer, Element element) {
    if (element.getName().equals("object")) {
      MapObject object = null;

      int x = element.getIntAttribute("x", 0);
      int y =
          (yUp
              ? mapHeightInPixels - element.getIntAttribute("y", 0)
              : element.getIntAttribute("y", 0));

      int width = element.getIntAttribute("width", 0);
      int height = element.getIntAttribute("height", 0);

      if (element.getChildCount() > 0) {
        Element child = null;
        if ((child = element.getChildByName("polygon")) != null) {
          String[] points = child.getAttribute("points").split(" ");
          float[] vertices = new float[points.length * 2];
          for (int i = 0; i < points.length; i++) {
            String[] point = points[i].split(",");
            vertices[i * 2] = Integer.parseInt(point[0]);
            vertices[i * 2 + 1] = Integer.parseInt(point[1]);
            if (yUp) {
              vertices[i * 2 + 1] *= -1;
            }
          }
          Polygon polygon = new Polygon(vertices);
          polygon.setPosition(x, y);
          object = new PolygonMapObject(polygon);
        } else if ((child = element.getChildByName("polyline")) != null) {
          String[] points = child.getAttribute("points").split(" ");
          float[] vertices = new float[points.length * 2];
          for (int i = 0; i < points.length; i++) {
            String[] point = points[i].split(",");
            vertices[i * 2] = Integer.parseInt(point[0]);
            vertices[i * 2 + 1] = Integer.parseInt(point[1]);
            if (yUp) {
              vertices[i * 2 + 1] *= -1;
            }
          }
          Polyline polyline = new Polyline(vertices);
          polyline.setPosition(x, y);
          object = new PolylineMapObject(polyline);
        } else if ((child = element.getChildByName("ellipse")) != null) {
          object = new EllipseMapObject(x, yUp ? y - height : y, width, height);
        }
      }
      if (object == null) {
        object = new RectangleMapObject(x, yUp ? y - height : y, width, height);
      }
      object.setName(element.getAttribute("name", null));
      String type = element.getAttribute("type", null);
      if (type != null) {
        object.getProperties().put("type", type);
      }
      int gid = element.getIntAttribute("gid", -1);
      if (gid != -1) {
        object.getProperties().put("gid", gid);
      }
      object.getProperties().put("x", x);
      object.getProperties().put("y", yUp ? y - height : y);
      object.setVisible(element.getIntAttribute("visible", 1) == 1);
      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(object.getProperties(), properties);
      }
      layer.getObjects().add(object);
    }
  }
  public SMovementState update(
      Vector2 currentPosition, Vector2 nextPosition, float width, float height, float offset) {
    if (getTileLayer() != null) {
      // Make sure the positions are even valid
      if (!validPosition(nextPosition.x, currentPosition.y)
          || !validPosition(nextPosition.x + width, currentPosition.y)) {
        nextPosition.x = currentPosition.x;
      }
      if (!validPosition(currentPosition.x, nextPosition.y)
          || !validPosition(currentPosition.x + width, nextPosition.y + height)) {
        nextPosition.y = currentPosition.y;
      }
      BoundingBox new_x_bbox =
          new BoundingBox(new Vector2(nextPosition.x, currentPosition.y), width, height, offset);
      Vector2 x_tile_1 = getTileIndex(new_x_bbox.BL.x, new_x_bbox.BL.y);
      Vector2 x_tile_2 = getTileIndex(new_x_bbox.TR.x, new_x_bbox.TR.y);
      for (int i = (int) x_tile_1.x; i <= (int) x_tile_2.x; i++) {
        for (int j = (int) x_tile_1.y; j <= (int) x_tile_2.y; j++) {
          if (isTileCollidable(i, j)) {
            nextPosition.x = currentPosition.x;
          }
        }
      }
      BoundingBox new_y_bbox =
          new BoundingBox(new Vector2(currentPosition.x, nextPosition.y), width, height, offset);
      Vector2 y_tile_1 = getTileIndex(new_y_bbox.BL.x, new_y_bbox.BL.y);
      Vector2 y_tile_2 = getTileIndex(new_y_bbox.TR.x, new_y_bbox.TR.y);
      for (int i = (int) y_tile_1.x; i <= (int) y_tile_2.x; i++) {
        for (int j = (int) y_tile_1.y; j <= (int) y_tile_2.y; j++) {
          if (isTileCollidable(i, j)) {
            nextPosition.y = currentPosition.y;
          }
        }
      }
    }

    Vector2 characterCenter = new Vector2(nextPosition.x + width / 2, nextPosition.y + height / 2);
    Vector2 centerTile = getTileIndex(characterCenter.x, characterCenter.y);
    boolean exitFound = false;
    String exitFilename = null;

    Iterator<MapObject> objects = objectLayer.getObjects().iterator();
    while (objects.hasNext()) {
      MapObject object = objects.next();
      if (object.getProperties().containsKey("x") && object.getProperties().containsKey("y")) {
        Vector2 propertyCenter =
            getTileIndex(
                object.getProperties().get("x", Float.class).intValue(),
                object.getProperties().get("y", Float.class).intValue());
        if (centerTile.x == propertyCenter.x && centerTile.y == propertyCenter.y) {
          if (object.getProperties().containsKey("exit_to")) {
            exitFound = true;
            exitFilename = object.getProperties().get("exit_to").toString();
          }
        }
      }
    }
    // set the tiles
    if (debug) {
      _displayDebug(nextPosition, width, height, offset);
    }
    return new SMovementState(nextPosition, exitFound, exitFilename);
  }