Skip to content

Commit d620fc7

Browse files
committed
Camera tests, some of them not finished because of bugs.
1 parent 08fb303 commit d620fc7

10 files changed

Lines changed: 293 additions & 88 deletions
2.46 KB
Loading
8.26 KB
Loading

Tests/cameras/basic follow.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/// <reference path="../../Phaser/Game.ts" />
2+
(function () {
3+
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update);
4+
5+
var ufo;
6+
var speed = 4;
7+
8+
function init() {
9+
game.world.setSize(1280, 600, true);
10+
game.load.image('ground', 'assets/tests/ground-2x.png');
11+
game.load.image('river', 'assets/tests/river-2x.png');
12+
game.load.image('sky', 'assets/tests/sky-2x.png');
13+
game.load.image('cloud0', 'assets/tests/cloud-big-2x.png');
14+
game.load.image('cloud1', 'assets/tests/cloud-narrow-2x.png');
15+
game.load.image('cloud2', 'assets/tests/cloud-small-2x.png');
16+
17+
game.load.spritesheet('ufo', 'assets/sprites/ufo.png', 24, 21);
18+
19+
game.load.start();
20+
}
21+
function create() {
22+
// background images
23+
game.add.sprite(0, 0, 'sky')
24+
.transform.scrollFactor.setTo(0, 0);
25+
game.add.sprite(0, 360, 'ground')
26+
.transform.scrollFactor.setTo(0.5, 0.5);
27+
game.add.sprite(0, 400, 'river')
28+
.transform.scrollFactor.setTo(1.3, 1.3);
29+
game.add.sprite(200, 120, 'cloud0')
30+
.transform.scrollFactor.setTo(0.3, 0.3);
31+
game.add.sprite(-60, 120, 'cloud1')
32+
.transform.scrollFactor.setTo(0.5, 0.3);
33+
game.add.sprite(900, 170, 'cloud2')
34+
.transform.scrollFactor.setTo(0.7, 0.3);
35+
36+
// ufo spirte
37+
ufo = game.add.sprite(320, 240, 'ufo');
38+
ufo.animations.add('fly', null, 30, false);
39+
ufo.animations.play('fly');
40+
ufo.transform.origin.setTo(0.5, 0.5);
41+
42+
// make camera follows ufo
43+
game.camera.follow(ufo);
44+
}
45+
function update() {
46+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
47+
ufo.x -= speed;
48+
ufo.rotation = -15;
49+
}
50+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
51+
ufo.x += speed;
52+
ufo.rotation = 15;
53+
}
54+
else {
55+
ufo.rotation = 0;
56+
}
57+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
58+
ufo.y -= speed;
59+
}
60+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
61+
ufo.y += speed;
62+
}
63+
}
64+
})();

Tests/cameras/camera fade.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@
1919
circle3 = game.add.sprite(221, 318, 'magenta');
2020

2121
circle1.input.start(0, false, true);
22+
circle1.events.onInputUp.add(fade1, this);
2223

23-
fx = game.camera.fx.add(Phaser.FX.Camera.Shake);
24+
fx = game.camera.fx.add(Phaser.FX.Camera.Fade);
2425
}
2526
function update() {
26-
if (circle1.input.justReleased(0, 20)) {
27-
console.log('pressed');
28-
fx.start(0.05, 0.5, function() {
29-
console.log('fin');
30-
});
31-
}
3227
}
3328
function render() {
29+
game.camera.fx.render(game.camera,
30+
game.camera.x, game.camera.y,
31+
game.camera.width, game.camera.height);
32+
game.camera.fx.postRender(game.camera,
33+
game.camera.x, game.camera.y,
34+
game.camera.width, game.camera.height);
35+
}
36+
function fade1(pointer) {
37+
console.log('pressed');
38+
fx.start(0.05, 0.5, function() {
39+
console.log('fin');
40+
});
3441
}
3542
})();

