Skip to content

Commit c1eafe8

Browse files
committed
First pass at integrating the new physics system.
1 parent f3c9049 commit c1eafe8

15 files changed

Lines changed: 3399 additions & 2327 deletions

File tree

Phaser/Phaser.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Content Include="components\animation\AnimationManager.js">
6262
<DependentUpon>AnimationManager.ts</DependentUpon>
6363
</Content>
64+
<TypeScriptCompile Include="components\sprite\Physics.ts" />
6465
<TypeScriptCompile Include="core\Rectangle.ts" />
6566
<TypeScriptCompile Include="core\Point.ts" />
6667
<TypeScriptCompile Include="components\sprite\Texture.ts" />
@@ -107,6 +108,14 @@
107108
<TypeScriptCompile Include="renderers\CanvasRenderer.ts" />
108109
<TypeScriptCompile Include="Statics.ts" />
109110
<TypeScriptCompile Include="renderers\HeadlessRenderer.ts" />
111+
<TypeScriptCompile Include="physics\PhysicsManager.ts" />
112+
<TypeScriptCompile Include="physics\AABB.ts" />
113+
<Content Include="physics\AABB.js">
114+
<DependentUpon>AABB.ts</DependentUpon>
115+
</Content>
116+
<Content Include="physics\PhysicsManager.js">
117+
<DependentUpon>PhysicsManager.ts</DependentUpon>
118+
</Content>
110119
<Content Include="renderers\HeadlessRenderer.js">
111120
<DependentUpon>HeadlessRenderer.ts</DependentUpon>
112121
</Content>

Phaser/World.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// <reference path="cameras/CameraManager.ts" />
33
/// <reference path="core/Group.ts" />
44
/// <reference path="core/Rectangle.ts" />
5+
/// <reference path="physics/PhysicsManager.ts" />
56

67
/**
78
* Phaser - World
@@ -34,6 +35,8 @@ module Phaser {
3435

3536
this.bounds = new Rectangle(0, 0, width, height);
3637

38+
this.physics = new Physics.PhysicsManager(this._game, width, height);
39+
3740
this.worldDivisions = 6;
3841

3942
}
@@ -61,6 +64,12 @@ module Phaser {
6164
*/
6265
public bounds: Rectangle;
6366

