Fix healthbar not showing 0 when killed
[ld40.git] / ld40.js
1 var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
2 var player;
3 var walls;
4 var determinations; // I know it's grammatically incorrect. :P
5 var monsters;
6 var nexttime_monsterspawn;
7 var nexttime_determination;
8
9 const WAIT_KEY = 200;
10 const WAIT_SHOOT = 200;
11 const WAIT_INVINCIBILITY = 800;
12 const WAIT_SOULMODE = 10000;
13 const WAIT_MONSTERSHOOT = 500;
14 const WAIT_MONSTERSPAWN = 20000;
15 const WAIT_DETERMINATION = 4000;
16 const WALL_THICKNESS = 16;
17 const WALL_BORDER = 8;
18 const WALL_BORDERBOTTOM = 48;
19 const HB_THICKNESS = 20;
20 const MAX_HEALTH = 20;
21 const MONSTER_HEALTH = 20;
22 const MODE_DETERMINATION = 0;
23 const MODE_JUSTICE = 1;
24 const SPEED_PLAYER = 150;
25 const SPEED_PPROJECTILE = 500;
26 const SPEED_MPROJECTILE = 200;
27
28 window.addEventListener("keydown", function(e) {
29 // Prevent default browser action for arrows and spacebar
30 if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
31 e.preventDefault();
32 }
33 }, false);
34
35
36 class Player extends Phaser.Text {
37 constructor(x, y) {
38 super(game, x, y, "♥️", { align: 'center', fill: 'red', font: 'Ubuntu Mono', fontSize: 32, fontWeight: 'bold' });
39 this.anchor.setTo(0.5, 0.5);
40 this.maxHealth = MAX_HEALTH;
41 this.health = this.maxHealth;
42 this.love = 1;
43 this.lastmovetime = 0;
44 this.lasttime_shoot = 0;
45 this.lasttime_mode = 0;
46 this.lasttime_damage = 0;
47 this.speed = SPEED_PLAYER;
48 this.switchmode(MODE_DETERMINATION);
49 this.weapon = new JusticeBlaster(game, this);
50 this.weapon.bulletSpeed = SPEED_PPROJECTILE;
51 this.healthBar = new HealthBar(60, game.world.height - (WALL_BORDERBOTTOM / 2));
52 }
53
54 enablePhysics() {
55 game.physics.arcade.enable(this);
56 this.body.bounce.y = 0.2;
57 this.body.bounce.x = 0.2;
58 this.body.collideWorldBounds = true;
59 }
60
61 overlap_determination(determination) {
62 determination.kill();
63 this.heal(2);
64 this.speed += 50;
65 this.switchmode(MODE_JUSTICE);
66 }
67
68 damage(amount) {
69 if ((game.time.now - this.lasttime_damage) > WAIT_INVINCIBILITY)
70 {
71 super.damage(amount);
72 this.lasttime_damage = game.time.now;
73 }
74 }
75
76 kill() {
77 this.health = 0;
78 this.healthBar.update(this.love, this.maxHealth, this.health);
79 super.kill();
80 }
81
82 loveUp() {
83 this.love++
84 this.maxHealth += this.love;
85 this.heal(this.love);
86 }
87
88 control() {
89 if (this.alive)
90 {
91 if (cursors.left.isDown)
92 {
93 player.body.velocity.x = -player.speed;
94 player.lastmovetime = game.time.now;
95 }
96 else if (cursors.right.isDown)
97 {
98 player.body.velocity.x = player.speed;
99 player.lastmovetime = game.time.now;
100 }
101 if (cursors.up.isDown)
102 {
103 player.body.velocity.y = -player.speed;
104 player.lastmovetime = game.time.now;
105 }
106 else if (cursors.down.isDown)
107 {
108 player.body.velocity.y = player.speed;
109 player.lastmovetime = game.time.now;
110 }
111 if ((this.mode == MODE_JUSTICE) && game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && ((game.time.now - this.lasttime_shoot) > WAIT_SHOOT))
112 {
113 this.shoot();
114 }
115 }
116 }
117
118 switchmode(soulmode) {
119 switch(soulmode)
120 {
121 case MODE_DETERMINATION:
122 this.addColor('red', 0);
123 break;
124 case MODE_JUSTICE:
125 this.addColor('yellow', 0);
126 this.lasttime_mode = game.time.now;
127 break;
128 }
129 this.mode = soulmode;
130 }
131
132 shoot() {
133 this.weapon.fire(this, 0, this.y);
134 this.weapon.fire(this, this.x, 0);
135 this.weapon.fire(this, this.x, game.world.height);
136 this.weapon.fire(this, game.world.width, this.y);
137 this.lasttime_shoot = game.time.now;
138 }
139
140 update() {
141 super.update();
142 this.control();
143 this.healthBar.update(this.love, this.maxHealth, this.health);
144 game.physics.arcade.overlap(monsters, this.weapon.bullets, function(m,b) { b.overlap_monster(m); });
145 if ((game.time.now - this.lasttime_mode) > WAIT_SOULMODE) { this.switchmode(MODE_DETERMINATION); }
146 }
147 }
148
149 class Monster extends Phaser.Sprite {
150 constructor(x, y) {
151 super(game, x, y, 'monster');
152 this.anchor.setTo(0.5, 0.5);
153 this.health = MONSTER_HEALTH;
154 this.nexttime_shoot = game.time.now;
155 this.weapon = new JusticeBlaster(game, this);
156 this.weapon.bulletSpeed = SPEED_MPROJECTILE;
157 this.weapon.trackSprite(this);
158 }
159
160 enablePhysics() {
161 game.physics.arcade.enable(this);
162 }
163
164 kill() {
165 super.kill();
166 player.loveUp();
167 }
168
169 overlap_player(player) {
170 player.damage(10);
171 player.switchmode(MODE_DETERMINATION);
172 }
173
174 update() {
175 super.update();
176 if (this.alive && player.alive && (game.time.now > this.nexttime_shoot))
177 {
178 this.weapon.fireAtSprite(player);
179 this.nexttime_shoot = game.time.now + (WAIT_MONSTERSHOOT / 2) + (Math.random() * WAIT_MONSTERSHOOT);
180 }
181 game.physics.arcade.overlap(player, this.weapon.bullets, function(p,b) { b.overlap_player(p); });
182 }
183 }
184
185 class Determination extends Phaser.Sprite {
186 constructor(x, y) {
187 super(game, x, y, 'determination');
188 this.anchor.setTo(0.5, 0.5);
189 }
190
191 enablePhysics() {
192 game.physics.arcade.enable(this);
193 }
194 }
195
196 class Justice extends Phaser.Bullet {
197 constructor(game, x, y, key, frame) {
198 super(game, x, y, 'projectile');
199 this.anchor.setTo(0.5, 0.5);
200 }
201
202 overlap_monster(entity) {
203 entity.damage(player.love);
204 this.kill();
205 }
206
207 overlap_player(player) {
208 player.damage(player.love);
209 this.kill();
210 }
211 }
212
213 class JusticeBlaster extends Phaser.Weapon {
214 constructor(game, parent) {
215 super(game, parent);
216 this.bulletClass = Justice;
217 this.multiFire = true;
218 this.createBullets(100, null);
219 }
220 }
221
222 class HealthBar {
223 constructor(x, y) {
224 this.mhealth = game.add.tileSprite(x + 10, y, 0, HB_THICKNESS, 'wall');
225 this.ahealth = game.add.tileSprite(x + 10, y, 0, HB_THICKNESS, 'wall');
226 this.mhealth.anchor.setTo(0, 0.5);
227 this.ahealth.anchor.setTo(0, 0.5);
228 this.mhealth.tint = '0xff0000';
229 this.ahealth.tint = '0xffff00';
230 this.text_love = new Phaser.Text(game, x - 50, y + 2, null, { align: 'center', fill: 'white', font: 'Ubuntu Mono', fontSize: 16, fontWeight: 'bold' });
231 this.text_love.anchor.setTo(0, 0.5);
232 game.add.existing(this.text_love);
233 }
234
235 update(love, mhealth, ahealth) {
236 this.mhealth.width = mhealth;
237 this.ahealth.width = ahealth;
238 this.text_love.text = "LV " + love;
239 }
240 }
241
242 function preload () {
243
244 game.load.image('wall', 'wall.png');
245 game.load.image('player', 'player.png');
246 game.load.image('monster', 'monster.png');
247 game.load.image('determination', 'determination.png');
248 game.load.image('projectile', 'projectile.png');
249
250 }
251
252 function create () {
253
254 game.world.setBounds(0, 0, 800, 600);
255 game.stage.backgroundColor = '#000000';
256 game.physics.startSystem(Phaser.Physics.ARCADE);
257
258 walls = game.add.group();
259 walls.classType = Phaser.TileSprite;
260 walls.enableBody = true;
261
262 walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
263 walls.add(game.add.tileSprite(WALL_BORDER, game.world.height - WALL_THICKNESS - WALL_BORDERBOTTOM, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
264 walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
265 walls.add(game.add.tileSprite(game.world.width - WALL_THICKNESS - WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
266 walls.children.forEach(function(wall) { wall.body.immovable = true; });
267
268 determinations = game.add.group();
269 nexttime_determination = Math.random() * WAIT_DETERMINATION;
270
271 monsters = game.add.group();
272 nexttime_monsterspawn = WAIT_MONSTERSPAWN / 4;
273
274 player = new Player(game.world.width / 2, game.world.height / 2);
275 player.enablePhysics();
276 game.add.existing(player);
277 game.camera.follow(player);
278 cursors = game.input.keyboard.createCursorKeys();
279
280 }
281
282 function update () {
283
284 game.physics.arcade.collide(player, walls);
285 game.physics.arcade.overlap(player, determinations, function(p,d) { p.overlap_determination(d); });
286 game.physics.arcade.overlap(player, monsters, function(p,m) { m.overlap_player(p); });
287
288 if (game.time.now > nexttime_determination)
289 {
290 var determination = new Determination((Math.random() * (game.world.width - ((WALL_THICKNESS + WALL_BORDER) * 4))) + ((WALL_THICKNESS + WALL_BORDER) * 2), (Math.random() * (game.world.height - ((WALL_THICKNESS + WALL_BORDER) * 4) - WALL_BORDERBOTTOM)) + ((WALL_THICKNESS + WALL_BORDER) * 2));
291 determination.enablePhysics();
292 determinations.add(determination);
293 nexttime_determination = game.time.now + (WAIT_DETERMINATION / 2) + (Math.random() * WAIT_DETERMINATION);
294 }
295
296 if (game.time.now > nexttime_monsterspawn)
297 {
298 var monster = new Monster(Math.random() * game.world.width, Math.random() * game.world.height);
299 monster.enablePhysics();
300 monsters.add(monster);
301 var waittime = WAIT_MONSTERSPAWN - ((Math.random() * 2000) * player.love);
302 nexttime_monsterspawn = game.time.now + waittime;
303 console.log('Next monster in ' + waittime);
304 }
305
306 if ((game.time.now - player.lastmovetime) > WAIT_KEY) player.body.velocity.x = player.body.velocity.y = 0;
307
308 }