Compare commits
No commits in common. "4c5006d1e7c55c11417b77ce129f6730f4b54604" and "954277688f73e562b53fb3ff0de00812153ddfe2" have entirely different histories.
4c5006d1e7
...
954277688f
@ -99,7 +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")
|
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
|
||||||
|
@ -10,8 +10,16 @@ local Duck = require "classes.duck"
|
|||||||
local ducks = List()
|
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(
|
||||||
|
function (duck)
|
||||||
|
return duck.state ~= duck.STATES.CHASE
|
||||||
|
end
|
||||||
|
)
|
||||||
|
if eligible_ducks:is_empty() then return end
|
||||||
|
eligible_ducks[1]:start_chase(f)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function delete_feed(f)
|
local function delete_feed(f)
|
||||||
@ -21,17 +29,11 @@ end
|
|||||||
local function duck_seek_feed(duck)
|
local function duck_seek_feed(duck)
|
||||||
local eligible_feeds = feed:filter(
|
local eligible_feeds = feed:filter(
|
||||||
function (f)
|
function (f)
|
||||||
return f.occupied == false
|
return feed.occupied == false
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
if eligible_feeds:is_empty() then return end
|
if eligible_feeds:is_empty() then return end
|
||||||
duck:start_chase(
|
duck:start_chase(eligible_feeds[1])
|
||||||
eligible_feeds:sorted(
|
|
||||||
function(a, b)
|
|
||||||
return a.position:distance_squared_to(duck.position) < b.position:distance_squared_to(duck.position)
|
|
||||||
end
|
|
||||||
):pop_front()
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- called every frame, with constant delta time
|
-- called every frame, with constant delta time
|
||||||
@ -52,6 +54,8 @@ function game_tick()
|
|||||||
duck.SeekFeed:connect(duck_seek_feed)
|
duck.SeekFeed:connect(duck_seek_feed)
|
||||||
duck.index = i
|
duck.index = i
|
||||||
end
|
end
|
||||||
|
print(ducks[1].AteFeed._connections)
|
||||||
|
print(ducks[2].AteFeed._connections)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,23 +68,16 @@ function List:pop()
|
|||||||
return table.remove(self, #self)
|
return table.remove(self, #self)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Removes the first element in the list and returns it.
|
|
||||||
---@return any
|
|
||||||
function List:pop_front()
|
|
||||||
return table.remove(self, 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
---Reduce.
|
---Reduce.
|
||||||
---@generic T
|
---@param f function called with element, accumulator, index
|
||||||
---@param f fun(element: any, accumulator: T, index: integer): T
|
---@param init any initial value of accumulator
|
||||||
---@param init T|nil initial value of accumulator
|
---@return any
|
||||||
---@return T
|
|
||||||
function List:reduce(f, init)
|
function List:reduce(f, init)
|
||||||
return reduce(self, f, init)
|
return reduce(self, f, init)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns a new List of all elements of this list that match the predicate function.
|
---Returns a new List of all elements of this list that match the predicate function.
|
||||||
---@param predicate fun(element: any): boolean
|
---@param predicate function called with element
|
||||||
---@return List
|
---@return List
|
||||||
function List:filter(predicate)
|
function List:filter(predicate)
|
||||||
return filter(self, predicate)
|
return filter(self, predicate)
|
||||||
@ -133,33 +126,4 @@ function List:is_empty()
|
|||||||
return #self == 0
|
return #self == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns a (shallow) copy of this list.
|
|
||||||
---@return List
|
|
||||||
function List:copy()
|
|
||||||
return List.create(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
---Returns a new copy of this list with the order of the elements shuffled around.
|
|
||||||
---@return List
|
|
||||||
function List:shuffled()
|
|
||||||
-- https://gist.github.com/Uradamus/10323382
|
|
||||||
local list = self:copy()
|
|
||||||
|
|
||||||
for i = #list, 2, -1 do
|
|
||||||
local j = math.random(i)
|
|
||||||
list[i], list[j] = list[j], list[i]
|
|
||||||
end
|
|
||||||
|
|
||||||
return list
|
|
||||||
end
|
|
||||||
|
|
||||||
---Returns a sorted copy of this list.
|
|
||||||
---@param f? fun(a: any, b: any)
|
|
||||||
---@return List
|
|
||||||
function List:sorted(f)
|
|
||||||
local list = self:copy()
|
|
||||||
table.sort(list, f)
|
|
||||||
return list
|
|
||||||
end
|
|
||||||
|
|
||||||
return List
|
return List
|
Loading…
Reference in New Issue
Block a user