public void addHighScore(int level, String name, float score) {
    JsonValue root;
    try {
      root = new JsonReader().parse(new FileReader(FilePath.highscoreTemp));
    } catch (FileNotFoundException e) {
      try {
        FileWriter f = new FileWriter(FilePath.highscoreTemp);
        f.write("[]");
        f.close();
        root = new JsonReader().parse(new FileReader(FilePath.highscoreTemp));
      } catch (IOException e1) {
        return;
      }
    }

    if (root == null) root = new JsonValue(false);
    root.setType(ValueType.array);

    JsonValue entry = new JsonValue(false);
    entry.setType(ValueType.array);
    entry.child = new JsonValue(level);
    entry.child.next = new JsonValue(name);
    entry.child.next.next = new JsonValue(score);

    if (root.child == null) root.child = entry;
    else {
      JsonValue last = root.child;
      while (last.next() != null) last = last.next();
      last.next = entry;
    }

    try {
      FileWriter f = new FileWriter(FilePath.highscoreTemp);
      JsonWriter j = new JsonWriter(f);
      j.array();
      for (JsonValue p = root.child; p != null; p = p.next) {
        j.array();
        j.value(p.getInt(0));
        j.value(p.getString(1));
        j.value(p.getFloat(2));
        j.pop();
      }

      j.close();
      f.close();
    } catch (IOException e) {
      return;
    }
  }
Beispiel #2
0
  public ArrayList<String> getGameModesList() {

    JsonValue obg = rootValue.get("gamemode");
    ArrayList<String> modes = new ArrayList<String>();
    do {
      String s = obg.child().asString();
      modes.add(s);
      System.out.println(s);
    } while ((obg = obg.next()) != null);
    return modes;
  }
Beispiel #3
0
  // Rules are just parameters the player can't see and have no control over.
  public ArrayList<GameModeParameter> getRulesForMode(String gameMode) {
    if (true) // fake out the compiler
    return null; // this isn't used currently
    JsonValue obg = rootValue.get("gamemode");
    do {
      if (obg.child().asString().equals(gameMode)) {
        obg = obg.child();
        break;
      }
    } while ((obg = obg.next()) != null);

    // next = parameters
    obg = obg.next();
    // next = rules
    obg = obg.next();
    // child = rule 1
    // from child, next = rule i
    ArrayList<GameModeParameter> params = new ArrayList<GameModeParameter>();
    obg = obg.child();
    do {
      String s = obg.getString("type");
      if (s.equals("int")) {
        params.add(new GameModeParameter.IntParameter(obg));
      } else if (s.equals("string")) {

      } else if (s.equals("spawnables")) {

      } else if (s.equals("powerups")) {

      } else if (s.equals("component")) {
        params.add(new GameModeParameter.ComponentParameter(obg));
      } else if (s.equals("conditions")) {

      } else {
        System.out.println("Unknown type: " + s);
      }
    } while ((obg = obg.next()) != null);

    return params;
  }
