-
Notifications
You must be signed in to change notification settings - Fork 4
/
crates.html
190 lines (158 loc) · 5.34 KB
/
crates.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Phaser - Opening some Crates</title>
<script type="text/javascript" src="js/phaser.min.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
<script type="text/javascript" >
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
function preload() {
game.load.image('sky', 'assets/sky.png');
game.load.image('ground', 'assets/platform.png');
game.load.image('key', 'assets/key.png');
game.load.spritesheet('crate', 'assets/crate.png', 32, 32);
game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
}
var player;
var platforms;
var cursors;
var keys;
var crates;
var colours = [
"b65611",
"60ac39",
"1fad83",
"b854d4",
"d43552"
];
function create() {
// We're going to be using physics, so enable the Arcade Physics system
game.physics.startSystem(Phaser.Physics.ARCADE);
// A simple background for our game
game.add.sprite(0, 0, 'sky');
// The platforms group contains the ground and the 2 ledges we can jump on
platforms = game.add.group();
// We will enable physics for any object that is created in this group
platforms.enableBody = true;
// Here we create the ground.
var ground = platforms.create(0, game.world.height - 64, 'ground');
// Scale it to fit the width of the game (the original sprite is 400x32 in size)
ground.scale.setTo(2, 2);
// This stops it from falling away when you jump on it
ground.body.immovable = true;
// Now let's create two ledges
var ledge = platforms.create(400, 400, 'ground');
ledge.body.immovable = true;
ledge = platforms.create(-150, 250, 'ground');
ledge.body.immovable = true;
// The player and its settings
player = game.add.sprite(32, game.world.height - 150, 'dude');
player.inventory = [];
// We need to enable physics on the player
game.physics.arcade.enable(player);
// Player physics properties. Give the little guy a slight bounce.
player.body.bounce.y = 0.1;
player.body.gravity.y = 400;
player.body.collideWorldBounds = true;
// Our two animations, walking left and right.
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
crates = game.add.group();
crates.enableBody = true;
keys = game.add.group();
keys.enableBody = true;
// Here we'll create 12 of them evenly spaced apart
for (var i = 0; i < colours.length; i++)
{
var key = keys.create(i * 180, 0, 'key');
key.body.gravity.y = 800;
key.body.bounce.y = 0.4 + Math.random() * 0.1;
key.tint = "0x" + colours[i];
key.id = i;
var crate = crates.create((Math.random() * 700 + 100), (Math.random() * 400), 'crate');
crate.body.gravity.y = 500;
crate.body.immovable = false;
crate.animations.add('open', [0, 1, 2, 3], 8, true);
crate.isClosed = true;
crate.rotation = Math.random();
crate.key = key;
crate.tint = "0x" + colours[i];
crate.id = i;
}
// Our controls.
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
// Collide the player and the keys with the platforms
game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(keys, platforms);
game.physics.arcade.collide(crates, platforms);
// Crates are stackable because I'm lazy
game.physics.arcade.collide(crates, crates);
game.physics.arcade.overlap(player, keys, collectKey, null, this);
game.physics.arcade.overlap(player, crates, openCrate, null, this);
// Reset the players velocity (movement)
player.body.velocity.x = 0;
if (cursors.left.isDown)
{
// Move to the left
player.body.velocity.x = -250;
player.animations.play('left');
}
else if (cursors.right.isDown)
{
// Move to the right
player.body.velocity.x = 250;
player.animations.play('right');
}
else
{
// Stand still
player.animations.stop();
player.frame = 4;
}
// Allow the player to jump if they are touching the ground.
if (cursors.up.isDown && player.body.touching.down)
{
player.body.velocity.y = -350;
}
// Check if a crate is opened. (or at least animated. yuck!)
crates.children.map(function(crate) {
if (crate.animations.currentFrame.index > 3) {
crate.kill();
}
})
}
function openCrate (player, crate) {
if((crate.isClosed) &&
(player.inventory.indexOf(crate.key.id) != -1)
) {
crate.body.velocity.y = -350;
crate.animations.play('open');
crate.lifespan = 300;
crate.isClosed = false;
}
else {
// How about a nice animation?
}
}
function collectKey (player, key) {
player.inventory.push(key.id);
key.kill();
game.add.text(
(30 * player.inventory.length) - 20,
16,
'⚿',
{ fontSize: '32px', fill: colours[key.id] }
);
}
</script>
</body>
</html>