-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameCode.js
137 lines (106 loc) · 4.32 KB
/
gameCode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
window.onload = function() {
// var cursors, fireButton, bombButton;
var reimu, reimuBullets, reimu_hp = 99;
var lastDirection = [0,-1];
var reimuLastFired = 0;
var marisa, marisa_hp = 99;
var gameTextR, gameTextM;
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', {
preload: preload,
create: create,
update: update
});
function preload() {
// game.load.image('logo', 'phaser.png');
game.load.image('reimu', 'img/reimu.png');
game.load.image('yinyang', 'img/yinyang.png?foo');
game.load.image('reimu-bullet', 'img/reimu-bullet.png');
game.load.image('marisa', 'img/marisa.png');
game.load.image('grass', 'img/grass.jpg?');
}
function update() {
if (reimu !== null) {
reimu.body.velocity.x = 0; // stop all movement
reimu.body.velocity.y = 0;
var movespeed = 250;
// detect keyboard movement
if (cursors.left.isDown) {
reimu.body.velocity.x = -movespeed;
lastDirection = [-1,0];
}
else if (cursors.right.isDown) {
reimu.body.velocity.x = movespeed;
lastDirection = [1,0];
}
// separate cause diagonals are allowed
if (cursors.up.isDown) {
reimu.body.velocity.y = -movespeed;
lastDirection = [0,-1];
} else if (cursors.down.isDown) {
reimu.body.velocity.y = movespeed;
lastDirection = [0, 1];
}
// handle bullets. bullets should move faster than regular movement speed.
if (fireButton.isDown && (reimuLastFired + 300 < game.time.now)) {
var bullet = reimuBullets.getFirstExists(false);
if (bullet) {
bullet.reset(reimu.x + lastDirection[1]*15,
reimu.y + lastDirection[0] * 15);
bullet.body.velocity.x = lastDirection[0] * movespeed * 1.5;
bullet.body.velocity.y = lastDirection[1] * movespeed * 1.5;
}
bullet = reimuBullets.getFirstExists(false);
if (bullet) {
bullet.reset(reimu.x - lastDirection[1]*15,
reimu.y - lastDirection[0] * 15);
bullet.body.velocity.x = lastDirection[0] * movespeed * 1.5;
bullet.body.velocity.y = lastDirection[1] * movespeed * 1.5;
}
reimuLastFired = game.time.now;
}
}
game.physics.arcade.overlap(marisa,reimuBullets, enemyGotHit, null, this);
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
// The background
starfield = game.add.tileSprite(0, 0, 800, 600, 'grass');
// init the player
reimu = game.add.sprite(game.world.centerX, game.world.centerY, 'reimu');
reimu.anchor.setTo(0.5, 0.5);
game.physics.arcade.enable(reimu);
// init player bullets
reimuBullets = game.add.group();
reimuBullets.enableBody = true;
reimuBullets.physicsBodyType = Phaser.Physics.ARCADE;
reimuBullets.createMultiple(30, 'reimu-bullet');
reimuBullets.setAll('anchor.x', 0.5);
reimuBullets.setAll('anchor.y', 1);
reimuBullets.setAll('outOfBoundsKill', true);
reimuBullets.setAll('checkWorldBounds', true);
// punching bag
marisa = game.add.sprite(600, 500, 'marisa');
marisa.anchor.setTo(0.5, 0.5);
game.physics.arcade.enable(marisa);
// handle inputs
cursors = game.input.keyboard.createCursorKeys();
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.Z);
// HP counts
gameTextR = game.add.text(10, 25, reimu_hp.toString(), { font: '36px Arial', fill: '#fff' });
gameTextM = game.add.text(game.world.width - 100, 25, marisa_hp.toString(), { font: '36px Arial', fill: '#fff' });
updateHP();
}
function updateHP() {
gameTextR.setText(reimu_hp.toString());
gameTextM.setText(marisa_hp.toString());
}
// handles collisions if enemy gets hit
function enemyGotHit(marisa, bullet) {
if (marisa_hp <= 0) return;
if (--marisa_hp === 0) {
marisa.kill();
}
bullet.kill();
updateHP();
}
};