Skip to content

Commit 78598ae

Browse files
committed
Implemented my own LinkedList class to make the new Input Handler easier to work with. And also just generally useful to have too.
1 parent fe6664e commit 78598ae

9 files changed

Lines changed: 359 additions & 586 deletions

File tree

Phaser.sublime-workspace

Lines changed: 27 additions & 581 deletions
Large diffs are not rendered by default.

examples/js.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<script src="../src/core/Camera.js"></script>
4646
<script src="../src/core/State.js"></script>
4747
<script src="../src/core/StateManager.js"></script>
48+
<script src="../src/core/LinkedList.js"></script>
4849
<script src="../src/core/Signal.js"></script>
4950
<script src="../src/core/SignalBinding.js"></script>
5051
<script src="../src/core/Plugin.js"></script>
@@ -59,6 +60,7 @@
5960
<script src="../src/input/MSPointer.js"></script>
6061
<script src="../src/input/Pointer.js"></script>
6162
<script src="../src/input/Touch.js"></script>
63+
<script src="../src/input/InputHandler.js"></script>
6264
<script src="../src/system/Canvas.js"></script>
6365
<script src="../src/gameobjects/GameObjectFactory.js"></script>
6466
<script src="../src/gameobjects/Sprite.js"></script>

examples/linkedlist1.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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('alien', 'assets/sprites/space-baddie.png');
19+
game.load.image('ship', 'assets/sprites/shmup-ship.png');
20+
}
21+
22+
var a;
23+
var b;
24+
var c;
25+
var d;
26+
var e;
27+
var f;
28+
29+
var list;
30+
31+
function create() {
32+
33+
a = game.add.sprite(game.world.randomX, game.world.randomY, 'ship');
34+
a.name = 's1';
35+
b = game.add.sprite(game.world.randomX, game.world.randomY, 'ship');
36+
b.name = 's2';
37+
c = game.add.sprite(game.world.randomX, game.world.randomY, 'ship');
38+
c.name = 's3';
39+
d = game.add.sprite(game.world.randomX, game.world.randomY, 'alien');
40+
d.name = 'a1';
41+
e = game.add.sprite(game.world.randomX, game.world.randomY, 'alien');
42+
e.name = 'a2';
43+
f = game.add.sprite(game.world.randomX, game.world.randomY, 'alien');
44+
f.name = 'a3';
45+
46+
list = new Phaser.LinkedList();
47+
48+
list.add(a.input);
49+
list.add(b.input);
50+
list.add(c.input);
51+
list.add(d.input);
52+
list.add(e.input);
53+
list.add(f.input);
54+
55+
list.dump();
56+
57+
list.remove(d.input);
58+
59+
list.dump();
60+
61+
}
62+
63+
function update() {
64+
}
65+
66+
function render() {
67+
}
68+
69+
})();
70+
71+
</script>
72+
73+
</body>
74+
</html>

