@@ -13,6 +13,21 @@ dampen = (val, speed, dt) ->
1313 else
1414 val
1515
16+ -- love it!
17+ join = ( objs) ->
18+ {
19+ all : ( fn) ->
20+ yes = true
21+ for o in * objs
22+ yes = fn o
23+ break if not yes
24+ yes
25+
26+ set : ( key, value) ->
27+ for o in * objs
28+ o[ key] = value
29+ }
30+
1631class Player extends Entity
1732 watch_class self
1833
@@ -38,6 +53,7 @@ class Player extends Entity
3853 }
3954
4055 cell_id : 0
56+ max_health : 100
4157
4258 new : ( ... ) =>
4359 super ...
@@ -49,10 +65,13 @@ class Player extends Entity
4965 @cur_point = 1
5066
5167 @movement_lock = 0
68+ @health = @max_health
5269
5370 draw : =>
5471 @bullets \ draw!
5572
73+ return if @health <= 0 and # @effects == 0
74+
5675 @effects \ apply ->
5776 @sprite \ draw_cell @cell_id , @box . x - @ox , @box . y - @oy
5877
@@ -69,21 +88,69 @@ class Player extends Entity
6988 @cur_point += 1
7089 @cur_point = 1 if @cur_point > # @shoot_points
7190
91+ take_hit : ( damage) =>
92+ return if @health <= 0 -- already dead
93+
94+ @health = math.max 0 , @health - damage
95+
96+ if @health <= 0
97+ @die !
98+ -- goto gameover
99+ else
100+ @movement_lock = 0.1
101+ @effects \ add effects. Flash 0.2
102+ @world . viewport\ shake!
103+
104+ die : =>
105+ @health = 0 if @health > 0
106+
107+ @movement_lock = nil -- STOP!!
108+ @effects \ add effects. Death 1.0
109+
110+ cx, cy = @box \ center!
111+
112+ @death_emitters = join {
113+ emitters. PourSmoke \ add @world , cx, cy
114+ emitters. RadialSpark \ add @world , cx, cy
115+ emitters. BigExplosion \ add @world , cx, cy
116+ }
117+
118+ @death_emitters . set " attach" , self
119+
72120 update : ( dt) =>
73121 @bullets \ update dt, @world
122+ @effects \ update dt
74123
75- -- see if enemies are hit
76- for e in * @world . enemies
77- if e. alive
78- for b in * @bullets
79- if b. alive and e. health > 0 and b\ touches_box e. box
80- e\ take_hit b
81- b. alive = false
82124
83- @effects \ update dt
125+ if @death_emitters
126+ cx, cy = @box \ center!
127+ running = @death_emitters . all ( e) ->
128+ if e. attach == self
129+ e. x = cx
130+ e. y = cy
131+ true
132+ @death_emitters = nil if not running
133+
134+ -- collide
135+ if @health > 0
136+ for e in * @world . enemies
137+ if e. alive and e. health > 0
138+
139+ -- see if a bullet is hitting enemy
140+ for b in * @bullets
141+ if b. alive and b\ touches_box e. box
142+ e\ take_hit b
143+ b. alive = false
144+
145+ -- see if we are hitting enemy
146+ if @movement_lock == 0 and @box \ touches_box e. box
147+ @take_hit 80
148+ @velocity = e. box\ vector_to( @box ) \ normalized! * 100
149+ e\ die!
84150
85151 -- movement
86- @movement_lock = math.max 0 , @movement_lock - dt
152+ if @movement_lock != nil
153+ @movement_lock = math.max 0 , @movement_lock - dt
87154
88155 if @movement_lock == 0
89156 move = movement_vector( @speed ) * dt * @accel
@@ -106,15 +173,14 @@ class Player extends Entity
106173
107174 cx, cy = @fit_move unpack @velocity * dt
108175 if cx -- hit a wall
109- @movement_lock = 0.1
110- @effects \ add effects. Flash 0.2
111- @world . viewport\ shake!
176+ @take_hit 5
112177 @velocity [ 1 ] = - @velocity [ 1 ]
113178
114- -- see if we are shooting
115- if keyboard. isDown @controls . shoot
116- t = timer. getTime!
117- if t - @last_shot > @fire_rate
118- @shoot !
119- @last_shot = t
179+ if @movement_lock == 0
180+ -- see if we are shooting
181+ if keyboard. isDown @controls . shoot
182+ t = timer. getTime!
183+ if t - @last_shot > @fire_rate
184+ @shoot !
185+ @last_shot = t
120186
0 commit comments