1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Tool_1 = require("./../Tool");
|
4 | /**
|
5 | * The entry class of all object in Sideral
|
6 | */
|
7 | var SideralObject = (function () {
|
8 | function SideralObject() {
|
9 | /* ATTRIBUTES */
|
10 | /**
|
11 | * Unique id of the object
|
12 | * @readonly
|
13 | */
|
14 | this.id = SideralObject.generateId();
|
15 | /**
|
16 | * Name of the object (used to identify object, you should not forget to fill this value)
|
17 | * @type {string}
|
18 | */
|
19 | this.name = "";
|
20 | /**
|
21 | * Properties of the object
|
22 | */
|
23 | this.props = {};
|
24 | /**
|
25 | * Last values of properties of the object
|
26 | */
|
27 | this.last = {};
|
28 | /**
|
29 | * Context is a object which you can store anything, the content of the context will be passed to its children
|
30 | */
|
31 | this.context = {};
|
32 | /**
|
33 | * List of all signals of the element
|
34 | */
|
35 | this.signals = {
|
36 | update: new Tool_1.SignalEvent(),
|
37 | propChange: new Tool_1.SignalEvent(),
|
38 | addChild: new Tool_1.SignalEvent(),
|
39 | removeChild: new Tool_1.SignalEvent()
|
40 | };
|
41 | /**
|
42 | * List of all children of this object
|
43 | */
|
44 | this.children = [];
|
45 | /**
|
46 | * Parent of this object
|
47 | */
|
48 | this.parent = null;
|
49 | /**
|
50 | * Know if this object has been initialized
|
51 | * @readonly
|
52 | */
|
53 | this.initialized = false;
|
54 | /**
|
55 | * Know if this object has been killed
|
56 | * @readonly
|
57 | */
|
58 | this.killed = false;
|
59 | }
|
60 | /* LIFECYCLE */
|
61 | /**
|
62 | * Lifecycle - When initialized by a parent (called only once when the instance is attached to the lifecycle of the game)
|
63 | * @access public
|
64 | * @param props - properties to merge
|
65 | */
|
66 | SideralObject.prototype.initialize = function (props) {
|
67 | var _this = this;
|
68 | if (props === void 0) { props = {}; }
|
69 | Object.keys(props).forEach(function (key) { return _this.props[key] = props[key]; });
|
70 | this.initialized = true;
|
71 | };
|
72 | /**
|
73 | * Lifecycle - Destroy the current instance
|
74 | * @access public
|
75 | */
|
76 | SideralObject.prototype.kill = function () {
|
77 | var _this = this;
|
78 | this.killed = true;
|
79 | Object.keys(this.signals).forEach(function (key) { return _this.signals[key].removeAll(); });
|
80 | this.children.forEach(function (child) { return child.kill(); });
|
81 | if (this.parent) {
|
82 | this.parent.children = this.parent.children.filter(function (child) { return child.id !== _this.id; });
|
83 | this.parent.signals.removeChild.dispatch(this.name, this);
|
84 | this.parent = null;
|
85 | }
|
86 | };
|
87 | /**
|
88 | * Lifecycle - Called every loop
|
89 | * @access protected
|
90 | * @param {number} tick - The tick factor (to prevent the dependance of the framerate)
|
91 | * @returns {void}
|
92 | */
|
93 | SideralObject.prototype.update = function (tick) {
|
94 | this.children.forEach(function (child) { return !child.killed && child.update(tick); });
|
95 | this.signals.update.dispatch(tick);
|
96 | };
|
97 | /**
|
98 | * Lifecycle - Called before a next game loop
|
99 | * @access protected
|
100 | * @returns {void}
|
101 | */
|
102 | SideralObject.prototype.nextCycle = function () {
|
103 | var _this = this;
|
104 | var propChanged = [];
|
105 | this.children.forEach(function (child) { return child.nextCycle(); });
|
106 | Object.keys(this.props).forEach(function (key) {
|
107 | var prop = _this.props[key];
|
108 | if (typeof prop !== "object" && prop !== _this.last[key]) {
|
109 | propChanged.push(key);
|
110 | _this.last[key] = _this.props[key];
|
111 | }
|
112 | });
|
113 | propChanged.forEach(function (prop) { return _this.signals.propChange.dispatch(prop, _this.props[prop]); });
|
114 | };
|
115 | /* METHODS */
|
116 | /**
|
117 | * Set new properties to the object. All attribute contained in "props" are public and can be edited by external source.
|
118 | * Properties can be observe via the "propChange" event. Update a property attribute via "setProps" will not fire the "propChange" event.
|
119 | * @access public
|
120 | * @param props - Properties to merge
|
121 | * @returns Current instance
|
122 | * @example
|
123 | * this.setProps({
|
124 | * test: 1
|
125 | * });
|
126 | *
|
127 | * this.props.test; // 1
|
128 | */
|
129 | SideralObject.prototype.setProps = function (props) {
|
130 | var _this = this;
|
131 | Object.keys(props).forEach(function (key) { return _this.last[key] = _this.props[key] = props[key]; });
|
132 | return this;
|
133 | };
|
134 | /**
|
135 | * Remove a property from the lifecycle of the object
|
136 | * @param name - Name of the property to remove
|
137 | * @returns Current instance
|
138 | */
|
139 | SideralObject.prototype.removeProp = function (name) {
|
140 | delete this.last[name];
|
141 | delete this.props[name];
|
142 | return this;
|
143 | };
|
144 | /**
|
145 | * Add an item to the current object. The item added will enter into the lifecycle of the object and will become a children
|
146 | * of this object. The method "initialize" of the item will be called.
|
147 | * @access public
|
148 | * @param item - A SideralObject
|
149 | * @param props - Props to merge to the item
|
150 | * @returns The item initialized
|
151 | */
|
152 | SideralObject.prototype.add = function (item, props) {
|
153 | var _this = this;
|
154 | if (props === void 0) { props = {}; }
|
155 | if (!(item instanceof SideralObject)) {
|
156 | throw new Error("SideralObject.add : item must be an instance of Sideral Abstract Class");
|
157 | }
|
158 | item.parent = this;
|
159 | Object.keys(this.context).forEach(function (key) { return item.context[key] = _this.context[key]; });
|
160 | this.children.push(item);
|
161 | item.initialize(props);
|
162 | this.signals.addChild.dispatch(item.name, item);
|
163 | return item;
|
164 | };
|
165 | /**
|
166 | * Add multiple items
|
167 | * @param params - Parameters of the multiple add
|
168 | */
|
169 | SideralObject.prototype.addMultiple = function (params) {
|
170 | var _this = this;
|
171 | params.forEach(function (param) {
|
172 | _this.add(param.item, param.props);
|
173 | if (param.assign) {
|
174 | _this[param.assign] = param.item;
|
175 | }
|
176 | if (param.callback) {
|
177 | param.callback(param.item);
|
178 | }
|
179 | });
|
180 | return this;
|
181 | };
|
182 | /**
|
183 | * Check if a property (an attribute from "this.props") has changed
|
184 | * @access public
|
185 | * @param propName - name of the property to check
|
186 | * @returns Property has changed ?
|
187 | */
|
188 | SideralObject.prototype.hasChanged = function (propName) {
|
189 | if (!this.props[propName]) {
|
190 | return false;
|
191 | }
|
192 | return this.props[propName] !== this.last[propName];
|
193 | };
|
194 | /* STATICS */
|
195 | /**
|
196 | * Generate an unique id
|
197 | * @returns The unique id
|
198 | */
|
199 | SideralObject.generateId = function () {
|
200 | return Math.floor((1 + Math.random()) * 0x100000);
|
201 | };
|
202 | return SideralObject;
|
203 | }());
|
204 | exports.SideralObject = SideralObject;
|