Skip to content

Commit f4dab18

Browse files
committed
Working on Linked List node swapping.
1 parent 10bc22b commit f4dab18

4 files changed

Lines changed: 276 additions & 11 deletions

File tree

examples/bringToTop.php

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
19+
}
20+
21+
var s;
22+
var a;
23+
var b;
24+
var c;
25+
var d;
26+
var e;
27+
var f;
28+
29+
function create() {
30+
31+
s = game.add.sprite(game.world.centerX, game.world.centerY, 'sonic');
32+
s.name = 'X';
33+
34+
a = game.add.child(s, -50, 0, 'sonic');
35+
b = game.add.child(s, -100, 0, 'sonic');
36+
c = game.add.child(s, -150, 0, 'sonic');
37+
d = game.add.child(s, -200, 0, 'sonic');
38+
e = game.add.child(s, -250, 0, 'sonic');
39+
f = game.add.child(s, -300, 0, 'sonic');
40+
41+
a.name = 'a';
42+
b.name = 'b';
43+
c.name = 'c';
44+
d.name = 'd';
45+
e.name = 'e';
46+
f.name = 'f';
47+
48+
game.input.onUp.add(runChange, this);
49+
50+
scanList(s);
51+
52+
}
53+
54+
function runChange () {
55+
changeOrder(a, d);
56+
}
57+
58+
function changeOrder (node1, node2) {
59+
60+
console.log('Changing order of', node1.name,'and',node2.name);
61+
62+
var index1 = s.children.indexOf(node1);
63+
var index2 = s.children.indexOf(node2);
64+
65+
if (index1 !== -1 && index2 !== -1)
66+
{
67+
// check for neighbours (cater for any order parameters)
68+
if (node1._iNext == node2)
69+
{
70+
console.log('A-B neighbour swap');
71+
72+
// Pre-swap:
73+
// X next: a prev: - first: X last: d
74+
// a next: b prev: X first: a last: a
75+
// b next: c prev: a first: b last: b
76+
// c next: d prev: b first: c last: c
77+
// d next: - prev: c first: d last: d
78+
79+
// Post-swap:
80+
// X next: b prev: - first: X last: d
81+
// b next: a prev: X first: b last: b
82+
// a next: c prev: b first: a last: a
83+
// c next: d prev: a first: c last: c
84+
// d next: - prev: c first: d last: d
85+
86+
var node1Prev = node1._iPrev;
87+
var node1Next = node1._iNext;
88+
var node2Prev = node2._iPrev;
89+
var node2Next = node2._iNext;
90+
91+
// Starting
92+
// Node 1 (A) Node 2 (B) X C
93+
// Next: B Next: C Next: A Next: D
94+
// Prev: X Prev: A Prev: - Prev: B
95+
96+
// Ending
97+
// Node 1 (A) Node 2 (B) X C
98+
// Next: C Next: A Next: B Next: D
99+
// Prev: B Prev: X Prev: - Prev: A
100+
101+
node1._iNext = node2Next;
102+
node1._iPrev = node2;
103+
node2._iNext = node1;
104+
node2._iPrev = node1Prev;
105+
106+
// Notify the head and tail
107+
if (node1Prev)
108+
{
109+
node1Prev._iNext = node2;
110+
}
111+
112+
if (node2Next)
113+
{
114+
node2Next._iPrev = node1;
115+
}
116+
}
117+
else if (node2._iNext == node1)
118+
{
119+
console.log('B-A neighbour swap');
120+
121+
// Pre-swap:
122+
// X next: a prev: - first: X last: d
123+
// a next: b prev: X first: a last: a
124+
// b next: c prev: a first: b last: b
125+
// c next: d prev: b first: c last: c
126+
// d next: - prev: c first: d last: d
127+
128+
// Post-swap:
129+
// X next: b prev: - first: X last: d
130+
// b next: a prev: X first: b last: b
131+
// a next: c prev: b first: a last: a
132+
// c next: d prev: a first: c last: c
133+
// d next: - prev: c first: d last: d
134+
135+
var node1Prev = node1._iPrev;
136+
var node1Next = node1._iNext;
137+
var node2Prev = node2._iPrev;
138+
var node2Next = node2._iNext;
139+
140+
// Starting
141+
// Node 1 (B) Node 2 (A) X C
142+
// Next: C Next: B Next: A Next: D
143+
// Prev: A Prev: X Prev: - Prev: B
144+
145+
// Ending
146+
// Node 1 (B) Node 2 (A) X C
147+
// Next: A Next: C Next: B Next: D
148+
// Prev: X Prev: B Prev: - Prev: A
149+
150+
node1._iNext = node2;
151+
node1._iPrev = node2Prev;
152+
node2._iNext = node1Next;
153+
node2._iPrev = node1;
154+
155+
// Notify the head and tail
156+
if (node2Prev)
157+
{
158+
node2Prev._iNext = node1;
159+
}
160+
161+
if (node1Next)
162+
{
163+
node2Next._iPrev = node2;
164+
}
165+
}
166+
}
167+
168+
scanList(s);
169+
170+
}
171+
172+
function scanList (sprite) {
173+
174+
var displayObject = sprite;
175+
176+
var testObject = displayObject.last._iNext;
177+
displayObject = displayObject.first;
178+
179+
do
180+
{
181+
var name = displayObject.name || 'nuffin';
182+
var nameNext = '-';
183+
var namePrev = '-';
184+
var nameFirst = '-';
185+
var nameLast = '-';
186+
187+
if (displayObject._iNext)
188+
{
189+
nameNext = displayObject._iNext.name;
190+
}
191+
192+
if (displayObject._iPrev)
193+
{
194+
namePrev = displayObject._iPrev.name;
195+
}
196+
197+
if (displayObject.first)
198+
{
199+
nameFirst = displayObject.first.name;
200+
}
201+
202+
if (displayObject.last)
203+
{
204+
nameLast = displayObject.last.name;
205+
}
206+
207+
if (typeof nameNext === 'undefined')
208+
{
209+
nameNext = '-';
210+
}
211+
212+
if (typeof namePrev === 'undefined')
213+
{
214+
namePrev = '-';
215+
}
216+
217+
if (typeof nameFirst === 'undefined')
218+
{
219+
nameFirst = '-';
220+
}
221+
222+
if (typeof nameLast === 'undefined')
223+
{
224+
nameLast = '-';
225+
}
226+
227+
console.log('node:', name, 'next:', nameNext, 'prev:', namePrev, 'first:', nameFirst, 'last:', nameLast);
228+
229+
displayObject = displayObject._iNext;
230+
231+
}
232+
while(displayObject != testObject)
233+
234+
}
235+
236+
function update() {
237+
}
238+
239+
function render() {
240+
241+
// game.debug.renderSpriteCorners(s, false, false);
242+
// game.debug.renderSpriteInfo(s, 20, 32);
243+
244+
}
245+
246+
})();
247+
248+
</script>
249+
250+
</body>
251+
</html>

