2017-09-05 05:22:52 +02:00
|
|
|
/**
|
|
|
|
* System takes care of updating all the entities (based on their component data)
|
|
|
|
* @param apiSystem GameLib.API.System
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.System.Animation = function(
|
|
|
|
apiSystem
|
|
|
|
) {
|
|
|
|
GameLib.System.call(
|
|
|
|
this,
|
|
|
|
apiSystem
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.System.Animation.prototype = Object.create(GameLib.System.prototype);
|
|
|
|
GameLib.System.Animation.prototype.constructor = GameLib.System.Animation;
|
|
|
|
|
|
|
|
GameLib.System.Animation.prototype.start = function() {
|
2017-09-14 05:42:38 +02:00
|
|
|
|
|
|
|
GameLib.System.prototype.start.call(this);
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
this.animationEntities = GameLib.EntityManager.Instance.findEntities([GameLib.D3.Animation]);
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
this.animationEntities.map(
|
|
|
|
function(animationEntity) {
|
2017-09-14 05:42:38 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* Get all meshes and all animation components
|
|
|
|
*/
|
|
|
|
var meshComponents = animationEntity.getComponents(GameLib.D3.Mesh);
|
|
|
|
var animationComponents = animationEntity.getComponents(GameLib.D3.Animation);
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
animationComponents.map(function(animation){
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
meshComponents.map(function(mesh){
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
if (animation.rotationSpeed) {
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* Back up the current property descriptor
|
|
|
|
*/
|
|
|
|
mesh.animationObject = {
|
|
|
|
backupAngleDescriptor : Object.getOwnPropertyDescriptor(mesh.quaternion, 'angle'),
|
|
|
|
targetAngle : mesh.quaternion.angle,
|
|
|
|
angleIncrement : true,
|
|
|
|
intermediateAngle : mesh.quaternion.angle,
|
2017-09-18 15:39:40 +02:00
|
|
|
subscription : null,
|
|
|
|
inProcess : false,
|
2017-09-19 21:27:11 +02:00
|
|
|
blocking : animation.blocking//,
|
|
|
|
// callbacks : [],
|
|
|
|
// storedValues : []
|
2017-09-17 19:12:45 +02:00
|
|
|
};
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
var getIntermediateAngle = function() {
|
|
|
|
return mesh.animationObject.intermediateAngle;
|
|
|
|
};
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-18 15:39:40 +02:00
|
|
|
var getTargetAngle = function() {
|
|
|
|
|
2017-09-19 21:27:11 +02:00
|
|
|
// if (mesh.animationObject.storedValues.length > 0) {
|
|
|
|
// return mesh.animationObject.storedValues[mesh.animationObject.storedValues.length - 1];
|
|
|
|
// }
|
2017-09-18 15:39:40 +02:00
|
|
|
|
|
|
|
return mesh.animationObject.targetAngle;
|
|
|
|
};
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
var animateRotation = function(value) {
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-18 15:39:40 +02:00
|
|
|
mesh.animationObject.intermediateAngle += value;
|
2017-09-17 19:12:45 +02:00
|
|
|
|
|
|
|
var done = false;
|
|
|
|
|
|
|
|
if (mesh.animationObject.angleIncrement) {
|
|
|
|
/**
|
|
|
|
* We are rotating upwards
|
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
if (mesh.animationObject.intermediateAngle >= mesh.animationObject.targetAngle) {
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* We need to stop
|
|
|
|
*/
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* We are rotating downwards
|
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
if (mesh.animationObject.intermediateAngle <= mesh.animationObject.targetAngle) {
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* We need to stop
|
|
|
|
*/
|
|
|
|
done = true;
|
|
|
|
}
|
2017-09-14 10:50:09 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
if (done) {
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
2017-09-18 15:39:40 +02:00
|
|
|
* We clamp to our target angle
|
2017-09-17 19:12:45 +02:00
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
mesh.animationObject.intermediateAngle = mesh.animationObject.targetAngle;
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
2017-09-18 15:39:40 +02:00
|
|
|
* We limit our intermediate angle between values of -pi and pi
|
2017-09-17 19:12:45 +02:00
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
while (mesh.animationObject.intermediateAngle > Math.PI) {
|
|
|
|
mesh.animationObject.intermediateAngle -= (Math.PI * 2);
|
|
|
|
}
|
2017-09-14 10:50:09 +02:00
|
|
|
|
2017-09-18 15:39:40 +02:00
|
|
|
while (mesh.animationObject.intermediateAngle < -(Math.PI)) {
|
|
|
|
mesh.animationObject.intermediateAngle += (Math.PI * 2);
|
|
|
|
}
|
2017-09-17 19:12:45 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-18 15:39:40 +02:00
|
|
|
* We apply our new intermediate angle to our target
|
2017-09-17 19:12:45 +02:00
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
mesh.animationObject.targetAngle = mesh.animationObject.intermediateAngle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the actual rotation to the mesh
|
|
|
|
*/
|
|
|
|
mesh.updateInstanceRotationFromAxisAngle(mesh.quaternion.axis, mesh.animationObject.intermediateAngle);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check again if we are done, we need to do some additional work -
|
|
|
|
*/
|
|
|
|
if (done) {
|
|
|
|
|
|
|
|
if (!mesh.animationObject.subscription) {
|
|
|
|
var message = 'mesh animation object subscription went missing for ';
|
|
|
|
message += mesh.name + ': ';
|
|
|
|
message += animation.name;
|
|
|
|
console.warn(message);
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
2017-09-17 19:12:45 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-18 15:39:40 +02:00
|
|
|
* Stop subscribing to before render events
|
2017-09-17 19:12:45 +02:00
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
mesh.animationObject.subscription.remove();
|
2017-09-17 19:12:45 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-18 15:39:40 +02:00
|
|
|
* @type {null}
|
2017-09-17 19:12:45 +02:00
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
mesh.animationObject.subscription = null;
|
2017-09-17 19:12:45 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-18 15:39:40 +02:00
|
|
|
* For some meshes, when we are done with the animation, we want to apply
|
|
|
|
* the current state of the mesh to the object data itself (i.e. update
|
|
|
|
* its vertices etc)
|
2017-09-17 19:12:45 +02:00
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
if (animation.applyToMeshWhenDone) {
|
|
|
|
/**
|
|
|
|
* Now we say that our intermediate angle is zero, because we will apply our rotation
|
|
|
|
* and this will prevent us from re-registering a new 'animationRender' event
|
|
|
|
*/
|
|
|
|
mesh.animationObject.targetAngle = 0;
|
|
|
|
mesh.animationObject.intermediateAngle = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply our position, rotation and scale to the mesh
|
|
|
|
*/
|
|
|
|
mesh.applyPositionRotationScale();
|
|
|
|
}
|
2017-09-17 19:12:45 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-18 15:39:40 +02:00
|
|
|
* Tell our animation component that it is no longer in process...
|
|
|
|
* @type {boolean}
|
2017-09-17 19:12:45 +02:00
|
|
|
*/
|
2017-09-18 15:39:40 +02:00
|
|
|
mesh.animationObject.inProcess = false;
|
|
|
|
|
|
|
|
// if (mesh.animationObject.callbacks.length > 0) {
|
|
|
|
// var callback = mesh.animationObject.callbacks[0];
|
|
|
|
// mesh.animationObject.callbacks.splice(0,1);
|
|
|
|
// callback();
|
|
|
|
// }
|
|
|
|
|
2017-09-19 21:27:11 +02:00
|
|
|
// mesh.animationObject.storedValues = [];
|
2017-09-17 19:12:45 +02:00
|
|
|
}
|
|
|
|
};
|
2017-09-18 15:39:40 +02:00
|
|
|
|
|
|
|
var setTargetAngle = function(value) {
|
2017-09-17 19:12:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we have work to do
|
|
|
|
*/
|
|
|
|
if (mesh.animationObject.intermediateAngle === value) {
|
2017-09-18 15:39:40 +02:00
|
|
|
|
|
|
|
mesh.animationObject.inProcess = false;
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* Nothing to do
|
|
|
|
*/
|
|
|
|
return;
|
2017-09-18 15:39:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we have another animation in process
|
|
|
|
*/
|
|
|
|
if (mesh.animationObject.inProcess && mesh.animationObject.blocking) {
|
|
|
|
|
|
|
|
console.log('another animation is already in process');
|
|
|
|
|
2017-09-19 21:27:11 +02:00
|
|
|
// setTargetAngle(value);
|
2017-09-18 15:39:40 +02:00
|
|
|
|
2017-09-19 21:27:11 +02:00
|
|
|
// GameLib.Utils.PushUnique(mesh.animationObject.storedValues, value);
|
|
|
|
//
|
2017-09-18 15:39:40 +02:00
|
|
|
// mesh.animationObject.callbacks.push(
|
|
|
|
// function(__value) {
|
|
|
|
// return function(){
|
|
|
|
// mesh.quaternion.angle = __value;
|
|
|
|
// }
|
|
|
|
// }(value)
|
|
|
|
// );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond that our angle is actually our target angle (it will be that soon)
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We indicate that we now have an animation in process
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
mesh.animationObject.inProcess = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ok - all good - lets start the animation
|
|
|
|
*/
|
|
|
|
if (mesh.animationObject.intermediateAngle > value) {
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* We will rotate towards by decrementing
|
|
|
|
*/
|
|
|
|
mesh.animationObject.angleIncrement = false;
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* We will rotate towards by incrementing
|
|
|
|
*/
|
|
|
|
mesh.animationObject.angleIncrement = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We say what our target angle is - when we reach our target angle, we want
|
|
|
|
* to stop our animation
|
|
|
|
*/
|
|
|
|
mesh.animationObject.targetAngle = value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now we subscribe to 'before render' events, and slowly increment the value
|
|
|
|
* @type {{fn, remove}}
|
|
|
|
*/
|
|
|
|
mesh.animationObject.subscription = GameLib.Event.Subscribe(
|
|
|
|
GameLib.Event.BEFORE_RENDER,
|
|
|
|
function(data) {
|
|
|
|
|
|
|
|
var increment = Math.abs(animation.rotationSpeed);
|
|
|
|
|
2017-09-18 15:39:40 +02:00
|
|
|
if (!mesh.animationObject.angleIncrement) {
|
2017-09-17 19:12:45 +02:00
|
|
|
increment *= -1;
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:39:40 +02:00
|
|
|
animateRotation(data.delta * increment);
|
2017-09-17 19:12:45 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the property descriptor - to slowly increment the angle, but report back its
|
|
|
|
* target angle as its actual angle.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(
|
|
|
|
mesh.quaternion,
|
|
|
|
'angle',
|
|
|
|
{
|
2017-09-18 15:39:40 +02:00
|
|
|
'get' : getTargetAngle,
|
|
|
|
'set' : setTargetAngle,
|
2017-09-17 19:12:45 +02:00
|
|
|
'configurable' : true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2017-09-14 05:42:38 +02:00
|
|
|
};
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* Stop Animation System
|
|
|
|
*/
|
2017-09-05 05:22:52 +02:00
|
|
|
GameLib.System.Animation.prototype.stop = function() {
|
2017-09-14 05:42:38 +02:00
|
|
|
|
|
|
|
GameLib.System.prototype.stop.call(this);
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
this.animationEntities = GameLib.EntityManager.Instance.findEntities([GameLib.D3.Animation]);
|
|
|
|
|
|
|
|
this.animationEntities.map(
|
|
|
|
function(animationEntity) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all meshes and all animation components
|
|
|
|
*/
|
|
|
|
var meshComponents = animationEntity.getComponents(GameLib.D3.Mesh);
|
|
|
|
var animationComponents = animationEntity.getComponents(GameLib.D3.Animation);
|
|
|
|
|
|
|
|
animationComponents.map(
|
|
|
|
function (animation) {
|
|
|
|
|
|
|
|
meshComponents.map(
|
|
|
|
function (mesh) {
|
|
|
|
|
|
|
|
if (animation.rotationSpeed) {
|
|
|
|
|
|
|
|
if (mesh.animationObject) {
|
|
|
|
|
|
|
|
if (mesh.animationObject.backupAngleDescriptor) {
|
|
|
|
Object.defineProperty(
|
|
|
|
mesh.quaternion,
|
|
|
|
'angle',
|
|
|
|
mesh.animationObject.backupAngleDescriptor
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh.animationObject.targetAngle) {
|
|
|
|
mesh.quaternion.angle = mesh.animationObject.targetAngle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh.animationObject.subscription) {
|
|
|
|
mesh.animationObject.subscription.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete mesh.animationObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
2017-09-05 05:22:52 +02:00
|
|
|
};
|
|
|
|
|