Mothback/data/scripts/entity.lua

160 lines
4.8 KiB
Lua

Entity = {class = "Entity"}
function Entity:New(x,y)
o = {}
o.pos = {x = x, y = y}
o.vel = {x = 0, y = 0}
o.direction = 0
o.boxCollision = {
from = {x = x, y = y},
to = {x = x, y = y},
}
o.target_offset = {x = 0, y = 0}
o.sprite_offset = {x = 0, y = 0}
o.sprite_scale = {x = 1, y = 1}
o.sprite_rotation = math.rad(0)
o.sprite_tint = {1,1,1}
o.sprite_alpha = 1
o.sprite_flip = { x = 1, y = 1}
o.illuminated = false
setmetatable(o, self)
self.__index = self
return o
end
function Entity:Smart()
end
function Entity:Move()
self.pos.x = self.pos.x + self.vel.x
self.pos.y = self.pos.y + self.vel.y
end
function Entity:CollisionMove()
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then
self.pos.x = self.pos.x + self.vel.x
else
self.vel.x = 0
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
self.pos.y = self.pos.y + self.vel.y
else
self.vel.y = 0
end
end
function Entity:LightAdjust()
if self.light ~= nil then
self.light.pos.x = self.pos.x
self.light.pos.y = self.pos.y
end
end
function Entity:Kill()
if self.light ~= nil then
KillLight(self.light)
end
if self.id ~= nil then
for _, e in pairs(LoadedEntities) do
if e.id > self.id then
e.id = e.id - 1
end
end
table.remove(LoadedEntities,self.id)
end
self = nil
end
function Entity:Draw(animation)
local c1, c2, c3, a = love.graphics.getColor()
love.graphics.setColor(self.sprite_tint[1],self.sprite_tint[2],self.sprite_tint[3],self.sprite_alpha)
animation: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
)
love.graphics.setColor(c1,c2,c3,a)
end
function Entity:centerOffset(animation,x,y)
local x = x or 0
local y = y or 0
self.sprite_offset.x = animation.imgs[1]:getWidth()/2 + x
self.sprite_offset.y = animation.imgs[1]:getHeight()/2 + y
end
function Entity:getBoundingBox(animation,top,left,bottom,right)
local left = left or 0
local right = right or 0
local top = top or 0
local bottom = bottom or 0
self.boxCollision.from.x = -animation.imgs[1]:getWidth()/2 + left
self.boxCollision.to.x = animation.imgs[1]:getWidth()/2 + right
self.boxCollision.from.y = -animation.imgs[1]:getHeight()/2 + top
self.boxCollision.to.y = animation.imgs[1]:getHeight()/2 + bottom
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 = x + self.boxCollision.from.x < col.to.x
and col.from.x < x + self.boxCollision.to.x
and y + self.boxCollision.from.y < col.to.y
and col.from.y < y + self.boxCollision.to.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 entity.pos.x + entity.boxCollision.from.x < self.pos.x + self.boxCollision.to.x
and self.pos.y + self.boxCollision.from.y < entity.pos.y + entity.boxCollision.to.y
and entity.pos.y + entity.boxCollision.from.y < self.pos.y + self.boxCollision.to.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:Debug()
-- draw center GREEN
love.graphics.setColor(0,1,0)
love.graphics.circle("fill", -Camera.pos.x + self.pos.x, -Camera.pos.y + self.pos.y, 1)
-- draw collision box PURPLE
love.graphics.setColor(1,0,1)
love.graphics.rectangle(
"line",
-Camera.pos.x + self.pos.x + self.boxCollision.from.x,
-Camera.pos.y + self.pos.y + self.boxCollision.from.y,
-Camera.pos.x + self.pos.x + self.boxCollision.to.x -(-Camera.pos.x + self.pos.x + self.boxCollision.from.x),
-Camera.pos.y + self.pos.y + self.boxCollision.to.y -(-Camera.pos.y + self.pos.y + self.boxCollision.from.y)
)
end
require "data/scripts/entities/kupo"
require "data/scripts/entities/arrow"
require "data/scripts/entities/decoration"
require "data/scripts/entities/player"
require "data/scripts/entities/fairy"
require "data/scripts/entities/cursed_book"
require "data/scripts/entities/particle"