Skip to content

Commit 70ee753

Browse files
committed
The complete Phaser Input Manager suite is now ported across. Not tested in earnest yet, but all the grunt work is at least done.
1 parent f9aa7f7 commit 70ee753

17 files changed

Lines changed: 5531 additions & 228 deletions

Phaser.sublime-workspace

Lines changed: 1045 additions & 214 deletions
Large diffs are not rendered by default.

examples/assets/fonts/desyrel.png

303 KB
Loading

examples/assets/fonts/desyrel.xml

Lines changed: 1922 additions & 0 deletions
Large diffs are not rendered by default.
72.3 KB
Loading

examples/camera1.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.AUTO, '', { preload: preload, create: create, update: update });
16+
17+
function preload() {
18+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
19+
}
20+
21+
function create() {
22+
23+
game.add.sprite(0, 0, 'mushroom');
24+
game.add.sprite(400, 0, 'mushroom');
25+
game.add.sprite(800, 0, 'mushroom');
26+
27+
}
28+
29+
function update() {
30+
}
31+
32+
})();
33+
34+
</script>
35+
36+
</body>
37+
</html>

examples/input1.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.AUTO, '', { preload: preload, create: create, update: update });
16+
17+
function preload() {
18+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
19+
}
20+
21+
function create() {
22+
23+
game.add.sprite(0, 0, 'mushroom');
24+
25+
game.input.onDown.add(newMushroom, this);
26+
27+
}
28+
29+
function newMushroom(pointer) {
30+
game.add.sprite(pointer.x, pointer.y, 'mushroom');
31+
}
32+
33+
function update() {
34+
}
35+
36+
})();
37+
38+
</script>
39+
40+
</body>
41+
</html>

examples/js.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<script src="../src/pixi/utils/Polyk.js"></script>
4343
<script src="../src/pixi/utils/Utils.js"></script>
4444

45+
<script src="../src/core/Camera.js"></script>
4546
<script src="../src/core/State.js"></script>
4647
<script src="../src/core/StateManager.js"></script>
4748
<script src="../src/core/Signal.js"></script>
@@ -51,25 +52,42 @@
5152
<script src="../src/core/Stage.js"></script>
5253
<script src="../src/core/World.js"></script>
5354
<script src="../src/core/Game.js"></script>
55+
56+
<script src="../src/input/Input.js"></script>
57+
<script src="../src/input/Keyboard.js"></script>
58+
<script src="../src/input/Mouse.js"></script>
59+
<script src="../src/input/MSPointer.js"></script>
60+
<script src="../src/input/Pointer.js"></script>
61+
<script src="../src/input/Touch.js"></script>
62+
63+
<script src="../src/system/Canvas.js"></script>
5464
<script src="../src/gameobjects/GameObjectFactory.js"></script>
5565
<script src="../src/gameobjects/Sprite.js"></script>
66+
5667
<script src="../src/system/Canvas.js"></script>
5768
<script src="../src/system/Device.js"></script>
5869
<script src="../src/system/RequestAnimationFrame.js"></script>
70+
5971
<script src="../src/math/RandomDataGenerator.js"></script>
6072
<script src="../src/math/Math.js"></script>
73+
6174
<script src="../src/geom/Circle.js"></script>
6275
<script src="../src/geom/Point.js"></script>
6376
<script src="../src/geom/Rectangle.js"></script>
77+
6478
<script src="../src/net/Net.js"></script>
79+
6580
<script src="../src/tween/TweenManager.js"></script>
6681
<script src="../src/tween/Tween.js"></script>
6782
<script src="../src/tween/Easing.js"></script>
83+
6884
<script src="../src/time/Time.js"></script>
85+
6986
<script src="../src/animation/AnimationManager.js"></script>
7087
<script src="../src/animation/Animation.js"></script>
7188
<script src="../src/animation/Frame.js"></script>
7289
<script src="../src/animation/FrameData.js"></script>
7390
<script src="../src/animation/Parser.js"></script>
91+
7492
<script src="../src/loader/Cache.js"></script>
7593
<script src="../src/loader/Loader.js"></script>

