5c84a995a30a5ca2e5a4df6f865ac57b969e6bd9
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_PROJECTILE
= 500;
27 window
.addEventListener("keydown", function(e
) {
28 // Prevent default browser action for arrows and spacebar
29 if([32, 37, 38, 39, 40].indexOf(e
.keyCode
) > -1) {
35 class Player
extends Phaser
.Text
{
37 super(game
, x
, y
, "♥️", { align
: 'center', fill
: 'red', font
: 'Ubuntu Mono', fontSize
: 32, fontWeight
: 'bold' });
38 this.anchor
.setTo(0.5, 0.5);
39 this.maxHealth
= MAX_HEALTH
;
40 this.health
= this.maxHealth
;
42 this.lastmovetime
= 0;
43 this.lasttime_shoot
= 0;
44 this.lasttime_mode
= 0;
45 this.lasttime_damage
= 0;
47 this.switchmode(MODE_DETERMINATION
);
48 this.projectiles
= game
.add
.group();
49 this.healthBar
= new HealthBar(60, game
.world
.height
- (WALL_BORDERBOTTOM
/ 2));
53 game
.physics
.arcade
.enable(this);
54 this.body
.bounce
.y
= 0.2;
55 this.body
.bounce
.x
= 0.2;
56 this.body
.collideWorldBounds
= true;
59 overlap_determination(determination
) {
63 this.switchmode(MODE_JUSTICE
);
67 if ((game
.time
.now
- this.lasttime_damage
) > WAIT_INVINCIBILITY
)
70 this.lasttime_damage
= game
.time
.now
;
76 this.maxHealth
+= this.love
;
83 if (cursors
.left
.isDown
)
85 player
.body
.velocity
.x
= -player
.speed
;
86 player
.lastmovetime
= game
.time
.now
;
88 else if (cursors
.right
.isDown
)
90 player
.body
.velocity
.x
= player
.speed
;
91 player
.lastmovetime
= game
.time
.now
;
93 if (cursors
.up
.isDown
)
95 player
.body
.velocity
.y
= -player
.speed
;
96 player
.lastmovetime
= game
.time
.now
;
98 else if (cursors
.down
.isDown
)
100 player
.body
.velocity
.y
= player
.speed
;
101 player
.lastmovetime
= game
.time
.now
;
103 if ((this.mode
== MODE_JUSTICE
) && game
.input
.keyboard
.isDown(Phaser
.Keyboard
.SPACEBAR
) && ((game
.time
.now
- this.lasttime_shoot
) > WAIT_SHOOT
))
105 this.shoot(0, SPEED_PROJECTILE
);
106 this.shoot(0, -SPEED_PROJECTILE
);
107 this.shoot(SPEED_PROJECTILE
, 0);
108 this.shoot(-SPEED_PROJECTILE
, 0);
113 switchmode(soulmode
) {
116 case MODE_DETERMINATION
:
117 this.addColor('red', 0);
120 this.addColor('yellow', 0);
121 this.lasttime_mode
= game
.time
.now
;
124 this.mode
= soulmode
;
127 shoot(velocityx
, velocityy
) {
128 var projectile
= new Justice(this.x
, this.y
, velocityx
, velocityy
);
129 projectile
.enablePhysics();
130 this.projectiles
.add(projectile
);
131 this.lasttime_shoot
= game
.time
.now
;
137 this.healthBar
.update(this.love
, this.maxHealth
, this.health
);
138 if ((game
.time
.now
- this.lasttime_mode
) > WAIT_SOULMODE
) { this.switchmode(MODE_DETERMINATION
); }
142 class Monster
extends Phaser
.Sprite
{
144 super(game
, x
, y
, 'monster');
145 this.anchor
.setTo(0.5, 0.5);
146 this.health
= MONSTER_HEALTH
;
147 this.nexttime_shoot
= game
.time
.now
;
148 this.weapon
= new Phaser
.Weapon(game
, this);
151 this.weapon
.createBullets(20, 'projectile');
155 game
.physics
.arcade
.enable(this);
163 overlap_player(player
) {
165 player
.switchmode(MODE_DETERMINATION
);
170 if (this.alive
&& player
.alive
&& (game
.time
.now
> this.nexttime_shoot
))
172 this.weapon
.fireAtSprite(player
);
173 this.nexttime_shoot
= game
.time
.now
+ (WAIT_MONSTERSHOOT
/ 2) + (Math
.random() * WAIT_MONSTERSHOOT
);
175 game
.physics
.arcade
.overlap(player
, this.weapon
.bullets
, function(p
,b
) { p
.damage(p
.love
); });
179 class Determination
extends Phaser
.Sprite
{
181 super(game
, x
, y
, 'determination');
182 this.anchor
.setTo(0.5, 0.5);
186 game
.physics
.arcade
.enable(this);
190 class Justice
extends Phaser
.Sprite
{
191 constructor(x
, y
, velocityx
, velocityy
) {
192 super(game
, x
, y
, 'projectile');
193 this.anchor
.setTo(0.5, 0.5);
194 this.velocityx
= velocityx
;
195 this.velocityy
= velocityy
;
199 game
.physics
.arcade
.enable(this);
200 this.body
.velocity
.x
= this.velocityx
;
201 this.body
.velocity
.y
= this.velocityy
;
205 entity
.damage(player
.love
);
212 this.mhealth
= game
.add
.tileSprite(x
+ 10, y
, 0, HB_THICKNESS
, 'wall');
213 this.ahealth
= game
.add
.tileSprite(x
+ 10, y
, 0, HB_THICKNESS
, 'wall');
214 this.mhealth
.anchor
.setTo(0, 0.5);
215 this.ahealth
.anchor
.setTo(0, 0.5);
216 this.mhealth
.tint
= '0xff0000';
217 this.ahealth
.tint
= '0xffff00';
218 this.text_love
= new Phaser
.Text(game
, x
- 50, y
+ 2, null, { align
: 'center', fill
: 'white', font
: 'Ubuntu Mono', fontSize
: 16, fontWeight
: 'bold' });
219 this.text_love
.anchor
.setTo(0, 0.5);
220 game
.add
.existing(this.text_love
);
223 update(love
, mhealth
, ahealth
) {
224 this.mhealth
.width
= mhealth
;
225 this.ahealth
.width
= ahealth
;
226 this.text_love
.text
= "LV " + love
;
230 function preload () {
232 game
.load
.image('wall', 'wall.png');
233 game
.load
.image('player', 'player.png');
234 game
.load
.image('monster', 'monster.png');
235 game
.load
.image('determination', 'determination.png');
236 game
.load
.image('projectile', 'projectile.png');
242 game
.world
.setBounds(0, 0, 800, 600);
243 game
.stage
.backgroundColor
= '#000000';
244 game
.physics
.startSystem(Phaser
.Physics
.ARCADE
);
246 walls
= game
.add
.group();
247 walls
.classType
= Phaser
.TileSprite
;
248 walls
.enableBody
= true;
250 walls
.add(game
.add
.tileSprite(WALL_BORDER
, WALL_BORDER
, game
.world
.width
- (WALL_BORDER
* 2), WALL_THICKNESS
, 'wall'));
251 walls
.add(game
.add
.tileSprite(WALL_BORDER
, game
.world
.height
- WALL_THICKNESS
- WALL_BORDERBOTTOM
, game
.world
.width
- (WALL_BORDER
* 2), WALL_THICKNESS
, 'wall'));
252 walls
.add(game
.add
.tileSprite(WALL_BORDER
, WALL_BORDER
, WALL_THICKNESS
, game
.world
.height
- WALL_BORDER
- WALL_BORDERBOTTOM
, 'wall'));
253 walls
.add(game
.add
.tileSprite(game
.world
.width
- WALL_THICKNESS
- WALL_BORDER
, WALL_BORDER
, WALL_THICKNESS
, game
.world
.height
- WALL_BORDER
- WALL_BORDERBOTTOM
, 'wall'));
254 walls
.children
.forEach(function(wall
) { wall
.body
.immovable
= true; });
256 determinations
= game
.add
.group();
257 nexttime_determination
= Math
.random() * WAIT_DETERMINATION
;
259 monsters
= game
.add
.group();
260 monsters
.add(new Monster(400, 500));
261 monsters
.children
.forEach(function(monster
) { monster
.enablePhysics(); });
262 nexttime_monsterspawn
= WAIT_MONSTERSPAWN
;
264 player
= new Player(game
.world
.width
/ 2, game
.world
.height
/ 2);
265 player
.enablePhysics();
266 game
.add
.existing(player
);
267 game
.camera
.follow(player
);
268 cursors
= game
.input
.keyboard
.createCursorKeys();
270 keyboard_handler
= keyPress_default
;
276 game
.physics
.arcade
.collide(player
, walls
);
277 game
.physics
.arcade
.overlap(player
, determinations
, function(p
,d
) { p
.overlap_determination(d
); });
278 game
.physics
.arcade
.overlap(player
, monsters
, function(p
,m
) { m
.overlap_player(p
); });
279 game
.physics
.arcade
.overlap(monsters
, player
.projectiles
, function(m
,j
) { j
.overlap(m
); });
281 if (game
.time
.now
> nexttime_determination
)
283 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));
284 determination
.enablePhysics();
285 determinations
.add(determination
);
286 nexttime_determination
= game
.time
.now
+ (WAIT_DETERMINATION
/ 2) + (Math
.random() * WAIT_DETERMINATION
);
289 if (game
.time
.now
> nexttime_monsterspawn
)
291 var monster
= new Monster(Math
.random() * game
.world
.width
, Math
.random() * game
.world
.height
);
292 monster
.enablePhysics();
293 monsters
.add(monster
);
294 nexttime_monsterspawn
= game
.time
.now
+ (WAIT_MONSTERSPAWN
/ player
.love
) + (Math
.random() * WAIT_MONSTERSPAWN
);
297 if ((game
.time
.now
- player
.lastmovetime
) > WAIT_KEY
) player
.body
.velocity
.x
= player
.body
.velocity
.y
= 0;
298 if (keyboard_handler
) keyboard_handler();
302 function keyPress_default() {
304 // Maybe needed later.