Mothback/data/scripts/entity.lua

66 lines
1.6 KiB
Lua
Raw Normal View History

2021-10-16 23:06:11 +00:00
entity = {class = "entity", anim_subframe = 0, anim_frame = 0, anim_imgs = {}, offset = {x = 0, y = 0}, scale = {x = 1, y = 1}, flip = { x = 1, y = 1}}
function entity:newEntity(x,y)
o = {}
o.pos = {x = x, y = y}
setmetatable(o, self)
self.__index = self
return o
end
function entity:Move(target, speed) -- target = {tx int, ty int} / speed = int
end
function entity:Draw()
if self.sprite ~= nil then
love.graphics.draw(
self.sprite,
self.pos.x - Camera.pos.x + self.offset.x * game.scale * self.scale.x * self.flip.x,
self.pos.y - Camera.pos.y + self.offset.y * game.scale * self.scale.y * self.flip.y,
0,
game.scale * self.scale.x * self.flip.x,
game.scale * self.scale.y * self.flip.y
)
end
end
function entity:Animate()
if game_paused ~= true then
-- try to animate
self.anim_subframe = self.anim_subframe + current_dt
if self.anim_subframe >= self.anim_speed then
self.anim_frame = self.anim_frame + 1
self.anim_subframe = self.anim_subframe - self.anim_speed
end
-- cycle
if self.anim_frame >= self.anim_frames+1 then
self.anim_frame = self.anim_frame - self.anim_frames
end
-- change
self.sprite = self.anim_imgs[self.anim_frame]
end
end
function entity:LoadAnimation(anim,frames,speed)
if self.anim_path ~= anim and self.anim_path ~= anim.path then
if frames ~= nil and speed ~= nil then
self.anim_path = anim or nil
self.anim_frames = frames or 4
self.anim_speed = speed or frames
else
self.anim_path = anim.path
self.anim_frames = anim.frames
self.anim_speed = anim.speed
end
self.anim_imgs = anim.imgs
end
end
require "data/scripts/entities/player"