Mothback/data/scripts/entity.lua

154 lines
4.0 KiB
Lua
Raw Normal View History

2021-10-25 23:19:22 +00:00
Entity = {
}
function Entity:New(x,y)
o = {}
o.pos = {x = x, y = y}
o.vel = {x = 0, y = 0}
o.class = "Entity"
o.anim = {}
o.anim.subframe = 0
o.anim.frame = 1
o.anim.imgs = {}
o.animations = {}
o.sprite_offset = {x = 0, y = 0}
o.sprite_scale = {x = 1, y = 1}
o.sprite_rotation = math.rad(0)
o.sprite_flip = { x = 1, y = 1}
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
local relative_position_x = self.pos.x - Camera.pos.x
local relative_position_y = self.pos.y - Camera.pos.y
local origin_compensation_x = - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation))
local origin_compensation_y = - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation))
local dimensions_x = self.sprite_scale.x * self.sprite_flip.x
local dimensions_y = self.sprite_scale.y * self.sprite_flip.y
love.graphics.draw(
self.sprite,
relative_position_x + origin_compensation_x * dimensions_x,
relative_position_y + origin_compensation_y * dimensions_y,
self.sprite_rotation,
self.sprite_scale.x * self.sprite_flip.x,
self.sprite_scale.y * self.sprite_flip.y
)
if debug_collision then
love.graphics.setColor(1, 0, 0)
love.graphics.circle( "line", relative_position_x, relative_position_y, 2 )
love.graphics.setColor(0, 1 ,0)
love.graphics.circle( "line",
relative_position_x + origin_compensation_x * dimensions_x,
relative_position_y + origin_compensation_y * dimensions_y,
2
)
end
love.graphics.setColor(1, 1 ,1)
end
end
function Entity:NewAnimation(anim,frames,speed)
local anim_data = {
frame = 1,
subframe = 1,
path = anim.path,
frames = anim.frames,
speed = anim.speed,
imgs = anim.imgs
}
self.animations[#self.animations+1] = anim_data
return self.animations[#self.animations]
end
function DrawAnimationFrame(animation, frame, x, y, rotate, sx, sy)
local x = x or 0
local y = y or 0
local sx = sx or 1
local sy = sy or 1
love.graphics.draw(
animation.imgs[frame],
x - Camera.pos.x,
y - Camera.pos.y,
rotate,
sx,
sy
)
end
function DrawAnimation(animation, x, y, rotate, sx, sy)
local x = x or 0
local y = y or 0
local sx = sx or 1
local sy = sy or 1
if game_paused ~= true then
-- try to animate
animation.subframe = animation.subframe + current_dt
if animation.subframe >= animation.speed then
animation.frame = animation.frame + 1
animation.subframe = animation.subframe - animation.speed
end
-- cycle
if animation.frame >= animation.frames+1 then
animation.frame = animation.frame - animation.frames
end
end
love.graphics.draw(
animation.imgs[animation.frame],
x - Camera.pos.x,
y - Camera.pos.y,
rotate,
sx,
sy
)
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/kupo"
require "data/scripts/entities/arrow"
require "data/scripts/entities/player"