Mothback/data/scripts/entity.lua

189 lines
5.2 KiB
Lua

Entity = {
}
function Entity:New(x,y)
o = {}
o.pos = {x = x, y = y}
o.vel = {x = 0, y = 0}
o.boxCollision = {
from = {x = x, y = y},
to = {x = x, y = y}
}
o.class = "Entity"
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}
o.illuminated = false
setmetatable(o, self)
self.__index = self
return o
end
-- returns true if theres a collision at that point. also marks collisioned tile as collision true
function Entity:isCollidingAt(x,y,object)
local result = false
for _, col in pairs(object) do
result = self.pos.x + self.boxCollision.from.x < col.to.x
and self.pos.x + self.boxCollision.to.x > col.from.x
and self.pos.y + self.boxCollision.from.y < col.to.y
and self.pos.y + self.boxCollision.to.y > col.from.y
if result == true then break end
end
return result
end
function Entity:isCollidingWith(entity)
return self.pos.x + self.boxCollision.from.x < entity.pos.x + entity.boxCollision.to.x
and self.pos.x + self.boxCollision.to.x > entity.pos.x + entity.boxCollision.from.x
and self.pos.y + self.boxCollision.from.y < entity.pos.y + entity.boxCollision.to.y
and self.pos.y + self.boxCollision.to.y > entity.pos.y + entity.boxCollision.from.y
end
function Entity:isCollidingAtAll(x,y)
local result = false
if not result then
result = self:isCollidingAt(x,y,objects.collisions)
end
if not result then
result = self:isCollidingAt(x,y,objects.ladders)
end
if not result then
result = self:isCollidingAt(x,y,objects.platforms)
end
return result
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 and self.anim.path ~= nil 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"