2023-09-27 06:24:22 +00:00
|
|
|
import "pixie"
|
|
|
|
|
|
|
|
class("Ball").extends(Pixie)
|
|
|
|
|
|
|
|
function Ball:init(width, height, color, x, y)
|
|
|
|
Ball.super.init(self, width, height, color, x, y)
|
2023-09-27 07:05:50 +00:00
|
|
|
-- trying to use a table / array for the direction vector doesn't work for bizarre reasons...
|
|
|
|
-- it fails on the init() call at runtime.
|
2023-09-27 06:24:22 +00:00
|
|
|
self.dirX = -1
|
|
|
|
self.dirY = 1
|
|
|
|
self.speed = 3
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
function Ball:levelUp()
|
|
|
|
self.speed += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function Ball:move()
|
|
|
|
self:moveBy(self.dirX * self.speed,
|
|
|
|
self.dirY * self.speed)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Ball:setDirection(x, y)
|
|
|
|
x = x or self.dirX
|
|
|
|
y = y or self.dirY
|
|
|
|
self.dirX = x
|
|
|
|
self.dirY = y
|
|
|
|
end
|
|
|
|
|
|
|
|
function Ball:_normalizePosition()
|
|
|
|
local changeVector = Ball.super._normalizePosition(self)
|
|
|
|
if changeVector[1] ~= 0 then
|
|
|
|
self.dirX = changeVector[1]
|
|
|
|
end
|
|
|
|
if changeVector[2] ~= 0 then
|
|
|
|
self.dirY = changeVector[2]
|
|
|
|
end
|
|
|
|
end
|