X Tutup
Skip to content

Commit eb648ff

Browse files
committed
player takes damage/dies, and hits enemies
1 parent 16a2855 commit eb648ff

File tree

5 files changed

+141
-39
lines changed

5 files changed

+141
-39
lines changed

enemy.moon

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Enemy extends Entity
162162
@box\move unpack @velocity * dt
163163
v = @world.viewport
164164

165-
if @health < 0 and #@effects == 0
165+
if @health <= 0 and #@effects == 0
166166
return false
167167

168168
-- make it follow!
@@ -174,20 +174,22 @@ class Enemy extends Entity
174174
-- are they in the world?
175175
@box\above_of v or v\touches_box @box
176176

177+
die: =>
178+
@health = 0 if @health > 0
179+
@velocity[2] /= 2
180+
@effects\add effects.Death 1.0
181+
182+
cx, cy = @box\center!
183+
emitters.Explosion\add w, cx, cy
184+
@death_emitter = with emitters.PourSmoke\add @world, cx, cy
185+
.attach = self
186+
177187
take_hit: (bullet) =>
178188
emitters.HitEnemy\add @world, bullet.x, bullet.y
179189

180190
@health -= bullet.damage
181-
if @health < 0
182-
@velocity[2] /= 2
183-
@effects\add effects.Death 1.0
184-
185-
cx, cy = @box\center!
186-
emitters.Explosion\add w, cx, cy
187-
@death_emitter = with emitters.PourSmoke\add w, cx, cy
188-
.attach = self
189-
190-
-- boost score!
191+
if @health <= 0
192+
@die!
191193
game.hud.score += 44
192194
else
193195
@effects\add effects.Flash 0.1

main.moon

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,9 @@ class Game
8787
@world\draw!
8888
@player\draw!
8989

90-
-- g.print tostring(timer.getFPS!), 2, 2
91-
92-
-- if Emitter.emitter_list
93-
-- g.print tostring(#Emitter.emitter_list), 2, 12
94-
-- g.print tostring(#Emitter.draw_list), 2, 22
90+
g.print tostring(timer.getFPS!), 2, 12
9591

9692
@hud\draw!
97-
9893
@viewport\pop!
9994

10095
snapper = nil
@@ -108,12 +103,14 @@ love.load = ->
108103
g.setFont font
109104

110105
love.mousepressed = (x,y, button) ->
111-
x, y = viewport\unproject x, y
106+
x, y = game.viewport\unproject x, y
112107
-- emitters.PourSmoke\add w, x, y
113108

114109
love.keypressed = (key, code) ->
115110
switch key
116111
when "escape" then os.exit!
112+
when "d"
113+
game.player\die!
117114
when "x" -- cool
118115
if snapper
119116
slow_mode = false

particle.moon

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,20 @@ class Explosion extends Emitter
217217
default_particle: particles.Explosion
218218
amount: 1
219219

220+
class BigExplosion extends Emitter
221+
default_particle: particles.Explosion
222+
amount: 4
223+
224+
spawn: (...) =>
225+
@x += math.random -2,2
226+
@y += math.random -2,2
227+
super ...
228+
229+
class RadialSpark extends Emitter
230+
default_particle: particles.Spark
231+
amount: 20
232+
rate: 0.03
233+
vel: 100
234+
ay: 500
235+
fan: 2*math.pi
236+

player.moon

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1631
class 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

ui.moon

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,28 @@ import timer, keyboard from love
44

55
export *
66

7+
class Dispatch -- this holds the stack of stuff
8+
nil
9+
710
class TitleScreen
811
nil
912

1013
class GameOver
11-
nil
14+
high_scores: {
15+
1000, 10000, 50000, 100000, 250000, 500000, 1000000
16+
}
17+
18+
new: =>
19+
@sort_scores!
20+
21+
sort_scores: =>
22+
table.sort @high_scores
23+
24+
add_score: (score) =>
25+
table.insert @high_scores, scores
26+
@sort_scores!
27+
28+
draw_high_scores: =>
1229

1330
class Pause
1431
nil
@@ -25,7 +42,9 @@ class HorizBar
2542
g.rectangle "line", x, y, @w, @h
2643

2744
g.setColor @color
28-
g.rectangle "fill", x + @padding, y + @padding, @w - @padding*2, @h - @padding*2
45+
w = @value * (@w - @padding*2)
46+
47+
g.rectangle "fill", x + @padding, y + @padding, w, @h - @padding*2
2948
g.pop!
3049

3150
g.setColor 255,255,255,255
@@ -46,5 +65,6 @@ class Hud
4665
if @display_score < @score
4766
@display_score += dt * math.max 200, @score - @display_score
4867
@display_score = math.min @display_score, @score
49-
68+
69+
@health_bar.value = @player.health / @player.max_health
5070

0 commit comments

Comments
 (0)
X Tutup