animation system takes care of texture animations

beta.r3js.org
-=yb4f310 2017-11-13 06:01:36 +01:00
parent 04844eecee
commit 0b9e1054be
4 changed files with 191 additions and 4 deletions

View File

@ -30,7 +30,7 @@ GameLib.Event.LOAD_COMPONENT_ERROR = 0xc;
GameLib.Event.LOGGED_IN = 0xd; GameLib.Event.LOGGED_IN = 0xd;
GameLib.Event.COMPONENT_CREATED = 0xe; GameLib.Event.COMPONENT_CREATED = 0xe;
GameLib.Event.COMPONENT_CLONED = 0xf; GameLib.Event.COMPONENT_CLONED = 0xf;
//GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x10; GameLib.Event.TEXTURE_ANIMATED_CHANGE = 0x10;
//GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED = 0x11; //GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED = 0x11;
//GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x12; //GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x12;
//GameLib.Event.TEXTURE_INSTANCE_CREATED = 0x13; //GameLib.Event.TEXTURE_INSTANCE_CREATED = 0x13;
@ -137,7 +137,7 @@ GameLib.Event.GetEventName = function(number) {
case 0xd : return 'logged_in'; case 0xd : return 'logged_in';
case 0xe : return 'component_created'; case 0xe : return 'component_created';
case 0xf : return 'component_cloned'; case 0xf : return 'component_cloned';
case 0x10 : return 'unused';//'scene_object_instance_created'; case 0x10 : return 'texture_animated_change';//'scene_object_instance_created';
case 0x11 : return 'unused';//'world_instance_created'; case 0x11 : return 'unused';//'world_instance_created';
case 0x12 : return 'unused';//'rigid_body_instance_created'; case 0x12 : return 'unused';//'rigid_body_instance_created';
case 0x13 : return 'unused';//'texture_instance_created'; case 0x13 : return 'unused';//'texture_instance_created';

View File

@ -23,6 +23,9 @@
* @param premultiplyAlpha * @param premultiplyAlpha
* @param encoding * @param encoding
* @param canvas * @param canvas
* @param animated
* @param reverseAnimation
* @param forward
* @param parentEntity * @param parentEntity
* @constructor * @constructor
*/ */
@ -50,6 +53,9 @@ GameLib.D3.API.Texture = function(
premultiplyAlpha, premultiplyAlpha,
encoding, encoding,
canvas, canvas,
animated,
reverseAnimation,
forward,
parentEntity parentEntity
) { ) {
if (GameLib.Utils.UndefinedOrNull(parentEntity)) { if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
@ -172,6 +178,21 @@ GameLib.D3.API.Texture = function(
} }
this.canvas = canvas; this.canvas = canvas;
if (GameLib.Utils.UndefinedOrNull(animated)) {
animated = false;
}
this.animated = animated;
if (GameLib.Utils.UndefinedOrNull(reverseAnimation)) {
reverseAnimation = false;
}
this.reverseAnimation = reverseAnimation;
if (GameLib.Utils.UndefinedOrNull(forward)) {
forward = true;
}
this.forward = forward;
this.needsUpdate = false; this.needsUpdate = false;
}; };
@ -208,6 +229,9 @@ GameLib.D3.API.Texture.FromObject = function(objectTexture) {
objectTexture.premultiplyAlpha, objectTexture.premultiplyAlpha,
objectTexture.encoding, objectTexture.encoding,
objectTexture.canvas, objectTexture.canvas,
objectTexture.animated,
objectTexture.reverseAnimation,
objectTexture.forward,
objectTexture.parentEntity objectTexture.parentEntity
) )
}; };

View File

