summaryrefslogtreecommitdiff
path: root/src/controllers/mouse_keyboard.moon
blob: cc33a7256710718f8ad43116073a473e5cd0e55b (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
ecs = require("ecs")
win = require("window")
world = require("world")
fish = require("spawn_fish")
rng = require("rng")
us = require("ui.sprites")
ui = require("ui")
minigame = require("minigame")
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)
					-- TODO: make net destory the fish, not the controller
					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
		pull = () ->
			win\mouse_pressed("left")
		minigame.run(@net, @bobber, fish, pull)

controller.text_size = 1
controller.Controller = MouseKeyboardControllerComponent

controller