summaryrefslogtreecommitdiff
path: root/src/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'src/controllers')
-rw-r--r--src/controllers/fish.moon54
-rw-r--r--src/controllers/mouse_keyboard.moon136
2 files changed, 183 insertions, 7 deletions
diff --git a/src/controllers/fish.moon b/src/controllers/fish.moon
new file mode 100644
index 0000000..1516574
--- /dev/null
+++ b/src/controllers/fish.moon
@@ -0,0 +1,54 @@
+ecs = require("ecs")
+world = require("world")
+controller = {}
+
+class FishControllerComponent extends ecs.Component
+ new: () =>
+ print("Fish controller started")
+ @node = am.group!
+ join: (entity) =>
+ super(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 < 4
+ --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.
+ if @net.properties.pos.x > 10 -- pick somewhere to the right
+ @net.properties.next_loc = vec2(
+ math.random(10.3,12),
+ math.random(-10,10)
+ )
+ @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}
diff --git a/src/controllers/mouse_keyboard.moon b/src/controllers/mouse_keyboard.moon
index b0b63b6..28c4f58 100644
--- a/src/controllers/mouse_keyboard.moon
+++ b/src/controllers/mouse_keyboard.moon
@@ -1,6 +1,11 @@
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
@@ -10,21 +15,26 @@ class MouseKeyboardControllerComponent extends ecs.Component
join: (ent) =>
graphic = ent\get("graphic")
@node = am.group!
- graphic.properties.node\append(@node)
+ graphic.node\append(@node)
net_component = ent\get("net")
+ @net = net_component
pred_component = ent\get("pred")
- bind_node = graphic.properties.node("bind")
+ @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.001
+ y += 0.0006
if win\key_down("s")
- y -= 0.001
+ y -= 0.0006
if win\key_down("a")
- x -= 0.001
+ x -= 0.0006
if win\key_down("d")
- x += 0.001
+ x += 0.0006
mouse_loc = win\mouse_position!
angle = -math.atan(mouse_loc.x / mouse_loc.y)
if mouse_loc.y > 0
@@ -43,10 +53,122 @@ class MouseKeyboardControllerComponent extends ecs.Component
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