/
game.moon
352 lines (262 loc) · 7.39 KB
/
game.moon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
{graphics: g, audio: a} = love
import Bin, HList, VList, Label, Border from require "lovekit.ui"
import Metronome, VisibilityMeter from require "ui"
import Track from require "track"
import BreakEmitter, SparkEmitter, ThreshEmitter from require "emitters"
import Face, Tongue from require "face"
class TrackField extends Box
w: 140
h: 240
lazy bg: -> imgfy "images/seat.png"
min_delta: 120
thresholds: {
{20, "great"}
{60, "good"}
{120, "meh"}
}
pixels_per_beat: 50
chain: 0
max_chain: 0
hits: 0
misses: 0
hit_great: 0
hit_good: 0
hit_meh: 0
new: (@game, @track, ...) =>
super ...
@notes_played = {}
@vib_seq = nil
@particles = DrawList!
@face = Face @track, 0, 0
@pixels_per_beat = @track.data.pixels_per_beat
print "setting pixels per beat to #{@pixels_per_beat}"
@main_seq = Sequence ->
wait_until -> @track.finished
import StatsSummary from require "ui"
DISPATCHER\push StatsSummary @
on_game_over: =>
print "entering game over"
@track\stop!
@track.finished = true
on_hit_note: (note) =>
@hits += 1
@chain += 1
@max_chain = math.max @max_chain, @chain
nx, ny = @note_position note
@eat_note note
thresh = @threshold_for_delta note.hit_delta
-- moonscript bug
@["hit_#{thresh}"] = @["hit_#{thresh}"] + 1
@particles\add ThreshEmitter thresh, @, nx,ny
@game\on_hit_note note
eat_note: (note) =>
nx, ny = @note_position note
spark = @particles\add SparkEmitter @, nx, ny
local sprite
seq = Sequence ->
tween sprite, 0.2, x: @face.tongue.x, y: @face.tongue.y
tween sprite, 0.1, a: 0
sprite = {
a: 1
x: nx, y: ny
update: (dt) =>
with seq\update dt
spark.x = @x
spark.y = @y
draw: =>
COLOR\pusha 255 * @a
note.sprite\draw note.quad, @x + note.ox, @y + note.oy
COLOR\pop!
}
@particles\add sprite
-- note might be nil if it was a mis-press
on_miss_note: (note, from_hit) =>
@chain = 0
if note
nx, ny = @note_position note
@particles\add BreakEmitter note, @, nx,ny
@misses += 1
@face\on_eye_pain note.col
if from_hit
@particles\add ThreshEmitter "miss", @, nx,ny
@shake!
if from_hit
AUDIO\play "miss"
@game\on_miss_note note, from_hit
shake: =>
@game.viewport\shake!
if joystick = CONTROLLER.joystick
@vib_seq = Sequence ->
print "vibrate start"
joystick\setVibration 0.5, 0.5
wait 0.1
print "vibrate stop"
joystick\setVibration!
@vib_seq = nil
draw: =>
return unless @track.playing or @track.finished
@bg\draw 10, @game.viewport.h - 140 + 2 * @track\sync_sin!
if DEBUG
g.rectangle "line", @unpack!
g.push!
g.translate @x, @y
@face\draw!
unless @track.finished
upper, lower = @get_bounds!
@current_top = upper
for note in @track.notes\each_note upper, lower
note\draw @note_position note
@particles\draw!
g.pop!
-- get the bottom and top in beats
get_bounds: =>
b, q = @track\get_beat!
b += q
_, offset_top = @face\eye_offset!
upper = b - (offset_top / @pixels_per_beat)
lower = b + (@h - offset_top) / @pixels_per_beat
upper, lower
can_input: =>
return false unless @track.playing
-- wait a moment
return false if love.timer.getTime! - @track.start_time < 0.2
true
update: (dt) =>
@particles\update dt
@main_seq\update dt if @main_seq
@face\update dt
return true unless @track.playing or @track.finished
local note, delta, pressed
if @vib_seq
@vib_seq\update dt
if @can_input!
if CONTROLLER\downed "one"
pressed = true
@face\hit_eye 1
note, delta = @find_hit_note 1
if CONTROLLER\downed "two"
pressed = true
@face\hit_eye 2
note, delta = @find_hit_note 2
if note
note.hit_delta = delta
@on_hit_note note
elseif pressed and not delta
@on_miss_note nil, true
-- note: we may have entered game over here from the miss note call above
@mark_missed_notes!
true
mark_missed_notes: =>
b, q = @track\get_beat!
return unless b
b += q
for note in @track.notes\each_note @get_bounds!
continue if note.hit_delta
continue if note.missed
beat_delta = b - note.beat
if beat_delta > 0
delta = @track\beat_to_ms beat_delta
if delta > @min_delta
note.missed = true
@on_miss_note note
-- returns note, delta is ms
find_hit_note: (col) =>
b, q = @track\get_beat!
return unless b
local min_note, min_d, early
bq = b + q
for note in @track.notes\each_note b - 1, b + 1
continue unless not col or note.col == col
continue if note.hit_delta
continue if note.missed
if min_note
delta = math.abs note.beat - bq
if delta < min_d
min_note = note
min_d = delta
early = note.beat > bq
else
min_note = note
min_d = math.abs note.beat - bq
early = note.beat > bq
return unless min_note
delta = @track\beat_to_ms min_d
if delta > @min_delta
min_note.missed = true
@on_miss_note min_note, true
return nil, true
-- print early and "hit early" or "hit late"
min_note, delta
-- relative to viewport
note_position: (note) =>
x = @face.eyes[note.col]\center!
y = (note.beat - @current_top) * @pixels_per_beat
x, y
threshold_for_delta: (delta) =>
for {t, name} in *@thresholds
if delta < t
return name
@thresholds[#@thresholds][2]
class Game
new: (@stage) =>
@viewport = DISPATCHER.viewport
@entities = EntityList!
@particles = DrawList!
@metronome = Metronome!
@visibility = VisibilityMeter!
@list = VList {
@visibility
VList {
with Label "game over if this goes to 0"
.color = {255,255,255, 100}
Label ""
Label "press -1- to start"
Label ""
Label "use -1- and -2- to flick tongue"
}
}
@hit_list = VList {}
@ui = Bin 0, 0, @viewport.w, @viewport.h, HList {
@list
-- VList {
-- Border VList({
-- Label -> "score: 0"
-- Label -> "chain: #{@field and @field.chain or 0}"
-- Label -> "hits: #{@field and @field.hits or 0}"
-- }), padding: 5
-- @hit_list
-- }
}
on_hit_note: (note) =>
@append_hit note.hit_delta
@visibility\increment!
on_miss_note: (note, from_hit) =>
@visibility\decrement!
if @visibility.p <= 0
@field\on_game_over!
append_hit: (delta) =>
table.insert @hit_list.items, 1, Label "#{math.floor delta}"
@hit_list.items[11] = nil
draw: =>
@ui\draw!
-- debug notes
-- if notes = @track and @track.notes
-- notes\draw!
on_key: (key) =>
if @field and @track and CONTROLLER\is_down("quit")
@field\on_game_over!
return true
update: (dt) =>
@ui\update dt
@entities\update dt
@particles\update dt
if CONTROLLER\downed "confirm", "1"
unless @track
print "Queue track"
@track = Track @stage.module
@track\prepare!
@entities\add @track
@metronome\set_track @track
@field = TrackField @, @track, 0,0
@list.items[2] = @field
{ :Game, :TrackField }