don't rely on shallow copy to construct new classes. also multiple ducks
This commit is contained in:
parent
d4c79731b0
commit
954277688f
@ -12,32 +12,34 @@ local States = {
|
|||||||
local WANDER_TIME = 1.0
|
local WANDER_TIME = 1.0
|
||||||
local IDLE_TIME = 1.0
|
local IDLE_TIME = 1.0
|
||||||
|
|
||||||
local Duck = {
|
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
Duck.__index = Duck
|
Duck.__index = Duck
|
||||||
|
|
||||||
function Duck.new(position)
|
function Duck.new(position)
|
||||||
local d = util.shallow_copy(Duck)
|
-- local d = util.shallow_copy(Duck)
|
||||||
d.position = position
|
local d = {
|
||||||
d.velocity = Vector3()
|
position = position:copy(),
|
||||||
d.direction = Vector3(0, 0, -1):rotated(Vector3.UP, util.random_float(-math.pi, math.pi))
|
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)
|
return setmetatable(d, Duck)
|
||||||
end
|
end
|
||||||
@ -68,6 +70,7 @@ function Duck:wander(delta)
|
|||||||
if self.wander_timer >= WANDER_TIME then
|
if self.wander_timer >= WANDER_TIME then
|
||||||
self.state = States.IDLE
|
self.state = States.IDLE
|
||||||
self.wander_timer = 0
|
self.wander_timer = 0
|
||||||
|
self.SeekFeed:emit(self)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -96,6 +99,7 @@ end
|
|||||||
|
|
||||||
function Duck:start_chase(feed)
|
function Duck:start_chase(feed)
|
||||||
if self.state == States.CHASE then return end
|
if self.state == States.CHASE then return end
|
||||||
|
print("duck " .. self.index .. " starting chase")
|
||||||
self.state = States.CHASE
|
self.state = States.CHASE
|
||||||
self.target = feed
|
self.target = feed
|
||||||
feed.occupied = true
|
feed.occupied = true
|
||||||
|
@ -2,22 +2,20 @@ local Vector3 = require "types.vector3"
|
|||||||
|
|
||||||
local TEXTURE = "images/tongue.png"
|
local TEXTURE = "images/tongue.png"
|
||||||
|
|
||||||
local Feed = {
|
local Feed = {}
|
||||||
position = {},
|
|
||||||
direction = {},
|
|
||||||
velocity = {},
|
|
||||||
landed = false,
|
|
||||||
occupied = false,
|
|
||||||
|
|
||||||
speed = 0.15,
|
|
||||||
}
|
|
||||||
|
|
||||||
Feed.__index = Feed
|
Feed.__index = Feed
|
||||||
|
|
||||||
function Feed.new(p_position, p_direction)
|
function Feed.new(position, direction)
|
||||||
local f = util.shallow_copy(Feed)
|
local f = {
|
||||||
f.position = p_position:copy()
|
position = position:copy(),
|
||||||
f.direction = p_direction:copy()
|
direction = direction:copy(),
|
||||||
|
velocity = Vector3(),
|
||||||
|
landed = false,
|
||||||
|
occupied = false,
|
||||||
|
|
||||||
|
speed = 0.15,
|
||||||
|
}
|
||||||
f.velocity = f.direction * f.speed
|
f.velocity = f.direction * f.speed
|
||||||
f.velocity.y = 0.1
|
f.velocity.y = 0.1
|
||||||
|
|
||||||
|
@ -7,23 +7,33 @@ local Feed = require "classes.feed"
|
|||||||
local feed = List()
|
local feed = List()
|
||||||
|
|
||||||
local Duck = require "classes.duck"
|
local Duck = require "classes.duck"
|
||||||
local ducks = List{Duck.new(Vector3(0, 0.4, 3))}
|
local ducks = List()
|
||||||
|
|
||||||
local function create_feed(position, direction)
|
local function create_feed(position, direction)
|
||||||
|
print("?")
|
||||||
local f = Feed.new(position, direction)
|
local f = Feed.new(position, direction)
|
||||||
feed:push(f)
|
feed:push(f)
|
||||||
local eligible_ducks = ducks:filter(
|
local eligible_ducks = ducks:filter(
|
||||||
function (duck)
|
function (duck)
|
||||||
return duck.state ~= Duck.STATES.CHASE
|
return duck.state ~= duck.STATES.CHASE
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
if #eligible_ducks == 0 then return end
|
if eligible_ducks:is_empty() then return end
|
||||||
eligible_ducks[1]:start_chase(f)
|
eligible_ducks[1]:start_chase(f)
|
||||||
f.occupied = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function delete_feed(f)
|
local function delete_feed(f)
|
||||||
util.list_remove_value(feed, f)
|
feed:remove_value(f)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function duck_seek_feed(duck)
|
||||||
|
local eligible_feeds = feed:filter(
|
||||||
|
function (f)
|
||||||
|
return feed.occupied == false
|
||||||
|
end
|
||||||
|
)
|
||||||
|
if eligible_feeds:is_empty() then return end
|
||||||
|
duck:start_chase(eligible_feeds[1])
|
||||||
end
|
end
|
||||||
|
|
||||||
-- called every frame, with constant delta time
|
-- called every frame, with constant delta time
|
||||||
@ -35,9 +45,18 @@ function game_tick()
|
|||||||
capture = false,
|
capture = false,
|
||||||
}
|
}
|
||||||
player.ThrowPressed:connect(create_feed)
|
player.ThrowPressed:connect(create_feed)
|
||||||
for _, v in ipairs(ducks) do
|
|
||||||
v.AteFeed:connect(delete_feed)
|
-- spawn some ducks
|
||||||
|
for i = 1, 5 do
|
||||||
|
local duck = Duck.new(Vector3(0, 0, -math.random() * 10.0):rotated(Vector3.UP, util.random_float(-math.pi, math.pi)))
|
||||||
|
ducks:push(duck)
|
||||||
|
duck.AteFeed:connect(delete_feed)
|
||||||
|
duck.SeekFeed:connect(duck_seek_feed)
|
||||||
|
duck.index = i
|
||||||
end
|
end
|
||||||
|
print(ducks[1].AteFeed._connections)
|
||||||
|
print(ducks[2].AteFeed._connections)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ctx.mouse_capture = ctx.udata.capture
|
ctx.mouse_capture = ctx.udata.capture
|
||||||
|
@ -55,10 +55,11 @@ function List:__tostring()
|
|||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
---Appends v to the end of the list.
|
---Appends value to the end of the list.
|
||||||
---@param v any
|
---@param value any
|
||||||
function List:push(v)
|
function List:push(value)
|
||||||
table.insert(self, v)
|
if self:has(value) then return end
|
||||||
|
table.insert(self, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Removes the last element in the list and returns it.
|
---Removes the last element in the list and returns it.
|
||||||
@ -82,6 +83,13 @@ function List:filter(predicate)
|
|||||||
return filter(self, predicate)
|
return filter(self, predicate)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Removes the value at the given index.
|
||||||
|
---@param idx integer
|
||||||
|
---@return any
|
||||||
|
function List:remove_at(idx)
|
||||||
|
return table.remove(self, idx)
|
||||||
|
end
|
||||||
|
|
||||||
---Returns the index of value, if it exists in the list, -1 otherwise.
|
---Returns the index of value, if it exists in the list, -1 otherwise.
|
||||||
---@param value any
|
---@param value any
|
||||||
---@return integer
|
---@return integer
|
||||||
@ -95,4 +103,27 @@ function List:find(value)
|
|||||||
end
|
end
|
||||||
return idx
|
return idx
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Returns true if the value exists in the list.
|
||||||
|
---@param value any
|
||||||
|
---@return boolean
|
||||||
|
function List:has(value)
|
||||||
|
return self:find(value) ~= -1
|
||||||
|
end
|
||||||
|
|
||||||
|
---Removes the value from the list, if it exists.
|
||||||
|
---@param value any
|
||||||
|
function List:remove_value(value)
|
||||||
|
local idx = self:find(value)
|
||||||
|
if idx ~= -1 then
|
||||||
|
table.remove(self, idx)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns true if the list is empty.
|
||||||
|
---@return boolean
|
||||||
|
function List:is_empty()
|
||||||
|
return #self == 0
|
||||||
|
end
|
||||||
|
|
||||||
return List
|
return List
|
@ -1,10 +1,19 @@
|
|||||||
---@class Signal
|
---@class Signal
|
||||||
local Signal = {
|
---@field private _connections table
|
||||||
_connections = {}
|
local Signal = {}
|
||||||
}
|
|
||||||
|
|
||||||
Signal.__index = Signal
|
Signal.__index = Signal
|
||||||
|
|
||||||
|
---Constructs a new signal.
|
||||||
|
---@return Signal
|
||||||
|
function Signal.new()
|
||||||
|
local s = {
|
||||||
|
_connections = {},
|
||||||
|
}
|
||||||
|
|
||||||
|
return setmetatable(s, Signal)
|
||||||
|
end
|
||||||
|
|
||||||
---Connects f to this signal. When the signal is emmitted, this function will be called.
|
---Connects f to this signal. When the signal is emmitted, this function will be called.
|
||||||
---@param f function
|
---@param f function
|
||||||
function Signal:connect(f)
|
function Signal:connect(f)
|
||||||
@ -26,14 +35,4 @@ function Signal:emit(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Constructs a new signal.
|
|
||||||
---@return Signal
|
|
||||||
function Signal.new()
|
|
||||||
local s = {
|
|
||||||
_connections = {},
|
|
||||||
}
|
|
||||||
|
|
||||||
return setmetatable(s, Signal)
|
|
||||||
end
|
|
||||||
|
|
||||||
return Signal
|
return Signal
|
Loading…
Reference in New Issue
Block a user