mirror of
https://github.com/vrld/HC.git
synced 2024-10-09 08:34:17 +00:00
Performance tweaks
This commit is contained in:
parent
fae80f07a0
commit
ec10437644
8
init.lua
8
init.lua
@ -87,6 +87,7 @@ function addPolygon(...)
|
||||
end
|
||||
end
|
||||
shape.move = hash_aware_member(shape.move)
|
||||
shape.moveTo = hash_aware_member(shape.moveTo)
|
||||
shape.rotate = hash_aware_member(shape.rotate)
|
||||
|
||||
function shape:_getNeighbors()
|
||||
@ -153,10 +154,6 @@ local _love_update
|
||||
function setAutoUpdate(max_step)
|
||||
assert(_love_update == nil, "Auto update already enabled!")
|
||||
|
||||
if max_step > 1 then -- assume it's a framerate
|
||||
max_step = 1 / max_step
|
||||
end
|
||||
|
||||
_love_update = love.update
|
||||
love.update = function(dt)
|
||||
_love_update(dt)
|
||||
@ -164,6 +161,9 @@ function setAutoUpdate(max_step)
|
||||
end
|
||||
|
||||
if type(max_step) == "number" then
|
||||
if max_step > 1 then -- assume it's a framerate
|
||||
max_step = 1 / max_step
|
||||
end
|
||||
local combined_update = love.update
|
||||
love.update = function(dt)
|
||||
while dt > max_step do
|
||||
|
14
polygon.lua
14
polygon.lua
@ -196,12 +196,16 @@ function Polygon:isConvex()
|
||||
return status
|
||||
end
|
||||
|
||||
function Polygon:move(direction, dy)
|
||||
if dy then direction = vector(direction,dy) end
|
||||
for i,v in ipairs(self.vertices) do
|
||||
self.vertices[i] = self.vertices[i] + direction
|
||||
function Polygon:move(dx, dy)
|
||||
if not dy then
|
||||
dx, dy = dx:unpack()
|
||||
end
|
||||
self.centroid = self.centroid + direction
|
||||
for i,v in ipairs(self.vertices) do
|
||||
self.vertices[i].x = self.vertices[i].x + dx
|
||||
self.vertices[i].y = self.vertices[i].y + dy
|
||||
end
|
||||
self.centroid.x = self.centroid.x + dx
|
||||
self.centroid.y = self.centroid.y + dy
|
||||
end
|
||||
|
||||
function Polygon:rotate(angle, center, cy)
|
||||
|
@ -32,10 +32,10 @@ local vector = require(_PACKAGE .. 'vector')
|
||||
-- to a string before using as keys
|
||||
local cell_meta = {}
|
||||
function cell_meta.__newindex(tbl, key, val)
|
||||
return rawset(tbl, tostring(key), val)
|
||||
return rawset(tbl, key.x..","..key.y, val)
|
||||
end
|
||||
function cell_meta.__index(tbl, key)
|
||||
local key = tostring(key)
|
||||
local key = key.x..","..key.y
|
||||
local ret = rawget(tbl, key)
|
||||
if not ret then
|
||||
ret = setmetatable({}, {__mode = "kv"})
|
||||
@ -64,7 +64,7 @@ function Spatialhash:insert(obj, ul, lr)
|
||||
local lr = self:cellCoords(lr)
|
||||
for i = ul.x,lr.x do
|
||||
for k = ul.y,lr.y do
|
||||
self.cells[vector(i,k)][obj] = obj
|
||||
self.cells[ {x=i,y=k} ][obj] = obj
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -83,7 +83,7 @@ function Spatialhash:remove(obj, ul, lr)
|
||||
-- els: remove only from bbox
|
||||
for i = ul.x,lr.x do
|
||||
for k = ul.y,lr.y do
|
||||
self.cells[vector(i,k)][obj] = nil
|
||||
self.cells[{x=i,y=k}][obj] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -104,9 +104,9 @@ function Spatialhash:update(obj, ul_old, lr_old, ul_new, lr_new)
|
||||
local region_old = i >= ul_old.x and i <= lr_old.x and k >= ul_old.y and k <= lr_old.y
|
||||
local region_new = i >= ul_new.x and i <= lr_new.x and k >= ul_new.y and k <= lr_new.y
|
||||
if region_new and not region_old then
|
||||
self.cells[vector(i,k)][obj] = obj
|
||||
self.cells[{x=i,y=k}][obj] = obj
|
||||
elseif not region_new and region_old then
|
||||
self.cells[vector(i,k)][obj] = nil
|
||||
self.cells[{x=i,y=k}][obj] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -118,7 +118,7 @@ function Spatialhash:getNeighbors(obj, ul, lr)
|
||||
local set,items = {}, {}
|
||||
for i = ul.x,lr.x do
|
||||
for k = ul.y,lr.y do
|
||||
local cell = self.cells[ vector(i,k) ] or {}
|
||||
local cell = self.cells[{x=i,y=k}] or {}
|
||||
for other,_ in pairs(cell) do
|
||||
if other ~= obj then set[other] = other end
|
||||
end
|
||||
|
13
vector.lua
13
vector.lua
@ -25,7 +25,7 @@ THE SOFTWARE.
|
||||
]]--
|
||||
|
||||
local setmetatable, getmetatable = setmetatable, getmetatable
|
||||
local assert, type, tonumber = assert, type, tonumber
|
||||
local type, tonumber = type, tonumber
|
||||
local sqrt, cos, sin = math.sqrt, math.cos, math.sin
|
||||
module(...)
|
||||
|
||||
@ -34,8 +34,7 @@ vector.__index = vector
|
||||
|
||||
function new(x,y)
|
||||
local v = {x = x or 0, y = y or 0}
|
||||
setmetatable(v, vector)
|
||||
return v
|
||||
return setmetatable(v, vector)
|
||||
end
|
||||
|
||||
function isvector(v)
|
||||
@ -59,12 +58,10 @@ function vector.__unm(a)
|
||||
end
|
||||
|
||||
function vector.__add(a,b)
|
||||
assert(isvector(a) and isvector(b), "Add: wrong argument types (<vector> expected)")
|
||||
return new(a.x+b.x, a.y+b.y)
|
||||
end
|
||||
|
||||
function vector.__sub(a,b)
|
||||
assert(isvector(a) and isvector(b), "Sub: wrong argument types (<vector> expected)")
|
||||
return new(a.x-b.x, a.y-b.y)
|
||||
end
|
||||
|
||||
@ -74,13 +71,11 @@ function vector.__mul(a,b)
|
||||
elseif type(b) == "number" then
|
||||
return new(b*a.x, b*a.y)
|
||||
else
|
||||
assert(isvector(a) and isvector(b), "Mul: wrong argument types (<vector> or <number> expected)")
|
||||
return a.x*b.x + a.y*b.y
|
||||
end
|
||||
end
|
||||
|
||||
function vector.__div(a,b)
|
||||
assert(isvector(a) and type(b) == "number", "wrong argument types (expected <vector> / <number>)")
|
||||
return new(a.x / b, a.y / b)
|
||||
end
|
||||
|
||||
@ -97,7 +92,6 @@ function vector.__le(a,b)
|
||||
end
|
||||
|
||||
function vector.permul(a,b)
|
||||
assert(isvector(a) and isvector(b), "permul: wrong argument types (<vector> expected)")
|
||||
return new(a.x*b.x, a.y*b.y)
|
||||
end
|
||||
|
||||
@ -110,7 +104,6 @@ function vector:len()
|
||||
end
|
||||
|
||||
function vector.dist(a, b)
|
||||
assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
|
||||
return (b-a):len()
|
||||
end
|
||||
|
||||
@ -139,12 +132,10 @@ function vector:perpendicular()
|
||||
end
|
||||
|
||||
function vector:projectOn(v)
|
||||
assert(isvector(v), "invalid argument: cannot project onto anything other than a new.")
|
||||
return (self * v) * v / v:len2()
|
||||
end
|
||||
|
||||
function vector:cross(other)
|
||||
assert(isvector(other), "cross: wrong argument types (<vector> expected)")
|
||||
return self.x * other.y - self.y * other.x
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user