ZLP1/scripts/animations.lua

148 lines
2.8 KiB
Lua

Animation = {
projectilePlasma = {
path = "assets/projectilePlasma",
frames = 6,
speed = 0.1,
random = true
},
projectilePiercing = {
path = "assets/projectilePiercing",
frames = 1,
speed = 1
},
projectileRocket = {
path = "assets/projectileRocket",
frames = 1,
speed = 1
},
projectileGreen = {
path = "assets/projectileGreenRocket",
frames = 1,
speed = 1
},
projectileRed = {
path = "assets/projectileRedRocket",
frames = 1,
speed = 1
},
projectileBlue = {
path = "assets/projectileBlueRocket",
frames = 1,
speed = 1
},
projectileCard = {
path = "assets/projectileCard",
frames = 8,
speed = 1/24
}
}
for _, anim in pairs(Animation) do
anim.imgs = {}
for i = 1, anim.frames do
table.insert(anim.imgs,love.graphics.newImage(anim.path..tostring(i)..".png"))
end
end
AnimationContainer = {}
function AnimationContainer:New(anim_data)
local o = {}
o.type = "AnimationContainer"
o.path = anim_data.path
o.frames = anim_data.frames
o.speed = anim_data.speed
o.imgs = anim_data.imgs
o.subframe = 0
local random = anim_data.random or false
if random == true then
o.frame = math.random(1,anim_data.frames)
else
o.frame = 1
end
table.insert(List.AnimationContainers,o)
o.id = #List.AnimationContainers
setmetatable(o, self)
self.__index = self
return o
end
function AnimationContainer:Delete()
for _, animation in pairs(List.AnimationContainers) do
if animation.id > self.id then
animation.id = animation.id - 1
end
end
table.remove(list,self.id.FriendlyBullet)
self = nil
end
function AnimationContainer:Set(anim_data)
if anim_data.path == self.path
then
return self
else
return Animation:New(anim_data)
end
end
-- to manually handle what frame
function AnimationContainer:DrawFrame(frame, x, y, rotate, sx, sy)
if frame > self.frames then
frame = self.frames
end
local x = x or 0
local y = y or 0
local sx = sx or 1
local sy = sy or 1
love.graphics.draw(
self.imgs[frame],
x - Camera.pos.x,
y - Camera.pos.y,
rotate,
sx,
sy
)
end
-- to linearly animate
function AnimationContainer:Animate()
-- try to animate
self.subframe = self.subframe + current_dt
if self.subframe > self.speed then
self.frame = self.frame + 1
self.subframe = self.subframe - self.speed
end
-- cycle
if self.frame >= self.frames+1 then
self.frame = self.frame - self.frames
end
end
-- to draw the current frame
function AnimationContainer:Draw(x, y, rotate, sx, sy, ox, oy)
local x = x or 0
local y = y or 0
local rotate = rotate or 0
local sx = sx or 1
local sy = sy or 1
local ox = ox or self.imgs[self.frame]:getPixelWidth()/2
local oy = oy or self.imgs[self.frame]:getPixelHeight()/2
love.graphics.draw(
self.imgs[self.frame],
x,
y,
rotate,
sx,
sy,
ox,
oy
)
end