Merge branches 'cleaning' and 'balancing'
[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 loveUp() {
77 this.love++
78 this.maxHealth += this.love;
79 this.heal(this.love);
80 }
81
82 control() {
83 if (this.alive)
84 {
85 if (cursors.left.isDown)
86 {
87 player.body.velocity.x = -player.speed;
88 player.lastmovetime = game.time.now;
89 }
90 else if (cursors.right.isDown)
91 {
92 player.body.velocity.x = player.speed;
93 player.lastmovetime = game.time.now;
94 }
95 if (cursors.up.isDown)
96 {
97 player.body.velocity.y = -player.speed;
98 player.lastmovetime = game.time.now;
99 }
100 else if (cursors.down.isDown)
101 {
102 player.body.velocity.y = player.speed;
103 player.lastmovetime = game.time.now;
104 }
105 if ((this.mode == MODE_JUSTICE) && game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && ((game.time.now - this.lasttime_shoot) > WAIT_SHOOT))
106 {
107 this.shoot();
108 }
109 }
110 }
111
112 switchmode(soulmode) {
113 switch(soulmode)
114 {
115 case MODE_DETERMINATION:
116 this.addColor('red', 0);
117 break;
118 case MODE_JUSTICE:
119 this.addColor('yellow', 0);
120 this.lasttime_mode = game.time.now;
121 break;
122 }
123 this.mode = soulmode;
124 }
125
126 shoot() {
127 this.weapon.fire(this, 0, this.y);
128 this.weapon.fire(this, this.x, 0);
129 this.weapon.fire(this, this.x, game.world.height);
130 this.weapon.fire(this, game.world.width, this.y);
131 this.lasttime_shoot = game.time.now;
132 }
133
134 update() {
135 super.update();
136 this.control();
137 this.healthBar.update(this.love, this.maxHealth, this.health);
138 game.physics.arcade.overlap(monsters, this.weapon.bullets, function(m,b) { b.overlap_monster(m); });
139 if ((game.time.now - this.lasttime_mode) > WAIT_SOULMODE) { this.switchmode(MODE_DETERMINATION); }
140 }
141 }
142
143 class Monster extends Phaser.Sprite {
144 constructor(x, y) {
145 super(game, x, y, 'monster');
146 this.anchor.setTo(0.5, 0.5);
147 this.health = MONSTER_HEALTH;
148 this.nexttime_shoot = game.time.now;
149 this.weapon = new JusticeBlaster(game, this);
150 this.weapon.bulletSpeed = SPEED_MPROJECTILE;
151 this.weapon.trackSprite(this);
152 }
153
154 enablePhysics() {
155 game.physics.arcade.enable(this);
156 }
157
158 kill() {
159 super.kill();
160 player.loveUp();
161 }
162
163 overlap_player(player) {
164 player.damage(10);
165 player.switchmode(MODE_DETERMINATION);
166 }
167
168 update() {
169 super.update();
170 if (this.alive && player.alive && (game.time.now > this.nexttime_shoot))
171 {
172 this.weapon.fireAtSprite(player);
173 this.nexttime_shoot = game.time.now + (WAIT_MONSTERSHOOT / 2) + (Math.random() * WAIT_MONSTERSHOOT);
174 }
175 game.physics.arcade.overlap(player, this.weapon.bullets, function(p,b) { b.overlap_player(p); });
176 }
177 }
178
179 class Determination extends Phaser.Sprite {
180 constructor(x, y) {
181 super(game, x, y, 'determination');
182 this.anchor.setTo(0.5, 0.5);
183 }
184
185 enablePhysics() {
186 game.physics.arcade.enable(this);
187 }
188 }
189
190 class Justice extends Phaser.Bullet {
191 constructor(game, x, y, key, frame) {
192 super(game, x, y, 'projectile');
193 this.anchor.setTo(0.5, 0.5);
194 }
195
196 overlap_monster(entity) {
197 entity.damage(player.love);
198 this.kill();
199 }
200
201 overlap_player(player) {
202 player.damage(player.love);
203 this.kill();
204 }
205 }
206
207 class JusticeBlaster extends Phaser.Weapon {
208 constructor(game, parent) {
209 super(game, parent);
210 this.bulletClass = Justice;
211 this.multiFire = true;
212 this.createBullets(100, null);
213 }
214 }
215
216 class HealthBar {
217 constructor(x, y) {
218 this.mhealth = game.add.tileSprite(x + 10, y, 0, HB_THICKNESS, 'wall');
219 this.ahealth = game.add.tileSprite(x + 10, y, 0, HB_THICKNESS, 'wall');
220 this.mhealth.anchor.setTo(0, 0.5);
221 this.ahealth.anchor.setTo(0, 0.5);
222 this.mhealth.tint = '0xff0000';
223 this.ahealth.tint = '0xffff00';
224 this.text_love = new Phaser.Text(game, x - 50, y + 2, null, { align: 'center', fill: 'white', font: 'Ubuntu Mono', fontSize: 16, fontWeight: 'bold' });
225 this.text_love.anchor.setTo(0, 0.5);
226 game.add.existing(this.text_love);
227 }
228
229 update(love, mhealth, ahealth) {
230 this.mhealth.width = mhealth;
231 this.ahealth.width = ahealth;
232 this.text_love.text = "LV " + love;
233 }
234 }
235
236 function preload () {
237
238 game.load.image('wall', 'wall.png');
239 game.load.image('player', 'player.png');
240 game.load.image('monster', 'monster.png');
241 game.load.image('determination', 'determination.png');
242 game.load.image('projectile', 'projectile.png');
243
244 }
245
246 function create () {
247
248 game.world.setBounds(0, 0, 800, 600);
249 game.stage.backgroundColor = '#000000';
250 game.physics.startSystem(Phaser.Physics.ARCADE);
251
252 walls = game.add.group();
253 walls.classType = Phaser.TileSprite;
254 walls.enableBody = true;
255
256 walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
257 walls.add(game.add.tileSprite(WALL_BORDER, game.world.height - WALL_THICKNESS - WALL_BORDERBOTTOM, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
258 walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
259 walls.add(game.add.tileSprite(game.world.width - WALL_THICKNESS - WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
260 walls.children.forEach(function(wall) { wall.body.immovable = true; });
261
262 determinations = game.add.group();
263 nexttime_determination = Math.random() * WAIT_DETERMINATION;
264
265 monsters = game.add.group();
266 nexttime_monsterspawn = WAIT_MONSTERSPAWN / 4;
267
268 player = new Player(game.world.width / 2, game.world.height / 2);
269 player.enablePhysics();
270 game.add.existing(player);
271 game.camera.follow(player);
272 cursors = game.input.keyboard.createCursorKeys();
273
274 }
275
276 function update () {
277
278 game.physics.arcade.collide(player, walls);
279 game.physics.arcade.overlap(player, determinations, function(p,d) { p.overlap_determination(d); });
280 game.physics.arcade.overlap(player, monsters, function(p,m) { m.overlap_player(p); });
281
282 if (game.time.now > nexttime_determination)
283 {
284 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));
285 determination.enablePhysics();
286 determinations.add(determination);
287 nexttime_determination = game.time.now + (WAIT_DETERMINATION / 2) + (Math.random() * WAIT_DETERMINATION);
288 }
289
290 if (game.time.now > nexttime_monsterspawn)
291 {
292 var monster = new Monster(Math.random() * game.world.width, Math.random() * game.world.height);
293 monster.enablePhysics();
294 monsters.add(monster);
295 var waittime = WAIT_MONSTERSPAWN - ((Math.random() * 2000) * player.love);
296 nexttime_monsterspawn = game.time.now + waittime;
297 console.log('Next monster in ' + waittime);
298 }
299
300 if ((game.time.now - player.lastmovetime) > WAIT_KEY) player.body.velocity.x = player.body.velocity.y = 0;
301
302 }