Tests/cameras/camera texture.js

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,19 @@
11
/// <reference path="../../Phaser/Game.ts" />
22
(function () {
3-
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
4-
5-
var radar;
6-
var ships = [];
7-
8-
var button;
3+
var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render);
94

105
function init() {
116
game.world.setSize(800, 600, true);
12-
game.load.image('radar-surface', 'assets/tests/radar-surface.png');
13-
game.load.image('ship', 'assets/sprites/asteroids_ship_white.png');
14-
game.load.image('enemy-ship', 'assets/sprites/asteroids_ship.png');
15-
16-
game.load.image('button', 'assets/tests/320x200.png');
7+
game.load.image('background', 'assets/misc/water_texture.jpg');
178

189
game.load.start();
1910
}
2011
function create() {
21-
for (var i = 0; i < 4; i++) {
22-
ships.push(game.add.sprite(100 + i * 10, 300 + i * 16, 'ship'));
23-
}
24-
ships.push(game.add.sprite(160, 320, 'enemy-ship'));
25-
radar = game.add.sprite(0, 0, 'radar-surface');
26-
27-
game.camera.setSize(400, 600);
28-
var camera2 = game.add.camera(0, 0, 400, 600);
29-
camera2.x = 400;
30-
31-
button = game.add.sprite(500, 100, 'button');
32-
button.input.start(0, false, true);
33-
}
34-
function update() {
35-
if (button.input.justReleased(0, 20)) {
36-
}
37-
38-
for (var i = 0; i < ships.length; i++) {
39-
ships[i].x += 1;
40-
if (ships[i].x > 400) {
41-
ships[i].x = 40;
42-
}
43-
}
12+
game.camera.texture.loadImage('background', false);
4413
}
4514
function render() {
15+
Phaser.DebugUtils.context.fillStyle = 'rgb(255, 255, 255)';
16+
Phaser.DebugUtils.context.fillText('Draw background image using camera.texture property.',
17+
196, 320);
4618
}
4719
})();

Tests/cameras/edit-template.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

Tests/cameras/follow styles.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/// <reference path="../../Phaser/Game.ts" />
2+
(function () {
3+
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
4+
5+
var ufo,
6+
speed = 4;
7+
8+
var btn0, btn1, btn2, btn3;
9+
var style = 'default';
10+
11+
function init() {
12+
game.world.setSize(1280, 800, true);
13+
game.load.image('ground', 'assets/tests/ground-2x.png');
14+
game.load.image('river', 'assets/tests/river-2x.png');
15+
game.load.image('sky', 'assets/tests/sky-2x.png');
16+
game.load.image('cloud0', 'assets/tests/cloud-big-2x.png');
17+
game.load.image('cloud1', 'assets/tests/cloud-narrow-2x.png');
18+
game.load.image('cloud2', 'assets/tests/cloud-small-2x.png');
19+
20+
game.load.spritesheet('button', 'assets/buttons/follow-style-button.png', 224, 70);
21+
22+
game.load.spritesheet('ufo', 'assets/sprites/ufo.png', 24, 21);
23+
24+
game.load.start();
25+
}
26+
function create() {
27+
// background images
28+
game.add.sprite(0, 0, 'sky')
29+
.transform.scrollFactor.setTo(0, 0);
30+
game.add.sprite(0, 360, 'ground')
31+
.transform.scrollFactor.setTo(0.5, 0.1);
32+
game.add.sprite(0, 400, 'river')
33+
.transform.scrollFactor.setTo(1.3, 0.16);
34+
game.add.sprite(200, 120, 'cloud0')
35+
.transform.scrollFactor.setTo(0.3, 0.1);
36+
game.add.sprite(-60, 120, 'cloud1')
37+
.transform.scrollFactor.setTo(0.5, 0.1);
38+
game.add.sprite(900, 170, 'cloud2')
39+
.transform.scrollFactor.setTo(0.7, 0.1);
40+
// ufo spirte
41+
ufo = game.add.sprite(360, 240, 'ufo');
42+
ufo.animations.add('fly', null, 30, false);
43+
ufo.animations.play('fly');
44+
ufo.transform.origin.setTo(0.5, 0.5);
45+
46+
// make camera follows ufo
47+
game.camera.follow(ufo);
48+
49+
// follow style switch buttons
50+
btn0 = game.add.button(16, 40, 'button', lockonFollow, 0, 0, 0);
51+
btn1 = game.add.button(16, 120, 'button', platformerFollow, 1, 1, 1);
52+
btn2 = game.add.button(16, 200, 'button', topdownFollow, 2, 2, 2);
53+
btn3 = game.add.button(16, 280, 'button', topdownTightFollow, 3, 3, 3);
54+
}
55+
function update() {
56+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
57+
ufo.x -= speed;
58+
ufo.rotation = -15;
59+
}
60+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
61+
ufo.x += speed;
62+
ufo.rotation = 15;
63+
}
64+
else {
65+
ufo.rotation = 0;
66+
}
67+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
68+
ufo.y -= speed;
69+
}
70+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
71+
ufo.y += speed;
72+
}
73+
}
74+
function render() {
75+
if (game.camera.deadzone) {
76+
Phaser.DebugUtils.renderRectangle(game.camera.deadzone, 'rgba(240, 112, 111, 0.4)');
77+
}
78+
// game.camera.renderDebugInfo(400, 16);
79+
Phaser.DebugUtils.context.fillStyle = '#fff';
80+
Phaser.DebugUtils.context.fillText('Click buttons to switch between different styles.', 360, 32);
81+
Phaser.DebugUtils.context.fillText('Current style: ' + style, 360, 48);
82+
}
83+
function lockonFollow() {
84+
game.camera.follow(ufo, Phaser.Camera.STYLE_LOCKON);
85+
style = 'STYLE_LOCKON';
86+
}
87+
function platformerFollow() {
88+
game.camera.follow(ufo, Phaser.Camera.STYLE_PLATFORMER);
89+
style = 'STYLE_PLATFORMER';
90+
}
91+
function topdownFollow() {
92+
game.camera.follow(ufo, Phaser.Camera.STYLE_TOPDOWN);
93+
style = 'STYLE_TOPDOWN';
94+
}
95+
function topdownTightFollow() {
96+
game.camera.follow(ufo, Phaser.Camera.STYLE_TOPDOWN_TIGHT);
97+
style = 'STYLE_TOPDOWN_TIGHT';
98+
}
99+
})();

