Skip to content

Commit b20a6ff

Browse files
committed
Working my way through putting all the Tests back in and fixing issues as I go.
1 parent 31c4d8c commit b20a6ff

22 files changed

Lines changed: 602 additions & 214 deletions

Phaser/core/Group.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ module Phaser {
174174
* Calls render on all members of this Group who have a status of visible=true and exists=true
175175
* You can also call Object.render directly, which will bypass the visible/exists check.
176176
*/
177-
public render(renderer:IRenderer, camera: Camera) {
177+
public render(camera: Camera) {
178178

179179
if (camera.isHidden(this) == true)
180180
{
@@ -201,17 +201,13 @@ module Phaser {
201201

202202
if (this._member != null && this._member.exists && this._member.visible && camera.isHidden(this._member) == false)
203203
{
204-
//this._member.render.call(renderer, camera, this._member);
205-
// call = context first, then parameters
206204
if (this._member.type == Types.GROUP)
207205
{
208-
//console.log('group rend');
209-
this._member.render.call(this._member, renderer, camera, this._member);
210-
//this._member.render.call(this, renderer, camera, this._member);
206+
this._member.render(camera);
211207
}
212208
else
213209
{
214-
this._member.render.call(renderer, camera, this._member);
210+
this.game.renderer.renderGameObject(this._member);
215211
}
216212
}
217213
}
@@ -225,6 +221,7 @@ module Phaser {
225221
{
226222
this.game.stage.context.restore();
227223
}
224+
228225
}
229226

230227
/**

Phaser/gameobjects/IGameObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Phaser {
3232
/**
3333
* Reference to the Renderer.renderSprite method. Can be overriden by custom classes.
3434
*/
35-
render;
35+
//render;
3636

3737
/**
3838
* Controls if both <code>update</code> and render are called by the core game loop.

Phaser/gameobjects/ScrollZone.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ module Phaser {
3131
super(game, x, y, key);
3232

3333
this.type = Phaser.Types.SCROLLZONE;
34-
this.render = game.renderer.renderScrollZone;
3534

3635
this.regions = [];
3736

Phaser/gameobjects/Sprite.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ module Phaser {
2727

2828
this.game = game;
2929
this.type = Phaser.Types.SPRITE;
30-
this.render = game.renderer.renderSprite;
3130

3231
this.exists = true;
3332
this.active = true;
@@ -42,6 +41,9 @@ module Phaser {
4241
this.y = y;
4342
this.z = 0; // not used yet
4443

44+
// If a texture has been given the body will be set to that size, otherwise 16x16
45+
this.body = new Phaser.Physics.Body(this, bodyType);
46+
4547
this.animations = new Phaser.Components.AnimationManager(this);
4648
this.texture = new Phaser.Components.Texture(this, key);
4749
this.cameraBlacklist = [];
@@ -50,10 +52,6 @@ module Phaser {
5052
this.origin = new Phaser.Vec2(0, 0);
5153
this.scale = new Phaser.Vec2(1, 1);
5254
this.skew = new Phaser.Vec2(0, 0);
53-
54-
// If a texture has been given the body will be set to that size, otherwise 16x16
55-
this.body = new Phaser.Physics.Body(this, bodyType);
56-
5755
}
5856

5957
/**
@@ -66,11 +64,6 @@ module Phaser {
6664
*/
6765
public type: number;
6866

69-
/**
70-
* Reference to the Renderer.renderSprite method. Can be overriden by custom classes.
71-
*/
72-
public render;
73-
7467
/**
7568
* Controls if both <code>update</code> and render are called by the core game loop.
7669
*/

Phaser/physics/Body.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,10 @@ module Phaser.Physics {
270270
this.parent.texture.context.fillStyle = color;
271271
this.parent.texture.context.fillText('Sprite: (' + this.parent.frameBounds.width + ' x ' + this.parent.frameBounds.height + ')', x, y);
272272
//this.parent.texture.context.fillText('x: ' + this._parent.frameBounds.x.toFixed(1) + ' y: ' + this._parent.frameBounds.y.toFixed(1) + ' rotation: ' + this._parent.rotation.toFixed(1), x, y + 14);
273-
this.parent.texture.context.fillText('x: ' + this.bounds.x.toFixed(1) + ' y: ' + this.bounds.y.toFixed(1) + ' angle: ' + this.angle.toFixed(1), x, y + 14);
273+
this.parent.texture.context.fillText('x: ' + this.bounds.x.toFixed(1) + ' y: ' + this.bounds.y.toFixed(1) + ' angle: ' + this.angle.toFixed(0), x, y + 14);
274274
this.parent.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28);
275-
this.parent.texture.context.fillText('ax: ' + this.acceleration.x.toFixed(1) + ' ay: ' + this.acceleration.y.toFixed(1), x, y + 42);
275+
this.parent.texture.context.fillText('acx: ' + this.acceleration.x.toFixed(1) + ' acy: ' + this.acceleration.y.toFixed(1), x, y + 42);
276+
this.parent.texture.context.fillText('angVx: ' + this.angularVelocity.toFixed(1) + ' angAc: ' + this.angularAcceleration.toFixed(1), x, y + 56);
276277

277278
}
278279

Phaser/renderers/CanvasRenderer.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,67 @@ module Phaser {
3636
private _camera: Camera;
3737
private _groupLength: number;
3838

39+
private _count: number;
40+
41+
public renderTotal: number;
42+
3943
public render() {
4044

4145
// Get a list of all the active cameras
4246

4347
this._cameraList = this._game.world.getAllCameras();
4448

49+
this._count = 0;
50+
4551
// Then iterate through world.group on them all (where not blacklisted, etc)
4652
for (var c = 0; c < this._cameraList.length; c++)
4753
{
4854
this._camera = this._cameraList[c];
4955

5056
this._camera.preRender();
5157

52-
this._game.world.group.render(this, this._camera);
58+
this._game.world.group.render(this._camera);
5359

5460
this._camera.postRender();
5561
}
5662

63+
this.renderTotal = this._count;
64+
65+
}
66+
67+
public renderGameObject(object) {
68+
69+
if (object.type == Types.SPRITE)
70+
{
71+
this.renderSprite(this._camera, object);
72+
}
73+
else if (object.type == Types.SCROLLZONE)
74+
{
75+
this.renderScrollZone(this._camera, object);
76+
}
77+
78+
}
79+
80+
/**
81+
* Check whether this object is visible in a specific camera rectangle.
82+
* @param camera {Rectangle} The rectangle you want to check.
83+
* @return {boolean} Return true if bounds of this sprite intersects the given rectangle, otherwise return false.
84+
*/
85+
public inCamera(camera: Camera, sprite: Sprite): bool {
86+
87+
// Object fixed in place regardless of the camera scrolling? Then it's always visible
88+
if (sprite.scrollFactor.x == 0 && sprite.scrollFactor.y == 0)
89+
{
90+
return true;
91+
}
92+
93+
this._dx = sprite.frameBounds.x - camera.worldView.x;
94+
this._dy = sprite.frameBounds.y - camera.worldView.y;
95+
this._dw = sprite.frameBounds.width * sprite.scale.x;
96+
this._dh = sprite.frameBounds.height * sprite.scale.y;
97+
98+
return (camera.worldView.right > this._dx) && (camera.worldView.x < this._dx + this._dw) && (camera.worldView.bottom > this._dy) && (camera.worldView.y < this._dy + this._dh);
99+
57100
}
58101

59102
/**
@@ -63,12 +106,13 @@ module Phaser {
63106
*/
64107
public renderSprite(camera: Camera, sprite: Sprite): bool {
65108

66-
// Render checks (needs inCamera check added)
67-
if (sprite.scale.x == 0 || sprite.scale.y == 0 || sprite.texture.alpha < 0.1)
109+
if (sprite.scale.x == 0 || sprite.scale.y == 0 || sprite.texture.alpha < 0.1 || this.inCamera(camera, sprite) == false)
68110
{
69111
return false;
70112
}
71113

114+
this._count++;
115+
72116
// Reset our temp vars
73117
this._ga = -1;
74118
this._sx = 0;
@@ -193,12 +237,13 @@ module Phaser {
193237

194238
public renderScrollZone(camera: Camera, scrollZone: ScrollZone): bool {
195239

196-
// Render checks (needs inCamera check added)
197-
if (scrollZone.scale.x == 0 || scrollZone.scale.y == 0 || scrollZone.texture.alpha < 0.1)
240+
if (scrollZone.scale.x == 0 || scrollZone.scale.y == 0 || scrollZone.texture.alpha < 0.1 || this.inCamera(camera, scrollZone) == false)
198241
{
199242
return false;
200243
}
201244

245+
this._count++;
246+
202247
// Reset our temp vars
203248
this._ga = -1;
204249
this._sx = 0;

Phaser/renderers/HeadlessRenderer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module Phaser {
1717

1818
public render() {}
1919

20+
public renderGameObject(object) {
21+
}
22+
2023
public renderSprite(camera: Camera, sprite: Sprite): bool {
2124
return true;
2225
}

Phaser/renderers/IRenderer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Phaser {
55
export interface IRenderer {
66

77
render();
8+
renderGameObject(object);
89
renderSprite(camera: Camera, sprite: Sprite): bool;
910
renderScrollZone(camera: Camera, sprite: ScrollZone): bool;
1011

Phaser/utils/SpriteUtils.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// <reference path="../core/Rectangle.ts" />
44
/// <reference path="../core/Circle.ts" />
55
/// <reference path="../gameobjects/Sprite.ts" />
6+
/// <reference path="RectangleUtils.ts" />
67

78
/**
89
* Phaser - SpriteUtils
@@ -154,28 +155,27 @@ module Phaser {
154155
*
155156
* @return Whether or not the point overlaps this object.
156157
*/
157-
/*
158-
static overlapsPoint(point: Point, inScreenSpace: bool = false, camera: Camera = null): bool {
158+
static overlapsPoint(sprite: Sprite, point: Point, inScreenSpace: bool = false, camera: Camera = null): bool {
159159

160160
if (!inScreenSpace)
161161
{
162-
return (point.x > this.x) && (point.x < this.x + this.width) && (point.y > this.y) && (point.y < this.y + this.height);
162+
return Phaser.RectangleUtils.containsPoint(sprite.body.bounds, point);
163+
//return (point.x > sprite.x) && (point.x < sprite.x + sprite.width) && (point.y > sprite.y) && (point.y < sprite.y + sprite.height);
163164
}
164165

165166
if (camera == null)
166167
{
167-
camera = this._game.camera;
168+
camera = sprite.game.camera;
168169
}
169170

170-
var X: number = point.x - camera.scroll.x;
171-
var Y: number = point.y - camera.scroll.y;
171+
//var x: number = point.x - camera.scroll.x;
172+
//var y: number = point.y - camera.scroll.y;
172173

173-
this.getScreenXY(this._point, camera);
174+
//this.getScreenXY(this._point, camera);
174175

175-
return (X > this._point.x) && (X < this._point.x + this.width) && (Y > this._point.y) && (Y < this._point.y + this.height);
176+
//return (x > this._point.x) && (X < this._point.x + this.width) && (Y > this._point.y) && (Y < this._point.y + this.height);
176177

177178
}
178-
*/
179179

180180
/**
181181
* Check and see if this object is currently on screen.
@@ -240,28 +240,6 @@ module Phaser {
240240
}
241241
*/
242242

243-
/**
244-
* Check whether this object is visible in a specific camera rectangle.
245-
* @param camera {Rectangle} The rectangle you want to check.
246-
* @return {boolean} Return true if bounds of this sprite intersects the given rectangle, otherwise return false.
247-
*/
248-
static inCamera(camera: Rectangle, cameraOffsetX: number, cameraOffsetY: number): bool {
249-
250-
// Object fixed in place regardless of the camera scrolling? Then it's always visible
251-
if (this.scrollFactor.x == 0 && this.scrollFactor.y == 0)
252-
{
253-
return true;
254-
}
255-
256-
this._dx = (this.frameBounds.x - camera.x);
257-
this._dy = (this.frameBounds.y - camera.y);
258-
this._dw = this.frameBounds.width * this.scale.x;
259-
this._dh = this.frameBounds.height * this.scale.y;
260-
261-
return (camera.right > this._dx) && (camera.x < this._dx + this._dw) && (camera.bottom > this._dy) && (camera.y < this._dy + this._dh);
262-
263-
}
264-
265243
/**
266244
* Handy for reviving game objects.
267245
* Resets their existence flags and position.
@@ -283,6 +261,19 @@ module Phaser {
283261

284262
}
285263

264+
static setOriginToCenter(sprite: Sprite, fromFrameBounds: bool = true, fromBody?: bool = false) {
265+
266+
if (fromFrameBounds)
267+
{
268+
sprite.origin.setTo(sprite.frameBounds.halfWidth, sprite.frameBounds.halfHeight);
269+
}
270+
else if (fromBody)
271+
{
272+
sprite.origin.setTo(sprite.body.bounds.halfWidth, sprite.body.bounds.halfHeight);
273+
}
274+
275+
}
276+
286277
/**
287278
* Set the world bounds that this GameObject can exist within. By default a GameObject can exist anywhere
288279
* in the world. But by setting the bounds (which are given in world dimensions, not screen dimensions)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ V1.0.0
5252
* Removed Sprite.rotation - use Sprite.angle instead
5353
* Optimised separateX/Y and overlap so they don't use any temporary vars any more.
5454
* Added the new Physics.Body object to all Sprites. Used for all physics calculations in-game. Will be extended for Fixtures/Joints in future.
55+
* Added SpriteUtils.setOriginToCenter to quickly set the origin of a sprite based on either frameBounds or body.bounds
56+
5557

5658

5759
V0.9.6

0 commit comments

Comments
 (0)