https://drive.google.com/file/d/...
// The following embedded xml is for the editor and describes how the behavior can be edited: // Supported types are: int, float, string, bool, color, vect3d, scenenode, texture, action /* <behavior jsname="behavior_FootSteps" description="Footsteps"> <property name="stepLength" type="float" default="40.0" /> <property name="actionOnStep" type="action" default="" /> <property name="groundCheckVectorDist" type="float" default="16.0" /> </behavior> */
var behavior_FootSteps = function () { this.traveledDistance = 0; this.lastPos = null; };
behavior_FootSteps.prototype.onAnimate = function (node, timeMs) { if (!this.lastPos) { this.lastPos = ccbGetSceneNodeProperty(node, 'Position'); this.lastPos.y = 0; return false; }
var pos = ccbGetSceneNodeProperty(node, 'Position');
// check if player is grounded var isGrounded = !!ccbGetCollisionPointOfWorldWithLine( pos.x, pos.y, pos.z, pos.x, pos.y - this.groundCheckVectorDist, pos.z );
// ignore vertical movement pos.y = 0;
this.traveledDistance += pos.substract(this.lastPos).getLength();
// reset traveled distance if stops if (pos.x === this.lastPos.x && pos.z === this.lastPos.z) { this.traveledDistance = 0; }
// reset traveled distance if not grounded if (!isGrounded) { this.traveledDistance = 0; }
if (this.traveledDistance > this.stepLength) { this.traveledDistance = - this.stepLength / 2; ccbInvokeAction(this.actionOnStep, node); }
this.lastPos = pos;
return true; }
|