src/core/Camera.js

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/**
2+
* Phaser - Camera
3+
*
4+
* A Camera is your view into the game world. It has a position and size and renders only those objects within its field of view.
5+
* The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y
6+
*/
7+
8+
Phaser.Camera = function (game, id, x, y, width, height) {
9+
10+
this.game = game;
11+
this.world = game.world;
12+
this.id = 0; // reserved for future multiple camera set-ups
13+
14+
// The view into the world we wish to render (by default the game dimensions)
15+
// The x/y values are in world coordinates, not screen coordinates, the width/height is how many pixels to render
16+
// Objects outside of this view are not rendered (unless set to ignore the Camera, i.e. UI?)
17+
this.view = new Phaser.Rectangle(x, y, width, height);
18+
19+
};
20+
21+
// Consts
22+
Phaser.Camera.FOLLOW_LOCKON = 0;
23+
Phaser.Camera.FOLLOW_PLATFORMER = 1;
24+
Phaser.Camera.FOLLOW_TOPDOWN = 2;
25+
Phaser.Camera.FOLLOW_TOPDOWN_TIGHT = 3;
26+
27+
Phaser.Camera.prototype = {
28+
29+
game: null,
30+
world: null,
31+
32+
id: 0,
33+
34+
/**
35+
* Camera view.
36+
* @type {Rectangle}
37+
*/
38+
view: null,
39+
40+
/**
41+
* Sprite moving inside this Rectangle will not cause camera moving.
42+
* @type {Rectangle}
43+
*/
44+
deadzone: null,
45+
46+
/**
47+
* Whether this camera is visible or not. (default is true)
48+
* @type {bool}
49+
*/
50+
visible: true,
51+
52+
/**
53+
* If the camera is tracking a Sprite, this is a reference to it, otherwise null
54+
* @type {Sprite}
55+
*/
56+
target: null,
57+
58+
/**
59+
* Tells this camera which sprite to follow.
60+
* @param target {Sprite} The object you want the camera to track. Set to null to not follow anything.
61+
* @param [style] {number} Leverage one of the existing "deadzone" presets. If you use a custom deadzone, ignore this parameter and manually specify the deadzone after calling follow().
62+
*/
63+
follow: function (target, style) {
64+
65+
if (typeof style === "undefined") { style = Phaser.Camera.FOLLOW_LOCKON; }
66+
67+
this.target = target;
68+
69+
var helper;
70+
71+
switch (style) {
72+
73+
case Phaser.Types.CAMERA_FOLLOW_PLATFORMER:
74+
var w = this.width / 8;
75+
var h = this.height / 3;
76+
this.deadzone = new Phaser.Rectangle((this.width - w) / 2, (this.height - h) / 2 - h * 0.25, w, h);
77+
break;
78+
79+
case Phaser.Types.CAMERA_FOLLOW_TOPDOWN:
80+
helper = Math.max(this.width, this.height) / 4;
81+
this.deadzone = new Phaser.Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper);
82+
break;
83+
84+
case Phaser.Types.CAMERA_FOLLOW_TOPDOWN_TIGHT:
85+
helper = Math.max(this.width, this.height) / 8;
86+
this.deadzone = new Phaser.Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper);
87+
break;
88+
89+
case Phaser.Types.CAMERA_FOLLOW_LOCKON:
90+
default:
91+
this.deadzone = null;
92+
break;
93+
}
94+
95+
},
96+
97+
/**
98+
* Move the camera focus to this location instantly.
99+
* @param x {number} X position.
100+
* @param y {number} Y position.
101+
*/
102+
focusOnXY: function (x, y) {
103+
104+
x += (x > 0) ? 0.0000001 : -0.0000001;
105+
y += (y > 0) ? 0.0000001 : -0.0000001;
106+
107+
this.view.x = Math.round(x - this.view.halfWidth);
108+
this.view.y = Math.round(y - this.view.halfHeight);
109+
110+
},
111+
112+
/**
113+
* Update focusing and scrolling.
114+
*/
115+
update: function () {
116+
117+
// this.plugins.preUpdate();
118+
119+
if (this.target !== null)
120+
{
121+
if (this.deadzone == null)
122+
{
123+
this.focusOnXY(this.target.x, this.target.y);
124+
}
125+
else
126+
{
127+
var edge;
128+
var targetX = this.target.x + ((this.target.x > 0) ? 0.0000001 : -0.0000001);
129+
var targetY = this.target.y + ((this.target.y > 0) ? 0.0000001 : -0.0000001);
130+
131+
edge = targetX - this.deadzone.x;
132+
133+
if (this.view.x > edge)
134+
{
135+
this.view.x = edge;
136+
}
137+
138+
edge = targetX + this.target.width - this.deadzone.x - this.deadzone.width;
139+
140+
if (this.view.x < edge)
141+
{
142+
this.view.x = edge;
143+
}
144+
145+
edge = targetY - this.deadzone.y;
146+
147+
if (this.view.y > edge)
148+
{
149+
this.view.y = edge;
150+
}
151+
152+
edge = targetY + this.target.height - this.deadzone.y - this.deadzone.height;
153+
154+
if (this.view.y < edge)
155+
{
156+
this.view.y = edge;
157+
}
158+
}
159+
}
160+
161+
// Make sure we didn't go outside the cameras worldBounds
162+
if (this.view.x < this.world.bounds.left)
163+
{
164+
this.view.x = this.world.bounds.left;
165+
}
166+
167+
if (this.view.x > this.world.bounds.right - this.width)
168+
{
169+
this.view.x = (this.world.bounds.right - this.width) + 1;
170+
}
171+
172+
if (this.view.y < this.world.bounds.top)
173+
{
174+
this.view.y = this.world.bounds.top;
175+
}
176+
177+
if (this.view.y > this.world.bounds.bottom - this.height)
178+
{
179+
this.view.y = (this.world.bounds.bottom - this.height) + 1;
180+
}
181+
182+
this.view.floor();
183+
184+
// this.plugins.update();
185+
186+
},
187+
188+
setPosition: function (x, y) {
189+
190+
this.view.x = x;
191+
this.view.y = y;
192+
193+
},
194+
195+
setSize: function (width, height) {
196+
197+
this.view.width = width;
198+
this.view.height = height;
199+
200+
}
201+
202+
};
203+
204+
Object.defineProperty(Phaser.Camera.prototype, "x", {
205+
206+
get: function () {
207+
return this.view.x;
208+
},
209+
210+
set: function (value) {
211+
this.view.x = value;
212+
},
213+
214+
enumerable: true,
215+
configurable: true
216+
});
217+
218+
Object.defineProperty(Phaser.Camera.prototype, "y", {
219+
220+
get: function () {
221+
return this.view.y;
222+
},
223+
224+
set: function (value) {
225+
this.view.y = value;
226+
},
227+
228+
enumerable: true,
229+
configurable: true
230+
});
231+
232+
Object.defineProperty(Phaser.Camera.prototype, "width", {
233+
234+
get: function () {
235+
return this.view.width;
236+
},
237+
238+
set: function (value) {
239+
this.view.width = value;
240+
},
241+
242+
enumerable: true,
243+
configurable: true
244+
});
245+
246+
Object.defineProperty(Phaser.Camera.prototype, "height", {
247+
248+
get: function () {
249+
return this.view.height;
250+
},
251+
252+
set: function (value) {
253+
this.view.height = value;
254+
},
255+
256+
enumerable: true,
257+
configurable: true
258+
});

0 commit comments

Comments
 (0)