-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjake.lua
382 lines (324 loc) · 9.77 KB
/
jake.lua
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
include("jake/lib/snake")
include("jake/lib/rib")
include("jake/lib/apple")
-- clock
lattice = require("lattice")
-- music
music = require("musicutil")
-- engine
local MollyThePoly = require("molly_the_poly/lib/molly_the_poly_engine")
engine.name = "MollyThePoly"
-- output options
output_names = {"audio", "midi", "audio + midi", "crow out 1+2", "crow ii JF"}
active_notes = {}
midi_out_device = 1
midi_out_channel = 1
-- init
function init()
-- scale/root params infrastructure
scale_names = {}
for n=1,#music.SCALES do
table.insert(scale_names, music.SCALES[n].name)
end
note_names = {}
for n=0,100 do
table.insert(note_names, music.note_num_to_name(n,true))
end
root_note = 48
scale_index = 11
scale_name = scale_names[scale_index]
scale = music.generate_scale_of_length(root_note, scale_name, 24)
-- params
-- output
params:add_option("output", "output", output_names, 5)
params:set_action("output", set_output)
params:add{
type = "number",
id = "midi_out_device",
name = "midi out device",
min = 1, max = 4, default = 1,
action = function(value)
midi_out_device = midi.connect(value)
end
}
params:add{
type = "number",
id = "midi_out_channel",
name = "midi out channel",
min = 1, max = 16, default = 1,
action = function(value)
all_notes_off()
midi_out_channel = value
end
}
-- scale and root
params:add_separator()
params:add_option("scale", "scale", scale_names, 11)
params:set_action("scale", set_scale)
-- TODO: figure out why this displays the note off by one
params:add_option("root_note", "root note", note_names, 48)
params:set_action("root_note", set_root_note)
-- engine
params:add_separator()
MollyThePoly.add_params()
game_state = 'menu'
-- create a lattice
my_lattice = lattice:new{
auto = true,
meter = 4,
ppqn = 96
}
-- start the lattice
my_lattice:start()
-- redraw screen
redraw()
end
-- are you a bad enough dude?
function new_game()
game_state = 'playing'
all_notes_off()
-- make a snake
-- TODO: randomize starting position
local new_ribs = {}
table.insert(new_ribs, Rib:create{x=7,y=5,note=math.random(10),brightness=6})
table.insert(new_ribs, Rib:create{x=6,y=5,note=math.random(10),brightness=3})
table.insert(new_ribs, Rib:create{x=5,y=5,note=math.random(10),brightness=3})
table.insert(new_ribs, Rib:create{x=4,y=5,note=math.random(10),brightness=3})
new_direction = 'E'
snake = Snake:create{ribs = new_ribs, direction=new_direction}
-- make a movement pattern
movement_pattern = my_lattice:new_pattern{
action = function(t) move_snake() end,
division = 1/8
}
-- make a sequencer pattern
last_note = nil
sequencer_pattern = my_lattice:new_pattern{
action = function(t) advance_sequence() end,
division = 1/16
}
-- make initial apple
-- if the apple would overlap with the snake, keep trying new coordinates
-- TODO: is this a memory leak?
-- TODO: include this logic in the Apple "class"
apple = Apple:create{x=math.random(16), y=math.random(8)}
while snake:check_coordinate_occupied(apple.x, apple.y) do
apple = Apple:create{x=math.random(16), y=math.random(8)}
end
movement_pattern.enabled = true
grid_redraw()
redraw()
end
-- game over, pal
function game_over()
game_state = 'game_over'
note_off(last_note)
movement_pattern:stop()
sequencer_pattern:destroy()
redraw()
end
-- move it along, buddy
function move_snake()
-- manage movement
snake:steer(new_direction)
snake:step()
-- check if we've lost
if snake:check_self_collision() then
game_over()
end
-- check if we've eaten an apple (yum)
if snake:check_apple_collision(apple) then
-- calculate note from apple position
-- TODO: calculate notes some other way
apple_note = apple.x + apple.y - 1
-- delete old apple and make a new apple
-- but, if the new apple would overlap with the snake, keep trying new coordinates
apple = Apple:create{x=math.random(16), y=math.random(8)}
while snake:check_coordinate_occupied(apple.x, apple.y) do
apple = Apple:create{x=math.random(16), y=math.random(8)}
end
-- grow a rib
snake:grow(apple_note)
end
-- redraw everything
grid_redraw()
redraw()
end
-- output options
function set_output(output)
all_notes_off()
if output == 4 then
-- TODO: change to ar
crow.output[2].action = "{to(5,0),to(0,0.25)}"
elseif output == 5 then
crow.ii.pullup(true)
crow.ii.jf.mode(1)
end
end
-- sequence and music stuff
function set_scale(new_scale_name)
scale = music.generate_scale_of_length(root_note, new_scale_name, 24)
end
function set_root_note(new_root_note)
root_note = new_root_note
scale = music.generate_scale_of_length(new_root_note, scale_name, 24)
end
function advance_sequence()
-- get next note based on scale and step
local this_note = scale[snake.ribs[snake.active_step].note]
-- turn off last note
-- TODO: gate length?
if last_note then
note_off(last_note)
end
-- play note
note_on(this_note)
last_note = this_note
-- advance counter
snake.active_step = (snake.active_step) % (#snake.ribs) + 1
-- TODO: redraw snake on grid to show active step?
end
-- turn on note for selected output
function note_on(note)
-- audio
if params:get("output") == 1 then
local freq = music.note_num_to_freq(note)
engine.noteOn(note, freq, 80)
table.insert(active_notes, note)
-- MIDI
elseif params:get("output") == 2 then
midi_out_device:note_on(note, 80, midi_out_channel)
table.insert(active_notes, note)
-- audio/MIDI
elseif params:get("output") == 3 then
local freq = music.note_num_to_freq(note)
engine.noteOn(note, freq, 80)
midi_out_device:note_on(note, 80, midi_out_channel)
table.insert(active_notes, note)
-- crow
elseif params:get("output") == 4 then
crow.output[1].volts = (note)/12
crow.output[2].execute()
-- jf
elseif params:get("output") == 5 then
crow.ii.jf.play_note((note-60)/12,5)
end
end
-- turn off note for MIDI and engine
function note_off(note)
-- if params:get("output") == 1 then
-- engine.noteOff(note)
-- elseif params:get("output") == 2 then
-- midi_out_device:note_off(note, nil, midi_out_channel)
-- elseif params:get("output") == 3 then
engine.noteOff(note)
midi_out_device:note_off(note, nil, midi_out_channel)
-- end
end
-- turn off all notes for MIDI and engine
function all_notes_off()
for n,note in pairs(active_notes) do
note_off(note)
end
active_notes = {}
end
-- grid interaction
g = grid.connect()
function grid_redraw()
g:all(0)
snake:draw()
apple:draw()
g:refresh()
end
g.key = function(x,y,z)
-- if the snake is traveling horizontally, a press above/below
-- will change the direction to be upward/downward.
-- if the snake is traveling vertically, a press to the left/right
-- will change the direction to be leftward/rightward.
if z == 1 then
if snake.direction == 'E' or snake.direction == 'W' then
if y < snake.ribs[1].y then
new_direction = 'N'
elseif y > snake.ribs[1].y then
new_direction = 'S'
end
elseif snake.direction == 'S' or snake.direction == 'N' then
if x < snake.ribs[1].x then
new_direction = 'W'
elseif x > snake.ribs[1].x then
new_direction = 'E'
end
end
end
end
-- key input
function key(n,z)
if n==3 and z==1 then
if game_state == 'menu' then
new_game()
movement_pattern:toggle()
end
if game_state == 'playing' then
movement_pattern:toggle()
end
if game_state == 'game_over' then
new_game()
end
end
end
-- enc input
function enc(n,delta)
end
-- screen redraw
-- TODO: the screen should probably display sequencer parameters
-- TODO: high score would be cute too
function redraw()
screen.clear()
if game_state == 'menu' then
screen.move(10, 20)
screen.level(5)
screen.font_face(13)
screen.font_size(24)
screen.text('main menu')
screen.move(15, 35)
screen.level(10)
screen.font_face(1)
screen.font_size(8)
screen.text('press k3 for a new game')
end
if game_state == 'playing' then
screen.move(10, 20)
screen.level(5)
screen.font_face(13)
screen.font_size(24)
screen.text('snake!')
screen.move(15, 35)
screen.level(10)
screen.font_face(1)
screen.font_size(8)
screen.text('snake!')
screen.move(20, 45)
screen.level(10)
screen.font_face(1)
screen.font_size(8)
screen.text("you're playing snake!")
end
if game_state == 'game_over' then
screen.move(10, 20)
screen.level(5)
screen.font_face(13)
screen.font_size(24)
screen.text('game over')
screen.move(15, 35)
screen.level(10)
screen.font_face(1)
screen.font_size(8)
screen.text('you are no longer a snake')
screen.move(20, 45)
screen.level(10)
screen.font_face(1)
screen.font_size(8)
screen.text('press k3 for a new game')
end
screen.update()
end