@ -45,6 +45,9 @@ GameLib.D3.Texture = function(
apiTexture.premultiplyAlpha, apiTexture.premultiplyAlpha,
apiTexture.encoding, apiTexture.encoding,
apiTexture.canvas, apiTexture.canvas,
apiTexture.animated,
apiTexture.reverseAnimation,
apiTexture.forward,
apiTexture.parentEntity apiTexture.parentEntity
); );
@ -283,7 +286,7 @@ GameLib.D3.Texture.prototype.updateInstance = function(property) {
} }
if (GameLib.Utils.UndefinedOrNull(property)) { if (GameLib.Utils.UndefinedOrNull(property)) {
//throw new Error('need to specify a property'); throw new Error('need to specify a property');
} }
if (property === 'image') { if (property === 'image') {
@ -383,6 +386,15 @@ GameLib.D3.Texture.prototype.updateInstance = function(property) {
if (property === 'wrapT') { if (property === 'wrapT') {
this.instance.wrapT = this.wrapT; this.instance.wrapT = this.wrapT;
} }
if (property === 'animated') {
GameLib.Event.Emit(
GameLib.Event.TEXTURE_ANIMATED_CHANGE,
{
texture : this
}
)
}
}; };
/** /**
@ -419,6 +431,9 @@ GameLib.D3.Texture.prototype.toApiObject = function() {
this.premultiplyAlpha, this.premultiplyAlpha,
this.encoding, this.encoding,
GameLib.Utils.IdOrNull(this.canvas), GameLib.Utils.IdOrNull(this.canvas),
this.animated,
this.reverseAnimation,
this.forward,
GameLib.Utils.IdOrNull(this.parentEntity) GameLib.Utils.IdOrNull(this.parentEntity)
); );

View File

@ -13,6 +13,8 @@ GameLib.System.Animation = function(
this.animations = {}; this.animations = {};
this.latest = {}; this.latest = {};
this.textures = {};
this.textureIds = [];
}; };
GameLib.System.Animation.prototype = Object.create(GameLib.System.prototype); GameLib.System.Animation.prototype = Object.create(GameLib.System.prototype);
@ -49,7 +51,90 @@ GameLib.System.Animation.prototype.start = function() {
function(data) { function(data) {
this.detachAnimation(data.mesh); this.detachAnimation(data.mesh);
}.bind(this) }.bind(this)
) );
this.instanceCreatedSubscription = GameLib.Event.Subscribe(
GameLib.Event.INSTANCE_CREATED,
this.instanceCreated.bind(this)
);
this.removeComponentSubscription = GameLib.Event.Subscribe(
GameLib.Event.REMOVE_COMPONENT,
this.removeComponent.bind(this)
);
this.textureAnimatedSubscription = GameLib.Event.Subscribe(
GameLib.Event.TEXTURE_ANIMATED_CHANGE,
this.textureAnimatedChange.bind(this)
);
};
GameLib.System.Animation.prototype.instanceCreated = function(data) {
if (
data.component instanceof GameLib.D3.Texture &&
data.component.animated
) {
if (data.texture.repeat.x > 1 || data.texture.repeat.x < 0) {
console.warn('cannot animate a texture with repeat.x greater than 1 or less than 0');
data.texture.animated = false;
return;
}
if (data.texture.repeat.y > 1 || data.texture.repeat.y < 0) {
console.warn('cannot animate a texture with repeat.y greater than 1 or less than 0');
data.texture.animated = false;
return;
}
this.textures[data.component.id] = data.component;
this.textureIds = Object.keys(this.textures);
}
};
GameLib.System.Animation.prototype.removeComponent = function(data) {
if (
data.component instanceof GameLib.D3.Texture &&
data.component.animated
) {
if (GameLib.Utils.UndefinedOrNull(this.textures[data.texture.id])) {
console.warn('tried to remove an animated texture, which should have been in the list but isnt: ' + data.texture.name);
} else {
delete this.textures[data.texture.id];
this.textureIds = Object.keys(this.textures);
}
}
};
GameLib.System.Animation.prototype.textureAnimatedChange = function(data) {
if (data.texture.animated) {
if (data.texture.repeat.x > 1 || data.texture.repeat.x < 0) {
console.warn('cannot animate a texture with repeat.x greater than 1 or less than 0');
data.texture.animated = false;
return;
}
if (data.texture.repeat.y > 1 || data.texture.repeat.y < 0) {
console.warn('cannot animate a texture with repeat.y greater than 1 or less than 0');
data.texture.animated = false;
return;
}
this.textures[data.texture.id] = data.texture;
this.textureIds = Object.keys(this.textures);
} else {
if (GameLib.Utils.UndefinedOrNull(this.textures[data.texture.id])) {
console.warn('tried to remove an animated texture, which should have been in the list but isnt: ' + data.texture.name);
} else {
delete this.textures[data.texture.id];
this.textureIds = Object.keys(this.textures);
}
}
}; };
GameLib.System.Animation.prototype.beforeRender = function(data) { GameLib.System.Animation.prototype.beforeRender = function(data) {
@ -140,6 +225,63 @@ GameLib.System.Animation.prototype.beforeRender = function(data) {
); );
} }
} }
this.textureIds.map(
function(textureId) {
var texture = this.textures[textureId];
if (texture.reverseAnimation) {
if (texture.forward) {
texture.offset.x += texture.repeat.x;
if (texture.offset.x >= (1 - texture.repeat.x)) {
if (texture.offset.y >= (1 - texture.repeat.y)) {
texture.offset.x = (1 - texture.repeat.x);
texture.offset.y = (1 - texture.repeat.y);
texture.forward = false;
} else {
texture.offset.x = 0;
texture.offset.y += texture.repeat.y;
}
}
}
if (!texture.forward) {
texture.offset.x -= texture.repeat.x;
if (texture.offset.x <= 0) {
texture.offset.x = 0;
if (texture.offset.y < texture.repeat.y) {
texture.offset.x = texture.repeat.x;
texture.offset.y = 0;
texture.forward = true;
} else {
texture.offset.x = (1 - texture.repeat.x);
texture.offset.y -= texture.repeat.y;
}
}
}
} else {
texture.offset.x += texture.repeat.x;
if (texture.offset.x >= (1 - texture.repeat.x)) {
if (texture.offset.y >= (1 - texture.repeat.y)) {
texture.offset.x = 0;
texture.offset.y = 0;
} else {
texture.offset.x = 0;
texture.offset.y += texture.repeat.y;
}
}
}
texture.updateInstance('offset');
}.bind(this)
)
}; };
GameLib.System.Animation.prototype.detachAnimation = function(mesh) { GameLib.System.Animation.prototype.detachAnimation = function(mesh) {
@ -935,5 +1077,11 @@ GameLib.System.Animation.prototype.stop = function() {
} }
}.bind(this) }.bind(this)
); );
this.instanceCreatedSubscription.remove();
this.removeComponentSubscription.remove();
this.textureAnimatedSubscription.remove();
}; };