11game . Entities = game . Entities || { } ;
22game . Entities . Laser = me . Entity . extend ( {
3- init ( x , y , direction = 'n' , damage = 50 ) {
3+ init ( x , y , direction = 'n' , damage = 50 , firedByEnemy ) {
44 this . _super ( me . Entity , 'init' , [ x , y , { width : game . Entities . Laser . width , height : game . Entities . Laser . height } ] ) ;
55 this . z = 5 ;
66 this . damage = damage ;
7+ this . firedByEnemy = firedByEnemy ;
78 this . alwaysUpdate = true ;
89
910 this . renderable = new ( me . Renderable . extend ( {
@@ -14,8 +15,16 @@ game.Entities.Laser = me.Entity.extend({
1415 draw ( renderer ) {
1516 const color = renderer . getColor ( ) ;
1617
17- renderer . setColor ( '#5EFF7E' ) ;
18- renderer . fillRect ( 0 , 0 , this . width , this . height ) ;
18+ if ( firedByEnemy ) {
19+ renderer . setColor ( '#5EFF7E' ) ;
20+ renderer . fillRect ( 0 , 0 , this . width , this . height ) ;
21+
22+ renderer . setColor ( '#822929' ) ;
23+ renderer . fillRect ( 1 , 1 , this . width - 2 , this . height - 2 ) ;
24+ } else {
25+ renderer . setColor ( '#5EFF7E' ) ;
26+ renderer . fillRect ( 0 , 0 , this . width , this . height ) ;
27+ }
1928 renderer . setColor ( color ) ;
2029 }
2130 } ) ) ( ) ;
@@ -99,7 +108,27 @@ game.Entities.Laser = me.Entity.extend({
99108 } ,
100109
101110 onCollision ( res , other ) {
102- if ( other . body . collisionType === me . collision . types . ENEMY_OBJECT ) {
111+ if ( res . a . firedByEnemy ) {
112+ // Shot was fired by an enemy entity
113+ if ( other . body . collisionType === me . collision . types . PLAYER_OBJECT ) {
114+ me . game . world . removeChild ( this ) ;
115+
116+ other . stats . health -= this . damage ;
117+ other . renderable . flicker ( 500 ) ;
118+
119+ if ( other . stats . health <= 0 ) {
120+ me . audio . pauseTrack ( ) ;
121+ game . data . endPlayTime = new Date ( ) - game . data . startPlayTime ;
122+
123+ me . state . change ( me . state . GAMEOVER ) ;
124+ }
125+ me . audio . play ( 'hit' ) ;
126+ return true ;
127+ }
128+
129+ return false ;
130+ } else if ( other . body . collisionType === me . collision . types . ENEMY_OBJECT ) {
131+ // Shot fired by the player collided with an enemy entity
103132 me . game . world . removeChild ( this ) ;
104133
105134 other . stats . health -= this . damage ;
@@ -122,12 +151,12 @@ game.Entities.Laser = me.Entity.extend({
122151 return true ;
123152 }
124153
125- if ( other . body . collisionType === me . collision . types . PROJECTILE_OBJECT ) {
126- // do nothing
127- return false ;
128- }
154+ // if (other.body.collisionType === me.collision.types.PROJECTILE_OBJECT) {
155+ // do nothing
156+ return false ;
157+ // }
129158 }
130159} ) ;
131160
132- game . Entities . Laser . width = 3 ;
161+ game . Entities . Laser . width = 4 ;
133162game . Entities . Laser . height = 20 ;
0 commit comments