aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-01-31 18:28:10 +0000
committers-ol <s-ol@users.noreply.github.com>2020-01-31 18:28:10 +0000
commit16962e1836251526fa08895b1a76dd72b7fd5ef9 (patch)
treeef60d08e792825407d41fb77c14c26ecb135e2fc /lib
parentnested parens in comments (diff)
downloadalive-16962e1836251526fa08895b1a76dd72b7fd5ef9.tar.gz
alive-16962e1836251526fa08895b1a76dd72b7fd5ef9.zip
modularize
Diffstat (limited to 'lib')
-rw-r--r--lib/debug.moon12
-rw-r--r--lib/gui.moon45
-rw-r--r--lib/math.moon68
-rw-r--r--lib/time.moon33
-rw-r--r--lib/util.moon13
5 files changed, 171 insertions, 0 deletions
diff --git a/lib/debug.moon b/lib/debug.moon
new file mode 100644
index 0000000..c2d0d52
--- /dev/null
+++ b/lib/debug.moon
@@ -0,0 +1,12 @@
+import Op from require 'base'
+
+class out extends Op
+ setup: (name, @chld) =>
+ @name = name\getc!
+
+ update: =>
+ print "#{@name} << ", @chld\get!
+
+{
+ :out
+}
diff --git a/lib/gui.moon b/lib/gui.moon
new file mode 100644
index 0000000..979854f
--- /dev/null
+++ b/lib/gui.moon
@@ -0,0 +1,45 @@
+{ graphics: lg } = love
+import Op from require 'base'
+import Copilot from require 'copilot'
+
+class out extends Op
+ setup: (name, @chld) =>
+ @@instances[@name] = nil if @name
+ @name = name\getc!
+ @@instances[@name] = @
+
+ update: =>
+ @value = @chld\get!
+
+ destroy: =>
+ @@instances[@name] = nil
+
+ MARGIN = 16
+ WIDTH = 24
+ HEIGHT = 120
+ @instances: {}
+ @draw_all: ->
+ outs = [name for name in pairs @@instances]
+ table.sort outs
+
+ x = MARGIN
+ lg.setColor 1, 1, 1
+ for name in *outs
+ value = @@instances[name].value * HEIGHT
+ lg.rectangle 'line', x, MARGIN, WIDTH, HEIGHT
+ lg.rectangle 'fill', x, MARGIN + HEIGHT - value, WIDTH, value
+ lg.print name, x, MARGIN + HEIGHT + MARGIN
+ x += WIDTH + MARGIN
+
+copilot = Copilot arg[#arg], env
+
+love.update = (dt) ->
+ copilot\poll!
+ env\update dt
+
+love.draw = ->
+ out.draw_all!
+
+{
+ :out
+}
diff --git a/lib/math.moon b/lib/math.moon
new file mode 100644
index 0000000..8ada38a
--- /dev/null
+++ b/lib/math.moon
@@ -0,0 +1,68 @@
+import Op from require 'base'
+unpack or= table.unpack
+
+class BinOp extends Op
+ setup: (...) =>
+ @children = { ... }
+ assert #@children >= 2, "#{@} needs at least two parameters"
+
+class add extends BinOp
+ update: =>
+ @value = 0
+ for child in *@children
+ @value += child\get!
+
+class sub extends BinOp
+ update: =>
+ @value = @children[1]\get!
+ for child in *@children[2,]
+ @value -= child\get!
+
+class mul extends BinOp
+ update: =>
+ @value = 1
+ for child in *@children
+ @value *= child\get!
+
+class div extends BinOp
+ update: =>
+ @value = @children[1]\get!
+ for child in *@children[2,]
+ @value /= child\get!
+
+func_op = (name, arity, func) ->
+ k = class extends Op
+ setup: (...) =>
+ @params = { ... }
+ if arity != '*'
+ assert #@params == arity, "#{@} needs exactly #{arity} parameters"
+
+ update: =>
+ @value = func unpack [param\get! for param in *@params]
+
+ k.__name = name
+ k
+
+module = {
+ :add, '+': add
+ :sub, '-': sub
+ :mul, '*': mul
+ :div, '/': div
+
+ mix: func_op 'mix', 3, (a, b, i) -> i*b + (1-i)*a
+}
+
+for name, arity in pairs {
+ exp: 1, log: 2, log10: 1, sqrt: 1
+
+ cos: 1, sin: 1, tan: 1
+ asin: 1, acos: 1, atan: 1, atan2: 2
+ sinh: 1, cosh: 1, tanh: 1
+
+ min: '*', max: '*'
+
+ floor: 1, ceil: 1, abs: 1
+}
+ module[name] = func_op name, arity, math[name]
+
+module
diff --git a/lib/time.moon b/lib/time.moon
new file mode 100644
index 0000000..f2ad70c
--- /dev/null
+++ b/lib/time.moon
@@ -0,0 +1,33 @@
+import Op, Const from require 'base'
+
+class lfo extends Op
+ tau = math.pi * 2
+ new: (...) =>
+ super ...
+ @phase = 0
+
+ setup: (@freq, @wave=Const 'sin') =>
+
+ update: (dt) =>
+ @phase += dt * @freq\get!
+ @value = switch @wave\get!
+ when 'sin' then .5 + .5 * math.cos @phase * tau
+ when 'saw' then @phase % 1
+ when 'tri' then math.abs (2*@phase % 2) - 1
+ else error "unknown wave type"
+
+class tick extends Op
+ new: (...) =>
+ super ...
+ @phase = 0
+
+ setup: (@freq) =>
+
+ update: (dt) =>
+ @phase += dt / @freq\get!
+ @value = math.floor @phase
+
+{
+ :lfo
+ :tick
+}
diff --git a/lib/util.moon b/lib/util.moon
new file mode 100644
index 0000000..7306fbc
--- /dev/null
+++ b/lib/util.moon
@@ -0,0 +1,13 @@
+import Op from require 'base'
+
+class pick extends Op
+ setup: (@i, ...) =>
+ @choices = { ... }
+
+ update: =>
+ i = 1 + (math.floor @i\get!) % #@choices
+ @value = @choices[i]\get!
+
+{
+ :pick
+}