diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 5213376..163132e 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -84,7 +84,7 @@ GameLib.Event.PARENT_WORLD_CHANGE = 0x42; GameLib.Event.ANIMATE = 0x43; GameLib.Event.ANIMATION_COMPILE_SUCCESS = 0x44; GameLib.Event.ANIMATION_COMPILE_FAILED = 0x45; -GameLib.Event.CUSTOM_CODE_SYSTEM_STARTED = 0x46; +//GameLib.Event.CUSTOM_CODE_SYSTEM_STARTED = 0x46; GameLib.Event.GAME_OVER = 0x47; GameLib.Event.GAME_START = 0x48; GameLib.Event.TOUCH_START = 0x49; @@ -191,7 +191,7 @@ GameLib.Event.GetEventName = function(number) { case 0x43 : return 'animate'; case 0x44 : return 'animation_compile_success'; case 0x45 : return 'animation_compile_failed'; - case 0x46 : return 'custom_code_system_started'; + case 0x46 : return 'unused'; case 0x47 : return 'game_over'; case 0x48 : return 'game_start'; case 0x49 : return 'touch_start'; diff --git a/src/game-lib-system-animation.js b/src/game-lib-system-animation.js index 4da7e39..32c597e 100644 --- a/src/game-lib-system-animation.js +++ b/src/game-lib-system-animation.js @@ -75,15 +75,15 @@ GameLib.System.Animation.prototype.instanceCreated = function(data) { data.component.animated ) { - if (data.texture.repeat.x > 1 || data.texture.repeat.x < 0) { + if (data.component.repeat.x > 1 || data.component.repeat.x < 0) { console.warn('cannot animate a texture with repeat.x greater than 1 or less than 0'); - data.texture.animated = false; + data.component.animated = false; return; } - if (data.texture.repeat.y > 1 || data.texture.repeat.y < 0) { + if (data.component.repeat.y > 1 || data.component.repeat.y < 0) { console.warn('cannot animate a texture with repeat.y greater than 1 or less than 0'); - data.texture.animated = false; + data.component.animated = false; return; } @@ -97,10 +97,10 @@ GameLib.System.Animation.prototype.removeComponent = function(data) { 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); + if (GameLib.Utils.UndefinedOrNull(this.textures[data.component.id])) { + console.warn('tried to remove an animated texture, which should have been in the list but isnt: ' + data.component.name); } else { - delete this.textures[data.texture.id]; + delete this.textures[data.component.id]; this.textureIds = Object.keys(this.textures); } } @@ -275,7 +275,7 @@ GameLib.System.Animation.prototype.beforeRender = function(data) { texture.offset.y += texture.repeat.y; } } - + } texture.updateInstance('offset'); diff --git a/src/game-lib-system-custom-code.js b/src/game-lib-system-custom-code.js index 9e7896b..afa99b7 100644 --- a/src/game-lib-system-custom-code.js +++ b/src/game-lib-system-custom-code.js @@ -12,9 +12,12 @@ GameLib.System.CustomCode = function( apiSystem ); - this.customCodeComponents = null; + this.instanceCreatedSubscription = null; + this.removeComponentSubscription = null; + this.compileSuccessSubscription = null; + this.compileFailedSubscription = null; - this.subscriptions = []; + this.subscriptions = {}; }; @@ -28,29 +31,73 @@ GameLib.System.CustomCode.prototype.start = function() { GameLib.System.prototype.start.call(this); - this.customCodeComponents = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.CustomCode); + this.instanceCreatedSubscription = GameLib.Event.Subscribe( + GameLib.Event.INSTANCE_CREATED, + this.instanceCreated.bind(this) + ); - this.customCodeComponents.map(function(customCodeComponent){ + this.removeComponentSubscription = GameLib.Event.Subscribe( + GameLib.Event.REMOVE_COMPONENT, + this.removeComponent.bind(this) + ); - this.subscriptions.push( - this.subscribe( - customCodeComponent.eventId, - customCodeComponent.instance - ) - ); + this.compileSuccessSubscription = GameLib.Event.Subscribe( + GameLib.Event.COMPILE_SUCCESS, + this.compileSuccess.bind(this) + ); - }.bind(this)); + this.compileFailedSubscription = GameLib.Event.Subscribe( + GameLib.Event.COMPILE_FAILED, + this.compileFailed.bind(this) + ); - GameLib.Event.Emit( - GameLib.Event.CUSTOM_CODE_SYSTEM_STARTED, - { - system : this, - components : this.customCodeComponents - } - ) }; +GameLib.System.CustomCode.prototype.instanceCreated = function(data) { + if (data.component instanceof GameLib.D3.CustomCode) { + + if (this.subscriptions[data.component.id]) { + console.warn('a component already existed'); + this.subscriptions[data.component.id].remove(); + } + + this.subscriptions[data.component.id] = GameLib.Event.Subscribe( + data.component.eventId, + data.component.instance + ); + } +}; + +GameLib.System.CustomCode.prototype.removeComponent = function(data) { + if (data.component instanceof GameLib.D3.CustomCode) { + if (this.subscriptions[data.component.id]) { + this.subscriptions[data.component.id].remove(); + delete this.subscriptions[data.component.id]; + } + } +}; + +GameLib.System.CustomCode.prototype.compileSuccess = function(data) { + + if (this.subscriptions[data.component.id]) { + this.subscriptions[data.component.id].remove(); + } + + this.subscriptions[data.component.id] = GameLib.Event.Subscribe( + data.component.eventId, + data.component.instance + ); +}; + +GameLib.System.CustomCode.prototype.compileFailed = function(data) { + if (this.subscriptions[data.component.id]) { + this.subscriptions[data.component.id].remove(); + delete this.subscriptions[data.component.id]; + } +}; + + /** * Stop the rendering system */ @@ -58,11 +105,19 @@ GameLib.System.CustomCode.prototype.stop = function() { GameLib.System.prototype.stop.call(this); - this.subscriptions.map(function(subscription){ - subscription.remove(); - }); + this.instanceCreatedSubscription.remove(); + this.removeComponentSubscription.remove(); + this.compileSuccessSubscription.remove(); + this.compileFailedSubscription.remove(); - this.subscriptions = []; + Object.keys(this.subscriptions).map( + function(componentId) { + if (this.subscriptions[componentId]) { + this.subscriptions[componentId].remove(); + delete this.subscriptions[componentId]; + } + }.bind(this) + ); };