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);
169 overlap_player(player
) {
171 player
.switchmode(MODE_DETERMINATION
);
176 if (this.alive
&& player
.alive
&& (game
.time
.now
> this.nexttime_shoot
))
178 this.weapon
.fireAtSprite(player
);
179 this.nexttime_shoot
= game
.time
.now
+ (WAIT_MONSTERSHOOT
/ 2) + (Math
.random() * WAIT_MONSTERSHOOT
);
181 game
.physics
.arcade
.overlap(player
, this.weapon
.bullets
, function(p
,b
) { b
.overlap_player(p
); });
185 class Determination
extends Phaser
.Sprite
{
187 super(game
, x
, y
, 'determination');
188 this.anchor
.setTo(0.5, 0.5);
192 game
.physics
.arcade
.enable(this);
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);
202 overlap_monster(entity
) {
203 entity
.damage(player
.love
);
207 overlap_player(player
) {
208 player
.damage(player
.love
);
213 class JusticeBlaster
extends Phaser
.Weapon
{
214 constructor(game
, parent
) {
216 this.bulletClass
= Justice
;
217 this.multiFire
= true;
218 this.createBullets(100, null);
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
);
235 update(love
, mhealth
, ahealth
) {
236 this.mhealth
.width
= mhealth
;
237 this.ahealth
.width
= ahealth
;
238 this.text_love
.text
= "LV " + love
;
242 function preload () {
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');
254 game
.world
.setBounds(0, 0, 800, 600);
255 game
.stage
.backgroundColor
= '#000000';
256 game
.physics
.startSystem(Phaser
.Physics
.ARCADE
);
258 walls
= game
.add
.group();
259 walls
.classType
= Phaser
.TileSprite
;
260 walls
.enableBody
= true;
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; });
268 determinations
= game
.add
.group();
269 nexttime_determination
= Math
.random() * WAIT_DETERMINATION
;
271 monsters
= game
.add
.group();
272 nexttime_monsterspawn
= WAIT_MONSTERSPAWN
/ 4;
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();
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
); });
288 if (game
.time
.now
> nexttime_determination
)
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
);
296 if (game
.time
.now
> nexttime_monsterspawn
)
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
);
306 if ((game
.time
.now
- player
.lastmovetime
) > WAIT_KEY
) player
.body
.velocity
.x
= player
.body
.velocity
.y
= 0;