Skip to content

Commit a07cf89

Browse files
committed
Fixed Kenny tiles. Fixed Ninja Circle vs. World bounds. Added Ninja tilemap to Tiles conversion. Created test.
1 parent 4910f27 commit a07cf89

9 files changed

Lines changed: 326 additions & 32 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{ "height":11,
2+
"layers":[
3+
{
4+
"data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 77, 95, 0, 0, 0, 0, 0, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 131, 36, 15, 20, 74, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 77, 77, 15, 20, 20, 34, 74, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 15, 20, 20, 20, 20, 20, 34, 34, 74, 95, 0, 89, 0, 137, 0, 0, 0, 0, 140, 0, 0, 0, 0, 36, 15, 20, 20, 20, 20, 20, 20, 34, 34, 34, 74, 32, 32, 32, 116, 135, 135, 135, 135, 119, 77, 77, 77, 77, 15, 20, 20, 20, 20, 20, 20, 20, 34, 34, 34, 34, 34, 34, 34, 34, 31, 31, 31, 31, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 34, 34, 34, 34, 34, 34, 34, 34, 31, 31, 31, 31, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
5+
"height":11,
6+
"name":"Tile Layer 1",
7+
"opacity":1,
8+
"type":"tilelayer",
9+
"visible":true,
10+
"width":25,
11+
"x":0,
12+
"y":0
13+
}],
14+
"orientation":"orthogonal",
15+
"properties":
16+
{
17+
18+
},
19+
"tileheight":70,
20+
"tilesets":[
21+
{
22+
"firstgid":1,
23+
"image":"..\/tiles\/kenney.png",
24+
"imageheight":490,
25+
"imagewidth":1470,
26+
"margin":0,
27+
"name":"kenney",
28+
"properties":
29+
{
30+
31+
},
32+
"spacing":0,
33+
"tileheight":70,
34+
"tileproperties":
35+
{
36+
"136":
37+
{
38+
"slope":"3"
39+
},
40+
"139":
41+
{
42+
"slope":"2"
43+
},
44+
"31":
45+
{
46+
"slope":"1"
47+
},
48+
"35":
49+
{
50+
"slope":"3"
51+
},
52+
"76":
53+
{
54+
"slope":"1"
55+
},
56+
"94":
57+
{
58+
"slope":"2"
59+
}
60+
},
61+
"tilewidth":70
62+
}],
63+
"tilewidth":70,
64+
"version":1,
65+
"width":25
66+
}
136 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<map version="1.0" orientation="orthogonal" width="25" height="11" tilewidth="70" tileheight="70">
3+
<tileset firstgid="1" name="kenney" tilewidth="70" tileheight="70">
4+
<image source="../tiles/kenney.png" width="1470" height="490"/>
5+
<tile id="31">
6+
<properties>
7+
<property name="slope" value="1"/>
8+
</properties>
9+
</tile>
10+
<tile id="35">
11+
<properties>
12+
<property name="slope" value="3"/>
13+
</properties>
14+
</tile>
15+
<tile id="76">
16+
<properties>
17+
<property name="slope" value="1"/>
18+
</properties>
19+
</tile>
20+
<tile id="94">
21+
<properties>
22+
<property name="slope" value="2"/>
23+
</properties>
24+
</tile>
25+
<tile id="136">
26+
<properties>
27+
<property name="slope" value="3"/>
28+
</properties>
29+
</tile>
30+
<tile id="139">
31+
<properties>
32+
<property name="slope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WQAAAAAAAACJAAAAAAAAAAAAAAAAAAAAAAAAAIwAAAAAAAAAAAAAAAAAAAAAAAAAJAAAAA8AAAAUAAAAFAAAABQAAAAUAAAAFAAAABQAAAAiAAAAIgAAACIAAABKAAAAIAAAACAAAAAgAAAAdAAAAIcAAACHAAAAhwAAAIcAAAB3AAAATQAAAE0AAABNAAAATQAAAA8AAAAUAAAAFAAAABQAAAAUAAAAFAAAABQAAAAUAAAAIgAAACIAAAAiAAAAIgAAACIAAAAiAAAAIgAAACIAAAAfAAAAHwAAAB8AAAAfAAAAFAAAABQAAAAUAAAAFAAAABQAAAAUAAAAFAAAABQAAAAUAAAAFAAAABQAAAAUAAAAFAAAACIAAAAiAAAAIgAAACIAAAAiAAAAIgAAACIAAAAiAAAAHwAAAB8AAAAfAAAAHwAAABQAAAAUAAAAFAAAABQAAAAUAAAAFAAAABQAAAAUAAAAFAAAABQAAAAUAAAAFAAAABQAAAA=
39+
</data>
40+
</layer>
41+
</map>

