Skip to content

Commit 56b00b4

Browse files
authored
Add files via upload
1 parent b791ab4 commit 56b00b4

29 files changed

Lines changed: 5415 additions & 0 deletions

collections/ListenerList.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
---Holds and manages a collection of functions and target objects
12+
local ListenerList_mt = Class(ListenerList)
13+
14+
15+
---Creates a new listener list.
16+
-- @param boolean? ignoreFirstArgument If this is true, the first argument is not passed along when invoking. Used to ignore the table instance when the listener list is a member of a table.
17+
-- @return ListenerList instance The created instance.
18+
function ListenerList.new(ignoreFirstArgument)
19+
20+
-- Create the instance.
21+
local self = setmetatable({}, ListenerList_mt)
22+
23+
-- The collection of listening functions.
24+
self.listeners = {}
25+
26+
-- If this is true, the first argument is not passed to the invoked functions. This is useful to ignore the table instance when the listener list is a member of a table.
27+
self.ignoreFirstArgument = ignoreFirstArgument == true
28+
29+
-- Return the created instance.
30+
return self
31+
end
32+
33+
34+
---Adds the given listener function and target object to the listeners list.
35+
-- @param (function|TargetedFunction) listenerFunction The function or TargetedFunction to add to the list.
36+
-- @param table? targetObject The optional target object.
37+
function ListenerList:registerListener(listenerFunction, targetObject)
38+
39+
-- Resolve the listener function.
40+
local listener = TargetedFunction.resolveListener(listenerFunction, targetObject)
41+
42+
-- Add the listener function to the list.
43+
table.insert(self.listeners, listener)
44+
end
45+
46+
47+
---Calls the listeners with the given arguments.
48+
-- @param any ... The collection of arguments to pass to the functions.
49+
function ListenerList:invoke(targetObject, ...)
50+
51+
-- Invoke each listener.
52+
if self.ignoreFirstArgument then
53+
for _, listener in ipairs(self.listeners) do
54+
listener:invoke(...)
55+
end
56+
else
57+
for _, listener in ipairs(self.listeners) do
58+
listener:invoke(targetObject, ...)
59+
end
60+
end
61+
end
62+
63+
64+
---Metamethod so the table itself can be called as a shortcut for :invoke().
65+
-- @param ListenerList instance The instance of the listener list.
66+
-- @param any ... The collection of arguments to pass to the functions.
67+
function ListenerList_mt.__call(instance, ...)
68+
return instance:invoke(...)
69+
end

collections/MapDataGrid.lua

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
---A map data grid that splits a map into multiple sections
13+
local MapDataGrid_mt = Class(MapDataGrid, DataGrid)
14+
15+
16+
---Creates a map grid instance with the given map size, cells per row/column, and optional custom metatable.
17+
-- @param integer mapSize map size
18+
-- @param integer blocksPerRowColumn blocks per row and column
19+
-- @param table? customMt custom metatable
20+
-- @return table instance instance of object
21+
function MapDataGrid.new(mapSize, blocksPerRowColumn, customMt)
22+
local self = DataGrid.new(blocksPerRowColumn, blocksPerRowColumn, customMt or MapDataGrid_mt)
23+
24+
self.blocksPerRowColumn = blocksPerRowColumn
25+
self.mapSize = mapSize
26+
self.blockSize = self.mapSize/self.blocksPerRowColumn
27+
28+
return self
29+
end
30+
31+
32+
---Creates a new data grid from the given map size and block size.
33+
-- @param integer mapSize The size of the map in metres.
34+
-- @param integer blockSize The size of one cell in metres.
35+
-- @param table? customMt The custom metatable to use, if any.
36+
-- @return MapDataGrid instance The created instance.
37+
function MapDataGrid.createFromBlockSize(mapSize, blockSize, customMt)
38+
39+
-- Calculate the blocks per row, then return the result of the constructor with this value.
40+
local blocksPerRowColumn = math.ceil(mapSize / blockSize)
41+
return MapDataGrid.new(mapSize, blocksPerRowColumn, customMt)
42+
end
43+
44+
45+
---Get value at world position
46+
-- @param float worldX world position x
47+
-- @param float worldZ world position z
48+
-- @return table value value at the given position
49+
function MapDataGrid:getValueAtWorldPos(worldX, worldZ)
50+
local rowIndex, colIndex = self:getRowColumnFromWorldPos(worldX, worldZ)
51+
return self:getValue(rowIndex, colIndex), rowIndex, colIndex
52+
end
53+
54+
55+
---Set value at world position
56+
-- @param float worldX world position x
57+
-- @param float worldZ world position z
58+
-- @param table value value at the given position
59+
function MapDataGrid:setValueAtWorldPos(worldX, worldZ, value)
60+
local rowIndex, colIndex = self:getRowColumnFromWorldPos(worldX, worldZ)
61+
self:setValue(rowIndex, colIndex, value)
62+
end
63+
64+
65+
---Gets clamped row and column at given world position
66+
-- @param float worldX world position x
67+
-- @param float worldZ world position z
68+
-- @return integer row row
69+
-- @return integer column column
70+
function MapDataGrid:getRowColumnFromWorldPos(worldX, worldZ)
71+
local mapSize = self.mapSize
72+
local blocksPerRowColumn = self.blocksPerRowColumn
73+
74+
local x = (worldX + mapSize*0.5) / mapSize
75+
local z = (worldZ + mapSize*0.5) / mapSize
76+
77+
local row = math.clamp(math.ceil(blocksPerRowColumn*z), 1, blocksPerRowColumn)
78+
local column = math.clamp(math.ceil(blocksPerRowColumn*x), 1, blocksPerRowColumn)
79+
80+
-- log(worldX, worldZ, " -> ", (worldX + self.mapSize*0.5), (worldZ + self.mapSize*0.5), z, x, row, column)
81+
82+
return row, column
83+
end
84+
85+
86+
---Calculates if the given world position is in range of the data.
87+
-- @param float worldX The x world position.
88+
-- @param float worldZ The z world position.
89+
-- @return boolean isInRange True if the position is in range of the data; otherwise false.
90+
function MapDataGrid:isWorldPositionInRange(worldX, worldZ)
91+
return worldX > self.mapSize * -0.5 and worldX <= self.mapSize * 0.5 and worldZ > self.mapSize * -0.5 and worldZ <= self.mapSize * 0.5
92+
end

