summaryrefslogtreecommitdiff
path: root/src/minigame.moon
diff options
context:
space:
mode:
Diffstat (limited to 'src/minigame.moon')
-rw-r--r--src/minigame.moon139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/minigame.moon b/src/minigame.moon
new file mode 100644
index 0000000..25d567a
--- /dev/null
+++ b/src/minigame.moon
@@ -0,0 +1,139 @@
+ui = require("ui")
+us = require("ui.sprites")
+settings = require("settings")
+win = require("window")
+color = require("color")
+game = {}
+
+caught = am.translate(-win.width/2,win.height/2)\append(am.scale(2)\append(am.text("Caught: 0",color.am_color.highlight,"left","top")))
+caught.hidden = true
+ui.node\append(caught)
+
+am.ascii_color_map = {
+ o: color.am_color.outline
+ p: color.am_color.foreground
+ b: color.am_lake_color.foreground
+}
+
+bar_sprite = (char, w, h) ->
+ sprite = {}
+ sprite[1] = "o"\rep(w)
+ for i = 2, h - 1
+ sprite[i] = char\rep(w)
+ sprite[h] = "o"\rep(w)
+ table.concat(sprite,"\n")
+bar_y = 276
+catch_bar = am.translate(0,bar_y)\append(am.scale(vec2(0,4))\append(am.sprite(
+ bar_sprite("p",256,8), nil, "left"
+)))
+escape_bar = am.translate(0,bar_y)\append(am.scale(vec2(0,4))\append(am.sprite(
+ bar_sprite("b",256,8), nil, "right"
+)))
+catch_bar_cutoff = am.translate(256,bar_y)\append(am.scale(vec2(1,4))\append(am.sprite(
+ bar_sprite("p",1,8), nil, "left"
+)))
+escape_bar_cutoff = am.translate(-256,bar_y)\append(am.scale(vec2(1,4))\append(am.sprite(
+ bar_sprite("b",1,8), nil, "right"
+)))
+game.run = (net, bobber, fish, pull) ->
+ node = am.group!
+ hook_location = 0 -- 0 -> 1?
+ hook_width = 5
+ hook_gravity = 5 -- how quickly do we fall
+ hook_force = 120 -- how quickly do we accelerate
+ hook_vel = 0
+ hook_length = 1
+ hook_bounce_damp = 0.2 --how elastic is the bottom and top?
+ fish_width = fish.width
+ fish_force = 100
+ fish_gravity = 0
+ fish_activity = 0.1
+ fish_vel = 0
+ fish_bounce_damp = 1 -- perfectly elsastic bounce
+ reel_progress = 0
+ escape_progress = 0
+ reel_speed = 0.02
+ escape_speed = 0.012
+ reel_cutoff = 10
+ escape_cutoff = 10
+ last_played = am.current_time!
+ play_delay = 0.1
+ fish_location = math.random(-256,256)
+ bar_sprite = require("ui.button")(40,-256,64,512,"").node
+ node\append(bar_sprite)
+ hook_sprite = am.translate(72,0)\append(am.scale(1)\append(am.sprite(us.hook)))
+ node\append(hook_sprite)
+ fish_g_sprite = am.sprite(us.fish_purple)
+ fish_b_sprite = am.sprite(us.fish_blue)
+ fish_sprite = am.translate(72,0)\append(am.scale(vec2(1,fish_width / fish_g_sprite.height))\append(am.group(fish_g_sprite, fish_b_sprite)))
+ node\append(fish_sprite)
+ ui.node\append(node)
+ assert(bobber, "Failed to find bobber")
+ node\append(catch_bar)
+ node\append(escape_bar)
+ node\append(catch_bar_cutoff)
+ node\append(escape_bar_cutoff)
+ caught\action(() =>
+ if net.properties.fish_caught > 0
+ @hidden = false
+ @("text").text = string.format("Caught: %d", net.properties.fish_caught)
+ )
+ node\action(() =>
+ if pull!
+ hook_vel += hook_force
+ else
+ hook_vel -= hook_gravity
+ hook_location += hook_vel * am.delta_time
+ if hook_location < -256 + 16 -- bounce
+ hook_vel = -hook_vel * hook_bounce_damp
+ hook_location = -256 + 16
+ elseif hook_location > 256 - 16
+ hook_vel = -hook_vel * hook_bounce_damp
+ hook_location = 256 - 16
+ if math.random! < fish_activity
+ fish_vel = fish_force * math.random(-1,1)
+ fish_location += fish_vel * am.delta_time
+ if fish_location < -256 + 16 -- bounce
+ fish_vel = -fish_vel * fish_bounce_damp
+ fish_location = -256 + 16
+ elseif fish_location > 256 - 16
+ fish_vel = -fish_vel * fish_bounce_damp
+ fish_location = 256 - 16
+
+ if hook_location - hook_width > fish_location - fish_width and hook_location + hook_width < fish_location + fish_width
+ fish_b_sprite.hidden = true
+ fish_g_sprite.hidden = false
+ reel_progress += reel_speed
+ if am.current_time! - last_played > play_delay
+ @action("sfx",am.play(32044500, false, 1 + (reel_progress/10), settings.volume))
+ last_played = am.current_time!
+ else
+ escape_progress += escape_speed
+ fish_g_sprite.hidden = true
+ fish_b_sprite.hidden = false
+ if am.current_time! - last_played > play_delay
+ @action("sfx",am.play(3469004, false, 1 + (escape_progress/10), settings.volume))
+ last_played = am.current_time!
+ catch_bar("scale").x = escape_progress / escape_cutoff
+ escape_bar("scale").x = reel_progress / reel_cutoff
+ if reel_progress > reel_cutoff
+ net.properties.fish_caught += 1
+ net.properties.casted = false
+ net.properties.reeling = 0
+ ui.node\remove(node)
+ bobber.which = nil
+ @action("sfx",am.play(12507000, false, 1, settings.volume))
+ elseif escape_progress > escape_cutoff
+ net.properties.casted = false
+ net.properties.reeling = 0
+ ui.node\remove(node)
+ bobber.which = nil
+ @action("sfx",am.play(9122106, false, 1, settings.volume))
+ --print("reel:", reel_progress, "escape:", escape_progress)
+ -- Updates all sprites at the end?
+ hook_sprite("translate").y = hook_location
+ fish_sprite("translate").y = fish_location
+ )
+
+
+game