examples/wip/ninja tilemap.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
4+
5+
function preload() {
6+
7+
game.load.tilemap('map', 'assets/tilemaps/maps/ninja-tilemap.json', null, Phaser.Tilemap.TILED_JSON);
8+
game.load.image('ball', 'assets/sprites/shinyball.png');
9+
game.load.image('sky', 'assets/skies/sky2.png');
10+
game.load.image('kenney', 'assets/tilemaps/tiles/kenney.png');
11+
12+
}
13+
14+
var sprite1;
15+
var cursors;
16+
var map;
17+
var layer;
18+
var tiles;
19+
20+
function create() {
21+
22+
var sky = game.add.image(0, 0, 'sky');
23+
sky.fixedToCamera = true;
24+
25+
// Activate the Ninja physics system
26+
game.physics.startSystem(Phaser.Physics.NINJA);
27+
28+
map = game.add.tilemap('map');
29+
30+
map.addTilesetImage('kenney');
31+
32+
layer = map.createLayer('Tile Layer 1');
33+
34+
layer.resizeWorld();
35+
36+
var slopeMap = { '32': 1, '77': 1, '95': 2, '36': 3, '137': 3, '140': 2 };
37+
38+
tiles = game.physics.ninja.convertTilemap(map, layer, slopeMap);
39+
40+
sprite1 = game.add.sprite(50, 50, 'ball');
41+
// game.physics.ninja.enableAABB(sprite1);
42+
game.physics.ninja.enableCircle(sprite1, sprite1.width / 2);
43+
44+
// A little more bounce
45+
sprite1.body.bounce = 0.5;
46+
47+
game.camera.follow(sprite1);
48+
49+
cursors = game.input.keyboard.createCursorKeys();
50+
51+
}
52+
53+
function update() {
54+
55+
for (var i = 0; i < tiles.length; i++)
56+
{
57+
sprite1.body.circle.collideCircleVsTile(tiles[i].tile);
58+
// sprite1.body.aabb.collideAABBVsTile(tiles[i].tile);
59+
}
60+
61+
// game.physics.ninja.collide(sprite1, tiles);
62+
63+
// sprite1.body.setZeroVelocity();
64+
65+
if (cursors.left.isDown)
66+
{
67+
sprite1.body.moveLeft(20);
68+
}
69+
else if (cursors.right.isDown)
70+
{
71+
sprite1.body.moveRight(20);
72+
}
73+
74+
if (cursors.up.isDown)
75+
{
76+
sprite1.body.moveUp(20);
77+
}
78+
else if (cursors.down.isDown)
79+
{
80+
sprite1.body.moveUp(20);
81+
}
82+
83+
}
84+
85+
function render() {
86+
87+
88+
// game.debug.text(sprite1.body.shape.velocity.x, 32, 32);
89+
// game.debug.text(sprite1.body.shape.velocity.y, 32, 64);
90+
// game.debug.text(game.math.radToDeg(sprite1.body.angle), 32, 96);
91+
92+
// tile1.render(game.context, 'ninja-tiles');
93+
// tile2.render(game.context, 'ninja-tiles');
94+
95+
// game.debug.geom(sprite1.body, 'rgba(0,255,0,0.4)', true, 1);
96+
97+
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
98+
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
99+
100+
}

