Add utility class.
This commit is contained in:
parent
a4127ef1ba
commit
0c3b0dbd64
|
@ -4,6 +4,7 @@ import "CoreLibs/graphics"
|
|||
import "CoreLibs/sprites"
|
||||
import "CoreLibs/timer"
|
||||
import "entity"
|
||||
import "util"
|
||||
|
||||
local gfx <const> = playdate.graphics
|
||||
local geom <const> = playdate.geometry
|
||||
|
@ -29,9 +30,7 @@ function Ika:onReady()
|
|||
end
|
||||
|
||||
function Ika:calculateVector(target)
|
||||
local vec = geom.point.new(target:getPosition()) -
|
||||
geom.point.new(self:getPosition())
|
||||
return vec:normalized() * 5
|
||||
return util.unitPointer(self:getPosition(), target:getPosition()) * 5
|
||||
end
|
||||
|
||||
function Ika:remove()
|
||||
|
|
|
@ -101,15 +101,15 @@ end
|
|||
-- State machine-controlled functions
|
||||
|
||||
function Entity:onInit()
|
||||
-- noop
|
||||
-- stub for subclassing
|
||||
end
|
||||
|
||||
function Entity:runInit()
|
||||
-- noop
|
||||
-- stub for subclassing
|
||||
end
|
||||
|
||||
function Entity:onIntro()
|
||||
-- noop
|
||||
-- stub for subclassing
|
||||
end
|
||||
|
||||
function Entity:runIntro()
|
||||
|
@ -121,7 +121,7 @@ function Entity:runIntro()
|
|||
end
|
||||
|
||||
function Entity:onReady()
|
||||
-- noop
|
||||
-- stub for subclassing
|
||||
end
|
||||
|
||||
function Entity:runReady()
|
||||
|
|
19
src/util.lua
Normal file
19
src/util.lua
Normal file
|
@ -0,0 +1,19 @@
|
|||
-- Generic utility functions, because sometimes being lazy is more efficient
|
||||
|
||||
import "CoreLibs/graphics"
|
||||
|
||||
local geom <const> = playdate.geometry
|
||||
|
||||
util = {}
|
||||
|
||||
-- creates a unit vector that "points" from point A to point B
|
||||
function util.unitPointer(a, b)
|
||||
return (b - a):normalized()
|
||||
end
|
||||
|
||||
-- create a unit vector that points from x1, y1 to x2, y2
|
||||
function util.unitPointer(x1, y1, x2, y2)
|
||||
local a = geom.point.new(x1, y1)
|
||||
local b = geom.point.new(x2, y2)
|
||||
return unitPointer(a,b)
|
||||
end
|
Loading…
Reference in New Issue
Block a user