@@ -21,12 +21,19 @@ var cursors;
2121var fireButton ;
2222var explosions ;
2323var starfield ;
24+ var score = 0 ;
25+ var scoreString = '' ;
26+ var scoreText ;
27+ var lives ;
28+ var enemyBullet ;
29+ var firingTimer = 0 ;
30+ var stateText ;
2431
2532function create ( ) {
2633
2734 // The scrolling starfield background
2835 starfield = game . add . tileSprite ( 0 , 0 , 800 , 600 , 'starfield' ) ;
29- // game.add.tileSprite(0, 0, 800, 600, 'background');
36+
3037
3138 // Our bullet group
3239 bullets = game . add . group ( ) ;
@@ -35,18 +42,65 @@ function create() {
3542 bullets . setAll ( 'anchor.y' , 1 ) ;
3643 bullets . setAll ( 'outOfBoundsKill' , true ) ;
3744
45+ // The enemy's bullets
46+ enemyBullets = game . add . group ( ) ;
47+ enemyBullets . createMultiple ( 30 , 'enemyBullet' ) ;
48+ enemyBullets . setAll ( 'anchor.x' , 0.5 ) ;
49+ enemyBullets . setAll ( 'anchor.y' , 1 ) ;
50+ enemyBullets . setAll ( 'outOfBoundsKill' , true ) ;
51+
3852 // The hero!
3953 player = game . add . sprite ( 400 , 500 , 'ship' ) ;
4054 player . anchor . setTo ( 0.5 , 0.5 ) ;
4155
4256 // The baddies!
57+
4358 aliens = game . add . group ( ) ;
4459
45- for ( var y = 0 ; y < 4 ; y ++ )
60+ createAliens ( ) ;
61+
62+ // The score :
63+
64+ scoreString = 'Score : ' ;
65+ scoreText = game . add . text ( 10 , 10 , scoreString + score , { fontSize : '34px' , fill : '#fff' } ) ;
66+
67+ // Lives
68+ lives = game . add . group ( ) ;
69+ game . add . text ( game . world . width - 100 , 10 , 'Lives : ' , { fontSize : '34px' , fill : '#fff' } ) ;
70+
71+ //state text
72+ stateText = game . add . text ( game . world . centerX , game . world . centerY , '' , { fontSize : '84px' , fill : '#fff' } ) ;
73+ stateText . anchor . setTo ( 0.5 , 0.5 ) ;
74+ stateText . visible = false ;
75+
76+
77+
78+ for ( var i = 0 ; i < 3 ; i ++ ) {
79+
80+ var ship = lives . create ( game . world . width - 100 + ( 30 * i ) , 60 , 'ship' ) ;
81+ ship . anchor . setTo ( 0.5 , 0.5 ) ;
82+ ship . angle = 90 ;
83+ ship . alpha = 0.4 ;
84+ } ;
85+
86+ // An explosion pool
87+ explosions = game . add . group ( ) ;
88+ explosions . createMultiple ( 30 , 'kaboom' ) ;
89+ explosions . forEach ( setupInvader , this ) ;
90+
91+ // And some controls to play the game with
92+ cursors = game . input . keyboard . createCursorKeys ( ) ;
93+ fireButton = game . input . keyboard . addKey ( Phaser . Keyboard . SPACEBAR ) ;
94+
95+ }
96+
97+ function createAliens ( ) {
98+ for ( var y = 0 ; y < 4 ; y ++ )
4699 {
47100 for ( var x = 0 ; x < 10 ; x ++ )
48101 {
49102 var alien = aliens . create ( x * 48 , y * 50 , 'invader' ) ;
103+ alien . anchor . setTo ( 0.5 , 0.5 ) ;
50104 alien . animations . add ( 'fly' , [ 0 , 1 , 2 , 3 ] , 20 , true ) ;
51105 alien . play ( 'fly' ) ;
52106 }
@@ -55,21 +109,11 @@ function create() {
55109 aliens . x = 100 ;
56110 aliens . y = 50 ;
57111
58- // An explosion pool
59- explosions = game . add . group ( ) ;
60- explosions . createMultiple ( 30 , 'kaboom' ) ;
61- explosions . forEach ( setupInvader , this ) ;
62-
63- // All this does is basically start the invaders moving. Notice we're move the Group they belong to, rather than the invaders directly.
112+ // All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly.
64113 var tween = game . add . tween ( aliens ) . to ( { x : 200 } , 2000 , Phaser . Easing . Linear . None , true , 0 , 1000 , true ) ;
65114
66115 // When the tween completes it calls descend, before looping again
67116 tween . onComplete . add ( descend , this ) ;
68-
69- // And some controls to play the game with
70- cursors = game . input . keyboard . createCursorKeys ( ) ;
71- fireButton = game . input . keyboard . addKey ( Phaser . Keyboard . SPACEBAR ) ;
72-
73117}
74118
75119function setupInvader ( invader ) {
@@ -109,11 +153,79 @@ function update() {
109153 fireBullet ( ) ;
110154 }
111155
156+ if ( game . time . now > firingTimer ) {
157+ enemyFires ( ) ;
158+ }
159+
112160 // Run collision
113161 game . physics . collide ( bullets , aliens , collisionHandler , null , this ) ;
162+ game . physics . collide ( enemyBullets , player , enemyHitsPlayer , null , this ) ;
163+
164+
165+ }
166+
167+ function enemyHitsPlayer ( player , bullet ) {
168+
169+ bullet . kill ( ) ;
170+
171+ live = lives . getFirstAlive ( ) ;
172+
173+ if ( live ) {
174+ live . kill ( ) ;
175+ }
176+
177+
178+ // And create an explosion :)
179+ var explosion = explosions . getFirstDead ( ) ;
180+ explosion . reset ( player . body . x , player . body . y ) ;
181+ explosion . play ( 'kaboom' , 30 , false , true ) ;
182+
183+ if ( lives . countLiving ( ) < 1 ) {
184+
185+ player . kill ( ) ;
186+ enemyBullets . callAll ( 'kill' , this ) ;
187+ stateText . content = " GAME OVER \n Click to restart" ;
188+ stateText . visible = true ;
189+
190+ game . input . onTap . addOnce ( restart , this ) ;
191+
192+
193+ }
114194
115195}
116196
197+ function restart ( ) {
198+
199+
200+ lives . callAll ( 'revive' ) ;
201+ // And brings the aliens back from the dead :)
202+ aliens . removeAll ( ) ;
203+
204+ createAliens ( ) ;
205+
206+ player . revive ( ) ;
207+
208+ stateText . visible = false ;
209+
210+
211+ }
212+
213+ function enemyFires ( ) {
214+
215+ // Grab the first bullet we can from the pool
216+ enemyBullet = enemyBullets . getFirstExists ( false ) ;
217+
218+ if ( enemyBullet ) {
219+
220+ var shooter = aliens . getRandom ( ) ;
221+ // And fire it
222+ enemyBullet . reset ( shooter . body . x , shooter . body . y ) ;
223+ // enemyBullet.body.velocity.y = 400;
224+ game . physics . moveToObject ( enemyBullet , player , 120 ) ;
225+ firingTimer = game . time . now + 2000 ;
226+ }
227+ }
228+
117229function fireBullet ( ) {
118230
119231 // To avoid them being allowed to fire too fast we set a time limit
@@ -147,10 +259,27 @@ function collisionHandler (bullet, alien) {
147259 alien . kill ( ) ;
148260
149261 // Increase the score
262+ score += 20 ;
263+ scoreText . content = scoreString + score ;
150264
151265 // And create an explosion :)
152266 var explosion = explosions . getFirstDead ( ) ;
153267 explosion . reset ( alien . body . x , alien . body . y ) ;
154268 explosion . play ( 'kaboom' , 30 , false , true ) ;
155269
270+ if ( aliens . countLiving ( ) == 0 ) {
271+ // New level starts
272+ score += 1000 ;
273+ scoreText . content = scoreString + score ;
274+
275+ enemyBullets . callAll ( 'kill' , this ) ;
276+ stateText . content = " You Won, \n Click to restart" ;
277+ stateText . visible = true ;
278+
279+ game . input . onTap . addOnce ( restart , this ) ;
280+
281+
282+
283+ }
284+
156285}
0 commit comments