72 lines
1.5 KiB
Lua
72 lines
1.5 KiB
Lua
-- A pixie is like a sprite, but better
|
|
|
|
import "CoreLibs/object"
|
|
import "CoreLibs/graphics"
|
|
import "CoreLibs/sprites"
|
|
|
|
local gfx <const> = playdate.graphics
|
|
|
|
class("Pixie").extends(gfx.sprite)
|
|
|
|
-- Crong pixies are simple rectangles, so they take an initial position, width, height, and color
|
|
-- They are also collidable
|
|
function Pixie:init(width, height, color, x, y)
|
|
x = x or 0
|
|
y = y or 0
|
|
|
|
Pixie.super.init(self, gfx.image.new(width, height, color))
|
|
self:setCollideRect(0, 0, self:getSize())
|
|
self:moveTo(x, y)
|
|
return self
|
|
end
|
|
|
|
function Pixie:moveTo(x, y)
|
|
Pixie.super.moveTo(self, x, y)
|
|
self:_normalizePosition()
|
|
end
|
|
|
|
function Pixie:moveBy(x, y)
|
|
Pixie.super.moveBy(self, x, y)
|
|
self:_normalizePosition()
|
|
end
|
|
|
|
function Pixie:_normalizePosition()
|
|
local limitTop = self.height / 2
|
|
local limitBottom = 240 - limitTop
|
|
local limitLeft = self.width / 2
|
|
local limitRight = 400 - limitLeft
|
|
|
|
local changedX = 0
|
|
local changedY = 0
|
|
local newX = self.x
|
|
local newY = self.y
|
|
|
|
if self.y < limitTop then
|
|
newY = limitTop
|
|
changedY = 1
|
|
end
|
|
|
|
if self.y > limitBottom then
|
|
newY = limitBottom
|
|
changedY = -1
|
|
end
|
|
|
|
if self.x < limitLeft then
|
|
newX = limitLeft
|
|
changedX = 1
|
|
end
|
|
|
|
if self.x > limitRight then
|
|
newX = limitRight
|
|
changedX = -1
|
|
end
|
|
|
|
if changedX ~= 0 or changedY ~= 0 then
|
|
-- call super to avoid the odd possibility that we recurse back into
|
|
-- this function
|
|
Pixie.super.moveTo(self, newX, newY)
|
|
end
|
|
|
|
return {changedX, changedY}
|
|
end
|