Tests/cameras/multi camera.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// <reference path="../../Phaser/Game.ts" />
2+
(function () {
3+
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
4+
5+
var zombieCamera;
6+
7+
var zombie;
8+
var walkSpeed = 2,
9+
direction = 1;
10+
11+
function init() {
12+
game.world.setSize(1280, 600, true);
13+
game.load.image('ground', 'assets/tests/ground-2x.png');
14+
game.load.image('river', 'assets/tests/river-2x.png');
15+
game.load.image('sky', 'assets/tests/sky-2x.png');
16+
17+
game.load.spritesheet('zombie', 'assets/sprites/metalslug_monster39x40.png', 39, 40);
18+
19+
game.load.start();
20+
}
21+
function create() {
22+
// background images
23+
game.add.sprite(0, 0, 'sky');
24+
game.add.sprite(0, 360, 'ground');
25+
game.add.sprite(0, 400, 'river');
26+
27+
// zombie spirte
28+
zombie = game.add.sprite(480, 336, 'zombie');
29+
zombie.animations.add('walk', null, 30, true);
30+
zombie.animations.play('walk');
31+
32+
// create a small camera which looks at the zombie
33+
zombieCamera = game.add.camera(0, 0, 800, 600);
34+
zombieCamera.x = 420;
35+
zombieCamera.y = 240;
36+
zombieCamera.setPosition(0, 0);
37+
zombieCamera.setSize(200, 200);
38+
}
39+
function update() {
40+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
41+
zombieCamera.x -= 2;
42+
}
43+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
44+
zombieCamera.x += 2;
45+
}
46+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
47+
zombieCamera.y -= 2;
48+
}
49+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
50+
zombieCamera.y += 2;
51+
}
52+
// zombie wandering update
53+
zombie.x += walkSpeed * direction;
54+
if (zombie.x > 540 || zombie.x < 440) {
55+
direction *= -1;
56+
zombie.transform.scale.setTo(direction, 1);
57+
}
58+
}
59+
function render() {
60+
game.camera.renderDebugInfo(32, 32);
61+
zombieCamera.renderDebugInfo(32, 128);
62+
}
63+
})();

0 commit comments

Comments
 (0)