From 8b628a7e294cd82ab19574ccb60b7d4269a6d2a9 Mon Sep 17 00:00:00 2001 From: serxoz Date: Wed, 1 Jun 2022 23:34:30 +0200 Subject: [PATCH] =?UTF-8?q?comezo=20colisi=C3=B3s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entidades/cercado.lua | 57 ++++++++++++++++++++++++++++++++++++++----- entidades/ovella.lua | 31 ++++++++++++++++++++--- main.lua | 2 +- 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/entidades/cercado.lua b/entidades/cercado.lua index 1210141..229be9a 100644 --- a/entidades/cercado.lua +++ b/entidades/cercado.lua @@ -1,18 +1,63 @@ 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) - self.sprite = love.graphics.newImage("img/cercado.png") - self.width = self.sprite:getWidth() - self.height = self.sprite:getHeight() + -- self.sprite = love.graphics.newImage("img/cercado.png") + self.tiles = { + {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.y = y-self.height/2 self.speed = 2 - self.proba = love.graphics.newImage("img/valla-frente.png") return self end function Cercado:draw() - love.graphics.draw(self.sprite, self.x, self.y) - love.graphics.draw(self.proba, self.x, self.y) + Y = 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 diff --git a/entidades/ovella.lua b/entidades/ovella.lua index ce0d8b4..4de89a7 100644 --- a/entidades/ovella.lua +++ b/entidades/ovella.lua @@ -45,11 +45,34 @@ function Ovella:update(pastor, cercado) if distancia < DISTANCIA then -- movemento -- 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 - -- Establece a nova posición - self.x = self.x - dX; - self.y = self.y - dY; + 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 + self.x = self.x - dX; + self.y = self.y - dY; + end end end diff --git a/main.lua b/main.lua index 1fbe913..20f181c 100644 --- a/main.lua +++ b/main.lua @@ -70,7 +70,7 @@ end function love.update(dt) --- update player object - player:update(dt) + player:update(dt, cercado) -- update ovella object for i = 1, #ovellas,1 do --itera sobre cada ovella na tabla ovellas[i]:update(player, cercado)