ZLP1/scripts/entity.lua

150 lines
3.1 KiB
Lua
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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(list)
table.insert(list,self)
if list == List.Simulate then
self.id.Simulate = #list
elseif list == List.Enemy then
self.id.Enemy = #list
elseif list == List.EnemyBullet then
self.id.EnemyBullet = #list
elseif list == List.FriendlyBullet then
self.id.FriendlyBullet = #list
end
end
function Entity:CollisionWith(obj)
if math.sqrt((obj.pos.x-self.pos.x)^2 + (obj.pos.y-self.pos.y)^2) < obj.hitbox + self.hitbox then
return true
else
return false
end
end
function Entity:RemoveList(list)
if list == List.Simulate then
for _, e in pairs(list) do
if e.id.Simulate > self.id.Simulate then
e.id.Simulate = e.id.Simulate - 1
end
end
table.remove(list,self.id.Simulate)
elseif list == List.Enemy then
for _, e in pairs(list) do
if e.id.Enemy > self.id.Enemy then
e.id.Enemy = e.id.Enemy - 1
end
end
table.remove(list,self.id.Enemy)
elseif list == List.EnemyBullet then
for _, e in pairs(list) do
if e.id.EnemyBullet > self.id.EnemyBullet then
e.id.EnemyBullet = e.id.EnemyBullet - 1
end
end
table.remove(list,self.id.EnemyBullet)
elseif list == List.FriendlyBullet then
for _, e in pairs(list) do
if e.id.FriendlyBullet > self.id.FriendlyBullet then
e.id.FriendlyBullet = e.id.FriendlyBullet - 1
end
end
table.remove(list,self.id.FriendlyBullet)
end
end
function Entity:AngleTowards(x,y)
target_x = x - self.pos.x
target_y = y - self.pos.y
self.angle = GetAngle(target_x,target_y)
end
function Entity:Kill()
if self.id.Simulate ~= nil then
self:RemoveList(List.Simulate)
end
if self.id.Enemy ~= nil then
self:RemoveList(List.Enemy)
end
if self.id.EnemyBullet ~= nil then
self:RemoveList(List.EnemyBullet)
end
if self.id.FriendlyBullet ~= nil then
self:RemoveList(List.FriendlyBullet)
end
self = nil
end
function Entity:MoveTowards(x,y,speed)
target_x = x - self.pos.x
target_y = y - self.pos.y
local angle = GetAngle(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"