Shake monsters when damaged
[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 damage(amount) {
165 super.damage(amount);
166 if (this.alive) { game.add.tween(this).to({ x: this.x + (Math.random() - 0.5) * 10 }, 10, Phaser.Easing.Sinusoidal.InOut, true, 0, 8, true); }
167 }
168
169 kill() {
170 super.kill();
171 player.loveUp();
172 }
173
174 overlap_player(player) {
175 player.damage(10);
176 player.switchmode(MODE_DETERMINATION);
177 }
178
179 update() {
180 super.update();
181 if (this.alive && player.alive && (game.time.now > this.nexttime_shoot))
182 {
183 this.weapon.fireAtSprite(player);
184 this.nexttime_shoot = game.time.now + (WAIT_MONSTERSHOOT / 2) + (Math.random() * WAIT_MONSTERSHOOT);
185 }
186 game.physics.arcade.overlap(player, this.weapon.bullets, function(p,b) { b.overlap_player(p); });
187 }
188 }
189
190 class Determination extends Phaser.Sprite {
191 constructor(x, y) {
192 super(game, x, y, 'determination');
193 this.anchor.setTo(0.5, 0.5);
194 }
195
196 enablePhysics() {
197 game.physics.arcade.enable(this);
198 }
199 }
200
201 class Justice extends Phaser.Bullet {
202 constructor(game, x, y, key, frame) {
203 super(game, x, y, 'projectile');
204 this.anchor.setTo(0.5, 0.5);
205 }
206
207 overlap_monster(entity) {
208 entity.damage(player.love);
209 this.kill();
210 }
211
212 overlap_player(player) {
213 player.damage(player.love);
214 this.kill();
215 }
216 }
217
218 class JusticeBlaster extends Phaser.Weapon {
219 constructor(game, parent) {
220 super(game, parent);
221 this.bulletClass = Justice;
222 this.multiFire = true;
223 this.createBullets(100, null);
224 }
225 }
226
227 class HealthBar {
228 constructor(x, y) {
229 this.mhealth = game.add.tileSprite(x + 10, y, 0, HB_THICKNESS, 'wall');
230 this.ahealth = game.add.tileSprite(x + 10, y, 0, HB_THICKNESS, 'wall');
231 this.mhealth.anchor.setTo(0, 0.5);
232 this.ahealth.anchor.setTo(0, 0.5);
233 this.mhealth.tint = '0xff0000';
234 this.ahealth.tint = '0xffff00';
235 this.text_love = new Phaser.Text(game, x - 50, y + 2, null, { align: 'center', fill: 'white', font: 'Ubuntu Mono', fontSize: 16, fontWeight: 'bold' });
236 this.text_love.anchor.setTo(0, 0.5);
237 game.add.existing(this.text_love);
238 }
239
240 update(love, mhealth, ahealth) {
241 this.mhealth.width = mhealth;
242 this.ahealth.width = ahealth;
243 this.text_love.text = "LV " + love;
244 }
245 }
246
247 function preload () {
248
249 game.load.image('wall', 'wall.png');
250 game.load.image('player', 'player.png');
251 game.load.image('monster', 'monster.png');
252 game.load.image('determination', 'determination.png');
253 game.load.image('projectile', 'projectile.png');
254
255 }
256
257 function create () {
258
259 game.world.setBounds(0, 0, 800, 600);
260 game.stage.backgroundColor = '#000000';
261 game.physics.startSystem(Phaser.Physics.ARCADE);
262
263 walls = game.add.group();
264 walls.classType = Phaser.TileSprite;
265 walls.enableBody = true;
266
267 walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
268 walls.add(game.add.tileSprite(WALL_BORDER, game.world.height - WALL_THICKNESS - WALL_BORDERBOTTOM, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
269 walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
270 walls.add(game.add.tileSprite(game.world.width - WALL_THICKNESS - WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
271 walls.children.forEach(function(wall) { wall.body.immovable = true; });
272
273 determinations = game.add.group();
274 nexttime_determination = Math.random() * WAIT_DETERMINATION;
275
276 monsters = game.add.group();
277 nexttime_monsterspawn = WAIT_MONSTERSPAWN / 4;
278
279 player = new Player(game.world.width / 2, game.world.height / 2);
280 player.enablePhysics();
281 game.add.existing(player);
282 game.camera.follow(player);
283 cursors = game.input.keyboard.createCursorKeys();
284
285 }
286
287 function update () {
288
289 game.physics.arcade.collide(player, walls);
290 game.physics.arcade.overlap(player, determinations, function(p,d) { p.overlap_determination(d); });
291 game.physics.arcade.overlap(player, monsters, function(p,m) { m.overlap_player(p); });
292
293 if (game.time.now > nexttime_determination)
294 {
295 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));
296 determination.enablePhysics();
297 determinations.add(determination);
298 nexttime_determination = game.time.now + (WAIT_DETERMINATION / 2) + (Math.random() * WAIT_DETERMINATION);
299 }
300
301 if (game.time.now > nexttime_monsterspawn)
302 {
303 var monster = new Monster(Math.random() * game.world.width, Math.random() * game.world.height);
304 monster.enablePhysics();
305 monsters.add(monster);
306 var waittime = WAIT_MONSTERSPAWN - ((Math.random() * 2000) * player.love);
307 nexttime_monsterspawn = game.time.now + waittime;
308 console.log('Next monster in ' + waittime);
309 }
310
311 if ((game.time.now - player.lastmovetime) > WAIT_KEY) player.body.velocity.x = player.body.velocity.y = 0;
312
313 }