From 0b9e1054beba2c43a2ec82e21e57dabfda368354 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Mon, 13 Nov 2017 06:01:36 +0100 Subject: [PATCH] animation system takes care of texture animations --- src/game-lib-a-1-event.js | 4 +- src/game-lib-d3-api-texture.js | 24 +++++ src/game-lib-d3-texture.js | 17 +++- src/game-lib-system-animation.js | 150 ++++++++++++++++++++++++++++++- 4 files changed, 191 insertions(+), 4 deletions(-) diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 7f95764..5213376 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -30,7 +30,7 @@ GameLib.Event.LOAD_COMPONENT_ERROR = 0xc; GameLib.Event.LOGGED_IN = 0xd; GameLib.Event.COMPONENT_CREATED = 0xe; 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.RIGID_BODY_INSTANCE_CREATED = 0x12; //GameLib.Event.TEXTURE_INSTANCE_CREATED = 0x13; @@ -137,7 +137,7 @@ GameLib.Event.GetEventName = function(number) { case 0xd : return 'logged_in'; case 0xe : return 'component_created'; 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 0x12 : return 'unused';//'rigid_body_instance_created'; case 0x13 : return 'unused';//'texture_instance_created'; diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 1bd2a5f..df9a29e 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -23,6 +23,9 @@ * @param premultiplyAlpha * @param encoding * @param canvas + * @param animated + * @param reverseAnimation + * @param forward * @param parentEntity * @constructor */ @@ -50,6 +53,9 @@ GameLib.D3.API.Texture = function( premultiplyAlpha, encoding, canvas, + animated, + reverseAnimation, + forward, parentEntity ) { if (GameLib.Utils.UndefinedOrNull(parentEntity)) { @@ -172,6 +178,21 @@ GameLib.D3.API.Texture = function( } 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; }; @@ -208,6 +229,9 @@ GameLib.D3.API.Texture.FromObject = function(objectTexture) { objectTexture.premultiplyAlpha, objectTexture.encoding, objectTexture.canvas, + objectTexture.animated, + objectTexture.reverseAnimation, + objectTexture.forward, objectTexture.parentEntity ) }; diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index a173e67..01fb45b 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -45,6 +45,9 @@ GameLib.D3.Texture = function( apiTexture.premultiplyAlpha, apiTexture.encoding, apiTexture.canvas, + apiTexture.animated, + apiTexture.reverseAnimation, + apiTexture.forward, apiTexture.parentEntity ); @@ -283,7 +286,7 @@ GameLib.D3.Texture.prototype.updateInstance = function(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') { @@ -383,6 +386,15 @@ GameLib.D3.Texture.prototype.updateInstance = function(property) { if (property === '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.encoding, GameLib.Utils.IdOrNull(this.canvas), + this.animated, + this.reverseAnimation, + this.forward, GameLib.Utils.IdOrNull(this.parentEntity) ); diff --git a/src/game-lib-system-animation.js b/src/game-lib-system-animation.js index eedc882..4da7e39 100644 --- a/src/game-lib-system-animation.js +++ b/src/game-lib-system-animation.js @@ -13,6 +13,8 @@ GameLib.System.Animation = function( this.animations = {}; this.latest = {}; + this.textures = {}; + this.textureIds = []; }; GameLib.System.Animation.prototype = Object.create(GameLib.System.prototype); @@ -49,7 +51,90 @@ GameLib.System.Animation.prototype.start = function() { function(data) { this.detachAnimation(data.mesh); }.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) { @@ -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) { @@ -935,5 +1077,11 @@ GameLib.System.Animation.prototype.stop = function() { } }.bind(this) ); + + this.instanceCreatedSubscription.remove(); + + this.removeComponentSubscription.remove(); + + this.textureAnimatedSubscription.remove(); };