Skip to content

Commit 8483eac

Browse files
committed
Removing all use of _cache from all Game Objects.
1 parent 9670c8a commit 8483eac

12 files changed

Lines changed: 376 additions & 559 deletions

File tree

src/gameobjects/BitmapText.js

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,36 @@ Phaser.BitmapText = function (game, x, y, font, text, size) {
112112
this.position.set(x, y);
113113

114114
/**
115-
* A small internal cache:
116-
* 0 = previous position.x
117-
* 1 = previous position.y
118-
* 2 = previous rotation
119-
* 3 = renderID
120-
* 4 = fresh? (0 = no, 1 = yes)
121-
* 5 = outOfBoundsFired (0 = no, 1 = yes)
122-
* 6 = exists (0 = no, 1 = yes)
123-
* 7 = fixed to camera (0 = no, 1 = yes)
124-
* 8 = destroy phase? (0 = no, 1 = yes)
125-
* @property {Array} _cache
126-
* @private
115+
* @property {Phaser.Point} previousPosition - The position the Sprite was in at the last update.
116+
* @readOnly
117+
*/
118+
this.previousPosition = new Phaser.Point(x, y);
119+
120+
/**
121+
* @property {number} previousRotation - The rotation angle the Sprite was in at the last update (in radians)
122+
* @readOnly
123+
*/
124+
this.previousRotation = 0;
125+
126+
/**
127+
* @property {number} renderOrderID - The render order ID. This is used internally by the renderer and input manager and should not be modified.
128+
* @readOnly
129+
*/
130+
this.renderOrderID = 0;
131+
132+
/**
133+
* A Sprite that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Sprite.cameraOffset.
134+
* Note that the cameraOffset values are in addition to any parent in the display list.
135+
* So if this Sprite was in a Group that has x: 200, then this will be added to the cameraOffset.x
136+
* @property {boolean} fixedToCamera
137+
*/
138+
this.fixedToCamera = false;
139+
140+
/**
141+
* @property {boolean} destroyPhase - As a Sprite runs through its destroy method this flag is set to true, and can be checked in any sub-systems it is being destroyed from.
142+
* @readOnly
127143
*/
128-
this._cache = [0, 0, 0, 0, 1, 0, 1, 0, 0];
144+
this.destroyPhase = false;
129145

130146
};
131147

