Mothback/data/scripts/entities/arrow.lua

69 lines
2.0 KiB
Lua

Arrow = Entity:New(x,y)
function Arrow:New(x,y,rotation,speed)
local o = Entity:New(x,y)
o.pos = {x = x, y = y}
o.speed = speed or 0
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)
setmetatable(o, self)
self.__index = self
table.insert(LoadedEntities,o)
return o
end
function Arrow:Smart()
end
function Arrow:HandleAnimation()
self.body:Draw(
self.pos.x - Camera.pos.x - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) * self.sprite_scale.x * self.sprite_flip.x,
self.pos.y - Camera.pos.y - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) * self.sprite_scale.y * self.sprite_flip.y,
self.sprite_rotation,
self.sprite_scale.x * self.sprite_flip.x,
self.sprite_scale.y * self.sprite_flip.y
)
end
function Arrow:DoPhysics()
-- horizontal collisions
if not isThereAnyCollisionAt(self.pos.x + self.vel.x, self.pos.y) then
self.pos.x = self.pos.x + self.vel.x
else
while not isThereCollisionAt(self.pos.x + math.sign(self.vel.x), self.pos.y) do
self.pos.x = self.pos.x + math.sign(self.vel.x)
end
self.stuck = true
end
-- vertical collision
if not isThereAnyCollisionAt(self.pos.x, self.pos.y + self.vel.y) then
self.pos.y = self.pos.y + self.vel.y
else
while not isThereCollisionAt(self.pos.x, self.pos.y + math.sign(self.vel.y)) do
self.pos.y = self.pos.y + math.sign(self.vel.y)
end
self.stuck = true
end
-- stuck into collisions
if self.stuck then
--lets allow the arrow to tip a bit into the thing
self.pos.x = self.pos.x + self.vel.x / 5
self.pos.y = self.pos.y + self.vel.y / 5
self.vel.x = 0
self.vel.y = 0
self.illuminated = false
end
end