Ejemplo n.º 1
0
public class EntityPrefabLoader extends ResourceLoader {
  private static final TypeChecker<Map<? extends String, ?>> checkerMapStringObject =
      TypeChecker.tMap(String.class, Object.class);
  private static final TypeChecker<List<? extends String>> checkerListString =
      TypeChecker.tList(String.class);

  public EntityPrefabLoader() {
    super("entity", "entity://Spout/fallbacks/entity.sep");
  }

  @SuppressWarnings("unchecked")
  @Override
  public ClientEntityPrefab load(InputStream in) {
    Engine engine = Spout.getEngine();
    if (!(engine instanceof Client)) {
      throw new IllegalStateException("Prefabs can only be loaded on the client.");
    }

    final Yaml yaml = new Yaml();
    final Map<? extends String, ?> resourceProperties = checkerMapStringObject.check(yaml.load(in));

    if (!(resourceProperties.containsKey("Name"))
        || !(resourceProperties.containsKey("Components"))
        || !(resourceProperties.containsKey("Data"))) {
      throw new IllegalStateException("A property is missing (Name, Components or Data)");
    }

    final Object name = resourceProperties.get("Name");
    if (!(name instanceof String)) {
      throw new IllegalStateException("Tried to load an entity prefab but wasn't given a name");
    }

    final List<? extends String> componentsPath =
        checkerListString.check(resourceProperties.get("Components"));
    final List<Class<? extends Component>> components = new ArrayList<Class<? extends Component>>();
    for (String path : componentsPath) {
      Class<?> componentClass;
      try {
        try {
          componentClass = CommonClassLoader.findPluginClass(path);
        } catch (ClassNotFoundException e) {
          componentClass = Class.forName(path);
        }
      } catch (ClassNotFoundException e) {
        throw new IllegalStateException("A component is missing: " + path);
      }
      if (EntityComponent.class.isAssignableFrom(componentClass)) {
        components.add((Class<? extends EntityComponent>) componentClass);
      } else {
        throw new IllegalStateException("This is not an entity component.");
      }
    }

    final Map<? extends String, ?> datasOld =
        checkerMapStringObject.check(resourceProperties.get("Data"));
    final Map<String, Object> datas = new HashMap<String, Object>();
    datas.putAll(datasOld);

    return new ClientEntityPrefab((Client) engine, (String) name, components, datas);
  }
}
Ejemplo n.º 2
0
@SuppressWarnings("unchecked")
public class AnimationLoader extends ResourceLoader {
  private static final TypeChecker<Map<? extends String, ?>> checkerMapStringObject =
      TypeChecker.tMap(String.class, Object.class);

  public AnimationLoader() {
    super("animation", "animation://Spout/entities/Spouty/spouty.sam");
  }

  private static Animation loadObj(InputStream stream) {
    final Yaml yaml = new Yaml();
    final Map<? extends String, ?> resourceProperties =
        checkerMapStringObject.check(yaml.load(stream));

    Skeleton skeleton =
        Spout.getFileSystem().getResource((String) resourceProperties.get("Skeleton"));

    int frames = (Integer) resourceProperties.get("frames");
    float delay = ((Double) resourceProperties.get("delay")).floatValue();

    Animation animation = new Animation(skeleton, frames, delay);

    Map<? extends String, ?> bones_data =
        (Map<? extends String, ?>) resourceProperties.get("bones_data");

    for (Entry<? extends String, ?> entry : bones_data.entrySet()) {

      Bone bone = skeleton.getBoneByName(entry.getKey());
      if (bone == null)
        throw new IllegalStateException("Animation file mapped with the bad Skeleton file");

      Map<? extends Integer, String> value = (Map<? extends Integer, String>) entry.getValue();

      int i = 0;
      for (Entry<? extends Integer, String> entryBone : value.entrySet()) {
        int frame = entryBone.getKey() - 1; // Start at 1

        BoneTransform boneTransform = new BoneTransform(loadFloatList(entryBone.getValue()));

        animation.setBoneTransform(bone.getId(), i /*frame*/, boneTransform);

        i++;
      }
    }

    // System.out.println("Animation loaded : (org.spout.engine.resources.loader.AnimationLoader
    // line 77)");
    // animation.dumbAnimation(" ");

    return animation;
  }

  private static float[] loadFloatList(String str) {
    String[] split = str.split(",");
    float[] result = new float[split.length];

    for (int i = 0; i < split.length; i++) {
      result[i] = (float) Double.parseDouble(split[i]);
    }

    return result;
  }

  @Override
  public Animation load(InputStream in) {
    return AnimationLoader.loadObj(in);
  }
}