package net.mgsx.game.examples.platformer.tasks;

import com.badlogic.gdx.ai.btree.Task;
import com.badlogic.gdx.ai.btree.annotation.TaskAttribute;
import com.badlogic.gdx.graphics.g3d.utils.AnimationController.AnimationDesc;
import com.badlogic.gdx.graphics.g3d.utils.AnimationController.AnimationListener;

import net.mgsx.game.plugins.btree.BTreePlugin.EntityLeafTask;
import net.mgsx.game.plugins.btree.EntityBlackboard;
import net.mgsx.game.plugins.btree.annotations.TaskAlias;
import net.mgsx.game.plugins.g3d.components.G3DModel;

@TaskAlias("animation")
public class AnimationTask extends EntityLeafTask implements AnimationListener
{
	@TaskAttribute(required=true)
	public String id;
	
	@TaskAttribute
	public int loops = 1;

	@TaskAttribute
	public float speed = 1;

	@TaskAttribute
	public float fade = 0;

	public boolean end = false;
	
	@Override
	public void start() {
		G3DModel model = G3DModel.components.get(getObject().entity);
		if(model != null){
			end = false;
			model.animationController.paused = false;
			model.animationController.allowSameAnimation = true;
			model.animationController.animate(id, loops, speed, this, fade);
		}
	}
	
	@Override
	public Status execute() 
	{
		// XXX bug with animation : onEnd not call if not remain ...
		G3DModel model = G3DModel.components.get(getObject().entity);
		
		end |= model.animationController.current.speed == 0;
		return end ? Status.SUCCEEDED : Status.RUNNING;
	}
	
	@Override
	protected Task<EntityBlackboard> copyTo(Task<EntityBlackboard> task) {
		AnimationTask clone = (AnimationTask)task;
		clone.fade = fade;
		clone.id = id;
		clone.loops = loops;
		clone.speed = speed;
		return super.copyTo(task);
	}

	@Override
	public void onEnd(AnimationDesc animation) {
		end = true;
	}

	@Override
	public void onLoop(AnimationDesc animation) {
	}
}

@@ -840,16 +840,37 @@
  false;%0A
+%09AnimationDesc desc;%0A
 %09%0A%09@Over
@@ -1098,24 +1098,31 @@
  = true;%0A%09%09%09
+desc = 
 model.animat
@@ -1282,16 +1282,21 @@
  remain 
+time 
 ...%0A%09%09G3
@@ -1359,11 +1359,8 @@
 y);%0A
-%09%09%0A
 %09%09en
@@ -1410,16 +1410,68 @@
 d == 0;%0A
+%09%09end %7C= desc != model.animationController.current;%0A
 %09%09return
@@ -1528,16 +1528,83 @@
 verride%0A
+%09public void end() %7B%0A%09%09desc = null;%0A%09%09super.end();%0A%09%7D%0A%09%0A%09@Override%0A
 %09protect
@@ -1898,24 +1898,26 @@
 d = true;%0A%09%7D
+%0A%09
 %0A%0A%09@Override

fix animation never end