@@ -151,9 +167,8 @@ Phaser.BitmapText.prototype.setStyle = function() {
151167
*/
152168
Phaser.BitmapText.prototype.preUpdate = function () {
153169

154-
this._cache[0] = this.world.x;
155-
this._cache[1] = this.world.y;
156-
this._cache[2] = this.rotation;
170+
this.previousPosition.set(this.world.x, this.world.y);
171+
this.previousRotation = this.rotation;
157172

158173
if (!this.exists || !this.parent.exists)
159174
{
@@ -171,7 +186,7 @@ Phaser.BitmapText.prototype.preUpdate = function () {
171186

172187
if (this.visible)
173188
{
174-
this._cache[3] = this.game.stage.currentRenderOrderID++;
189+
this.renderOrderID = this.game.stage.currentRenderOrderID++;
175190
}
176191

177192
return true;
@@ -194,7 +209,7 @@ Phaser.BitmapText.prototype.update = function() {
194209
Phaser.BitmapText.prototype.postUpdate = function () {
195210

196211
// Fixed to Camera?
197-
if (this._cache[7] === 1)
212+
if (this.fixedToCamera)
198213
{
199214
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
200215
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
@@ -213,7 +228,7 @@ Phaser.BitmapText.prototype.destroy = function(destroyChildren) {
213228

214229
if (typeof destroyChildren === 'undefined') { destroyChildren = true; }
215230

216-
this._cache[8] = 1;
231+
this.destroyPhase = true;
217232

218233
if (this.parent)
219234
{
@@ -258,7 +273,7 @@ Phaser.BitmapText.prototype.destroy = function(destroyChildren) {
258273
this.mask = null;
259274
this.game = null;
260275

261-
this._cache[8] = 0;
276+
this.destroyPhase = false;
262277

263278
};
264279

@@ -437,48 +452,3 @@ Object.defineProperty(Phaser.BitmapText.prototype, "inputEnabled", {
437452
}
438453

439454
});
440-
441-
/**
442-
* An BitmapText that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in BitmapText.cameraOffset.
443-
* Note that the cameraOffset values are in addition to any parent in the display list.
444-
* So if this BitmapText was in a Group that has x: 200, then this will be added to the cameraOffset.x
445-
*
446-
* @name Phaser.BitmapText#fixedToCamera
447-
* @property {boolean} fixedToCamera - Set to true to fix this BitmapText to the Camera at its current world coordinates.
448-
*/
449-
Object.defineProperty(Phaser.BitmapText.prototype, "fixedToCamera", {
450-
451-
get: function () {
452-
453-
return !!this._cache[7];
454-
455-
},
456-
457-
set: function (value) {
458-
459-
if (value)
460-
{
461-
this._cache[7] = 1;
462-
this.cameraOffset.set(this.x, this.y);
463-
}
464-
else
465-
{
466-
this._cache[7] = 0;
467-
}
468-
}
469-
470-
});
471-
472-
/**
473-
* @name Phaser.BitmapText#destroyPhase
474-
* @property {boolean} destroyPhase - True if this object is currently being destroyed.
475-
*/
476-
Object.defineProperty(Phaser.BitmapText.prototype, "destroyPhase", {
477-
478-
get: function () {
479-
480-
return !!this._cache[8];
481-
482-
}
483-
484-
});

src/gameobjects/Graphics.js

Lines changed: 35 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,36 @@ Phaser.Graphics = function (game, x, y) {
6262
this.position.set(x, y);
6363

6464
/**
65-
* A small internal cache:
66-
* 0 = previous position.x
67-
* 1 = previous position.y
68-
* 2 = previous rotation
69-
* 3 = renderID
70-
* 4 = fresh? (0 = no, 1 = yes)
71-
* 5 = outOfBoundsFired (0 = no, 1 = yes)
72-
* 6 = exists (0 = no, 1 = yes)
73-
* 7 = fixed to camera (0 = no, 1 = yes)
74-
* 8 = destroy phase? (0 = no, 1 = yes)
75-
* @property {Array} _cache
76-
* @private
65+
* @property {Phaser.Point} previousPosition - The position the Sprite was in at the last update.
66+
* @readOnly
7767
*/
78-
this._cache = [ 0, 0, 0, 0, 1, 0, 1, 0, 0 ];
68+
this.previousPosition = new Phaser.Point(x, y);
69+
70+
/**
71+
* @property {number} previousRotation - The rotation angle the Sprite was in at the last update (in radians)
72+
* @readOnly
73+
*/
74+
this.previousRotation = 0;
75+
76+
/**
77+
* @property {number} renderOrderID - The render order ID. This is used internally by the renderer and input manager and should not be modified.
78+
* @readOnly
79+
*/
80+
this.renderOrderID = 0;
81+
82+
/**
83+
* A Sprite that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Sprite.cameraOffset.
84+
* Note that the cameraOffset values are in addition to any parent in the display list.
85+
* So if this Sprite was in a Group that has x: 200, then this will be added to the cameraOffset.x
86+
* @property {boolean} fixedToCamera
87+
*/
88+
this.fixedToCamera = false;
89+
90+
/**
91+
* @property {boolean} destroyPhase - As a Sprite runs through its destroy method this flag is set to true, and can be checked in any sub-systems it is being destroyed from.
92+
* @readOnly
93+
*/
94+
this.destroyPhase = false;
7995

8096
};
8197

@@ -88,9 +104,8 @@ Phaser.Graphics.prototype.constructor = Phaser.Graphics;
88104
*/
89105
Phaser.Graphics.prototype.preUpdate = function () {
90106

91-
this._cache[0] = this.world.x;
92-
this._cache[1] = this.world.y;
93-
this._cache[2] = this.rotation;
107+
this.previousPosition.set(this.world.x, this.world.y);
108+
this.previousRotation = this.rotation;
94109

95110
if (!this.exists || !this.parent.exists)
96111
{
@@ -108,7 +123,7 @@ Phaser.Graphics.prototype.preUpdate = function () {
108123

109124
if (this.visible)
110125
{
111-
this._cache[3] = this.game.stage.currentRenderOrderID++;
126+
this.renderOrderID = this.game.stage.currentRenderOrderID++;
112127
}
113128

114129
return true;
@@ -131,8 +146,7 @@ Phaser.Graphics.prototype.update = function() {
131146
*/
132147
Phaser.Graphics.prototype.postUpdate = function () {
133148

134-
// Fixed to Camera?
135-
if (this._cache[7] === 1)
149+
if (this.fixedToCamera)
136150
{
137151
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
138152
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
@@ -152,7 +166,7 @@ Phaser.Graphics.prototype.destroy = function(destroyChildren) {
152166

153167
if (typeof destroyChildren === 'undefined') { destroyChildren = true; }
154168

155-
this._cache[8] = 1;
169+
this.destroyPhase = false;
156170

157171
this.clear();
158172

@@ -190,7 +204,7 @@ Phaser.Graphics.prototype.destroy = function(destroyChildren) {
190204

191205
this.game = null;
192206

193-
this._cache[8] = 0;
207+
this.destroyPhase = true;
194208

195209
};
196210

@@ -337,48 +351,3 @@ Object.defineProperty(Phaser.Graphics.prototype, 'angle', {
337351
}
338352

339353
});
340-
341-
/**
342-
* An Graphics that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Graphics.cameraOffset.
343-
* Note that the cameraOffset values are in addition to any parent in the display list.
344-
* So if this Graphics was in a Group that has x: 200, then this will be added to the cameraOffset.x
345-
*
346-
* @name Phaser.Graphics#fixedToCamera
347-
* @property {boolean} fixedToCamera - Set to true to fix this Graphics to the Camera at its current world coordinates.
348-
*/
349-
Object.defineProperty(Phaser.Graphics.prototype, "fixedToCamera", {
350-
351-
get: function () {
352-
353-
return !!this._cache[7];
354-
355-
},
356-
357-
set: function (value) {
358-
359-
if (value)
360-
{
361-
this._cache[7] = 1;
362-
this.cameraOffset.set(this.x, this.y);
363-
}
364-
else
365-
{
366-
this._cache[7] = 0;
367-
}
368-
}
369-
370-
});
371-
372-
/**
373-
* @name Phaser.Graphics#destroyPhase
374-
* @property {boolean} destroyPhase - True if this object is currently being destroyed.
375-
*/
376-
Object.defineProperty(Phaser.Graphics.prototype, "destroyPhase", {
377-
378-
get: function () {
379-
380-
return !!this._cache[8];
381-
382-
}
383-
384-
});

0 commit comments

Comments
 (0)