Skip to content

Commit 25137b9

Browse files
committed
[#1091] remove more internal references to me.game
- the `sortOn` property is now a proper getter/setter for the App World instance `sortOn` property - Container now defines a `getRootAncestor()` method that will return the reference to the root container parent (aka world container) - remove more internal reference to me.game in Container and Trigger
1 parent 2433461 commit 25137b9

File tree

4 files changed

+67
-38
lines changed

4 files changed

+67
-38
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
### Added
66
- Renderer: add new `toBlob()`, `toDataURL()` and `toImageBitmap()` methods to `CanvasTexture`
77
- Renderer: add new `toBlob()`, `toDataURL()` and `toImageBitmap()` methods to the all Renderers
8+
- Container: new `getRootAncestor()` method that returns the root container's parent (aka World Container)
9+
10+
### Changed
11+
- Application: the `sortOn` property is now a proper getter/setter for the App World instance `sortOn` property
812

913
### Fixed
1014
- Renderer: add missing export for the `CanvasTexture` class

src/application/application.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ import { CANVAS, WEBGL, AUTO } from "../const.js";
7272
*/
7373
this.mergeGroup = true;
7474

75-
/**
76-
* Specify the property to be used when sorting renderables.
77-
* Accepted values : "x", "y", "z"
78-
* @type {string}
79-
* @default "z"
80-
*/
81-
this.sortOn = "z";
82-
8375
/**
8476
* Last time the game update loop was executed. <br>
8577
* Use this value to implement frame prediction in drawing events,
@@ -221,7 +213,7 @@ import { CANVAS, WEBGL, AUTO } from "../const.js";
221213
}
222214

223215
// create a new physic world
224-
this.world = new World();
216+
this.world = new World(0, 0, this.settings.width, this.settings.height);
225217
// set the reference to this application instance
226218
this.world.app = this;
227219
this.lastUpdate = globalThis.performance.now();
@@ -249,6 +241,19 @@ import { CANVAS, WEBGL, AUTO } from "../const.js";
249241
this.updateFrameRate();
250242
}
251243

244+
/**
245+
* Specify the property to be used when sorting renderables for this application game world.
246+
* Accepted values : "x", "y", "z"
247+
* @type {string}
248+
* @see World.sortOn
249+
*/
250+
get sortOn() {
251+
return this.world.sortOn;
252+
}
253+
set sortOn(value) {
254+
this.world.sortOn = value;
255+
}
256+
252257
/**
253258
* Fired when a level is fully loaded and all renderable instantiated. <br>
254259
* Additionnaly the level id will also be passed to the called function.

src/renderable/container.js

+42-24
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ let globalFloatingCounter = 0;
5757
this.children = undefined;
5858

5959
/**
60-
* The property of the child object that should be used to sort on <br>
60+
* The property of the child object that should be used to sort on this container
6161
* value : "x", "y", "z"
6262
* @type {string}
63-
* @default me.game.sortOn
63+
* @default "z"
6464
*/
65-
this.sortOn = game.sortOn;
65+
this.sortOn = "z";
6666

6767
/**
6868
* Specify if the children list should be automatically sorted when adding a new child
@@ -170,9 +170,9 @@ let globalFloatingCounter = 0;
170170
* Add a child to the container <br>
171171
* if auto-sort is disable, the object will be appended at the bottom of the list.
172172
* Adding a child to the container will automatically remove it from its other container.
173-
* Meaning a child can only have one parent. This is important if you add a renderable
174-
* to a container then add it to the me.game.world container it will move it out of the
175-
* orginal container. Then when the me.game.world.reset() is called the renderable
173+
* Meaning a child can only have one parent. This is important if you add a renderable
174+
* to a container then add it to the World container it will move it out of the
175+
* orginal container. Then when the World container reset() method is called the renderable
176176
* will not be in any container. <br>
177177
* if the given child implements a onActivateEvent method, that method will be called
178178
* once the child is added to this container.
@@ -219,7 +219,7 @@ let globalFloatingCounter = 0;
219219

220220
// force repaint in case this is a static non-animated object
221221
if (this.isAttachedToRoot() === true) {
222-
game.repaint();
222+
this.isDirty = true;
223223
}
224224

225225
// force bounds update if required
@@ -229,7 +229,7 @@ let globalFloatingCounter = 0;
229229

230230
// if a physic body is defined, add it to the game world
231231
if (child.body instanceof Body) {
232-
game.world.addBody(child.body);
232+
this.getRootAncestor().addBody(child.body);
233233
}
234234

235235
// triggered callback if defined
@@ -268,7 +268,7 @@ let globalFloatingCounter = 0;
268268

269269
// force repaint in case this is a static non-animated object
270270
if (this.isAttachedToRoot() === true) {
271-
game.repaint();
271+
this.isDirty = true;
272272
}
273273

274274
// force bounds update if required
@@ -278,7 +278,7 @@ let globalFloatingCounter = 0;
278278

279279
// if a physic body is defined, add it to the game world
280280
if (child.body instanceof Body) {
281-
game.world.addBody(child.body);
281+
this.getRootAncestor().addBody(child.body);
282282
}
283283

284284
// triggered callback if defined
@@ -300,14 +300,14 @@ let globalFloatingCounter = 0;
300300
* @param {Function} callback - fnction to execute on each element
301301
* @param {object} [thisArg] - value to use as this(i.e reference Object) when executing callback.
302302
* @example
303-
* // iterate through all children of the root container
304-
* me.game.world.forEach((child) => {
303+
* // iterate through all children of this container
304+
* container.forEach((child) => {
305305
* // do something with the child
306306
* child.doSomething();
307307
* });
308-
* me.game.world.forEach((child, index) => { ... });
309-
* me.game.world.forEach((child, index, array) => { ... });
310-
* me.game.world.forEach((child, index, array) => { ... }, thisArg);
308+
* container.forEach((child, index) => { ... });
309+
* container.forEach((child, index, array) => { ... });
310+
* container.forEach((child, index, array) => { ... }, thisArg);
311311
*/
312312
forEach(callback, thisArg) {
313313
var context = this, i = 0;
@@ -411,15 +411,15 @@ let globalFloatingCounter = 0;
411411
* var ent = myContainer.getChildByProp("name", "mainPlayer");
412412
*
413413
* // or query the whole world :
414-
* var ent = me.game.world.getChildByProp("name", "mainPlayer");
414+
* var ent = container.getChildByProp("name", "mainPlayer");
415415
*
416416
* // partial property matches are also allowed by using a RegExp.
417417
* // the following matches "redCOIN", "bluecoin", "bagOfCoins", etc :
418-
* var allCoins = me.game.world.getChildByProp("name", /coin/i);
418+
* var allCoins = container.getChildByProp("name", /coin/i);
419419
*
420420
* // searching for numbers or other data types :
421-
* var zIndex10 = me.game.world.getChildByProp("z", 10);
422-
* var inViewport = me.game.world.getChildByProp("inViewport", true);
421+
* var zIndex10 = container.getChildByProp("z", 10);
422+
* var inViewport = container.getChildByProp("inViewport", true);
423423
*/
424424
getChildByProp(prop, value) {
425425
var objList = [];
@@ -531,7 +531,6 @@ let globalFloatingCounter = 0;
531531

532532
/**
533533
* Checks if this container is root or if it's attached to the root container.
534-
* @private
535534
* @returns {boolean}
536535
*/
537536
isAttachedToRoot() {
@@ -549,6 +548,25 @@ let globalFloatingCounter = 0;
549548
}
550549
}
551550

551+
/**
552+
* Returns the instance of the root container (i.e. the current application World container).
553+
* @returns {Container}
554+
*/
555+
getRootAncestor() {
556+
if (this.root === true) {
557+
return this;
558+
} else {
559+
var ancestor = this.ancestor;
560+
while (ancestor) {
561+
if (ancestor.root === true) {
562+
break;
563+
}
564+
ancestor = ancestor.ancestor;
565+
}
566+
return ancestor;
567+
}
568+
}
569+
552570
/**
553571
* update the cointainer's bounding rect (private)
554572
* @ignore
@@ -610,7 +628,7 @@ let globalFloatingCounter = 0;
610628
// remove the body first to avoid a condition where a body can be detached
611629
// from its parent, before the body is removed from the game world
612630
if (child.body instanceof Body) {
613-
game.world.removeBody(child.body);
631+
this.getRootAncestor().removeBody(child.body);
614632
}
615633

616634
if (!keepalive) {
@@ -633,7 +651,7 @@ let globalFloatingCounter = 0;
633651

634652
// force repaint in case this is a static non-animated object
635653
if (this.isAttachedToRoot() === true) {
636-
game.repaint();
654+
this.isDirty = true;
637655
}
638656

639657
// force bounds update if required
@@ -746,7 +764,7 @@ let globalFloatingCounter = 0;
746764
// clear the defer id
747765
this.pendingSort = null;
748766
// make sure we redraw everything
749-
game.repaint();
767+
this.isDirty = true;
750768
}, this);
751769
}
752770
}
@@ -815,7 +833,7 @@ let globalFloatingCounter = 0;
815833

816834
/**
817835
* container update function. <br>
818-
* automatically called by the game manager {@link game}
836+
* automatically called by the application update loop {@link Application}
819837
* @protected
820838
* @param {number} dt - time since the last update in milliseconds.
821839
* @returns {boolean} true if the Container is dirty

src/renderable/trigger.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Renderable from "./renderable.js";
22
import collision from "./../physics/collision.js";
33
import Body from "./../physics/body.js";
44
import level from "./../level/level.js";
5-
import { game } from "../index.js";
65
import pool from "./../system/pooling.js";
76

87
/**
@@ -26,7 +25,7 @@ import pool from "./../system/pooling.js";
2625
* @param {boolean} [settings.flatten] - Flatten all objects into the target container. See {@link level.load}
2726
* @param {boolean} [settings.setViewportBounds] - Resize the viewport to match the level. See {@link level.load}
2827
* @example
29-
* me.game.world.addChild(new me.Trigger(
28+
* world.addChild(new me.Trigger(
3029
* x, y, {
3130
* shapes: [new me.Rect(0, 0, 100, 100)],
3231
* "duration" : 250,
@@ -87,9 +86,10 @@ import pool from "./../system/pooling.js";
8786
* @ignore
8887
*/
8988
getTriggerSettings() {
89+
var world = this.ancestor.getRootAncestor();
9090
// Lookup for the container instance
9191
if (typeof(this.triggerSettings.container) === "string") {
92-
this.triggerSettings.container = game.world.getChildByName(this.triggerSettings.container)[0];
92+
this.triggerSettings.container = world.getChildByName(this.triggerSettings.container)[0];
9393
}
9494
return this.triggerSettings;
9595
}
@@ -98,8 +98,9 @@ import pool from "./../system/pooling.js";
9898
* @ignore
9999
*/
100100
onFadeComplete() {
101+
var world = this.ancestor.getRootAncestor();
101102
level.load(this.gotolevel, this.getTriggerSettings());
102-
game.viewport.fadeOut(this.fade, this.duration);
103+
world.app.viewport.fadeOut(this.fade, this.duration);
103104
}
104105

105106
/**
@@ -110,6 +111,7 @@ import pool from "./../system/pooling.js";
110111
*/
111112
triggerEvent() {
112113
var triggerSettings = this.getTriggerSettings();
114+
var world = this.ancestor.getRootAncestor();
113115

114116
if (triggerSettings.event === "level") {
115117
this.gotolevel = triggerSettings.to;
@@ -118,7 +120,7 @@ import pool from "./../system/pooling.js";
118120
if (this.fade && this.duration) {
119121
if (!this.fading) {
120122
this.fading = true;
121-
game.viewport.fadeIn(this.fade, this.duration,
123+
world.app.viewport.fadeIn(this.fade, this.duration,
122124
this.onFadeComplete.bind(this));
123125
}
124126
} else {

0 commit comments

Comments
 (0)