113 lines
2.1 KiB
Lua
113 lines
2.1 KiB
Lua
Entity = {}
|
|
|
|
function Entity:New(x,y)
|
|
o = {}
|
|
o.pos = {x = x, y = y}
|
|
o.vel = {x = 0, y = 0}
|
|
o.target = {x = 0, y = 0}
|
|
o.hitbox = 0
|
|
o.angle = 0
|
|
o.direction = 0
|
|
o.show_health = false
|
|
o.id = {}
|
|
setmetatable(o, self)
|
|
self.__index = self
|
|
return o
|
|
end
|
|
|
|
function Entity:Damage(damage)
|
|
self.health = self.health - damage
|
|
if self.health <= 0 then
|
|
self:Kill()
|
|
end
|
|
end
|
|
|
|
function Entity:DrawHealth()
|
|
local width = 400
|
|
local height = 10
|
|
love.graphics.setColor(1,1,1)
|
|
love.graphics.rectangle(
|
|
"fill",
|
|
self.pos.x - width/2,
|
|
self.pos.y - height * self.draw_scale,
|
|
width,
|
|
height
|
|
)
|
|
love.graphics.setColor(1,0,0)
|
|
love.graphics.rectangle(
|
|
"fill",
|
|
self.pos.x - width/2+1,
|
|
self.pos.y - height * self.draw_scale+1,
|
|
self.health/self.max_health*(width-2),
|
|
height-2
|
|
)
|
|
love.graphics.setColor(1,1,1)
|
|
|
|
end
|
|
|
|
function Entity:AddList( target)
|
|
if List[target] == nil then List[target] = {} end
|
|
table.insert(List[target],self)
|
|
self.id[target] = #List[target]
|
|
end
|
|
|
|
function Entity:RemoveList(target)
|
|
if self.id[target] ~= nil then
|
|
for _, e in pairs(List[target]) do
|
|
if e.id[target] > self.id[target] then
|
|
e.id[target] = e.id[target] - 1
|
|
end
|
|
end
|
|
table.remove(List[target],self.id[target])
|
|
end
|
|
end
|
|
|
|
function Entity:DistanceWith(obj, distance)
|
|
if math.sqrt((obj.pos.x-self.pos.x)^2 + (obj.pos.y-self.pos.y)^2) < distance then
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
function Entity:Kill()
|
|
for target, _ in pairs(self.id) do
|
|
self:RemoveList(target)
|
|
end
|
|
|
|
self = nil
|
|
end
|
|
|
|
function Entity:AngleTowards(x,y)
|
|
target_x = x - self.pos.x
|
|
target_y = y - self.pos.y
|
|
|
|
self.angle = GetAngleFromVector(target_x,target_y)
|
|
end
|
|
|
|
|
|
function Entity:MoveTowards(x,y,speed)
|
|
target_x = x - self.pos.x
|
|
target_y = y - self.pos.y
|
|
|
|
local angle = GetAngleFromVector(target_x,target_y)
|
|
|
|
if speed ~= 0 then
|
|
if target_x ~= 0 then
|
|
self.vel.x = speed * math.cos(angle)
|
|
else
|
|
self.vel.x = 0
|
|
end
|
|
if target_y ~= 0 then
|
|
self.vel.y = speed * math.sin(angle)
|
|
else
|
|
self.vel.y = 0
|
|
end
|
|
end
|
|
end
|
|
|
|
require "scripts/entities/player"
|
|
require "scripts/entities/enemy/fang"
|
|
require "scripts/entities/enemy/shooter"
|
|
require "scripts/entities/bullet"
|