Add utility class.

This commit is contained in:
Anna Rose 2023-10-05 16:23:49 -04:00
parent a4127ef1ba
commit 0c3b0dbd64
3 changed files with 25 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import "CoreLibs/graphics"
import "CoreLibs/sprites" import "CoreLibs/sprites"
import "CoreLibs/timer" import "CoreLibs/timer"
import "entity" import "entity"
import "util"
local gfx <const> = playdate.graphics local gfx <const> = playdate.graphics
local geom <const> = playdate.geometry local geom <const> = playdate.geometry
@ -29,9 +30,7 @@ function Ika:onReady()
end end
function Ika:calculateVector(target) function Ika:calculateVector(target)
local vec = geom.point.new(target:getPosition()) - return util.unitPointer(self:getPosition(), target:getPosition()) * 5
geom.point.new(self:getPosition())
return vec:normalized() * 5
end end
function Ika:remove() function Ika:remove()

View File

@ -101,15 +101,15 @@ end
-- State machine-controlled functions -- State machine-controlled functions
function Entity:onInit() function Entity:onInit()
-- noop -- stub for subclassing
end end
function Entity:runInit() function Entity:runInit()
-- noop -- stub for subclassing
end end
function Entity:onIntro() function Entity:onIntro()
-- noop -- stub for subclassing
end end
function Entity:runIntro() function Entity:runIntro()
@ -121,7 +121,7 @@ function Entity:runIntro()
end end
function Entity:onReady() function Entity:onReady()
-- noop -- stub for subclassing
end end
function Entity:runReady() function Entity:runReady()

19
src/util.lua Normal file
View 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