ovellas/entidades/ovella.lua

62 lines
1.4 KiB
Lua
Raw Normal View History

2022-05-30 20:34:58 +02:00
require("../lib/helpers")
2022-05-28 00:05:01 +02:00
Ovella = {}
local OvellaMT = {__index = Ovella}
function Ovella:new(id, x, y)
local instance = {}
instance.id = id
instance.sprite = love.graphics.newImage("img/ovella.png")
instance.width = instance.sprite:getWidth()
instance.height = instance.sprite:getHeight()
instance.x = x-instance.width/2
instance.y = y-instance.height/2
instance.speed = 1
return setmetatable(instance, OvellaMT)
end
2022-05-30 20:34:58 +02:00
function Ovella:update(pastor, cercado)
2022-05-28 00:05:01 +02:00
-- vector
local X = pastor.x - self.x;
local Y = pastor.y - self.y;
distancia = math.sqrt(math.pow(X, 2) + math.pow(Y, 2));
--normalizase o vector
X = X / distancia;
Y = Y / distancia;
-- redondease ó int máis cercano
if X < 0 then
X = math.floor(X - 0.5);
else
X = math.floor(X + 0.5);
end
if Y < 0 then
Y = math.floor(Y - 0.5);
else
Y = math.floor(Y + 0.5);
end
-- velocidade
dX = X * self.speed;
dY = Y * self.speed;
if distancia < DISTANCIA then
-- movemento
-- FIXME comprobar que non se vai de marxenes e non choca con muros
2022-05-30 20:34:58 +02:00
print(Helpers:CheckCollision(self, cercado))
-- Establece a nova posición
2022-05-28 00:05:01 +02:00
self.x = self.x - dX;
self.y = self.y - dY;
end
end
function Ovella:draw()
love.graphics.draw(self.sprite, self.x, self.y)
end
return Ovella