entities become self-aware of init state

master
Theunis J. Botha 2021-09-23 11:39:21 +02:00
parent 823dcdbf14
commit f6978c091e
34 changed files with 712 additions and 217 deletions

9
dist/index.html vendored
View File

@ -32,10 +32,15 @@
let image = new R3.Image();
let slider = new R3.Entity.Slider(
{
canvas : canvas,
images : [image]
canvas : null
}
);
slider.canvas = canvas;
slider.images = [image];
slider.canvas = null;
slider.canvas = canvas;
slider.images = [image];
});
</script>
</body>

355
dist/r3.js vendored
View File

@ -1,6 +1,6 @@
class R3 {
static version = '3.0.17';
static compileDate = '2021 Sep 22 - 14:41:58 pm';
static version = '3.0.58';
static compileDate = '2021 Sep 23 - 11:36:17 am';
}
class Runtime {
@ -1153,6 +1153,8 @@ class SystemInput extends System {
*/
static OnSliderEntityInitialized(object) {
console.log('Slider Entity Initialized');
}
/**
@ -1356,6 +1358,11 @@ class SystemLinking extends System {
SystemLinking.OnCreateInstanceBefore
);
SystemLinking.Subscriptions['ENTITY_PROPERTY_UPDATE'] = Event.Subscribe(
Event.ENTITY_PROPERTY_UPDATE,
SystemLinking.OnEntityPropertyUpdate
);
SystemLinking.Started = true;
console.log('Started system: SystemLinking');
@ -1384,6 +1391,9 @@ class SystemLinking extends System {
SystemLinking.Subscriptions['CREATE_INSTANCE_BEFORE'].remove();
delete SystemLinking.Subscriptions['CREATE_INSTANCE_BEFORE'];
SystemLinking.Subscriptions['ENTITY_PROPERTY_UPDATE'].remove();
delete SystemLinking.Subscriptions['ENTITY_PROPERTY_UPDATE'];
SystemLinking.Started = false;
console.log('Stopped system: SystemLinking');
@ -1477,6 +1487,37 @@ class SystemLinking extends System {
}
/**
* OnEntityPropertyUpdate()
* - Listens to events of type Event.ENTITY_PROPERTY_UPDATE and executes this function.
* @param object (The event data passed as argument - typically an R3Object)
* @return
*/
static OnEntityPropertyUpdate(object) {
console.log('Entity object update', object);
console.log('canvas = ' + object.entity.canvas);
console.log('images = ' + object.entity.images);
if (!object.entity.underConstruction) {
object.entity.initializeDepth = 0;
object.entity.initialize();
if (object.entity.started && !object.entity.initialized) {
object.entity.stop();
}
}
// if (!object.entity.initialized) {
// object.entity.initialize();
// } else {
// object.entity.stop();
// }
// object.entity.initialize();
}
}
/**
@ -2299,40 +2340,41 @@ Event.DISPOSE_OBJECT = 0x7;
Event.DOM_COMPONENT_INITIALIZED = 0x8;
Event.ENTITY_CREATED = 0x9;
Event.ENTITY_INITIALIZED = 0xa;
Event.ENTITY_STARTED = 0xb;
Event.GET_API_URL = 0xc;
Event.GET_RUNTIME = 0xd;
Event.GET_WINDOW_SIZE = 0xe;
Event.GRAPHICS_COMPONENT_INITIALIZED = 0xf;
Event.IMAGE_COMPONENT_INITIALIZED = 0x10;
Event.IMAGE_INSTANCE_CREATED = 0x11;
Event.INPUT_COMPONENT_INITIALIZED = 0x12;
Event.INSTANCE_CREATED = 0x13;
Event.INSTANCE_DISPOSED = 0x14;
Event.KEYBOARD_DOWN = 0x15;
Event.KEYBOARD_UP = 0x16;
Event.MOUSE_DOWN = 0x17;
Event.MOUSE_MOVE = 0x18;
Event.MOUSE_UP = 0x19;
Event.MOUSE_WHEEL = 0x1a;
Event.OBJECT_CREATED = 0x1b;
Event.OBJECT_INITIALIZED = 0x1c;
Event.PAUSE = 0x1d;
Event.PROJECT_INITIALIZED = 0x1e;
Event.RESTART = 0x1f;
Event.SLIDER_ENTITY_INITIALIZED = 0x20;
Event.START = 0x21;
Event.TOUCH_CANCEL = 0x22;
Event.TOUCH_END = 0x23;
Event.TOUCH_MOVE = 0x24;
Event.TOUCH_START = 0x25;
Event.UPDATE_FROM_INSTANCE_AFTER = 0x26;
Event.UPDATE_FROM_INSTANCE_BEFORE = 0x27;
Event.UPDATE_INSTANCE_AFTER = 0x28;
Event.UPDATE_INSTANCE_BEFORE = 0x29;
Event.UPDATE_INSTANCE_PROPERTY = 0x2a;
Event.UPDATE_PROPERTY_FROM_INSTANCE = 0x2b;
Event.MAX_EVENTS = 0x2c;
Event.ENTITY_PROPERTY_UPDATE = 0xb;
Event.ENTITY_STARTED = 0xc;
Event.GET_API_URL = 0xd;
Event.GET_RUNTIME = 0xe;
Event.GET_WINDOW_SIZE = 0xf;
Event.GRAPHICS_COMPONENT_INITIALIZED = 0x10;
Event.IMAGE_COMPONENT_INITIALIZED = 0x11;
Event.IMAGE_INSTANCE_CREATED = 0x12;
Event.INPUT_COMPONENT_INITIALIZED = 0x13;
Event.INSTANCE_CREATED = 0x14;
Event.INSTANCE_DISPOSED = 0x15;
Event.KEYBOARD_DOWN = 0x16;
Event.KEYBOARD_UP = 0x17;
Event.MOUSE_DOWN = 0x18;
Event.MOUSE_MOVE = 0x19;
Event.MOUSE_UP = 0x1a;
Event.MOUSE_WHEEL = 0x1b;
Event.OBJECT_CREATED = 0x1c;
Event.OBJECT_INITIALIZED = 0x1d;
Event.PAUSE = 0x1e;
Event.PROJECT_INITIALIZED = 0x1f;
Event.RESTART = 0x20;
Event.SLIDER_ENTITY_INITIALIZED = 0x21;
Event.START = 0x22;
Event.TOUCH_CANCEL = 0x23;
Event.TOUCH_END = 0x24;
Event.TOUCH_MOVE = 0x25;
Event.TOUCH_START = 0x26;
Event.UPDATE_FROM_INSTANCE_AFTER = 0x27;
Event.UPDATE_FROM_INSTANCE_BEFORE = 0x28;
Event.UPDATE_INSTANCE_AFTER = 0x29;
Event.UPDATE_INSTANCE_BEFORE = 0x2a;
Event.UPDATE_INSTANCE_PROPERTY = 0x2b;
Event.UPDATE_PROPERTY_FROM_INSTANCE = 0x2c;
Event.MAX_EVENTS = 0x2d;
Event.GetEventName = function(eventId) {
@ -2347,39 +2389,40 @@ Event.GetEventName = function(eventId) {
case 0x8 : return 'dom_component_initialized';
case 0x9 : return 'entity_created';
case 0xa : return 'entity_initialized';
case 0xb : return 'entity_started';
case 0xc : return 'get_api_url';
case 0xd : return 'get_runtime';
case 0xe : return 'get_window_size';
case 0xf : return 'graphics_component_initialized';
case 0x10 : return 'image_component_initialized';
case 0x11 : return 'image_instance_created';
case 0x12 : return 'input_component_initialized';
case 0x13 : return 'instance_created';
case 0x14 : return 'instance_disposed';
case 0x15 : return 'keyboard_down';
case 0x16 : return 'keyboard_up';
case 0x17 : return 'mouse_down';
case 0x18 : return 'mouse_move';
case 0x19 : return 'mouse_up';
case 0x1a : return 'mouse_wheel';
case 0x1b : return 'object_created';
case 0x1c : return 'object_initialized';
case 0x1d : return 'pause';
case 0x1e : return 'project_initialized';
case 0x1f : return 'restart';
case 0x20 : return 'slider_entity_initialized';
case 0x21 : return 'start';
case 0x22 : return 'touch_cancel';
case 0x23 : return 'touch_end';
case 0x24 : return 'touch_move';
case 0x25 : return 'touch_start';
case 0x26 : return 'update_from_instance_after';
case 0x27 : return 'update_from_instance_before';
case 0x28 : return 'update_instance_after';
case 0x29 : return 'update_instance_before';
case 0x2a : return 'update_instance_property';
case 0x2b : return 'update_property_from_instance';
case 0xb : return 'entity_property_update';
case 0xc : return 'entity_started';
case 0xd : return 'get_api_url';
case 0xe : return 'get_runtime';
case 0xf : return 'get_window_size';
case 0x10 : return 'graphics_component_initialized';
case 0x11 : return 'image_component_initialized';
case 0x12 : return 'image_instance_created';
case 0x13 : return 'input_component_initialized';
case 0x14 : return 'instance_created';
case 0x15 : return 'instance_disposed';
case 0x16 : return 'keyboard_down';
case 0x17 : return 'keyboard_up';
case 0x18 : return 'mouse_down';
case 0x19 : return 'mouse_move';
case 0x1a : return 'mouse_up';
case 0x1b : return 'mouse_wheel';
case 0x1c : return 'object_created';
case 0x1d : return 'object_initialized';
case 0x1e : return 'pause';
case 0x1f : return 'project_initialized';
case 0x20 : return 'restart';
case 0x21 : return 'slider_entity_initialized';
case 0x22 : return 'start';
case 0x23 : return 'touch_cancel';
case 0x24 : return 'touch_end';
case 0x25 : return 'touch_move';
case 0x26 : return 'touch_start';
case 0x27 : return 'update_from_instance_after';
case 0x28 : return 'update_from_instance_before';
case 0x29 : return 'update_instance_after';
case 0x2a : return 'update_instance_before';
case 0x2b : return 'update_instance_property';
case 0x2c : return 'update_property_from_instance';
default :
throw new Error('Event type not defined : ' + eventId);
}
@ -2610,9 +2653,10 @@ class Entity extends R3Object {
*/
initialize() {
this.initialized = true;
this.checkRequirements();
this.setInitialized();
if (this.initialized) {
Event.Emit(Event.ENTITY_INITIALIZED, this);
}
if (this.initializeDepth === this.maxDepth) {
throw new Error('You should not try to instantiate this base class - extend it rather...');
@ -2623,14 +2667,82 @@ class Entity extends R3Object {
}
/**
* checkRequirements()
* setInitialized()
* - Checks whether all required components are initialized before triggering an Event.ENTITY_INITIALIZED
*/
checkRequirements() {
setInitialized() {
//TODO: Check all required components have initialized - once this is the case - fire an Event.ENTITY_INITIALIZED
let ready = true;
for (let property in this.required) {
if (this.required.hasOwnProperty(property)) {
Event.Emit(Event.ENTITY_INITIALIZED, this);
console.log('checking requirements for ' + property);
let initialized = true;
if (this.required[property] instanceof Array) {
/**
* First we need to check if we have this array defined
*/
if (!(this[property] instanceof Array)) {
throw new Error('The property ' + property + ' of this object was not properly defined');
}
if (this[property].length === 0) {
initialized = false;
}
let isInstance = false;
/**
* Check all properties are an instance of this.required[property]
*/
for (let i = 0; i < this[property].length; i++) {
this.required[property].map(
function(constructor) {
if (this[property][i] instanceof constructor) {
isInstance = true;
}
}.bind(this)
);
if (!isInstance) {
throw new Error('The property ' + property + ' of this object is not of the correct type');
}
}
for (let i = 0; i < this[property].length; i++) {
if (this[property][i].initialized !== true) {
initialized = false;
}
}
} else {
if (this[property] === null) {
initialized = false;
} else {
if (typeof this[property] === 'undefined') {
throw new Error('The ' + property + ' was not properly defined');
}
if (this[property].initialized !== true) {
initialized = false;
}
}
}
if (!initialized) {
ready = false;
console.log('This property ' + property + ' is not yet initialized');
}
}
}
this.initialized = ready;
}
@ -2829,13 +2941,64 @@ class EntitySlider extends Entity {
* images - We need a list of at least one image which to slide
*/
options.required.images = [R3.Image];
/**
this.imagesBackup = options.images;
Object.defineProperty(
this,
'images',
{
configurable : true,
enumerable : true,
set: function(x) {
this.imagesBackup = x;
Event.Emit(
Event.ENTITY_PROPERTY_UPDATE,
{
entity : this,
property : 'images',
value : x
}
);
return x;
},
get : function() {
return this.imagesBackup;
}
}
) /**
* canvas - We need a canvas to attach our Input events
*/
options.required.canvas = R3.Canvas;
this.canvasBackup = options.canvas;
Object.defineProperty(
this,
'canvas',
{
configurable : true,
enumerable : true,
set: function(x) {
this.canvasBackup = x;
Event.Emit(
Event.ENTITY_PROPERTY_UPDATE,
{
entity : this,
property : 'canvas',
value : x
}
);
return x;
},
get : function() {
return this.canvasBackup;
}
}
)
this.underConstruction = true;
Object.assign(this, options);
this.underConstruction = false;
if (options.callDepth === 0) {
this.initialize();
} else {
@ -2852,7 +3015,10 @@ class EntitySlider extends Entity {
super.initialize();
Event.Emit(Event.SLIDER_ENTITY_INITIALIZED, this);
if (this.initialized) {
Event.Emit(Event.SLIDER_ENTITY_INITIALIZED, this);
}
//TODO: stop() entity if it is no longer ready
if (this.initializeDepth === this.maxDepth) {
@ -2860,7 +3026,7 @@ class EntitySlider extends Entity {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -2871,14 +3037,12 @@ class EntitySlider extends Entity {
}
/**
* checkRequirements()
* setInitialized()
* - Checks all required Components for this Entity initialized and can perform some custom actions
*/
checkRequirements() {
setInitialized() {
super.checkRequirements();
Event.Emit(Event.SLIDER_ENTITY_INITIALIZED, this);
super.setInitialized();
}
@ -3105,7 +3269,6 @@ class Component extends R3Object {
initialize() {
this.initialized = true;
Event.Emit(Event.COMPONENT_INITIALIZED, this);
if (this.initializeDepth === this.maxDepth) {
@ -3383,7 +3546,7 @@ class ComponentDOM extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -3662,7 +3825,7 @@ class ComponentCanvas extends ComponentDOM {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -3952,7 +4115,7 @@ class ComponentGraphics extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -4281,7 +4444,7 @@ class ComponentImage extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -4727,7 +4890,7 @@ class ComponentMaterial extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -4947,7 +5110,7 @@ class ComponentMesh extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -5167,7 +5330,7 @@ class ComponentTexture extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -5371,7 +5534,7 @@ class ComponentInput extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -5591,7 +5754,7 @@ class ComponentTouch extends ComponentInput {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -5793,7 +5956,7 @@ class ComponentCode extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -5940,7 +6103,7 @@ class Project extends R3Object {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -1,6 +1,6 @@
{
"name": "r3",
"version" : "3.0.17",
"version" : "3.0.58",
"description": "",
"private": true,
"dependencies": {

53
r3.php
View File

@ -578,11 +578,64 @@ function generateRequiredComponents($file, $tokens, $token, $section)
updateSection($file, $section , $updates);
}
function stripRequiredComponents(&$store, $tokens)
{
$excluded = getTokenStore('CUSTOM_REQUIRED_COMPONENTS', $tokens);
if (sizeof($excluded) >= 0) {
$newStore = [];
$excludedIndexes = [];
foreach ($excluded as $excludedItem) {
$excludedItem = trim($excludedItem);
$matches = [];
$key_value = preg_match('/^(\s*\w+)=(.*)$/', $excludedItem, $matches);
if ($key_value === false) {
return '';
}
$excludedKey = $matches[1];
for ($i = 0; $i < sizeof($store); $i++) {
$item = trim($store[$i]);
$matches = [];
$key_value = preg_match('/^(\s*\w+)=(.*)$/', $item, $matches);
if ($key_value === false) {
continue;
}
$actualKey = $matches[1];
if ($actualKey === $excludedKey) {
array_push($excludedIndexes, $i);
}
}
}
for ($i = 0; $i < sizeof($store); $i++) {
if (!in_array($i, $excludedIndexes)){
array_push($newStore, $store[$i]);
}
}
$store = $newStore;
}
}
function generateInitOptions($file, $tokens, $token, $section)
{
$store = getTokenStore($token, $tokens);
// stripRequiredComponents($store, $tokens);
if (sizeof($store) <= 0) {
return;
}

View File

@ -238,7 +238,7 @@ class ComponentCode extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -289,7 +289,7 @@ class ComponentCanvas extends ComponentDOM {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -247,7 +247,7 @@ class ComponentDOM extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -239,7 +239,7 @@ class ComponentGraphics extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -380,7 +380,7 @@ class ComponentImage extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -239,7 +239,7 @@ class ComponentInput extends Component {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -256,7 +256,7 @@ class ComponentMaterial extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -256,7 +256,7 @@ class ComponentMesh extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -256,7 +256,7 @@ class ComponentTexture extends ComponentGraphics {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -256,7 +256,7 @@ class ComponentTouch extends ComponentInput {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -171,10 +171,10 @@ class Component extends R3Object {
initialize() {
//GENERATED_INITIALIZE_METHOD_START
this.initialized = true;
//GENERATED_INITIALIZE_METHOD_END
//CUSTOM_INITIALIZE_METHOD_START
this.initialized = true;
Event.Emit(Event.COMPONENT_INITIALIZED, this);
//CUSTOM_INITIALIZE_METHOD_END

View File

@ -128,7 +128,7 @@ const Entity = require('.././r3-entity.js');
TEMPLATE_METHODS_START
initialize() - Notifies all systems listening that this component initialized.
checkRequirements() - Checks all required Components for this Entity initialized and can perform some custom actions
setInitialized() - Checks all required Components for this Entity initialized and can perform some custom actions
start() - Starts the entity by subscribing to all events
stop() - Stops this entity by removing all subscriptions to events
TEMPLATE_METHODS_END
@ -247,14 +247,65 @@ class EntitySlider extends Entity {
* images - We need a list of at least one image which to slide
*/
options.required.images = [R3.Image];
/**
this.imagesBackup = options.images;
Object.defineProperty(
this,
'images',
{
configurable : true,
enumerable : true,
set: function(x) {
this.imagesBackup = x;
Event.Emit(
Event.ENTITY_PROPERTY_UPDATE,
{
entity : this,
property : 'images',
value : x
}
);
return x;
},
get : function() {
return this.imagesBackup;
}
}
) /**
* canvas - We need a canvas to attach our Input events
*/
options.required.canvas = R3.Canvas;
this.canvasBackup = options.canvas;
Object.defineProperty(
this,
'canvas',
{
configurable : true,
enumerable : true,
set: function(x) {
this.canvasBackup = x;
Event.Emit(
Event.ENTITY_PROPERTY_UPDATE,
{
entity : this,
property : 'canvas',
value : x
}
);
return x;
},
get : function() {
return this.canvasBackup;
}
}
)
//GENERATED_REQUIRED_COMPONENTS_END
this.underConstruction = true;
Object.assign(this, options);
this.underConstruction = false;
//CUSTOM_OPTIONS_INIT_START
//CUSTOM_OPTIONS_INIT_END
@ -285,7 +336,10 @@ class EntitySlider extends Entity {
//GENERATED_INITIALIZE_METHOD_END
//CUSTOM_INITIALIZE_METHOD_START
Event.Emit(Event.SLIDER_ENTITY_INITIALIZED, this);
if (this.initialized) {
Event.Emit(Event.SLIDER_ENTITY_INITIALIZED, this);
}
//TODO: stop() entity if it is no longer ready
//CUSTOM_INITIALIZE_METHOD_END
//GENERATED_INITIALIZE_METHOD_AFTER_START
@ -295,7 +349,7 @@ class EntitySlider extends Entity {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}
@ -307,21 +361,20 @@ class EntitySlider extends Entity {
}
/**
* checkRequirements()
* setInitialized()
* - Checks all required Components for this Entity initialized and can perform some custom actions
*/
checkRequirements() {
setInitialized() {
//GENERATED_CHECK_REQUIREMENTS_METHOD_START
super.checkRequirements();
//GENERATED_CHECK_REQUIREMENTS_METHOD_END
//GENERATED_SET_INITIALIZED_METHOD_START
super.setInitialized();
//GENERATED_SET_INITIALIZED_METHOD_END
//CUSTOM_CHECK_REQUIREMENTS_METHOD_START
Event.Emit(Event.SLIDER_ENTITY_INITIALIZED, this);
//CUSTOM_CHECK_REQUIREMENTS_METHOD_END
//CUSTOM_SET_INITIALIZED_METHOD_START
//CUSTOM_SET_INITIALIZED_METHOD_END
//GENERATED_CHECK_REQUIREMENTS_METHOD_AFTER_START
//GENERATED_CHECK_REQUIREMENTS_METHOD_AFTER_END
//GENERATED_SET_INITIALIZED_METHOD_AFTER_START
//GENERATED_SET_INITIALIZED_METHOD_AFTER_END
}

View File

@ -99,7 +99,7 @@ const R3Object = require('.././r3-r3-object.js');
TEMPLATE_METHODS_START
initialize() - Should raises an event(s) which indicates that this object initialized
checkRequirements() - Checks whether all required components are initialized before triggering an Event.ENTITY_INITIALIZED
setInitialized() - Checks whether all required components are initialized before triggering an Event.ENTITY_INITIALIZED
TEMPLATE_METHODS_END
CUSTOM_METHODS_START
@ -172,11 +172,13 @@ class Entity extends R3Object {
initialize() {
//GENERATED_INITIALIZE_METHOD_START
this.initialized = true;
//GENERATED_INITIALIZE_METHOD_END
//CUSTOM_INITIALIZE_METHOD_START
this.checkRequirements();
this.setInitialized();
if (this.initialized) {
Event.Emit(Event.ENTITY_INITIALIZED, this);
}
//CUSTOM_INITIALIZE_METHOD_END
//GENERATED_INITIALIZE_METHOD_AFTER_START
@ -190,21 +192,91 @@ class Entity extends R3Object {
}
/**
* checkRequirements()
* setInitialized()
* - Checks whether all required components are initialized before triggering an Event.ENTITY_INITIALIZED
*/
checkRequirements() {
setInitialized() {
//GENERATED_CHECK_REQUIREMENTS_METHOD_START
//TODO: Check all required components have initialized - once this is the case - fire an Event.ENTITY_INITIALIZED
//GENERATED_CHECK_REQUIREMENTS_METHOD_END
//GENERATED_SET_INITIALIZED_METHOD_START
let ready = true;
for (let property in this.required) {
if (this.required.hasOwnProperty(property)) {
//CUSTOM_CHECK_REQUIREMENTS_METHOD_START
Event.Emit(Event.ENTITY_INITIALIZED, this);
//CUSTOM_CHECK_REQUIREMENTS_METHOD_END
console.log('checking requirements for ' + property);
//GENERATED_CHECK_REQUIREMENTS_METHOD_AFTER_START
//GENERATED_CHECK_REQUIREMENTS_METHOD_AFTER_END
let initialized = true;
if (this.required[property] instanceof Array) {
/**
* First we need to check if we have this array defined
*/
if (!(this[property] instanceof Array)) {
throw new Error('The property ' + property + ' of this object was not properly defined');
}
if (this[property].length === 0) {
initialized = false;
}
let isInstance = false;
/**
* Check all properties are an instance of this.required[property]
*/
for (let i = 0; i < this[property].length; i++) {
this.required[property].map(
function(constructor) {
if (this[property][i] instanceof constructor) {
isInstance = true;
}
}.bind(this)
);
if (!isInstance) {
throw new Error('The property ' + property + ' of this object is not of the correct type');
}
}
for (let i = 0; i < this[property].length; i++) {
if (this[property][i].initialized !== true) {
initialized = false;
}
}
} else {
if (this[property] === null) {
initialized = false;
} else {
if (typeof this[property] === 'undefined') {
throw new Error('The ' + property + ' was not properly defined');
}
if (this[property].initialized !== true) {
initialized = false;
}
}
}
if (!initialized) {
ready = false;
console.log('This property ' + property + ' is not yet initialized');
}
}
}
this.initialized = ready;
//GENERATED_SET_INITIALIZED_METHOD_END
//CUSTOM_SET_INITIALIZED_METHOD_START
//CUSTOM_SET_INITIALIZED_METHOD_END
//GENERATED_SET_INITIALIZED_METHOD_AFTER_START
//GENERATED_SET_INITIALIZED_METHOD_AFTER_END
}
//GENERATED_TEMPLATE_METHODS_END

View File

@ -355,40 +355,41 @@ Event.DISPOSE_OBJECT = 0x7;
Event.DOM_COMPONENT_INITIALIZED = 0x8;
Event.ENTITY_CREATED = 0x9;
Event.ENTITY_INITIALIZED = 0xa;
Event.ENTITY_STARTED = 0xb;
Event.GET_API_URL = 0xc;
Event.GET_RUNTIME = 0xd;
Event.GET_WINDOW_SIZE = 0xe;
Event.GRAPHICS_COMPONENT_INITIALIZED = 0xf;
Event.IMAGE_COMPONENT_INITIALIZED = 0x10;
Event.IMAGE_INSTANCE_CREATED = 0x11;
Event.INPUT_COMPONENT_INITIALIZED = 0x12;
Event.INSTANCE_CREATED = 0x13;
Event.INSTANCE_DISPOSED = 0x14;
Event.KEYBOARD_DOWN = 0x15;
Event.KEYBOARD_UP = 0x16;
Event.MOUSE_DOWN = 0x17;
Event.MOUSE_MOVE = 0x18;
Event.MOUSE_UP = 0x19;
Event.MOUSE_WHEEL = 0x1a;
Event.OBJECT_CREATED = 0x1b;
Event.OBJECT_INITIALIZED = 0x1c;
Event.PAUSE = 0x1d;
Event.PROJECT_INITIALIZED = 0x1e;
Event.RESTART = 0x1f;
Event.SLIDER_ENTITY_INITIALIZED = 0x20;
Event.START = 0x21;
Event.TOUCH_CANCEL = 0x22;
Event.TOUCH_END = 0x23;
Event.TOUCH_MOVE = 0x24;
Event.TOUCH_START = 0x25;
Event.UPDATE_FROM_INSTANCE_AFTER = 0x26;
Event.UPDATE_FROM_INSTANCE_BEFORE = 0x27;
Event.UPDATE_INSTANCE_AFTER = 0x28;
Event.UPDATE_INSTANCE_BEFORE = 0x29;
Event.UPDATE_INSTANCE_PROPERTY = 0x2a;
Event.UPDATE_PROPERTY_FROM_INSTANCE = 0x2b;
Event.MAX_EVENTS = 0x2c;
Event.ENTITY_PROPERTY_UPDATE = 0xb;
Event.ENTITY_STARTED = 0xc;
Event.GET_API_URL = 0xd;
Event.GET_RUNTIME = 0xe;
Event.GET_WINDOW_SIZE = 0xf;
Event.GRAPHICS_COMPONENT_INITIALIZED = 0x10;
Event.IMAGE_COMPONENT_INITIALIZED = 0x11;
Event.IMAGE_INSTANCE_CREATED = 0x12;
Event.INPUT_COMPONENT_INITIALIZED = 0x13;
Event.INSTANCE_CREATED = 0x14;
Event.INSTANCE_DISPOSED = 0x15;
Event.KEYBOARD_DOWN = 0x16;
Event.KEYBOARD_UP = 0x17;
Event.MOUSE_DOWN = 0x18;
Event.MOUSE_MOVE = 0x19;
Event.MOUSE_UP = 0x1a;
Event.MOUSE_WHEEL = 0x1b;
Event.OBJECT_CREATED = 0x1c;
Event.OBJECT_INITIALIZED = 0x1d;
Event.PAUSE = 0x1e;
Event.PROJECT_INITIALIZED = 0x1f;
Event.RESTART = 0x20;
Event.SLIDER_ENTITY_INITIALIZED = 0x21;
Event.START = 0x22;
Event.TOUCH_CANCEL = 0x23;
Event.TOUCH_END = 0x24;
Event.TOUCH_MOVE = 0x25;
Event.TOUCH_START = 0x26;
Event.UPDATE_FROM_INSTANCE_AFTER = 0x27;
Event.UPDATE_FROM_INSTANCE_BEFORE = 0x28;
Event.UPDATE_INSTANCE_AFTER = 0x29;
Event.UPDATE_INSTANCE_BEFORE = 0x2a;
Event.UPDATE_INSTANCE_PROPERTY = 0x2b;
Event.UPDATE_PROPERTY_FROM_INSTANCE = 0x2c;
Event.MAX_EVENTS = 0x2d;
Event.GetEventName = function(eventId) {
@ -403,39 +404,40 @@ Event.GetEventName = function(eventId) {
case 0x8 : return 'dom_component_initialized';
case 0x9 : return 'entity_created';
case 0xa : return 'entity_initialized';
case 0xb : return 'entity_started';
case 0xc : return 'get_api_url';
case 0xd : return 'get_runtime';
case 0xe : return 'get_window_size';
case 0xf : return 'graphics_component_initialized';
case 0x10 : return 'image_component_initialized';
case 0x11 : return 'image_instance_created';
case 0x12 : return 'input_component_initialized';
case 0x13 : return 'instance_created';
case 0x14 : return 'instance_disposed';
case 0x15 : return 'keyboard_down';
case 0x16 : return 'keyboard_up';
case 0x17 : return 'mouse_down';
case 0x18 : return 'mouse_move';
case 0x19 : return 'mouse_up';
case 0x1a : return 'mouse_wheel';
case 0x1b : return 'object_created';
case 0x1c : return 'object_initialized';
case 0x1d : return 'pause';
case 0x1e : return 'project_initialized';
case 0x1f : return 'restart';
case 0x20 : return 'slider_entity_initialized';
case 0x21 : return 'start';
case 0x22 : return 'touch_cancel';
case 0x23 : return 'touch_end';
case 0x24 : return 'touch_move';
case 0x25 : return 'touch_start';
case 0x26 : return 'update_from_instance_after';
case 0x27 : return 'update_from_instance_before';
case 0x28 : return 'update_instance_after';
case 0x29 : return 'update_instance_before';
case 0x2a : return 'update_instance_property';
case 0x2b : return 'update_property_from_instance';
case 0xb : return 'entity_property_update';
case 0xc : return 'entity_started';
case 0xd : return 'get_api_url';
case 0xe : return 'get_runtime';
case 0xf : return 'get_window_size';
case 0x10 : return 'graphics_component_initialized';
case 0x11 : return 'image_component_initialized';
case 0x12 : return 'image_instance_created';
case 0x13 : return 'input_component_initialized';
case 0x14 : return 'instance_created';
case 0x15 : return 'instance_disposed';
case 0x16 : return 'keyboard_down';
case 0x17 : return 'keyboard_up';
case 0x18 : return 'mouse_down';
case 0x19 : return 'mouse_move';
case 0x1a : return 'mouse_up';
case 0x1b : return 'mouse_wheel';
case 0x1c : return 'object_created';
case 0x1d : return 'object_initialized';
case 0x1e : return 'pause';
case 0x1f : return 'project_initialized';
case 0x20 : return 'restart';
case 0x21 : return 'slider_entity_initialized';
case 0x22 : return 'start';
case 0x23 : return 'touch_cancel';
case 0x24 : return 'touch_end';
case 0x25 : return 'touch_move';
case 0x26 : return 'touch_start';
case 0x27 : return 'update_from_instance_after';
case 0x28 : return 'update_from_instance_before';
case 0x29 : return 'update_instance_after';
case 0x2a : return 'update_instance_before';
case 0x2b : return 'update_instance_property';
case 0x2c : return 'update_property_from_instance';
default :
throw new Error('Event type not defined : ' + eventId);
}

View File

@ -166,7 +166,7 @@ class Project extends R3Object {
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -1,6 +1,6 @@
class R3 {
static version = '3.0.17';
static compileDate = '2021 Sep 22 - 14:41:58 pm';
static version = '3.0.58';
static compileDate = '2021 Sep 23 - 11:36:17 am';
}
//GENERATED_IMPORTS_START

View File

@ -288,6 +288,7 @@ class SystemInput extends System {
//GENERATED_STATIC_ON_SLIDER_ENTITY_INITIALIZED_METHOD_END
//CUSTOM_STATIC_ON_SLIDER_ENTITY_INITIALIZED_METHOD_START
console.log('Slider Entity Initialized');
//CUSTOM_STATIC_ON_SLIDER_ENTITY_INITIALIZED_METHOD_END
}

View File

@ -70,6 +70,7 @@ const System = require('.././r3-system.js');
Event.ENTITY_INITIALIZED
Event.INSTANCE_CREATED
Event.CREATE_INSTANCE_BEFORE - @returns boolean delayInstance which indicates whether or not instance creation is delayed (handled) by another system. (i.e. where instance creation order is of importance)
Event.ENTITY_PROPERTY_UPDATE
CUSTOM_STATIC_EVENT_LISTENERS_END
TEMPLATE_METHODS_START
@ -158,6 +159,11 @@ class SystemLinking extends System {
SystemLinking.OnCreateInstanceBefore
);
SystemLinking.Subscriptions['ENTITY_PROPERTY_UPDATE'] = Event.Subscribe(
Event.ENTITY_PROPERTY_UPDATE,
SystemLinking.OnEntityPropertyUpdate
);
//GENERATED_STATIC_EVENT_LISTENERS_START_END
//CUSTOM_BEFORE_STATIC_SYSTEM_START_START
@ -199,6 +205,9 @@ class SystemLinking extends System {
SystemLinking.Subscriptions['CREATE_INSTANCE_BEFORE'].remove();
delete SystemLinking.Subscriptions['CREATE_INSTANCE_BEFORE'];
SystemLinking.Subscriptions['ENTITY_PROPERTY_UPDATE'].remove();
delete SystemLinking.Subscriptions['ENTITY_PROPERTY_UPDATE'];
//GENERATED_STATIC_EVENT_LISTENERS_STOP_END
//CUSTOM_BEFORE_STATIC_SYSTEM_STOP_START
@ -334,6 +343,43 @@ class SystemLinking extends System {
return false;
//CUSTOM_STATIC_ON_CREATE_INSTANCE_BEFORE_METHOD_END
}
/**
* OnEntityPropertyUpdate()
* - Listens to events of type Event.ENTITY_PROPERTY_UPDATE and executes this function.
* @param object (The event data passed as argument - typically an R3Object)
* @return
*/
static OnEntityPropertyUpdate(object) {
//GENERATED_STATIC_ON_ENTITY_PROPERTY_UPDATE_METHOD_START
//GENERATED_STATIC_ON_ENTITY_PROPERTY_UPDATE_METHOD_END
//CUSTOM_STATIC_ON_ENTITY_PROPERTY_UPDATE_METHOD_START
console.log('Entity object update', object);
console.log('canvas = ' + object.entity.canvas);
console.log('images = ' + object.entity.images);
if (!object.entity.underConstruction) {
object.entity.initializeDepth = 0;
object.entity.initialize();
if (object.entity.started && !object.entity.initialized) {
object.entity.stop();
}
}
// if (!object.entity.initialized) {
// object.entity.initialize();
// } else {
// object.entity.stop();
// }
// object.entity.initialize();
//CUSTOM_STATIC_ON_ENTITY_PROPERTY_UPDATE_METHOD_END
}
//GENERATED_STATIC_EVENT_LISTENER_METHODS_END

View File

@ -1 +0,0 @@
//TODO: Check all required components have initialized - once this is the case - fire an Event.ENTITY_INITIALIZED

View File

@ -1 +0,0 @@
this.initialized = true;

View File

@ -0,0 +1,72 @@
let ready = true;
for (let property in this.required) {
if (this.required.hasOwnProperty(property)) {
console.log('checking requirements for ' + property);
let initialized = true;
if (this.required[property] instanceof Array) {
/**
* First we need to check if we have this array defined
*/
if (!(this[property] instanceof Array)) {
throw new Error('The property ' + property + ' of this object was not properly defined');
}
if (this[property].length === 0) {
initialized = false;
}
let isInstance = false;
/**
* Check all properties are an instance of this.required[property]
*/
for (let i = 0; i < this[property].length; i++) {
this.required[property].map(
function(constructor) {
if (this[property][i] instanceof constructor) {
isInstance = true;
}
}.bind(this)
);
if (!isInstance) {
throw new Error('The property ' + property + ' of this object is not of the correct type');
}
}
for (let i = 0; i < this[property].length; i++) {
if (this[property][i].initialized !== true) {
initialized = false;
}
}
} else {
if (this[property] === null) {
initialized = false;
} else {
if (typeof this[property] === 'undefined') {
throw new Error('The ' + property + ' was not properly defined');
}
if (this[property].initialized !== true) {
initialized = false;
}
}
}
if (!initialized) {
ready = false;
console.log('This property ' + property + ' is not yet initialized');
}
}
}
this.initialized = ready;

View File

@ -22,7 +22,7 @@ const R3Object = require('.././r3-r3-object.js');
TEMPLATE_METHODS_START
initialize() - Should raises an event(s) which indicates that this object initialized
checkRequirements() - Checks whether all required components are initialized before triggering an Event.ENTITY_INITIALIZED
setInitialized() - Checks whether all required components are initialized before triggering an Event.ENTITY_INITIALIZED
TEMPLATE_METHODS_END
CUSTOM_METHODS_START

View File

@ -28,7 +28,7 @@ const EXTEND_CLASS = require('INCLUDE_PATH/EXTEND_CLASS_FILE_NAME');
TEMPLATE_METHODS_START
initialize() - Notifies all systems listening that this component initialized.
checkRequirements() - Checks all required Components for this Entity initialized and can perform some custom actions
setInitialized() - Checks all required Components for this Entity initialized and can perform some custom actions
start() - Starts the entity by subscribing to all events
stop() - Stops this entity by removing all subscriptions to events
TEMPLATE_METHODS_END

View File

@ -35,8 +35,12 @@
//GENERATED_REQUIRED_COMPONENTS_START
//GENERATED_REQUIRED_COMPONENTS_END
this.underConstruction = true;
Object.assign(this, options);
this.underConstruction = false;
//CUSTOM_OPTIONS_INIT_START
//CUSTOM_OPTIONS_INIT_END

View File

@ -1 +0,0 @@
super.checkRequirements();

View File

@ -0,0 +1 @@
super.setInitialized();

View File

@ -2,3 +2,27 @@
* KEY - COMMENT
*/
options.required.KEY = VALUE;
this.KEYBackup = options.KEY;
Object.defineProperty(
this,
'KEY',
{
configurable : true,
enumerable : true,
set: function(x) {
this.KEYBackup = x;
Event.Emit(
Event.ENTITY_PROPERTY_UPDATE,
{
entity : this,
property : 'KEY',
value : x
}
);
return x;
},
get : function() {
return this.KEYBackup;
}
}
)

View File

@ -4,7 +4,7 @@
this.createInstance();
}
if (this instanceof R3.Entity) {
if (this instanceof R3.Entity && this.initialized) {
this.start();
}

View File

@ -2,8 +2,6 @@ GENERATED_ASYNC_METHOD
GENERATED_ASYNC_METHOD_AFTER
GENERATED_BUILD_INSTANCE_METHOD
GENERATED_BUILD_INSTANCE_METHOD_AFTER
GENERATED_CHECK_REQUIREMENTS_METHOD
GENERATED_CHECK_REQUIREMENTS_METHOD_AFTER
GENERATED_CONSTRUCTOR
GENERATED_CREATE_INSTANCE_METHOD
GENERATED_CREATE_INSTANCE_METHOD_AFTER
@ -38,6 +36,8 @@ GENERATED_ON_TOUCH_START_METHOD_AFTER
GENERATED_OPTIONS_INIT
GENERATED_OUT_OF_CLASS_IMPLEMENTATION
GENERATED_REQUIRED_COMPONENTS
GENERATED_SET_INITIALIZED_METHOD
GENERATED_SET_INITIALIZED_METHOD_AFTER
GENERATED_SET_RUNTIME_METHOD
GENERATED_SET_RUNTIME_METHOD_AFTER
GENERATED_START_METHOD
@ -53,6 +53,7 @@ GENERATED_STATIC_ON_COMPONENT_INITIALIZED_METHOD
GENERATED_STATIC_ON_CREATE_INSTANCE_BEFORE_METHOD
GENERATED_STATIC_ON_DOM_COMPONENT_INITIALIZED_METHOD
GENERATED_STATIC_ON_ENTITY_INITIALIZED_METHOD
GENERATED_STATIC_ON_ENTITY_PROPERTY_UPDATE_METHOD
GENERATED_STATIC_ON_GET_API_URL_METHOD
GENERATED_STATIC_ON_GET_RUNTIME_METHOD
GENERATED_STATIC_ON_IMAGE_INSTANCE_CREATED_METHOD
@ -98,7 +99,6 @@ CUSTOM_BEFORE_STATIC_SYSTEM_STOP
CUSTOM_BEFORE_SYSTEM_START
CUSTOM_BEFORE_SYSTEM_STOP
CUSTOM_BUILD_INSTANCE_METHOD
CUSTOM_CHECK_REQUIREMENTS_METHOD
CUSTOM_CONVENIENT_DEFINES
CUSTOM_CREATE_INSTANCE_METHOD
CUSTOM_DISPOSE_INSTANCE_METHOD
@ -119,6 +119,7 @@ CUSTOM_OPTIONS
CUSTOM_OPTIONS_INIT
CUSTOM_OUT_OF_CLASS_IMPLEMENTATION
CUSTOM_REQUIRED_COMPONENTS
CUSTOM_SET_INITIALIZED_METHOD
CUSTOM_SET_RUNTIME_METHOD
CUSTOM_START_METHOD
CUSTOM_STATIC_ASYNC_METHOD
@ -130,6 +131,7 @@ CUSTOM_STATIC_ON_COMPONENT_INITIALIZED_METHOD
CUSTOM_STATIC_ON_CREATE_INSTANCE_BEFORE_METHOD
CUSTOM_STATIC_ON_DOM_COMPONENT_INITIALIZED_METHOD
CUSTOM_STATIC_ON_ENTITY_INITIALIZED_METHOD
CUSTOM_STATIC_ON_ENTITY_PROPERTY_UPDATE_METHOD
CUSTOM_STATIC_ON_GET_API_URL_METHOD
CUSTOM_STATIC_ON_GET_RUNTIME_METHOD
CUSTOM_STATIC_ON_IMAGE_INSTANCE_CREATED_METHOD

View File

@ -1 +1 @@
3.0.17
3.0.58