Exemple #1
0
  public void parseLevel(FileHandle file) throws Exception {

    Element root = new XmlReader().parse(file);

    this.pathway = Pathway.parsePathway(root);

    // Loading game objects
    Element entities = root.getChildByName("entities");
    this.gameObjects = new ArrayList<GameObject>();
    if (entities != null) {
      for (int i = 0; i < entities.getChildCount(); i++) {
        Element gameObject = entities.getChild(i);
        this.gameObjects.add(GameObject.parseGameObject(gameObject));
      }
    }

    // Loading universal objects
    Element universalObjects = root.getChildByName("drawings");
    this.universalObjects = new ArrayList<GameObject>();
    if (universalObjects != null) {
      for (int i = 0; i < universalObjects.getChildCount(); i++) {
        Element universalObject = universalObjects.getChild(i);
        this.universalObjects.add(GameObject.parseGameObject(universalObject));
      }
    }

    // Loading car
    Element car = root.getChildByName(Car.name);
    this.car = Car.parseCar(car);

    // Loading start
    Element start = root.getChildByName(Start.name);
    this.start = (Start) GameObject.parseGameObject(start);

    // Loading finish
    Element finish = root.getChildByName(Finish.name);
    this.finish = (Finish) GameObject.parseGameObject(finish);

    // Background
    Element background = root.getChildByName("background");
    this.background = LevelBackground.parseLevelBackground(background);

    // Time limit
    this.timeLimit = root.getFloat("timeLimit");

    Element difficulty = root.getChildByName("difficulty");
    this.setDifficulty(Difficulty.valueOf(difficulty.getText()));

    System.out.println("Loading level \"" + file.name() + "\" done.");
  }
  protected TiledMap loadMap(
      Element root,
      FileHandle tmxFile,
      AtlasResolver resolver,
      AtlasTiledMapLoaderParameters parameter) {
    TiledMap map = new TiledMap();

    String mapOrientation = root.getAttribute("orientation", null);
    int mapWidth = root.getIntAttribute("width", 0);
    int mapHeight = root.getIntAttribute("height", 0);
    int tileWidth = root.getIntAttribute("tilewidth", 0);
    int tileHeight = root.getIntAttribute("tileheight", 0);
    String mapBackgroundColor = root.getAttribute("backgroundcolor", null);

    MapProperties mapProperties = map.getProperties();
    if (mapOrientation != null) {
      mapProperties.put("orientation", mapOrientation);
    }
    mapProperties.put("width", mapWidth);
    mapProperties.put("height", mapHeight);
    mapProperties.put("tilewidth", tileWidth);
    mapProperties.put("tileheight", tileHeight);
    if (mapBackgroundColor != null) {
      mapProperties.put("backgroundcolor", mapBackgroundColor);
    }
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;

    for (int i = 0, j = root.getChildCount(); i < j; i++) {
      Element element = root.getChild(i);
      String elementName = element.getName();
      if (elementName.equals("properties")) {
        loadProperties(map.getProperties(), element);
      } else if (elementName.equals("tileset")) {
        loadTileset(map, element, tmxFile, resolver, parameter);
      } else if (elementName.equals("layer")) {
        loadTileLayer(map, element);
      } else if (elementName.equals("objectgroup")) {
        loadObjectGroup(map, element);
      }
    }
    return map;
  }
  public Animation read(FileHandle handle) {
    XmlReader rdr = new XmlReader();
    try {

      Element root = rdr.parse(handle);
      int frameRate = Integer.parseInt(root.getChildByName("FrameRate").getText());
      int loopFrame = Integer.parseInt(root.getChildByName("LoopFrame").getText());

      Animation anim = new Animation();
      anim.FrameRate = frameRate;
      anim.LoopFrame = loopFrame;
      anim.Textures = new ArrayList<TextureEntry>();
      anim.Loop = loopFrame != -1;
      anim.Keyframes = new ArrayList<Keyframe>();

      for (int i = 0; i < root.getChildCount(); ++i) {
        Element ch = root.getChild(i);
        if (ch.getName().equals("Texture")) {
          TextureRegion reg = imgSrc.getImage(ch.getText());
          if (reg != null) {
            TextureAtlas.AtlasRegion r = (TextureAtlas.AtlasRegion) reg;
            anim.Textures.add(
                new TextureEntry(
                    reg,
                    new TextureBounds(
                        new Rectangle(0, 0, r.originalWidth, r.originalHeight),
                        new Vector2(r.originalWidth / 2, r.originalHeight / 2))));
          } else {
            throw new Exception("Unable to resolve image: " + ch.getText());
          }
        }
      }

      for (int i = 0; i < root.getChildCount(); ++i) {
        Element ch = root.getChild(i);
        if (ch.getName().equals("Keyframe")) {
          Keyframe frame = new Keyframe();
          frame.Bones = new ArrayList<Bone>();
          frame.FrameNumber = ch.getIntAttribute("frame");
          frame.Trigger = ch.getAttribute("trigger", "");
          frame.FlipHorizontally = ch.getAttribute("hflip", "False").equals("True");
          frame.FlipVertically = ch.getAttribute("vflip", "False").equals("True");

          for (int j = 0; j < ch.getChildCount(); ++j) {
            Element bone = ch.getChild(j);
            if (bone.getName().equals("Bone")) {
              Element posElem = bone.getChildByName("Position");
              Element sclElem = bone.getChildByName("Scale");
              Vector2 pos = new Vector2();
              Vector2 scl = new Vector2();

              pos.x = Float.parseFloat(posElem.getChildByName("X").getText());
              pos.y = Float.parseFloat(posElem.getChildByName("Y").getText());

              scl.x = Float.parseFloat(sclElem.getChildByName("X").getText());
              scl.y = Float.parseFloat(sclElem.getChildByName("Y").getText());

              Bone b = new Bone();
              b.Hidden = bone.getChildByName("Hidden").getText().equals("True");
              b.Name = bone.getAttribute("name");
              b.TextureFlipHorizontal =
                  bone.getChildByName("TextureFlipHorizontal").getText().equals("True");
              b.TextureFlipVertical =
                  bone.getChildByName("TextureFlipVertical").getText().equals("True");
              b.ParentIndex = Integer.parseInt(bone.getChildByName("ParentIndex").getText());
              b.TextureIndex = Integer.parseInt(bone.getChildByName("TextureIndex").getText());
              b.Rotation = Float.parseFloat(bone.getChildByName("Rotation").getText());

              b.Position = pos;
              b.Scale = scl;
              b.SelfIndex = j;

              frame.Bones.add(b);
            }
          }

          frame.SortBones();
          anim.Keyframes.add(frame);
        }

        float fr = 1.0f / anim.FrameRate;
        anim.LoopTime = anim.LoopFrame * anim.FrameRate;
        anim.Loop = anim.LoopFrame != -1;
        for (Keyframe kf : anim.Keyframes) {
          kf.FrameTime = fr * kf.FrameNumber;
        }
      }

      return anim;
    } catch (Exception ex) {
      Gdx.app.log("AnimationReader", "read", ex);
      return null;
    }
  }
