예제 #1
0
파일: Player.java 프로젝트: marshauf/uni
 @Override
 public void read(Json json, JsonValue jsonData) {
   Iterator<JsonValue> iter = jsonData.iterator();
   JsonValue value;
   while (iter.hasNext()) {
     value = iter.next();
     switch (value.name()) {
       case "name":
         name = value.asString();
         break;
       case "dir":
         dir.x = value.get("x").asFloat();
         dir.y = value.get("x").asFloat();
         break;
       case "speed":
         speed = value.asFloat();
         break;
       case "bounds":
         bounds.width = value.getFloat("width");
         bounds.height = value.getFloat("height");
         setPosition(
             new Vector2(
                 value.getFloat("x") + bounds.width / 2, value.getFloat("y") + bounds.height / 2));
         break;
       case "score":
         score.read(json, value);
         break;
     }
   }
 }
예제 #2
0
  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;
  }
예제 #3
0
  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));
  }