2022-01-17 23:14:54 +00:00
|
|
|
Entity = {class = "Entity"}
|
2021-10-25 23:19:22 +00:00
|
|
|
|
|
|
|
function Entity:New(x,y)
|
|
|
|
o = {}
|
|
|
|
o.pos = {x = x, y = y}
|
|
|
|
o.vel = {x = 0, y = 0}
|
2021-11-28 02:38:30 +00:00
|
|
|
|
2022-01-19 23:53:59 +00:00
|
|
|
o.direction = 0
|
|
|
|
|
2021-11-28 02:38:30 +00:00
|
|
|
o.boxCollision = {
|
|
|
|
from = {x = x, y = y},
|
2022-01-17 23:14:54 +00:00
|
|
|
to = {x = x, y = y},
|
2021-11-28 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 23:14:54 +00:00
|
|
|
o.target_offset = {x = 0, y = 0}
|
2022-01-19 23:53:59 +00:00
|
|
|
|
2021-10-25 23:19:22 +00:00
|
|
|
o.sprite_offset = {x = 0, y = 0}
|
|
|
|
o.sprite_scale = {x = 1, y = 1}
|
|
|
|
o.sprite_rotation = math.rad(0)
|
2022-01-17 23:14:54 +00:00
|
|
|
o.sprite_tint = {1,1,1}
|
2022-01-19 22:29:02 +00:00
|
|
|
o.sprite_alpha = 1
|
2021-10-25 23:19:22 +00:00
|
|
|
o.sprite_flip = { x = 1, y = 1}
|
2021-10-27 09:06:08 +00:00
|
|
|
o.illuminated = false
|
2021-11-28 02:38:30 +00:00
|
|
|
|
2021-10-25 23:19:22 +00:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
2022-01-19 22:29:02 +00:00
|
|
|
|
2021-10-25 23:19:22 +00:00
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2022-01-19 23:53:59 +00:00
|
|
|
function Entity:Smart()
|
|
|
|
end
|
|
|
|
|
2022-01-22 22:10:21 +00:00
|
|
|
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
|
2022-01-29 07:49:45 +00:00
|
|
|
else
|
|
|
|
self.vel.x = 0
|
2022-01-22 22:10:21 +00:00
|
|
|
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
|
2022-01-29 07:49:45 +00:00
|
|
|
else
|
|
|
|
self.vel.y = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-03 05:02:06 +00:00
|
|
|
function Entity:LightAdjust(x,y)
|
2022-01-29 07:49:45 +00:00
|
|
|
if self.light ~= nil then
|
2022-02-03 05:02:06 +00:00
|
|
|
local x = x or 0
|
|
|
|
local y = y or 0
|
2022-01-29 07:49:45 +00:00
|
|
|
self.light.pos.x = self.pos.x
|
|
|
|
self.light.pos.y = self.pos.y
|
2022-01-22 22:10:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-19 23:53:59 +00:00
|
|
|
function Entity:Kill()
|
2022-01-20 13:06:40 +00:00
|
|
|
if self.light ~= nil then
|
|
|
|
KillLight(self.light)
|
|
|
|
end
|
2022-01-19 23:53:59 +00:00
|
|
|
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
|
|
|
|
|
2022-02-08 07:22:27 +00:00
|
|
|
function Entity:CheckVisionLine(entity,range)
|
2022-02-05 11:32:47 +00:00
|
|
|
local target_x = entity.pos.x + entity.target_offset.x
|
|
|
|
local target_y = entity.pos.y + entity.target_offset.y
|
|
|
|
|
|
|
|
local distance_x = target_x - self.pos.x
|
|
|
|
local distance_y = target_y - self.pos.y
|
|
|
|
|
|
|
|
local angle = GetAngleFromVector(distance_x,distance_y)
|
|
|
|
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
|
|
|
|
|
2022-02-08 07:22:27 +00:00
|
|
|
local is_colliding = true
|
|
|
|
|
|
|
|
if distance < range then
|
|
|
|
is_colliding = false
|
|
|
|
for i=1, distance, game.scale do
|
|
|
|
if isThereObjectAt(
|
|
|
|
self.pos.x+math.cos(angle)*i,
|
|
|
|
self.pos.y+math.sin(angle)*i,
|
|
|
|
LoadedObjects.Collisions
|
|
|
|
) then
|
|
|
|
is_colliding = true
|
|
|
|
end
|
2022-02-05 11:32:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return not is_colliding
|
|
|
|
end
|
|
|
|
|
2022-01-19 23:53:59 +00:00
|
|
|
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
|
|
|
|
|
2022-01-17 23:14:54 +00:00
|
|
|
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
|
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
function Entity:getBoundingBox(animation,top,left,bottom,right)
|
2022-01-17 23:14:54 +00:00
|
|
|
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
|
|
|
|
|
2021-11-28 02:38:30 +00:00
|
|
|
-- returns true if theres a collision at that point. also marks collisioned tile as collision true
|
|
|
|
function Entity:isCollidingAt(x,y,object)
|
2022-02-08 07:22:27 +00:00
|
|
|
for _, collision in pairs(object) do
|
|
|
|
if collision.disable then
|
|
|
|
-- Dont calculate if disabled
|
|
|
|
elseif x + self.boxCollision.from.x < collision.to.x
|
|
|
|
and x + self.boxCollision.to.x > collision.from.x
|
|
|
|
and y + self.boxCollision.from.y < collision.to.y
|
|
|
|
and y + self.boxCollision.to.y > collision.from.y
|
|
|
|
then
|
|
|
|
collision.isColliding = true
|
|
|
|
return true
|
|
|
|
end
|
2021-11-28 02:38:30 +00:00
|
|
|
end
|
2022-02-08 07:22:27 +00:00
|
|
|
return false
|
2021-11-28 02:38:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Entity:isCollidingWith(entity)
|
|
|
|
return self.pos.x + self.boxCollision.from.x < entity.pos.x + entity.boxCollision.to.x
|
2022-01-17 23:14:54 +00:00
|
|
|
and entity.pos.x + entity.boxCollision.from.x < self.pos.x + self.boxCollision.to.x
|
2021-11-28 02:38:30 +00:00
|
|
|
and self.pos.y + self.boxCollision.from.y < entity.pos.y + entity.boxCollision.to.y
|
2022-01-17 23:14:54 +00:00
|
|
|
and entity.pos.y + entity.boxCollision.from.y < self.pos.y + self.boxCollision.to.y
|
2021-11-28 02:38:30 +00:00
|
|
|
end
|
2021-10-25 23:19:22 +00:00
|
|
|
|
2021-11-28 02:38:30 +00:00
|
|
|
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
|
2021-10-25 23:19:22 +00:00
|
|
|
end
|
|
|
|
|
2022-02-08 07:22:27 +00:00
|
|
|
function Entity:CheckVisionLineDebug(entity,range)
|
2022-02-05 11:32:47 +00:00
|
|
|
local c1, c2, c3, a = love.graphics.getColor()
|
|
|
|
|
|
|
|
local target_x = entity.pos.x + entity.target_offset.x
|
|
|
|
local target_y = entity.pos.y + entity.target_offset.y
|
|
|
|
|
|
|
|
local distance_x = target_x - self.pos.x
|
|
|
|
local distance_y = target_y - self.pos.y
|
|
|
|
|
|
|
|
local angle = GetAngleFromVector(distance_x,distance_y)
|
|
|
|
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
|
|
|
|
|
2022-02-08 07:22:27 +00:00
|
|
|
if distance < range then
|
|
|
|
for i=1, distance, game.scale do
|
|
|
|
if isThereObjectAt(
|
|
|
|
self.pos.x+math.cos(angle)*i,
|
|
|
|
self.pos.y+math.sin(angle)*i,
|
|
|
|
LoadedObjects.Collisions
|
|
|
|
) then
|
|
|
|
love.graphics.setColor(1,0,0)
|
|
|
|
else
|
|
|
|
love.graphics.setColor(0,1,0)
|
|
|
|
end
|
|
|
|
love.graphics.line(
|
|
|
|
self.pos.x+math.cos(angle)*i-1 - Camera.pos.x,
|
|
|
|
self.pos.y+math.sin(angle)*i-1 - Camera.pos.y,
|
|
|
|
self.pos.x+math.cos(angle)*i - Camera.pos.x,
|
|
|
|
self.pos.y+math.sin(angle)*i - Camera.pos.y
|
|
|
|
)
|
2022-02-05 11:32:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
love.graphics.setColor(c1,c2,c3,a)
|
|
|
|
end
|
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
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)
|
|
|
|
)
|
2022-02-08 07:22:27 +00:00
|
|
|
if self.target ~= nil then
|
|
|
|
love.graphics.line(
|
|
|
|
-Camera.pos.x + self.pos.x,
|
|
|
|
-Camera.pos.y + self.pos.y,
|
|
|
|
-Camera.pos.x + self.target.x,
|
|
|
|
-Camera.pos.y + self.target.y
|
|
|
|
)
|
|
|
|
end
|
2022-01-29 07:49:45 +00:00
|
|
|
end
|
2021-10-25 23:19:22 +00:00
|
|
|
require "data/scripts/entities/kupo"
|
|
|
|
require "data/scripts/entities/arrow"
|
2022-01-17 23:14:54 +00:00
|
|
|
require "data/scripts/entities/decoration"
|
2021-10-25 23:19:22 +00:00
|
|
|
require "data/scripts/entities/player"
|
2022-01-17 23:14:54 +00:00
|
|
|
require "data/scripts/entities/fairy"
|
2022-01-29 07:49:45 +00:00
|
|
|
require "data/scripts/entities/cursed_book"
|
2022-01-19 22:29:02 +00:00
|
|
|
require "data/scripts/entities/particle"
|