package net.alcuria.umbracraft.engine.components;

import net.alcuria.umbracraft.Game;
import net.alcuria.umbracraft.definitions.anim.AnimationCollectionDefinition;
import net.alcuria.umbracraft.engine.components.AnimationGroupComponent.Direction;
import net.alcuria.umbracraft.engine.entities.Entity;

import com.badlogic.gdx.utils.ObjectMap;

/** A component containing a collection of groups. Handles logic for updating the
 * reference to the current group based on the entity's velocity, position, and
 * so on.
 * @author Andrew Keturi */
public class AnimationCollectionComponent implements BaseComponent {

	public static enum Pose {
		FALLING, IDLE, JUMPING, RUNNING, WALKING
	}

	private Direction currentDirection;
	private AnimationGroupComponent currentGroup;
	private Pose currentPose;
	private final AnimationCollectionDefinition definition;

	private ObjectMap<Pose, AnimationGroupComponent> groups;

	public AnimationCollectionComponent(AnimationCollectionDefinition definition) {
		this.definition = definition;
	}

	@Override
	public void create() {
		if (definition != null) {
			//FIXME: ugly
			groups = new ObjectMap<Pose, AnimationGroupComponent>();
			groups.put(Pose.IDLE, new AnimationGroupComponent(Game.db().animGroup(definition.idle)));
			groups.put(Pose.WALKING, new AnimationGroupComponent(Game.db().animGroup(definition.walking)));
			groups.put(Pose.FALLING, new AnimationGroupComponent(Game.db().animGroup(definition.falling)));
			groups.put(Pose.JUMPING, new AnimationGroupComponent(Game.db().animGroup(definition.jumping)));
			groups.put(Pose.RUNNING, new AnimationGroupComponent(Game.db().animGroup(definition.running)));
			for (AnimationGroupComponent anim : groups.values()) {
				anim.create();
			}
			currentGroup = groups.get(Pose.IDLE);
		}
	}

	@Override
	public void dispose() {

	}

	@Override
	public void render(Entity object) {
		if (currentGroup != null) {
			currentGroup.render(object);
		}
	}

	@Override
	public void update(Entity object) {
		Pose lastPose = currentPose;
		if (object.velocity.isZero()) {
			currentPose = Pose.IDLE;
		} else {
			currentPose = Pose.WALKING;
			currentDirection = currentGroup.getDirection();
		}
		if (currentPose != lastPose) {
			currentGroup = groups.get(currentPose);
			currentGroup.setDirection(currentDirection);
			Game.log("Set direction: " + currentDirection + " Set pose: " + currentPose);
		} else {
			currentGroup.update(object);
		}
	}

}

@@ -1994,16 +1994,70 @@
 ject) %7B%0A
+%09%09//save off last pose and get current pose/direction%0A
 %09%09Pose l
@@ -2238,16 +2238,97 @@
 ();%0A%09%09%7D%0A
+%09%09// if the pose has updated, update reference to currentGroup and set direction%0A
 %09%09if (cu
@@ -2447,89 +2447,8 @@
 n);%0A
-%09%09%09Game.log(%22Set direction: %22 + currentDirection + %22 Set pose: %22 + currentPose);%0A
 %09%09%7D 

Remove logging and add comments