summaryrefslogtreecommitdiff
path: root/src/minigame.moon
blob: 25d567a88904176b127f32b37e66bad7b831104d (plain)
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
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