custom code system independent

beta.r3js.org
-=yb4f310 2017-11-13 06:49:42 +01:00
parent 0b9e1054be
commit ccc030a2eb
3 changed files with 87 additions and 32 deletions

View File

@ -84,7 +84,7 @@ GameLib.Event.PARENT_WORLD_CHANGE = 0x42;
GameLib.Event.ANIMATE = 0x43; GameLib.Event.ANIMATE = 0x43;
GameLib.Event.ANIMATION_COMPILE_SUCCESS = 0x44; GameLib.Event.ANIMATION_COMPILE_SUCCESS = 0x44;
GameLib.Event.ANIMATION_COMPILE_FAILED = 0x45; 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_OVER = 0x47;
GameLib.Event.GAME_START = 0x48; GameLib.Event.GAME_START = 0x48;
GameLib.Event.TOUCH_START = 0x49; GameLib.Event.TOUCH_START = 0x49;
@ -191,7 +191,7 @@ GameLib.Event.GetEventName = function(number) {
case 0x43 : return 'animate'; case 0x43 : return 'animate';
case 0x44 : return 'animation_compile_success'; case 0x44 : return 'animation_compile_success';
case 0x45 : return 'animation_compile_failed'; case 0x45 : return 'animation_compile_failed';
case 0x46 : return 'custom_code_system_started'; case 0x46 : return 'unused';
case 0x47 : return 'game_over'; case 0x47 : return 'game_over';
case 0x48 : return 'game_start'; case 0x48 : return 'game_start';
case 0x49 : return 'touch_start'; case 0x49 : return 'touch_start';

View File

@ -75,15 +75,15 @@ GameLib.System.Animation.prototype.instanceCreated = function(data) {
data.component.animated 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'); 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; 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'); 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; return;
} }
@ -97,10 +97,10 @@ GameLib.System.Animation.prototype.removeComponent = function(data) {
data.component instanceof GameLib.D3.Texture && data.component instanceof GameLib.D3.Texture &&
data.component.animated data.component.animated
) { ) {
if (GameLib.Utils.UndefinedOrNull(this.textures[data.texture.id])) { 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.texture.name); console.warn('tried to remove an animated texture, which should have been in the list but isnt: ' + data.component.name);
} else { } else {
delete this.textures[data.texture.id]; delete this.textures[data.component.id];
this.textureIds = Object.keys(this.textures); this.textureIds = Object.keys(this.textures);
} }
} }
@ -275,7 +275,7 @@ GameLib.System.Animation.prototype.beforeRender = function(data) {
texture.offset.y += texture.repeat.y; texture.offset.y += texture.repeat.y;
} }
} }
} }
texture.updateInstance('offset'); texture.updateInstance('offset');

View File

@ -12,9 +12,12 @@ GameLib.System.CustomCode = function(
apiSystem 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); 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.compileSuccessSubscription = GameLib.Event.Subscribe(
this.subscribe( GameLib.Event.COMPILE_SUCCESS,
customCodeComponent.eventId, this.compileSuccess.bind(this)
customCodeComponent.instance );
)
);
}.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 * Stop the rendering system
*/ */
@ -58,11 +105,19 @@ GameLib.System.CustomCode.prototype.stop = function() {
GameLib.System.prototype.stop.call(this); GameLib.System.prototype.stop.call(this);
this.subscriptions.map(function(subscription){ this.instanceCreatedSubscription.remove();
subscription.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)
);
}; };