src/physics/ninja/Body.js

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
* @param {number} [type=1] - The type of Ninja shape to create. 1 = AABB, 2 = Circle or 3 = Tile.
1717
* @param {number} [id=1] - If this body is using a Tile shape, you can set the Tile id here, i.e. Phaser.Physics.Ninja.Tile.SLOPE_45DEGpn, Phaser.Physics.Ninja.Tile.CONVEXpp, etc.
1818
* @param {number} [radius=16] - If this body is using a Circle shape this controls the radius.
19+
* @param {number} [x=0] - The x coordinate of this Body. This is only used if a sprite is not provided.
20+
* @param {number} [y=0] - The y coordinate of this Body. This is only used if a sprite is not provided.
21+
* @param {number} [width=0] - The width of this Body. This is only used if a sprite is not provided.
22+
* @param {number} [height=0] - The height of this Body. This is only used if a sprite is not provided.
1923
*/
20-
Phaser.Physics.Ninja.Body = function (system, sprite, type, id, radius) {
24+
Phaser.Physics.Ninja.Body = function (system, sprite, type, id, radius, x, y, width, height) {
25+
26+
sprite = sprite || null;
2127

2228
if (typeof type === 'undefined') { type = 1; }
2329
if (typeof id === 'undefined') { id = 1; }
@@ -31,7 +37,7 @@ Phaser.Physics.Ninja.Body = function (system, sprite, type, id, radius) {
3137
/**
3238
* @property {Phaser.Game} game - Local reference to game.
3339
*/
34-
this.game = sprite.game;
40+
this.game = system.game;
3541

3642
/**
3743
* @property {number} type - The type of physics system this body belongs to.
@@ -131,32 +137,37 @@ Phaser.Physics.Ninja.Body = function (system, sprite, type, id, radius) {
131137
*/
132138
this.maxSpeed = 8;
133139

134-
var sx = sprite.x;
135-
var sy = sprite.y;
136-
137-
if (sprite.anchor.x === 0)
140+
if (sprite)
138141
{
139-
sx += (sprite.width * 0.5);
140-
}
142+
x = sprite.x;
143+
y = sprite.y;
144+
width = sprite.width;
145+
height = sprite.height;
141146

142-
if (sprite.anchor.y === 0)
143-
{
144-
sy += (sprite.height * 0.5);
147+
if (sprite.anchor.x === 0)
148+
{
149+
x += (sprite.width * 0.5);
150+
}
151+
152+
if (sprite.anchor.y === 0)
153+
{
154+
y += (sprite.height * 0.5);
155+
}
145156
}
146157

147158
if (type === 1)
148159
{
149-
this.aabb = new Phaser.Physics.Ninja.AABB(this, sx, sy, sprite.width, sprite.height);
160+
this.aabb = new Phaser.Physics.Ninja.AABB(this, x, y, width, height);
150161
this.shape = this.aabb;
151162
}
152163
else if (type === 2)
153164
{
154-
this.circle = new Phaser.Physics.Ninja.Circle(this, sx, sy, radius);
165+
this.circle = new Phaser.Physics.Ninja.Circle(this, x, y, radius);
155166
this.shape = this.circle;
156167
}
157168
else if (type === 3)
158169
{
159-
this.tile = new Phaser.Physics.Ninja.Tile(this, sx, sy, sprite.width, sprite.height, id);
170+
this.tile = new Phaser.Physics.Ninja.Tile(this, x, y, width, height, id);
160171
this.shape = this.tile;
161172
}
162173

@@ -202,16 +213,19 @@ Phaser.Physics.Ninja.Body.prototype = {
202213
*/
203214
postUpdate: function () {
204215

205-
if (this.sprite.type === Phaser.TILESPRITE)
206-
{
207-
// TileSprites don't use their anchor property, so we need to adjust the coordinates
208-
this.sprite.x = this.shape.pos.x - this.shape.xw;
209-
this.sprite.y = this.shape.pos.y - this.shape.yw;
210-
}
211-
else
216+
if (this.sprite)
212217
{
213-
this.sprite.x = this.shape.pos.x;
214-
this.sprite.y = this.shape.pos.y;
218+
if (this.sprite.type === Phaser.TILESPRITE)
219+
{
220+
// TileSprites don't use their anchor property, so we need to adjust the coordinates
221+
this.sprite.x = this.shape.pos.x - this.shape.xw;
222+
this.sprite.y = this.shape.pos.y - this.shape.yw;
223+
}
224+
else
225+
{
226+
this.sprite.x = this.shape.pos.x;
227+
this.sprite.y = this.shape.pos.y;
228+
}
215229
}
216230

217231
if (this.velocity.x < 0)

src/physics/ninja/Circle.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,31 +199,31 @@ Phaser.Physics.Ninja.Circle.prototype = {
199199
*/
200200
collideWorldBounds: function () {
201201

202-
var dx = this.system.bounds.x - (this.pos.x - this.xw);
202+
var dx = this.system.bounds.x - (this.pos.x - this.radius);
203203

204204
if (0 < dx)
205205
{
206206
this.reportCollisionVsWorld(dx, 0, 1, 0, null);
207207
}
208208
else
209209
{
210-
dx = (this.pos.x + this.xw) - this.system.bounds.width;
210+
dx = (this.pos.x + this.radius) - this.system.bounds.width;
211211

212212
if (0 < dx)
213213
{
214214
this.reportCollisionVsWorld(-dx, 0, -1, 0, null);
215215
}
216216
}
217217

218-
var dy = this.system.bounds.y - (this.pos.y - this.yw);
218+
var dy = this.system.bounds.y - (this.pos.y - this.radius);
219219

220220
if (0 < dy)
221221
{
222222
this.reportCollisionVsWorld(0, dy, 0, 1, null);
223223
}
224224
else
225225
{
226-
dy = (this.pos.y + this.yw) - this.system.bounds.height;
226+
dy = (this.pos.y + this.radius) - this.system.bounds.height;
227227

228228
if (0 < dy)
229229
{

src/physics/ninja/Tile.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ Phaser.Physics.Ninja.Tile.prototype = {
300300

301301
},
302302

303+
/**
304+
* Destroys this Tiles reference to Body and System.
305+
*
306+
* @method Phaser.Physics.Ninja.Tile#destroy
307+
*/
308+
destroy: function () {
309+
310+
this.body = null;
311+
this.system = null;
312+
313+
},
314+
303315
/**
304316
* This converts a tile from implicitly-defined (via id), to explicit (via properties).
305317
* Don't call directly, instead of setType.

0 commit comments

Comments
 (0)