67+
/**
68+
* Reference to the physics manager.
69+
* @type {Physics.PhysicsManager}
70+
*/
71+
public physics: Physics.PhysicsManager;
72+
6473
/**
6574
* @type {number}
6675
*/
@@ -71,6 +80,7 @@ module Phaser {
7180
*/
7281
public update() {
7382

83+
this.physics.update();
7484
this.group.update();
7585
this.cameras.update();
7686

@@ -81,8 +91,8 @@ module Phaser {
8191
*/
8292
public destroy() {
8393

94+
//this.physics.destroy();
8495
this.group.destroy();
85-
8696
this.cameras.destroy();
8797

8898
}
@@ -94,7 +104,7 @@ module Phaser {
94104
* @param height {number} New height of the world.
95105
* @param [updateCameraBounds] {boolean} Update camera bounds automatically or not. Default to true.
96106
*/
97-
public setSize(width: number, height: number, updateCameraBounds: bool = true) {
107+
public setSize(width: number, height: number, updateCameraBounds: bool = true, updatePhysicsBounds: bool = true) {
98108

99109
this.bounds.width = width;
100110
this.bounds.height = height;
@@ -104,6 +114,11 @@ module Phaser {
104114
this._game.camera.setBounds(0, 0, width, height);
105115
}
106116

117+
if (updatePhysicsBounds == true)
118+
{
119+
//this.physics.bounds.copyFrom(this.bounds);
120+
}
121+
107122
// dispatch world resize event
108123

109124
}

Phaser/components/sprite/Physics.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path="../../core/Vec2.ts" />
22
/// <reference path="../../core/Point.ts" />
3+
/// <reference path="../../physics/AABB.ts" />
34

45
/**
56
* Phaser - Components - Physics
@@ -11,6 +12,28 @@ module Phaser.Components {
1112

1213
export class Physics {
1314

15+
constructor(parent: Sprite) {
16+
17+
this._game = parent.game;
18+
this._sprite = parent;
19+
20+
//this.AABB = new Phaser.Physics.AABB(this._game, this._sprite, this._sprite.x, this._sprite.y, this._sprite.width, this._sprite.height);
21+
this.AABB = this._game.world.physics.add(new Phaser.Physics.AABB(this._game, this._sprite, this._sprite.x, this._sprite.y, this._sprite.width, this._sprite.height));
22+
23+
}
24+
25+
/**
26+
*
27+
*/
28+
private _game: Game;
29+
30+
/**
31+
*
32+
*/
33+
private _sprite: Sprite;
34+
35+
public AABB: Phaser.Physics.AABB;
36+
1437
/**
1538
* Whether this object will be moved by impacts with other objects or not.
1639
* @type {boolean}
@@ -126,9 +149,9 @@ module Phaser.Components {
126149
*
127150
* @return {boolean} Whether the object is touching an object in (any of) the specified direction(s) this frame.
128151
*/
129-
public isTouching(direction: number): bool {
130-
return (this.touching & direction) > Collision.NONE;
131-
}
152+
//public isTouching(direction: number): bool {
153+
// return (this.touching & direction) > Collision.NONE;
154+
//}
132155

133156
/**
134157
* Handy function for checking if this object just landed on a particular surface.
@@ -137,16 +160,22 @@ module Phaser.Components {
137160
*
138161
* @returns {boolean} Whether the object just landed on any specicied surfaces.
139162
*/
140-
public justTouched(direction: number): bool {
141-
return ((this.touching & direction) > Collision.NONE) && ((this.wasTouching & direction) <= Collision.NONE);
142-
}
163+
//public justTouched(direction: number): bool {
164+
// return ((this.touching & direction) > Collision.NONE) && ((this.wasTouching & direction) <= Collision.NONE);
165+
//}
143166

144167

145168
/**
146169
* Internal function for updating the position and speed of this object.
147170
*/
148171
public update() {
149172

173+
if (this.moves)
174+
{
175+
this._sprite.x = this.AABB.position.x - this.AABB.halfWidth;
176+
this._sprite.y = this.AABB.position.y - this.AABB.halfHeight;
177+
}
178+
150179
/*
151180
var delta: number;
152181
var velocityDelta: number;
@@ -176,20 +205,20 @@ module Phaser.Components {
176205
* the object will collide from, use collision constants (like LEFT, FLOOR, etc)
177206
* to set the value of allowCollisions directly.
178207
*/
179-
public get solid(): bool {
180-
return (this.allowCollisions & Collision.ANY) > Collision.NONE;
181-
}
208+
//public get solid(): bool {
209+
// return (this.allowCollisions & Collision.ANY) > Collision.NONE;
210+
//}
182211

183212
public set solid(value: bool) {
184213

185-
if (value)
186-
{
187-
this.allowCollisions = Collision.ANY;
188-
}
189-
else
190-
{
191-
this.allowCollisions = Collision.NONE;
192-
}
214+
//if (value)
215+
//{
216+
// this.allowCollisions = Collision.ANY;
217+
//}
218+
//else
219+
//{
220+
// this.allowCollisions = Collision.NONE;
221+
//}
193222

194223
}
195224

Phaser/gameobjects/GameObjectFactory.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path="../Game.ts" />
22
/// <reference path="../tweens/Tween.ts" />
3+
/// <reference path="../physics/AABB.ts" />
34

45
/**
56
* Phaser - GameObjectFactory
@@ -89,6 +90,19 @@ module Phaser {
8990
return <Group> this._world.group.add(new Group(this._game, maxSize));
9091
}
9192

93+
/**
94+
* Create a new Sprite with specific position and sprite sheet key.
95+
*
96+
* @param x {number} X position of the new sprite.
97+
* @param y {number} Y position of the new sprite.
98+
* @param key {string} Optional, key for the sprite sheet you want it to use.
99+
* @returns {Sprite} The newly created sprite object.
100+
* WILL NEED TO TRACK A SPRITE
101+
*/
102+
public physicsAABB(x: number, y: number, width: number, height: number): Physics.AABB {
103+
return <Physics.AABB> this._world.physics.add(new Physics.AABB(this._game, null, x, y, width, height));
104+
}
105+
92106
/**
93107
* Create a new Particle.
94108
*

Phaser/gameobjects/Sprite.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// <reference path="../core/Rectangle.ts" />
44
/// <reference path="../components/animation/AnimationManager.ts" />
55
/// <reference path="../components/sprite/Texture.ts" />
6+
/// <reference path="../components/sprite/Physics.ts" />
67

78
/**
89
* Phaser - Sprite
@@ -47,6 +48,8 @@ module Phaser {
4748
this.width = this.frameBounds.width;
4849
this.height = this.frameBounds.height;
4950

51+
this.physics = new Phaser.Components.Physics(this);
52+
5053
// Transform related (if we add any more then move to a component)
5154
this.origin = new Phaser.Vec2(0, 0);
5255
this.scale = new Phaser.Vec2(1, 1);
@@ -99,6 +102,11 @@ module Phaser {
99102
public width: number;
100103
public height: number;
101104

105+
/**
106+
* Sprite physics.
107+
*/
108+
public physics: Phaser.Components.Physics;
109+
102110
/**
103111
* The texture used to render the Sprite.
104112
*/
@@ -213,10 +221,8 @@ module Phaser {
213221
*/
214222
public preUpdate() {
215223

216-
//this.last.x = this.frameBounds.x;
217-
//this.last.y = this.frameBounds.y;
218-
219-
//this.collisionMask.preUpdate();
224+
this.frameBounds.x = this.x;
225+
this.frameBounds.y = this.y;
220226

221227
if (this.modified == false && (!this.scale.equals(1) || !this.skew.equals(0) || this.rotation != 0 || this.rotationOffset != 0 || this.texture.flippedX || this.texture.flippedY))
222228
{
@@ -237,13 +243,9 @@ module Phaser {
237243
public postUpdate() {
238244

239245
this.animations.update();
246+
this.physics.update();
240247

241248
/*
242-
if (this.moves)
243-
{
244-
this.updateMotion();
245-
}
246-
247249
if (this.worldBounds != null)
248250
{
249251
if (this.outOfBoundsAction == GameObject.OUT_OF_BOUNDS_KILL)

0 commit comments

Comments
 (0)