summaryrefslogtreecommitdiff
path: root/src/spawn_fish.moon
diff options
context:
space:
mode:
authorAlexander M Pickering <alex@cogarr.net>2025-01-26 14:31:52 -0600
committerAlexander M Pickering <alex@cogarr.net>2025-01-26 14:31:52 -0600
commitc39ff632b46c179709101c5b50a061ebd723689f (patch)
tree23060311477b996cd11c50851e7dfb889d339346 /src/spawn_fish.moon
parentc1030d5ddbf34b1b19fa7fd169f3cf5a7b7f98f3 (diff)
downloadggj25-c39ff632b46c179709101c5b50a061ebd723689f.tar.gz
ggj25-c39ff632b46c179709101c5b50a061ebd723689f.tar.bz2
ggj25-c39ff632b46c179709101c5b50a061ebd723689f.zip
final commit
Diffstat (limited to 'src/spawn_fish.moon')
-rw-r--r--src/spawn_fish.moon141
1 files changed, 134 insertions, 7 deletions
diff --git a/src/spawn_fish.moon b/src/spawn_fish.moon
index 5bc090b..58c13d9 100644
--- a/src/spawn_fish.moon
+++ b/src/spawn_fish.moon
@@ -3,9 +3,92 @@ ecs = require("ecs")
world = require("world")
win = require("window")
sprites = require("world.sprites")
+colors = require("color")
+
+class BubbleGraphicsComponent extends world.GraphicsComponent
+ @loctbl = {
+ {-1,-1}
+ {-1,1}
+ {1,1}
+ {1,1}
+ {1,-1}
+ {-1,-1}
+ }
+ @max_bubbles = 6--128
+ @bubble_anim = {
+ sprites.sphere_normal_1
+ sprites.sphere_normal_2
+ sprites.sphere_normal_3
+ sprites.sphere_normal_4
+ sprites.sphere_normal_5
+ sprites.sphere_normal_6
+ }
+ buf_size: () =>
+ @@max_bubbles * 6
+ populate_buf: (geom_buffer, uv_buffer, offset) =>
+ -- we only need to store 1 frame info per 6 vertexes
+ @bubble_frames = am.buffer(@buf_size! / 6)\view("ubyte")
+ @buf = geom_buffer
+ @uv = uv_buffer
+ for i = 1, @@max_bubbles * 6-- must be divisible by 6
+ geom_buffer[i] = vec3(0,0,0)
+ uv_buffer[i] = vec2(1,0)
+ join: (entity) =>
+ super(entity)
+ aspect = win.width / win.height
+ program = @.node("use_program")
+ @cursor = 0
+ @node\remove(program)
+ @node\append(am.blend("off")\append(program))
+ bind = @.node("bind")
+ bind.water = 3
+ for name, color in pairs(colors.am_lake_color)
+ bind[name] = color
+ @ent = entity
+ comp = @
+ max_frames = @buf_size! / 6
+ bf = @bubble_frames
+ uv = @uv
+ buf = @buf
+ @node\action(() ->
+ bubble_anim = comp.__class.bubble_anim
+ for i = 1,max_frames
+ last_frame = bf[i]
+ if last_frame < 6
+ next_frame = last_frame + 1
+ nuv = bubble_anim[next_frame]
+ ustart = (i-1) * 6
+ uv[ustart + 1] = vec2(nuv.s1, nuv.t1)
+ uv[ustart + 2] = vec2(nuv.s2, nuv.t1)
+ uv[ustart + 3] = vec2(nuv.s2, nuv.t2)
+ uv[ustart + 4] = vec2(nuv.s2, nuv.t2)
+ uv[ustart + 5] = vec2(nuv.s1, nuv.t2)
+ uv[ustart + 6] = vec2(nuv.s1, nuv.t1)
+ bf[i] += 1
+
+ )
+ add_bubble: (pos) =>
+ h = 0.1
+ --print("About to add bubble, cursor is", @cursor)
+ @bubble_frames[(@cursor / 6)+1] = 1
+ for i = 1,6
+ @buf[@cursor + i] = vec3(@@loctbl[i][1]*h + pos.x, @@loctbl[i][2]*h + pos.y, 0)
+ @cursor += 6
+ if @cursor == (@@max_bubbles * 6)
+ @cursor = 0
+
+[[
+bg = BubbleGraphicsComponent("graphic",{})
+bubble_ent = ecs.Entity(nil,{
+ graphic: bg
+})
+bubble = (pos) ->
+ -- Create a bubble
+ bg\add_bubble(pos)
+ ]]
class FishGraphicComponent extends world.GraphicsComponent
- @fish_size = 0.5
+ @fish_size = 0.3
@static = false
@loctbl = {
{-1,-1}
@@ -15,6 +98,7 @@ class FishGraphicComponent extends world.GraphicsComponent
{1,-1}
{-1,-1}
}
+ @bubble_freq = 1
buf_size: () =>
6
populate_buf: (geom_view, normal_view, offset) =>
@@ -26,7 +110,7 @@ class FishGraphicComponent extends world.GraphicsComponent
for i = 1,6
loctbl = @@loctbl[i]
geom_view[i] = vec3(@@loctbl[i][1] * h, @@loctbl[i][2] * h, -1.5)
- uv = sprites.player_normal
+ uv = sprites.fish_normal
normal_view[1] = vec2(uv.s1,uv.t1)
normal_view[2] = vec2(uv.s1,uv.t2)
normal_view[3] = vec2(uv.s2,uv.t2)
@@ -42,20 +126,32 @@ class FishGraphicComponent extends world.GraphicsComponent
assert(@pred, "Fish graphic must have a predicted component")
@node\remove(program)
@node\append(am.blend("alpha")\append(program))
+ bind = @.node("bind")
+ for name, color in pairs(colors.am_lake_color)
+ bind[name] = color
+ bind.water = 3
@ent = entity
+ @last_bubble = am.current_time!
comp = @
move: (pos) =>
h = @@fish_size / 2
--print("Calling move with", pos)
--if @ent.state ~= "swimming"
-- error("called move while not swimming")
+ --@phys.shape\moveTo(pos.x,pos.y)
for i = 1,6
@buf[i] = vec3(@@loctbl[i][1] * h, @@loctbl[i][2] * h, -0.13) + vec3(pos.x, pos.y, 0)
+ --Fuck the bubles, the fish looks better without em
+ --if am.current_time! - @last_bubble > @@bubble_freq
+ --world.Bubble(pos)
+ --bubble(pos)
+ --print("should spawn bubble")
+ --@last_bubble = am.current_time!
friction = 0.1
class FishPredictedComponent extends ecs.PredictedComponent
new: (id) =>
- fish_speed = 0.00001
+ fish_speed = 0.0001
super(id, {accel: vec2(0,0), vel: vec2(0,0), pos: vec2(0,0)}, "netc", {
accel: () =>
--print("ent state:", @cc.state)
@@ -82,6 +178,7 @@ class FishPredictedComponent extends ecs.PredictedComponent
@gc = entity\get("graphic")
@properties.pos = @net.properties.pos
@cc = entity\get("control")
+ @pc = entity\get("phys")
@ent = entity
print("Got graphic component:",@gc, @gc.node)
print("And my node is", @node)
@@ -95,8 +192,13 @@ class FishPredictedComponent extends ecs.PredictedComponent
if @ent.state == "catching"
@gc.node.hidden=true
else
+ --TODO: Fish debugging
+ --@gc.node.hidden=true
@gc\move(@properties.pos)
+ @pc.shape\moveTo(@properties.pos.x, @properties.pos.y)
+max_fish = 100
+nfish = 0
class Fish extends ecs.Entity
new: (id, pos) =>
@width = 40 -- TODO: randomize fish width
@@ -108,23 +210,48 @@ class Fish extends ecs.Entity
phys: world.PhysicsComponent("phys",{},"circle",{pos.x, pos.y, @width / 256})
}
super(id, components)
+ nfish += 1
destroy: (...) =>
super(...)
- error("Where did fish go")
class SpawnFishComponent extends ecs.Component
new: (id, properties, spawnrect) =>
super(id, properties)
+ @spawnrect = spawnrect
+ @spawned = 0
+ @last_spawned = world.sync_time!
+ @spawn_freq = 8
join: (entity) =>
- graphic = entity\get("graphic")
+ --TODO: only do this on the elected peer
+ graphic = entity\get("water")
+ assert(graphic, "Spawn fish must have a graphic node")
+ comp = @
+ graphic.node\action(() =>
+ if nfish < max_fish and world.sync_time! - comp.last_spawned > comp.spawn_freq
+ comp.last_spawned = world.sync_time!
+ comp\spawn_fish!
+ )
+ for i = 1,10 do
+ @spawn_fish!
+ spawn_fish: () =>
+ rngx = math.random(@spawnrect.x, @spawnrect.z)
+ rngy = math.random(@spawnrect.y, @spawnrect.w)
+ Fish(nil, vec2(rngx, rngy))
+ [[
router = router.r!
set_spawnable = () ->
if router.state == "elected"
graphic\action(() ->
error("Spawn fish callback")
)
- set_spawnable!
- router\onchange(set_spawnable)
+ ]]
+ --set_spawnable!
+ --router\onchange(set_spawnable)
+ [[
+ graphic\action(() ->
+ error("spawn fish component")
+ )
+ ]]
{:Fish, :SpawnFishComponent}