comezo colisiós

This commit is contained in:
serxoz 2022-06-01 23:34:30 +02:00
parent bf94860af3
commit 8b628a7e29
3 changed files with 79 additions and 11 deletions

View File

@ -1,18 +1,63 @@
Cercado = {} Cercado = {}
-- configuracion tamaño
Cercado.filas = 4
Cercado.columnas = 6
--inicialización tablas para os tiles do cercado
vsi = {x=0, y=0, width=50, height=50}
vsd = {x=0, y=0, width=50, height=50}
vii = {x=0, y=0, width=50, height=50}
vid = {x=0, y=0, width=50, height=50}
vli = {x=0, y=0, width=50, height=50}
vld = {x=0, y=0, width=50, height=50}
vfo = {x=0, y=0, width=50, height=50}
vfr = {x=0, y=0, width=50, height=50}
-- imaxen de cada tile
vsi.sprite = love.graphics.newImage("img/valla-sup-ida.png")
vsd.sprite = love.graphics.newImage("img/valla-sup-dta.png")
vii.sprite = love.graphics.newImage("img/valla-inf-ida.png")
vid.sprite = love.graphics.newImage("img/valla-inf-dta.png")
vli.sprite = love.graphics.newImage("img/valla-lat-ida.png")
vld.sprite = love.graphics.newImage("img/valla-lat-dta.png")
vfo.sprite = love.graphics.newImage("img/valla-fondo.png")
vfr.sprite = love.graphics.newImage("img/valla-frente.png")
function Cercado:new(x, y) function Cercado:new(x, y)
self.sprite = love.graphics.newImage("img/cercado.png") -- self.sprite = love.graphics.newImage("img/cercado.png")
self.width = self.sprite:getWidth() self.tiles = {
self.height = self.sprite:getHeight() {vsi,vfo,vfo,vfo,vfo,vsd},
{vli,'x','x','x','x',vld},
{vli,'x','x','x','x',vld},
{vii,vfr,'x','x',vfr,vid},
}
-- self.width = self.sprite:getWidth()
-- self.height = self.sprite:getHeight()
self.width = 300 -- cada bloque 50x50 px
self.height = 240
self.x = x-self.width/2 self.x = x-self.width/2
self.y = y-self.height/2 self.y = y-self.height/2
self.speed = 2 self.speed = 2
self.proba = love.graphics.newImage("img/valla-frente.png")
return self return self
end end
function Cercado:draw() function Cercado:draw()
love.graphics.draw(self.sprite, self.x, self.y) Y = self.y
love.graphics.draw(self.proba, self.x, self.y) blockSize = 50
for y = 1,self.filas do
X = self.x
for x = 1,self.columnas do
if self.tiles[y][x] ~= 'x' then
-- acutaliza coordenadas
self.tiles[y][x].x = X
self.tiles[y][x].y = Y
-- dibuxa
love.graphics.draw(self.tiles[y][x].sprite, X, Y)
end
X = X + blockSize
end
Y = Y + blockSize
end
end end

View File

@ -45,12 +45,35 @@ function Ovella:update(pastor, cercado)
if distancia < DISTANCIA then if distancia < DISTANCIA then
-- movemento -- movemento
-- FIXME comprobar que non se vai de marxenes e non choca con muros -- FIXME comprobar que non se vai de marxenes e non choca con muros
print(Helpers:CheckCollision(self, cercado)) -- print(Helpers:CheckCollision(self, cercado)) -- comprobaría con todo o cercado
futuro = {}
futuro.x = self.x - dX;
futuro.y = self.y - dY;
futuro.width = self.width--/3;
futuro.height = self.height--/3;
-- futuraOvella = Ovella:new(self.id, self.x-dX, self.y-dY)
-- comprobar colisios con cada baldosa do cercado
colision = false
for y = 1,cercado.filas do
for x = 1,cercado.columnas do
if cercado.tiles[y][x] ~= 'x' then
-- print(Helpers:CheckCollision(self, cercado.tiles[y][x]))
-- if Helpers:CheckCollision(futuro, cercado.tiles[y][x]) then
if Helpers:CheckCollision( cercado.tiles[y][x],futuro) then
colision = true
end
end
end
end
if not colision then
-- Establece a nova posición -- Establece a nova posición
self.x = self.x - dX; self.x = self.x - dX;
self.y = self.y - dY; self.y = self.y - dY;
end end
end
end end

View File

@ -70,7 +70,7 @@ end
function love.update(dt) function love.update(dt)
--- update player object --- update player object
player:update(dt) player:update(dt, cercado)
-- update ovella object -- update ovella object
for i = 1, #ovellas,1 do --itera sobre cada ovella na tabla for i = 1, #ovellas,1 do --itera sobre cada ovella na tabla
ovellas[i]:update(player, cercado) ovellas[i]:update(player, cercado)