ZLP1/scripts/entity.lua

113 lines
2.1 KiB
Lua
Raw Permalink Normal View History

2021-12-31 01:46:21 +00:00
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
2022-01-17 23:53:03 +00:00
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])
2021-12-31 01:46:21 +00:00
end
end
2022-01-17 23:53:03 +00:00
function Entity:DistanceWith(obj, distance)
if math.sqrt((obj.pos.x-self.pos.x)^2 + (obj.pos.y-self.pos.y)^2) < distance then
2021-12-31 01:46:21 +00:00
return true
else
return false
end
end
2022-01-17 23:53:03 +00:00
function Entity:Kill()
for target, _ in pairs(self.id) do
self:RemoveList(target)
2021-12-31 01:46:21 +00:00
end
2022-01-17 23:53:03 +00:00
self = nil
2021-12-31 01:46:21 +00:00
end
function Entity:AngleTowards(x,y)
target_x = x - self.pos.x
target_y = y - self.pos.y
2022-01-17 23:53:03 +00:00
self.angle = GetAngleFromVector(target_x,target_y)
2021-12-31 01:46:21 +00:00
end
function Entity:MoveTowards(x,y,speed)
target_x = x - self.pos.x
target_y = y - self.pos.y
2022-01-17 23:53:03 +00:00
local angle = GetAngleFromVector(target_x,target_y)
2021-12-31 01:46:21 +00:00
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"