src/gameobjects/GameObjectFactory.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ Phaser.GameObjectFactory.prototype = {
2525

2626
},
2727

28+
/**
29+
* Create a new Sprite with specific position and sprite sheet key.
30+
*
31+
* @param x {number} X position of the new sprite.
32+
* @param y {number} Y position of the new sprite.
33+
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this sprite
34+
* @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name.
35+
* @returns {Sprite} The newly created sprite object.
36+
*/
37+
child: function (parent, x, y, key, frame) {
38+
39+
var child = this.world.add(new Phaser.Sprite(this.game, x, y, key, frame));
40+
parent.addChild(child);
41+
return child;
42+
43+
},
44+
2845
/**
2946
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
3047
*

src/input/Input.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Phaser.Input = function (game) {
1313

1414
};
1515

16-
Phaser.Mouse_OVERRIDES_TOUCH = 0;
17-
Phaser.Touch_OVERRIDES_MOUSE = 1;
18-
Phaser.Mouse_TOUCH_COMBINE = 2;
16+
Phaser.Input.MOUSE_OVERRIDES_TOUCH = 0;
17+
Phaser.Input.TOUCH_OVERRIDES_MOUSE = 1;
18+
Phaser.Input.MOUSE_TOUCH_COMBINE = 2;
1919

2020
Phaser.Input.prototype = {
2121

@@ -60,7 +60,7 @@ Phaser.Input.prototype = {
6060
/**
6161
* Controls the expected behaviour when using a mouse and touch together on a multi-input device
6262
*/
63-
multiInputOverride: Phaser.Mouse_TOUCH_COMBINE,
63+
multiInputOverride: Phaser.Input.MOUSE_TOUCH_COMBINE,
6464

6565
/**
6666
* A vector object representing the current position of the Pointer.

src/input/Pointer.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ Phaser.Pointer.prototype = {
238238
// x and y are the old values here?
239239
this.positionDown.setTo(this.x, this.y);
240240

241-
if (this.game.input.multiInputOverride == Phaser.Mouse_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Mouse_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Touch_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
241+
if (this.game.input.multiInputOverride == Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
242242
{
243243
//this.game.input.x = this.x * this.game.input.scale.x;
244244
//this.game.input.y = this.y * this.game.input.scale.y;
@@ -272,7 +272,7 @@ Phaser.Pointer.prototype = {
272272
{
273273
if (this._holdSent == false && this.duration >= this.game.input.holdRate)
274274
{
275-
if (this.game.input.multiInputOverride == Phaser.Mouse_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Mouse_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Touch_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
275+
if (this.game.input.multiInputOverride == Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
276276
{
277277
this.game.input.onHold.dispatch(this);
278278
}
@@ -295,9 +295,6 @@ Phaser.Pointer.prototype = {
295295
this._history.shift();
296296
}
297297
}
298-
299-
// Check which camera they are over
300-
// this.camera = this.game.world.cameras.getCameraUnderPoint(this.x, this.y);
301298
}
302299

303300
},
@@ -335,7 +332,7 @@ Phaser.Pointer.prototype = {
335332
this.circle.x = this.x;
336333
this.circle.y = this.y;
337334

338-
if (this.game.input.multiInputOverride == Phaser.Mouse_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Mouse_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Touch_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
335+
if (this.game.input.multiInputOverride == Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
339336
{
340337
this.game.input.activePointer = this;
341338
this.game.input.x = this.x;
@@ -453,7 +450,7 @@ Phaser.Pointer.prototype = {
453450

454451
this.timeUp = this.game.time.now;
455452

456-
if (this.game.input.multiInputOverride == Phaser.Mouse_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Mouse_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Touch_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
453+
if (this.game.input.multiInputOverride == Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
457454
{
458455
this.game.input.onUp.dispatch(this);
459456

0 commit comments

Comments
 (0)