@@ -10,6 +10,7 @@ class Game{
10
10
this . homeButton = homeButton ;
11
11
this . dt = 0 ;
12
12
this . show = false ;
13
+ this . ctx = document . getElementById ( "canvas" ) . getContext ( "2d" ) ;
13
14
}
14
15
get draw ( ) {
15
16
this . reDraw ( ) ;
@@ -76,31 +77,55 @@ class Game{
76
77
this . replayButton . draw ;
77
78
}
78
79
if ( this . asteroids ) {
79
- for ( var asteroid in this . asteroids ) {
80
- if ( ! this . asteroids [ asteroid ] . destroyed ) {
81
- CollisionDetector . rect1 = this . asteroids [ asteroid ] ;
80
+ for ( var i in this . asteroids ) {
81
+ var create_asteroids_in_end = [ ]
82
+ asteroid . draw ;
83
+ asteroid . x += this . asteroids [ i ] . x_speed * this . dt ;
84
+ asteroid . y += this . asteroids [ i ] . y_speed * this . dt ;
85
+ if ( asteroid . y > canvas . height ) {
86
+ if ( this . health . health && ! asteroid . destroyed ) {
87
+ this . health . health -= 1 ;
88
+ }
89
+ this . asteroids . splice ( asteroid , 1 ) ;
90
+ }
91
+ if ( this . asteroids [ i ] ) {
92
+ asteroid = this . asteroids [ i ] . object ;
93
+ } else {
94
+ asteroid = { "destroyed" :true } ;
95
+ }
96
+ if ( ! asteroid . destroyed ) {
97
+ CollisionDetector . rect1 = asteroid ;
82
98
for ( var i in this . player . lasers ) {
83
99
var laser = this . player . lasers [ i ] ;
84
100
CollisionDetector . rect2 = laser ;
85
101
if ( CollisionDetector . get_rect_rect_collision ) {
86
- this . asteroids [ asteroid ] . health -= this . player . shoot_damage ;
87
- if ( this . asteroids [ asteroid ] . health <= 0 && this . health . health ) {
102
+ asteroid . health -= this . player . shoot_damage ;
103
+ if ( asteroid . health <= 0 && this . health . health ) {
88
104
this . score . score += 100 ;
89
- this . asteroids [ asteroid ] . destroyed = true ;
105
+ asteroid . destroyed = true ;
106
+ } else {
107
+ var small_asteroid = JSON . parse ( JSON . stringify ( asteroid ) ) ;
108
+ small_asteroid . w /= 2 ;
109
+ small_asteroid . h /= 2 ;
110
+ small_asteroid . health /= 2 ;
111
+ small_asteroid . x = asteroid . x + ( asteroid . w / 2 ) ;
112
+ small_asteroid . y = asteroid . y
113
+ var asteroid_1 = new Asteroid ( this . ctx , 100 , 30 , 30 , 50 , 50 , "img/meteors/meteorBrown_small1.png" )
114
+ this . asteroids [ i ] . object = asteroid_1 ;
115
+ this . asteroids [ i ] . object . y_speed = - 300 ;
116
+ //create_asteroids_in_end.push({"size":size,"object":asteroid_1,"x_speed":300,"y_speed":300})
90
117
}
91
118
this . player . lasers . splice ( i , 1 ) ;
92
119
}
93
120
}
94
121
}
95
- asteroid = this . asteroids [ asteroid ] ;
96
- asteroid . draw ;
97
- asteroid . y += asteroid . speed * this . dt ;
98
- if ( asteroid . y > canvas . height ) {
99
- if ( this . health . health && ! asteroid . destroyed ) {
100
- this . health . health -= 1 ;
101
- }
102
- this . asteroids . splice ( asteroid , 1 ) ;
103
- }
122
+ for ( var i in create_asteroids_in_end ) {
123
+ if ( create_asteroids_in_end [ i ] ) {
124
+ this . asteroids . push ( create_asteroids_in_end [ i ] ) ;
125
+ console . log ( this . asteroids ) ;
126
+ }
127
+ create_asteroids_in_end = [ ] ;
128
+ }
104
129
}
105
130
}
106
131
createAsteroid ( this . dt , score . score ) ;
0 commit comments