53e41f87ce314147e88d5301a5a3b91ea399bfc0
1 var game
= new Phaser
.Game(800, 600, Phaser
.AUTO
, '', { preload
: preload
, create
: create
, update
: update
});
4 var determinations
; // I know it's grammatically incorrect. :P
6 var nexttime_monsterspawn
;
7 var nexttime_determination
;
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;
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) {
36 class Player
extends Phaser
.Text
{
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
;
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));
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;
61 overlap_determination(determination
) {
65 this.switchmode(MODE_JUSTICE
);
69 if ((game
.time
.now
- this.lasttime_damage
) > WAIT_INVINCIBILITY
)
72 this.lasttime_damage
= game
.time
.now
;
78 this.healthBar
.update(this.love
, this.maxHealth
, this.health
);
84 this.maxHealth
+= this.love
;
91 if (cursors
.left
.isDown
)
93 player
.body
.velocity
.x
= -player
.speed
;
94 player
.lastmovetime
= game
.time
.now
;
96 else if (cursors
.right
.isDown
)
98 player
.body
.velocity
.x
= player
.speed
;
99 player
.lastmovetime
= game
.time
.now
;
101 if (cursors
.up
.isDown
)
103 player
.body
.velocity
.y
= -player
.speed
;
104 player
.lastmovetime
= game
.time
.now
;
106 else if (cursors
.down
.isDown
)
108 player
.body
.velocity
.y
= player
.speed
;
109 player
.lastmovetime
= game
.time
.now
;
111 if ((this.mode
== MODE_JUSTICE
) && game
.input
.keyboard
.isDown(Phaser
.Keyboard
.SPACEBAR
) && ((game
.time
.now
- this.lasttime_shoot
) > WAIT_SHOOT
))
118 switchmode(soulmode
) {
121 case MODE_DETERMINATION
:
122 this.addColor('red', 0);
125 this.addColor('yellow', 0);
126 this.lasttime_mode
= game
.time
.now
;
129 this.mode
= soulmode
;
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
;
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
); }
149 class Monster
extends Phaser
.Sprite
{
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);
161 game
.physics
.arcade
.enable(this);
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); }
174 overlap_player(player
) {
176 player
.switchmode(MODE_DETERMINATION
);
181 if (this.alive
&& player
.alive
&& (game
.time
.now
> this.nexttime_shoot
))
183 this.weapon
.fireAtSprite(player
);
184 this.nexttime_shoot
= game
.time
.now
+ (WAIT_MONSTERSHOOT
/ 2) + (Math
.random() * WAIT_MONSTERSHOOT
);
186 game
.physics
.arcade
.overlap(player
, this.weapon
.bullets
, function(p
,b
) { b
.overlap_player(p
); });
190 class Determination
extends Phaser
.Sprite
{
192 super(game
, x
, y
, 'determination');
193 this.anchor
.setTo(0.5, 0.5);
197 game
.physics
.arcade
.enable(this);
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);
207 overlap_monster(entity
) {
208 entity
.damage(player
.love
);
212 overlap_player(player
) {
213 player
.damage(player
.love
);
218 class JusticeBlaster
extends Phaser
.Weapon
{
219 constructor(game
, parent
) {
221 this.bulletClass
= Justice
;
222 this.multiFire
= true;
223 this.createBullets(100, null);
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
);
240 update(love
, mhealth
, ahealth
) {
241 this.mhealth
.width
= mhealth
;
242 this.ahealth
.width
= ahealth
;
243 this.text_love
.text
= "LV " + love
;
247 function preload () {
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');
259 game
.world
.setBounds(0, 0, 800, 600);
260 game
.stage
.backgroundColor
= '#000000';
261 game
.physics
.startSystem(Phaser
.Physics
.ARCADE
);
263 walls
= game
.add
.group();
264 walls
.classType
= Phaser
.TileSprite
;
265 walls
.enableBody
= true;
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; });
273 determinations
= game
.add
.group();
274 nexttime_determination
= Math
.random() * WAIT_DETERMINATION
;
276 monsters
= game
.add
.group();
277 nexttime_monsterspawn
= WAIT_MONSTERSPAWN
/ 4;
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();
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
); });
293 if (game
.time
.now
> nexttime_determination
)
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
);
301 if (game
.time
.now
> nexttime_monsterspawn
)
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
);
311 if ((game
.time
.now
- player
.lastmovetime
) > WAIT_KEY
) player
.body
.velocity
.x
= player
.body
.velocity
.y
= 0;