diff --git a/src/enemy/ika.lua b/src/enemy/ika.lua index 29cf159..dc506d2 100644 --- a/src/enemy/ika.lua +++ b/src/enemy/ika.lua @@ -4,6 +4,7 @@ import "CoreLibs/graphics" import "CoreLibs/sprites" import "CoreLibs/timer" import "entity" +import "util" local gfx = playdate.graphics local geom = 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() diff --git a/src/entity.lua b/src/entity.lua index 1356db4..e38a049 100644 --- a/src/entity.lua +++ b/src/entity.lua @@ -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() diff --git a/src/util.lua b/src/util.lua new file mode 100644 index 0000000..cb0facd --- /dev/null +++ b/src/util.lua @@ -0,0 +1,19 @@ +-- Generic utility functions, because sometimes being lazy is more efficient + +import "CoreLibs/graphics" + +local geom = 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