Exemple #4
0
  private void loadLayer(TiledMap map, Element element) {
    if (element.getName().equals("Layer")) {
      String id = element.getAttribute("Id");
      String visible = element.getAttribute("Visible");

      Element dimensions = element.getChildByName("Dimensions");
      String layerSize = dimensions.getAttribute("LayerSize");
      String tileSize = dimensions.getAttribute("TileSize");

      String[] layerSizeParts = layerSize.split(" x ");
      int layerSizeX = Integer.parseInt(layerSizeParts[0]);
      int layerSizeY = Integer.parseInt(layerSizeParts[1]);

      String[] tileSizeParts = tileSize.split(" x ");
      int tileSizeX = Integer.parseInt(tileSizeParts[0]);
      int tileSizeY = Integer.parseInt(tileSizeParts[1]);

      TiledMapTileLayer layer = new TiledMapTileLayer(layerSizeX, layerSizeY, tileSizeX, tileSizeY);
      Element tileArray = element.getChildByName("TileArray");
      Array<Element> rows = tileArray.getChildrenByName("Row");
      TiledMapTileSets tilesets = map.getTileSets();
      TiledMapTileSet currentTileSet = null;
      int firstgid = 0;
      int x, y;
      for (int row = 0, rowCount = rows.size; row < rowCount; row++) {
        Element currentRow = rows.get(row);
        y = rowCount - 1 - row;
        x = 0;
        for (int child = 0, childCount = currentRow.getChildCount(); child < childCount; child++) {
          Element currentChild = currentRow.getChild(child);
          String name = currentChild.getName();
          if (name.equals("TileSheet")) {
            currentTileSet = tilesets.getTileSet(currentChild.getAttribute("Ref"));
            firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
          } else if (name.equals("Null")) {
            x += currentChild.getIntAttribute("Count");
          } else if (name.equals("Static")) {
            Cell cell = new Cell();
            cell.setTile(currentTileSet.getTile(firstgid + currentChild.getIntAttribute("Index")));
            layer.setCell(x++, y, cell);
          } else if (name.equals("Animated")) {
            // Create an AnimatedTile
            int interval = currentChild.getInt("Interval");
            Element frames = currentChild.getChildByName("Frames");
            Array<StaticTiledMapTile> frameTiles = new Array<StaticTiledMapTile>();
            for (int frameChild = 0, frameChildCount = frames.getChildCount();
                frameChild < frameChildCount;
                frameChild++) {
              Element frame = frames.getChild(frameChild);
              String frameName = frame.getName();
              if (frameName.equals("TileSheet")) {
                currentTileSet = tilesets.getTileSet(frame.getAttribute("Ref"));
                firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
              } else if (frameName.equals("Static")) {
                frameTiles.add(
                    (StaticTiledMapTile)
                        currentTileSet.getTile(firstgid + frame.getIntAttribute("Index")));
              }
            }
            Cell cell = new Cell();
            cell.setTile(new AnimatedTiledMapTile(interval / 1000f, frameTiles));
            layer.setCell(x++, y, cell); // TODO: Reuse existing animated tiles
          }
        }
      }
      map.getLayers().add(layer);
    }
  }