summaryrefslogtreecommitdiff
path: root/src/controllers/mouse_keyboard.moon
blob: 28c4f58fa601c99a4b0364e49bc55dc78c92cdbd (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
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
ecs = require("ecs")
win = require("window")
world = require("world")
fish = require("spawn_fish")
rng = require("rng")
us = require("ui.sprites")
ui = require("ui")
assert(fish.Fish, "Failed to find fish from spawn_fish")
controller = {}

class MouseKeyboardControllerComponent extends ecs.Component
	new: (id, properties) =>
		@text_size = 1
		super(id, properties)
	join: (ent) =>
		graphic = ent\get("graphic")
		@node = am.group!
		graphic.node\append(@node)
		net_component = ent\get("net")
		@net = net_component
		pred_component = ent\get("pred")
		@pred = pred_component
		bind_node = graphic.node("bind")
		line = ent\get("line")
		comp = @
		@minigame_started = false
		@node\action(() =>
			x,y = 0,0
			accel = {x:0,y:0} -- x,y
			if win\key_down("w")
				y += 0.0006
			if win\key_down("s")
				y -= 0.0006
			if win\key_down("a")
				x -= 0.0006
			if win\key_down("d")
				x += 0.0006
			mouse_loc = win\mouse_position!
			angle = -math.atan(mouse_loc.x / mouse_loc.y)
			if mouse_loc.y > 0
				angle = angle + math.pi
			graphic\face(angle)
			--print("angle is:", angle)
			should_update = false
			-- Need to update net component somehow
			final_accel = vec2(x, y)
			if net_component.properties.accel ~= final_accel
				net_component.properties.accel = final_accel
			net_component.properties.last_update = am.current_time!
				--net_component.properties.vel = vec2(0,0)
				--should_update = true
			--print("Mouse keyboard action")
			pred_loc = pred_component.properties.pos
			world.world_x = pred_loc.x
			world.world_y = pred_loc.y
			--graphic\move(pred_loc.x, pred_loc.y)
			bind_node.world_x = pred_loc.x
			bind_node.world_y = pred_loc.y
			if win\mouse_pressed("left")
				if net_component.properties.casted and not pred_component.properties.can_reel and net_component.properties.reeling == 0
					--rectract line
					net_component.properties.casted = false
				elseif net_component.properties.casted and pred_component.properties.can_reel and net_component.properties.reeling == 0
					--catch a fish, gather all the info and delete the fish here.
					print("Before starting, reeling is", net_component.properties.reeling)
					net_component.properties.reeling = 1
					comp.bobber = ent.bobber
					f = ent.bobber.which
					f.state = "catching"
					comp\start_minigame(f)
					f\destroy!
				elseif net_component.properties.reeling > 0
					print("Reeling in fish!")
				else
					worldpos = world.fromscreen(win\mouse_position!)
					net_component.properties.cast = worldpos
					net_component.properties.casted = true
					net_component.properties.cast_time = world.sync_time!
					print("Set cast", net_component.properties)
				--test = require("test_entity")
				--ent = test.TestEntity(nil, vec3(worldpos,-1.1))
			if win\mouse_pressed("right")
				worldpos = world.fromscreen(win\mouse_position!)
				print("fish is:", fish)
				f = fish.Fish(nil,worldpos)
				f\get("net").properties.next_loc = worldpos + vec2(0,5)
				f\get("net").properties.next_loc_time = am.current_time!
		)
	start_minigame: (fish) =>
		@minigame_started = true
		node = am.group!
		hook_location = 0 -- 0 -> 1?
		hook_width = 5
		hook_gravity = 5 -- how quickly do we fall
		hook_force = 80 -- 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.1
		escape_speed = 0.01
		reel_cutoff = 10
		escape_cutoff = 10
		fish_location = math.random(-256,256)
		bar_sprite = require("ui.button")(40,-256,64,512,"").node
		node\append(bar_sprite)
		hook_sprite = am.translate(0,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(0,0)\append(am.scale(1)\append(am.group(fish_g_sprite, fish_b_sprite)))
		node\append(fish_sprite)
		ui.node\append(node)
		net = @net
		bobber = @bobber
		assert(bobber, "Failed to find bobber")
		node\action(() =>
			if win\mouse_pressed("left")
				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
			else
				escape_progress += escape_speed
				fish_g_sprite.hidden = true
				fish_b_sprite.hidden = false
			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
			elseif escape_progress > escape_cutoff
				net.properties.casted = false
				net.properties.reeling = 0
				ui.node\remove(node)
				bobber.which = nil
			--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

		)
		--error("Starting fishing minigame")

controller.text_size = 1
controller.Controller = MouseKeyboardControllerComponent

controller