Play idle action for Character trait
This commit is contained in:
parent
3d42331115
commit
3b0a4904b8
|
@ -11,12 +11,13 @@ class Character extends Trait {
|
|||
|
||||
var actionIdle:String;
|
||||
var actionMove:String;
|
||||
var currentAction:String;
|
||||
var animation:Animation;
|
||||
|
||||
var speed = 0.0;
|
||||
var loc:Vec4 = new Vec4();
|
||||
var lastLoc:Vec4 = null;
|
||||
var state = 0; // Idle, walking
|
||||
var framesIdle = 0; // Number of frames character did not move
|
||||
|
||||
public function new(actionIdle:String, actionMove:String) {
|
||||
super();
|
||||
|
@ -24,61 +25,56 @@ class Character extends Trait {
|
|||
if(actionIdle != null && actionMove != null){
|
||||
this.actionIdle = actionIdle;
|
||||
this.actionMove = actionMove;
|
||||
currentAction = actionIdle;
|
||||
}
|
||||
|
||||
notifyOnInit(init);
|
||||
notifyOnUpdate(update);
|
||||
}
|
||||
|
||||
function init() {
|
||||
animation = object.animation;
|
||||
|
||||
// Try first child if we are running from armature
|
||||
if (animation == null){
|
||||
if (animation == null) {
|
||||
if(object.children.length > 0) {
|
||||
animation = object.children[0].animation;
|
||||
}
|
||||
}
|
||||
|
||||
// null check animation
|
||||
if(animation != null) {
|
||||
animation.pause();
|
||||
}
|
||||
if (animation == null) return;
|
||||
notifyOnUpdate(update);
|
||||
}
|
||||
|
||||
function update() {
|
||||
// get current position
|
||||
// Get current position
|
||||
var tr = object.transform;
|
||||
loc.set(tr.worldx(), tr.worldy(), tr.worldz());
|
||||
|
||||
// set previous position to current position if there is no previous position
|
||||
// Set previous position to current position if there is no previous position
|
||||
if (lastLoc == null) lastLoc = new Vec4(loc.x, loc.y, loc.z);
|
||||
|
||||
// check if character moved compared from last position
|
||||
// Check if character moved compared from last position
|
||||
speed = Vec4.distance3d(loc, lastLoc);
|
||||
|
||||
// update previous position to current position
|
||||
// Update previous position to current position
|
||||
// in preparation for next check
|
||||
lastLoc.setFrom(loc);
|
||||
|
||||
// if state is zero (idle) and speed is greater than zero, play move walk animation
|
||||
if (state == 0 && speed > 0) {
|
||||
state = 1;
|
||||
if (speed == 0) framesIdle++;
|
||||
else framesIdle = 0;
|
||||
|
||||
// null check animation and actionMove
|
||||
if(animation != null && actionMove != null){
|
||||
animation.play(actionMove);
|
||||
}
|
||||
// If state is idle and character is in movement, play move walk animation
|
||||
if (currentAction == actionIdle && framesIdle == 0) {
|
||||
currentAction = actionMove;
|
||||
|
||||
if (actionMove != null) animation.play(actionMove);
|
||||
}
|
||||
|
||||
// otherwise if state is one (walking) and speed equals zero, pause the walk animation
|
||||
else if (state == 1 && speed == 0) {
|
||||
state = 0;
|
||||
// Otherwise if state is walking and character is idle, play idle animation
|
||||
else if (currentAction == actionMove && framesIdle > 2) {
|
||||
currentAction = actionIdle;
|
||||
|
||||
// null check animation
|
||||
if(animation != null){
|
||||
animation.pause();
|
||||
}
|
||||
if (actionIdle != null) animation.play(actionIdle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue