don't rely on shallow copy to construct new classes. also multiple ducks

This commit is contained in:
2025-02-15 17:25:27 +03:00
parent d4c79731b0
commit 954277688f
5 changed files with 110 additions and 59 deletions

View File

@ -12,32 +12,34 @@ local States = {
local WANDER_TIME = 1.0
local IDLE_TIME = 1.0
local Duck = {
position = {},
velocity = {},
direction = {},
target = nil,
state = States.IDLE,
wander_timer = 0,
speed = 0.02,
chase_speed = 0.04,
accel = 0.5,
STATES = States,
AteFeed = Signal.new()
}
local Duck = {}
Duck.__index = Duck
function Duck.new(position)
local d = util.shallow_copy(Duck)
d.position = position
d.velocity = Vector3()
d.direction = Vector3(0, 0, -1):rotated(Vector3.UP, util.random_float(-math.pi, math.pi))
-- local d = util.shallow_copy(Duck)
local d = {
position = position:copy(),
velocity = Vector3(),
direction = Vector3(0, 0, -1):rotated(Vector3.UP, util.random_float(-math.pi, math.pi)),
target = nil,
state = States.IDLE,
wander_timer = 0,
speed = 0.02,
chase_speed = 0.04,
accel = 0.5,
STATES = States,
AteFeed = Signal.new(),
SeekFeed = Signal.new(),
index = 1,
}
d.position.y = 0.4
return setmetatable(d, Duck)
end
@ -68,6 +70,7 @@ function Duck:wander(delta)
if self.wander_timer >= WANDER_TIME then
self.state = States.IDLE
self.wander_timer = 0
self.SeekFeed:emit(self)
return
end
@ -96,6 +99,7 @@ end
function Duck:start_chase(feed)
if self.state == States.CHASE then return end
print("duck " .. self.index .. " starting chase")
self.state = States.CHASE
self.target = feed
feed.occupied = true

View File

@ -2,22 +2,20 @@ local Vector3 = require "types.vector3"
local TEXTURE = "images/tongue.png"
local Feed = {
position = {},
direction = {},
velocity = {},
landed = false,
occupied = false,
speed = 0.15,
}
local Feed = {}
Feed.__index = Feed
function Feed.new(p_position, p_direction)
local f = util.shallow_copy(Feed)
f.position = p_position:copy()
f.direction = p_direction:copy()
function Feed.new(position, direction)
local f = {
position = position:copy(),
direction = direction:copy(),
velocity = Vector3(),
landed = false,
occupied = false,
speed = 0.15,
}
f.velocity = f.direction * f.speed
f.velocity.y = 0.1