Mothback/code/entities/arrow.lua
lustlion 0acbfd5e9d more naming convention
now there are spawn objects!
now entity spawns are loaded from level!
now entity spawns are saved to level!
2022-03-08 09:34:51 +01:00

56 lines
1.2 KiB
Lua

Arrow = Entity:new()
Arrow.type = "Arrow"
function Arrow:new(x,y,rotation,speed)
local o = Entity:new(x,y)
o.pos = {x = x, y = y}
o.speed = speed or 10
o.sprite_rotation = rotation or 0
o.vel = {
x = o.speed * math.cos(o.sprite_rotation),
y = o.speed * math.sin(o.sprite_rotation)
}
o.sprite_offset = {x = 13, y = 1}
o.stuck = false
o.illuminated = true
-- animations
o.body = Animation:new(animation.kupo.arrow)
o.box = {
from = {x = -0.5, y = -0.5}, --gameworld pixels
to = {x = 0.5, y = 0.5} -- gameworld pixels
}
o:id()
setmetatable(o, self)
self.__index = self
return o
end
function Arrow:drawBackground()
self:draw(self.body)
end
function Arrow:doPhysics()
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then
self.pos.x = self.pos.x + self.vel.x
else
self.stuck = true
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
self.pos.y = self.pos.y + self.vel.y
else
self.stuck = true
end
if self.stuck then
self.pos.x = self.pos.x + self.vel.x * (2/3)
self.pos.y = self.pos.y + self.vel.y * (2/3)
self.vel.x = 0
self.vel.y = 0
end
end