|
1 | 1 |
|
2 | 2 | g = love.graphics |
3 | 3 | import timer, keyboard from love |
| 4 | +import insert, remove from table |
4 | 5 |
|
5 | 6 | export * |
6 | 7 |
|
7 | | -class Dispatch -- this holds the stack of stuff |
8 | | - nil |
| 8 | +-- ugh |
| 9 | +fade_effect = (prev, current, t) -> |
| 10 | + t = smoothstep 0, 1, t |
| 11 | + |
| 12 | + if t < 0.5 |
| 13 | + prev\draw! |
| 14 | + |
| 15 | + g.setColor 0,0,0, 255 * t * 2 |
| 16 | + g.rectangle "fill", 0,0, g.getWidth!, g.getHeight! |
| 17 | + g.setColor 255,255,255,255 |
| 18 | + else |
| 19 | + current\draw! |
| 20 | + |
| 21 | + g.setColor 0,0,0, 255 * (1 - t) * 2 |
| 22 | + g.rectangle "fill", 0,0, g.getWidth!, g.getHeight! |
| 23 | + g.setColor 255,255,255,255 |
| 24 | + |
| 25 | +class Dispatch |
| 26 | + new: (initial) => |
| 27 | + @stack = { initial } |
| 28 | + |
| 29 | + send: (event, ...) => |
| 30 | + current = @top! |
| 31 | + current[event] current, ... if current and current[event] |
| 32 | + |
| 33 | + send_key: (...) => @send "on_key", ... |
| 34 | + |
| 35 | + top: => @stack[#@stack] |
| 36 | + parent: => @stack[#@stack - 1] |
| 37 | + |
| 38 | + push: (state) => |
| 39 | + @blend_effect = nil |
| 40 | + insert @stack, state |
| 41 | + |
| 42 | + push_with_effect: (state, time, efx) => |
| 43 | + @elapsed = 0 |
| 44 | + @effect_time = time |
| 45 | + |
| 46 | + prev = @top! |
| 47 | + @blend_effect = (t) => efx prev, state, t |
| 48 | + |
| 49 | + insert @stack, state |
| 50 | + |
| 51 | + pop: => |
| 52 | + @blend_effect = nil |
| 53 | + |
| 54 | + os.exit! if #stack == 0 |
| 55 | + remove @stack |
| 56 | + |
| 57 | + draw: => |
| 58 | + if @blend_effect |
| 59 | + @blend_effect @elapsed / @effect_time |
| 60 | + else |
| 61 | + @send "draw" |
| 62 | + |
| 63 | + update: (dt) => |
| 64 | + if @blend_effect |
| 65 | + @elapsed += dt |
| 66 | + @blend_effect = nil if @elapsed > @effect_time |
| 67 | + |
| 68 | + @send "update", dt |
9 | 69 |
|
10 | 70 | class TitleScreen |
11 | | - nil |
| 71 | + new: => |
| 72 | + @viewport = Viewport scale: 4 |
| 73 | + @img = imgfy"img/title.png" |
| 74 | + |
| 75 | + on_key: (key) => |
| 76 | + switch key |
| 77 | + when "escape" then os.exit! |
| 78 | + when "return" then @start_game! |
| 79 | + |
| 80 | + start_game: => |
| 81 | + dispatch\push_with_effect Game!, 1, fade_effect |
| 82 | + |
| 83 | + draw: => |
| 84 | + @viewport\apply! |
| 85 | + @img\draw 0, 0 |
12 | 86 |
|
13 | 87 | class GameOver |
14 | 88 | high_scores: { |
@@ -65,6 +139,6 @@ class Hud |
65 | 139 | if @display_score < @score |
66 | 140 | @display_score += dt * math.max 200, @score - @display_score |
67 | 141 | @display_score = math.min @display_score, @score |
68 | | - |
| 142 | + |
69 | 143 | @health_bar.value = @player.health / @player.max_health |
70 | 144 |
|
0 commit comments