src/core/Group.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ Phaser.Group.prototype = {
205205
* @param {string} index The <code>string</code> name of the member variable you want to sort on. Default value is "z".
206206
* @param {number} order A <code>Group</code> constant that defines the sort order. Possible values are <code>Group.ASCENDING</code> and <code>Group.DESCENDING</code>. Default value is <code>Group.ASCENDING</code>.
207207
*/
208+
209+
// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.c
210+
208211
sort: function (index, order) {
209212
// if (typeof index === "undefined") { index = 'z'; }
210213
// if (typeof order === "undefined") { order = Phaser.Types.SORT_ASCENDING; }

src/core/LinkedList.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
Phaser.LinkedList = function () {
2+
};
3+
4+
Phaser.LinkedList.prototype = {
5+
6+
_iNext: null,
7+
_iPrev: null,
8+
first: null,
9+
last: null,
10+
sprite: { name: 'HD' },
11+
12+
add: function (child) {
13+
14+
// If the list is empty
15+
if (this.first == null && this.last == null)
16+
{
17+
this.first = child;
18+
this.last = child;
19+
this._iNext = child;
20+
child._iPrev = this;
21+
return;
22+
}
23+
24+
// Get gets appended to the end of the list, regardless of anything, and it won't have any children of its own (non-nested list)
25+
this.last._iNext = child;
26+
27+
child._iPrev = this.last;
28+
29+
this.last = child;
30+
31+
},
32+
33+
remove: function (child) {
34+
35+
// If the list is empty
36+
if (this.first == null && this.last == null)
37+
{
38+
return;
39+
}
40+
41+
// The only node?
42+
if (this.first == child && this.last == child)
43+
{
44+
this.first = null;
45+
this.last = null;
46+
this._iNext = null;
47+
child._iNext = null;
48+
child._iPrev = null;
49+
return;
50+
}
51+
52+
var childPrev = child._iPrev;
53+
54+
// Tail node?
55+
if (child._iNext)
56+
{
57+
// Has another node after it?
58+
child._iNext._iPrev = child._iPrev;
59+
}
60+
61+
childPrev._iNext = child._iNext;
62+
63+
},
64+
65+
dump: function () {
66+
67+
console.log("\nNode\t\t|\t\tNext\t\t|\t\tPrev\t\t|\t\tFirst\t\t|\t\tLast");
68+
console.log("\t\t\t|\t\t\t\t\t|\t\t\t\t\t|\t\t\t\t\t|");
69+
70+
var nameNext = '-';
71+
var namePrev = '-';
72+
var nameFirst = '-';
73+
var nameLast = '-';
74+
75+
if (this._iNext)
76+
{
77+
nameNext = this._iNext.sprite.name;
78+
}
79+
80+
if (this._iPrev)
81+
{
82+
namePrev = this._iPrev.sprite.name;
83+
}
84+
85+
if (this.first)
86+
{
87+
nameFirst = this.first.sprite.name;
88+
}
89+
90+
if (this.last)
91+
{
92+
nameLast = this.last.sprite.name;
93+
}
94+
95+
if (typeof nameNext === 'undefined')
96+
{
97+
nameNext = '-';
98+
}
99+
100+
if (typeof namePrev === 'undefined')
101+
{
102+
namePrev = '-';
103+
}
104+
105+
if (typeof nameFirst === 'undefined')
106+
{
107+
nameFirst = '-';
108+
}
109+
110+
if (typeof nameLast === 'undefined')
111+
{
112+
nameLast = '-';
113+
}
114+
115+
console.log('HD' + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast);
116+
117+
var entity = this;
118+
119+
var testObject = entity.last._iNext;
120+
entity = entity.first;
121+
122+
do
123+
{
124+
var name = entity.sprite.name || '*';
125+
var nameNext = '-';
126+
var namePrev = '-';
127+
var nameFirst = '-';
128+
var nameLast = '-';
129+
130+
if (entity._iNext)
131+
{
132+
nameNext = entity._iNext.sprite.name;
133+
}
134+
135+
if (entity._iPrev)
136+
{
137+
namePrev = entity._iPrev.sprite.name;
138+
}
139+
140+
if (entity.first)
141+
{
142+
nameFirst = entity.first.sprite.name;
143+
}
144+
145+
if (entity.last)
146+
{
147+
nameLast = entity.last.sprite.name;
148+
}
149+
150+
if (typeof nameNext === 'undefined')
151+
{
152+
nameNext = '-';
153+
}
154+
155+
if (typeof namePrev === 'undefined')
156+
{
157+
namePrev = '-';
158+
}
159+
160+
if (typeof nameFirst === 'undefined')
161+
{
162+
nameFirst = '-';
163+
}
164+
165+
if (typeof nameLast === 'undefined')
166+
{
167+
nameLast = '-';
168+
}
169+
170+
console.log(name + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast);
171+
172+
entity = entity._iNext;
173+
174+
}
175+
while(entity != testObject)
176+
177+
}
178+
179+
};

src/gameobjects/Sprite.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ Phaser.Sprite = function (game, x, y, key, frame) {
1010
// If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all
1111
this.exists = true;
1212

13-
// An "invisible" sprite isn't rendered at all
14-
this.visible = true;
15-
1613
// This is a handy little var your game can use to determine if a sprite is alive or not, it doesn't effect rendering
1714
this.alive = true;
1815

@@ -58,6 +55,8 @@ Phaser.Sprite = function (game, x, y, key, frame) {
5855
this.currentFrame = this.game.cache.getFrame(key);
5956
}
6057

58+
this.input = new Phaser.InputHandler(this);
59+
6160
/**
6261
* The anchor sets the origin point of the texture.
6362
* The default is 0,0 this means the textures origin is the top left

src/input/Input.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ Phaser.Input.prototype = {
253253
onTap: null,
254254
onHold: null,
255255

256+
// A linked list of interactive objects
257+
interactiveItems: new Phaser.LinkedList(),
258+
256259
/**
257260
* Starts the Input Manager running
258261
* @method start

src/input/InputHandler.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Phaser.InputHandler = function (sprite) {
2+
3+
this.game = sprite.game;
4+
this.sprite = sprite;
5+
6+
this.enabled = false;
7+
8+
// Linked list references
9+
this.last = this;
10+
this.first = this;
11+
12+
/**
13+
* The PriorityID controls which Sprite receives an Input event first if they should overlap.
14+
*/
15+
this.priorityID = 0;
16+
17+
this.isDragged = false;
18+
this.dragPixelPerfect = false;
19+
this.allowHorizontalDrag = true;
20+
this.allowVerticalDrag = true;
21+
this.bringToTop = false;
22+
this.snapOnDrag = false;
23+
this.snapOnRelease = false;
24+
this.snapX = 0;
25+
this.snapY = 0;
26+
27+
/**
28+
* Is this sprite allowed to be dragged by the mouse? true = yes, false = no
29+
* @default false
30+
*/
31+
this.draggable = false;
32+
33+
/**
34+
* A region of the game world within which the sprite is restricted during drag
35+
* @default null
36+
*/
37+
this.boundsRect = null;
38+
39+
/**
40+
* An Sprite the bounds of which this sprite is restricted during drag
41+
* @default null
42+
*/
43+
this.boundsSprite = null;
44+
45+
/**
46+
* If this object is set to consume the pointer event then it will stop all propogation from this object on.
47+
* For example if you had a stack of 6 sprites with the same priority IDs and one consumed the event, none of the others would receive it.
48+
* @type {bool}
49+
*/
50+
this.consumePointerEvent = false;
51+
52+
};
53+
54+
Phaser.InputHandler.prototype = {
55+
56+
game: null,
57+
sprite: null,
58+
59+
// Linked list references
60+
parent: null,
61+
_iNext: null,
62+
_iPrev: null,
63+
first: null,
64+
last: null,
65+
66+
enable: function () {
67+
},
68+
69+
};

src/physics/arcade/Body.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ Phaser.Physics.Arcade.Body.prototype = {
122122

123123
},
124124

125-
/*
126125
postUpdate: function () {
127126

128127
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
@@ -134,7 +133,6 @@ Phaser.Physics.Arcade.Body.prototype = {
134133
}
135134

136135
},
137-
*/
138136

139137
checkWorldBounds: function () {
140138

0 commit comments

Comments
 (0)