Beispiel #4
0
  public ArrayList<GameModeParameter> getParametersForMode(String gameMode) {
    JsonValue obg = rootValue.get("gamemode");
    do {
      if (obg.child().asString().equals(gameMode)) {
        obg = obg.child();
        break;
      }
    } while ((obg = obg.next()) != null);

    // next = parameters

    obg = obg.next();
    // child = param 1
    // from child, next = param i
    ArrayList<GameModeParameter> params = new ArrayList<GameModeParameter>();
    obg = obg.child();
    do {
      String s = obg.getString("type");
      if (s.equals("int")) {
        params.add(new GameModeParameter.IntParameter(obg));
      } else if (s.equals("string")) {

      } else if (s.equals("spawnables")) {

      } else if (s.equals("powerups")) {

      } else if (s.equals("meh")) {

      } else if (s.equals("nu-huh")) {

      } else {
        System.out.println("Unknown type: " + s);
      }
    } while ((obg = obg.next()) != null);

    return params;
  }
  private RigidBodyModel readRigidBody(JsonValue bodyElem) {
    RigidBodyModel rbModel = new RigidBodyModel();
    rbModel.name = bodyElem.getString("name");
    rbModel.imagePath = bodyElem.getString("imagePath");

    JsonValue originElem = bodyElem.get("origin");
    rbModel.origin.x = originElem.getFloat("x");
    rbModel.origin.y = originElem.getFloat("y");

    // polygons
    JsonValue polygonsElem = bodyElem.getChild("polygons");
    for (; polygonsElem != null; polygonsElem = polygonsElem.next()) {

      PolygonModel polygon = new PolygonModel();
      rbModel.polygons.add(polygon);

      JsonValue vertexElem = polygonsElem.child();
      for (; vertexElem != null; vertexElem = vertexElem.next()) {
        float x = vertexElem.getFloat("x");
        float y = vertexElem.getFloat("y");
        polygon.vertices.add(new Vector2(x, y));
      }

      polygon.buffer = new Vector2[polygon.vertices.size()];
    }

    // circles
    JsonValue circleElem = bodyElem.getChild("circles");

    for (; circleElem != null; circleElem = circleElem.next()) {
      CircleModel circle = new CircleModel();
      rbModel.circles.add(circle);

      circle.center.x = circleElem.getFloat("cx");
      circle.center.y = circleElem.getFloat("cy");
      circle.radius = circleElem.getFloat("r");
    }

    return rbModel;
  }
  public SkeletonData readSkeletonData(FileHandle file) {
    if (file == null) throw new IllegalArgumentException("file cannot be null.");

    SkeletonData skeletonData = new SkeletonData();
    skeletonData.setName(file.nameWithoutExtension());

    JsonValue root = new JsonReader().parse(file);

    // Bones.
    for (JsonValue boneMap = root.getChild("bones"); boneMap != null; boneMap = boneMap.next()) {
      BoneData parent = null;
      String parentName = boneMap.getString("parent", null);
      if (parentName != null) {
        parent = skeletonData.findBone(parentName);
        if (parent == null)
          throw new SerializationException("Parent bone not found: " + parentName);
      }
      BoneData boneData = new BoneData(boneMap.getString("name"), parent);
      boneData.length = boneMap.getFloat("length", 0) * scale;
      boneData.x = boneMap.getFloat("x", 0) * scale;
      boneData.y = boneMap.getFloat("y", 0) * scale;
      boneData.rotation = boneMap.getFloat("rotation", 0);
      boneData.scaleX = boneMap.getFloat("scaleX", 1);
      boneData.scaleY = boneMap.getFloat("scaleY", 1);
      boneData.inheritScale = boneMap.getBoolean("inheritScale", true);
      boneData.inheritRotation = boneMap.getBoolean("inheritRotation", true);
      skeletonData.addBone(boneData);
    }

    // Slots.
    for (JsonValue slotMap = root.getChild("slots"); slotMap != null; slotMap = slotMap.next()) {
      String slotName = slotMap.getString("name");
      String boneName = slotMap.getString("bone");
      BoneData boneData = skeletonData.findBone(boneName);
      if (boneData == null) throw new SerializationException("Slot bone not found: " + boneName);
      SlotData slotData = new SlotData(slotName, boneData);

      String color = slotMap.getString("color", null);
      if (color != null) slotData.getColor().set(Color.valueOf(color));

      slotData.setAttachmentName(slotMap.getString("attachment", null));

      slotData.additiveBlending = slotMap.getBoolean("additive", false);

      skeletonData.addSlot(slotData);
    }

    // Skins.
    for (JsonValue skinMap = root.getChild("skins"); skinMap != null; skinMap = skinMap.next()) {
      Skin skin = new Skin(skinMap.name());
      for (JsonValue slotEntry = skinMap.child(); slotEntry != null; slotEntry = slotEntry.next()) {
        int slotIndex = skeletonData.findSlotIndex(slotEntry.name());
        for (JsonValue entry = slotEntry.child(); entry != null; entry = entry.next()) {
          Attachment attachment = readAttachment(skin, entry.name(), entry);
          if (attachment != null) skin.addAttachment(slotIndex, entry.name(), attachment);
        }
      }
      skeletonData.addSkin(skin);
      if (skin.name.equals("default")) skeletonData.setDefaultSkin(skin);
    }

    // Animations.
    for (JsonValue animationMap = root.getChild("animations");
        animationMap != null;
        animationMap = animationMap.next())
      readAnimation(animationMap.name(), animationMap, skeletonData);

    skeletonData.bones.shrink();
    skeletonData.slots.shrink();
    skeletonData.skins.shrink();
    skeletonData.animations.shrink();
    return skeletonData;
  }
  private void readAnimation(String name, JsonValue map, SkeletonData skeletonData) {
    Array<Timeline> timelines = new Array();
    float duration = 0;

    for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next()) {
      int boneIndex = skeletonData.findBoneIndex(boneMap.name());
      if (boneIndex == -1) throw new SerializationException("Bone not found: " + boneMap.name());

      for (JsonValue timelineMap = boneMap.child();
          timelineMap != null;
          timelineMap = timelineMap.next()) {
        String timelineName = timelineMap.name();
        if (timelineName.equals(TIMELINE_ROTATE)) {
          RotateTimeline timeline = new RotateTimeline(timelineMap.size());
          timeline.setBoneIndex(boneIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            timeline.setFrame(frameIndex, time, valueMap.getFloat("angle"));
            readCurve(timeline, frameIndex, valueMap);
            frameIndex++;
          }
          timelines.add(timeline);
          duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);

        } else if (timelineName.equals(TIMELINE_TRANSLATE) || timelineName.equals(TIMELINE_SCALE)) {
          TranslateTimeline timeline;
          float timelineScale = 1;
          if (timelineName.equals(TIMELINE_SCALE)) timeline = new ScaleTimeline(timelineMap.size());
          else {
            timeline = new TranslateTimeline(timelineMap.size());
            timelineScale = scale;
          }
          timeline.setBoneIndex(boneIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0);
            timeline.setFrame(frameIndex, time, x * timelineScale, y * timelineScale);
            readCurve(timeline, frameIndex, valueMap);
            frameIndex++;
          }
          timelines.add(timeline);
          duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);

        } else
          throw new RuntimeException(
              "Invalid timeline type for a bone: " + timelineName + " (" + boneMap.name() + ")");
      }
    }

    for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next()) {
      int slotIndex = skeletonData.findSlotIndex(slotMap.name());

      for (JsonValue timelineMap = slotMap.child();
          timelineMap != null;
          timelineMap = timelineMap.next()) {
        String timelineName = timelineMap.name();
        if (timelineName.equals(TIMELINE_COLOR)) {
          ColorTimeline timeline = new ColorTimeline(timelineMap.size());
          timeline.setSlotIndex(slotIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            Color color = Color.valueOf(valueMap.getString("color"));
            timeline.setFrame(frameIndex, time, color.r, color.g, color.b, color.a);
            readCurve(timeline, frameIndex, valueMap);
            frameIndex++;
          }
          timelines.add(timeline);
          duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);

        } else if (timelineName.equals(TIMELINE_ATTACHMENT)) {
          AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size());
          timeline.setSlotIndex(slotIndex);

          int frameIndex = 0;
          for (JsonValue valueMap = timelineMap.child();
              valueMap != null;
              valueMap = valueMap.next()) {
            float time = valueMap.getFloat("time");
            timeline.setFrame(frameIndex++, time, valueMap.getString("name"));
          }
          timelines.add(timeline);
          duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);

        } else
          throw new RuntimeException(
              "Invalid timeline type for a slot: " + timelineName + " (" + slotMap.name() + ")");
      }
    }

    timelines.shrink();
    skeletonData.addAnimation(new Animation(name, timelines, duration));
  }