collections/ObjectPool.lua

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
---Holds a collection of pooled objects and handles creating new ones when the pool is empty.
13+
local ObjectPool_mt = Class(ObjectPool)
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
---Creates a new object pool with the given constructor.
24+
-- @param function? objectConstructor The constructor function to call in order to create new objects when the pool is empty. If this is nil, ObjectPool.EMPTY_TABLE_CONSTRUCTOR will be used.
25+
-- @param any ... The arguments to pass through to the constructor.
26+
-- @return ObjectPool instance The created instance.
27+
function ObjectPool.new(objectConstructor, ...)
28+
29+
-- Ensure a constructor was given or is nil.
30+
--#debug Assert.isNilOrType(objectConstructor, "function", "Object constructor was not a function or nil!")
31+
32+
-- Create the instance.
33+
local self = setmetatable({}, ObjectPool_mt)
34+
35+
-- The object constructor and arguments.
36+
self.objectConstructor = objectConstructor or ObjectPool.EMPTY_TABLE_CONSTRUCTOR
37+
self.objectConstructorArguments = table.pack(...)
38+
39+
-- The pool of objects.
40+
self.pool = {}
41+
42+
-- The set of pooled objects to avoid duplicates.
43+
self.poolSet = {}
44+
45+
-- Return the created instance.
46+
return self
47+
end
48+
49+
50+
---Gets the total count of objects in the pool.
51+
-- @return integer count The number of items in the pool.
52+
function ObjectPool:getLength()
53+
-- TODO: Replace with __len metafunction in Lua 5.2
54+
return #self.pool
55+
end
56+
57+
58+
---Removes all instances from the pool's collections.
59+
function ObjectPool:clear()
60+
table.clear(self.poolSet)
61+
table.clear(self.pool)
62+
end
63+
64+
65+
---Gets and returns an instance from the pool, or creates one if the pool is empty.
66+
-- @return table instance The pooled or created object.
67+
function ObjectPool:getOrCreateNext()
68+
69+
-- Get the next object in the pool.
70+
local instance = table.remove(self.pool)
71+
72+
-- If there was no object in the pool, create a new one.
73+
if not instance then
74+
75+
-- If the constructor is the default empty table, skip the unpacking.
76+
if self.objectConstructor == ObjectPool.EMPTY_TABLE_CONSTRUCTOR then
77+
instance = self.objectConstructor()
78+
-- Otherwise; unpack the arguments and call the constructor.
79+
else
80+
instance = self.objectConstructor(table.unpack(self.objectConstructorArguments, 1, self.objectConstructorArguments.n))
81+
end
82+
-- Otherwise; also remove the object from the set.
83+
else
84+
self.poolSet[instance] = nil
85+
end
86+
87+
-- Return the created or gotten object.
88+
return instance
89+
end
90+
91+
92+
---Returns the given instance to the pool.
93+
-- @param table instance The instance to return to the pool.
94+
function ObjectPool:returnToPool(instance)
95+
96+
-- Ensure the given instance exists.
97+
if instance == nil then
98+
return false
99+
end
100+
--#debug Assert.isType(instance, "table", "Given instance was not a table!")
101+
102+
-- Ensure the object is not already pooled.
103+
if self.poolSet[instance] ~= nil then
104+
return false
105+
end
106+
107+
-- If the object constructor is the default empty table, clear the table.
108+
if self.objectConstructor == ObjectPool.EMPTY_TABLE_CONSTRUCTOR then
109+
table.clear(instance)
110+
-- Otherwise; if the instance has a reset function, call it.
111+
elseif instance.reset ~= nil then
112+
instance:reset()
113+
end
114+
115+
-- Add the object to the collections.
116+
table.insert(self.pool, instance)
117+
self.poolSet[instance] = instance
118+
119+
-- Return true, as the addition was a success.
120+
return true
121+
end

collections/Queue.lua

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
---
14+
local Queue_mt = Class(Queue)
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
---Returns the element from the front of the queue without popping it.
68+
-- @return table? value The element at the front of the queue, or nil if the queue is empty.
69+
function Queue:peek()
70+
return self.first
71+
end

0 commit comments

Comments
 (0)