summaryrefslogtreecommitdiff
path: root/src/controllers/fish.moon
blob: 3c829f35af4afcb08ad62bb34062ad2bd0e59522 (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
ecs = require("ecs")
world = require("world")
controller = {}




class FishControllerComponent extends ecs.Component
	new: () =>
		print("Fish controller started")
		@node = am.group!
	join: (entity) =>
		super(entity)
		@ent = entity
		graphic = entity\get("graphic")
		assert(graphic, "Fish controller must have a graphic")
		pred_component = entity\get("pred")
		assert(pred_component, "Fish controller must have a predicted component")
		net_component = entity\get("net")
		assert(net_component, "Fish controller must have a net component")
		@net = net_component
		graphic.node\append(@node)
		comp = @
		locpicker = coroutine.create(() =>
			comp\pick_next_location!
			coroutine.yield!
		)
		--either "swimming", "waiting", or "catching"
		@state = "swimming"
		@node\action(coroutine.create(() =>
			while comp.state == "swimming"
				comp.state = "waiting"
				start_wait = world.sync_time!
				while world.sync_time! - start_wait < 8
					--TODO: look for nearby hooks and get caught
					coroutine.yield!
				if comp.state == "waiting"
					comp\pick_next_location!
					comp.state = "swimming"
					while math.distance(pred_component.properties.pos, net_component.properties.next_loc) > 0.01
						--print("At ", pred_component.properties.pos, "waiting to get to next location, ", net_component.properties.next_loc, "it is ", math.distance(pred_component.properties.pos, net_component.properties.next_loc), " away")
						coroutine.yield!

		))
	pick_next_location: () =>
		-- Pick somewhere to swim based on where we are?
		-- This can only be done on the host.
		--TODO: spawn fish on all sides.
		-- everything has to be floats so math.random returns a float.
		if @net.properties.pos.x > 10 -- pick somewhere to the right
			@net.properties.next_loc = vec2(
				math.random(11.1,15),
				math.random(-10.1,10)
			)
		elseif @net.properties.pos.x < -10
			@net.properties.next_loc = vec2(
				math.random(-15.1,-11)
				math.random(-10.1,10)
			)
		elseif @net.properties.pos.y > 10
			@net.properties.next_loc = vec2(
				math.random(-10.1,10)
				math.random(11.1,15)
			)
		elseif @net.properties.pos.y < -10
			@net.properties.next_loc = vec2(
				math.random(-10.1,10)
				math.random(-15.1,-11)
			)
		else
			--If none of these apply, we're stuck on land somehow, delete ourselves
			@entity\destroy!

		@net.properties.next_loc_time = world.sync_time!
		--@net.properties.next_loc = @net.properties.pos + vec2(0,1)
		--@net.properties.next_loc_time = world.sync_time!
		print("Picking next location, it was ", @net.properties.next_loc)

{:FishControllerComponent}