-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththegame.js
176 lines (157 loc) · 5.23 KB
/
thegame.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var thegame = function(game) {
var player;
var enemies;
var starfield;
var lasers;
var fireButton;
var laserTimer;// the lasers come out too fast
var enemyTimer;
var score;
var scoreText;
var laserSound;
var MIN_ENEMY_SPACING;
var MAX_ENEMY_SPACING;
var ACCELERATION;
var DRAG;
var MAXSPEED;
var bank;
var explosions;
}
thegame.prototype = {
create: function() {
// We're going to be using physics, so enable the Arcade Physics system
this.game.physics.startSystem(Phaser.Physics.ARCADE);
laserTimer = 0;
// scrolling background of stars
starfield = this.game.add.tileSprite(0, 0, 800, 600, 'background');
// spaceship
ACCELERATION = 600;
DRAG = 400;
MAXSPEED = 400;
player = this.game.add.sprite(400, 500, 'player');
player.scale.setTo(0.5, 0.5);
player.anchor.setTo(0.5, 0.5);
this.game.physics.enable(player, Phaser.Physics.ARCADE);
player.body.maxVelocity.setTo(MAXSPEED, MAXSPEED);
player.body.drag.setTo(DRAG, DRAG);
// lasers
lasers = this.game.add.group();
lasers.enableBody = true;
lasers.physicsBodyType = Phaser.Physics.ARCADE;
lasers.createMultiple(30, 'laser');
lasers.setAll('anchor.x', 0.5);
lasers.setAll('anchor.y', 1);
lasers.setAll('outOfBoundsKill', true); // kill lasers when out of bounds
lasers.setAll('checkWorldBounds', true);
laserSound = this.game.add.audio('laserAudio'); // laser audio
// bad guys
enemies = this.game.add.group();
enemies.enableBody = true;
enemies.physicsBodyType = Phaser.Physics.ARCADE;
enemies.createMultiple(5, 'enemy');
enemies.setAll('anchor.x', 0.5);
enemies.setAll('anchor.y', 0.5);
enemies.setAll('scale.x', 0.5);
enemies.setAll('scale.y', 0.5);
enemies.setAll('angle', 180);
enemies.setAll('outOfBoundsKill', true);
enemies.setAll('checkWorldBounds', true);
enemyTimer = 0;
// set up keyboard controls
cursors = this.game.input.keyboard.createCursorKeys();
fireButton = this.game.input.keyboard.addKey(Phaser.Keyboard.S);
score = 0;
scoreText = this.game.add.text(16, 16, 'Score: 0', {fontSize: '32px', fill: 'white'});
scoreText.font = 'Press Start 2P';
scoreText.fontSize = 16;
explosions = this.game.add.group();
explosions.enableBody = true;
explosions.physicsBodyType = Phaser.Physics.ARCADE;
console.log("start");
explosions.createMultiple(30, 'explosion');
explosions.setAll('anchor.x', 0.5);
explosions.setAll('anchor.y', 0.5);
explosions.forEach(this.setupExplosions, this);
},
update: function() {
starfield.tilePosition.y += 2;
player.body.acceleration.x = 0;
if (cursors.left.isDown) {
player.body.acceleration.x = -ACCELERATION;
}
else if (cursors.right.isDown) {
player.body.acceleration.x = ACCELERATION;
}
else if (cursors.up.isDown) {
player.body.velocity.y = -200;
}
else if (cursors.down.isDown) {
player.body.velocity.y = 200;
}
if (player.x > this.game.width - 30) {
player.x = this.game.width - 30;
}
if (player.x < 30) {
player.x = 30;
}
if (player.y > this.game.height - 30) {
player.y = this.game.height - 30;
}
if (player.y < 30) {
player.y = 30;
}
bank = player.body.velocity.x / MAXSPEED;
player.scale.x = .5 - Math.abs(bank) / 8;
if (fireButton.isDown) {
this.fireLaser();
}
this.launchNewEnemy();
this.game.physics.arcade.overlap(player, enemies, this.shipsCollide, null, this);
this.game.physics.arcade.overlap(enemies, lasers, this.enemyHit, null, this);
},
fireLaser: function() {
if (this.game.time.now > laserTimer){
var LASER_SPEED = 200;
var laser = lasers.getFirstExists(false);
if (laser) { // fire a laser
laser.reset(player.x, player.y - 16);
laser.body.velocity.y = -400;
laserTimer = this.game.time.now + LASER_SPEED;
laserSound.play();
}
}
},
launchNewEnemy: function() {
if (this.game.time.now > enemyTimer) {
var ENEMY_SPEED = 300;
var enemy = enemies.getFirstExists(false);
if (enemy) {
enemy.reset((this.game.rnd.integerInRange(0, this.game.width)), -20); // start position of enemy
enemy.body.velocity.x = this.game.rnd.integerInRange(-300, 300);
enemy.body.velocity.y = ENEMY_SPEED;
enemy.body.drag.x = 100;
enemy.body.setSize(enemy.width * 0.75, enemy.height * 0.75);
enemyTimer = this.game.time.now + 500;
}
}
},
setupExplosions: function(enemy) {
enemy.animations.add('explosion'); // add explosion animation
},
shipsCollide: function(player, enemy) {
var explosion = explosions.getFirstExists(false);
explosion.reset(enemy.body.x + enemy.body.halfWidth, enemy.body.y + enemy.body.halfHeight);
explosion.play('explosion');
enemy.kill();
this.game.state.start('GameOver');
},
enemyHit: function(enemy, laser) {
var explosion = explosions.getFirstExists(false);
explosion.reset(enemy.body.x + enemy.body.halfWidth, enemy.body.y + enemy.body.halfHeight);
explosion.play('explosion');
enemy.kill();
laser.kill();
score+=10;
scoreText.text = "Score: " + score;
}
}