From 8135d6a4544718b54d8ac9ee6a60ec142a082cf8 Mon Sep 17 00:00:00 2001 From: serxoz Date: Mon, 27 Jun 2022 23:48:45 +0200 Subject: [PATCH] por fin dibuxa todo o cercado --- entidades/cercado.lua | 93 ++++++++++++++++++++++++++++--------------- entidades/pastor.lua | 15 +++++-- main.lua | 10 ++++- 3 files changed, 81 insertions(+), 37 deletions(-) diff --git a/entidades/cercado.lua b/entidades/cercado.lua index 229be9a..ea29e36 100644 --- a/entidades/cercado.lua +++ b/entidades/cercado.lua @@ -5,32 +5,44 @@ 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} +vsi1 = {x=0, y=0, width=50, height=50} +vsd1 = {x=0, y=0, width=50, height=50} +vii1 = {x=0, y=0, width=50, height=50} +vid1 = {x=0, y=0, width=50, height=50} +vli1 = {x=0, y=0, width=50, height=50} +vli2 = {x=0, y=0, width=50, height=50} +vld1 = {x=0, y=0, width=50, height=50} +vld2 = {x=0, y=0, width=50, height=50} +vfo1 = {x=0, y=0, width=50, height=50} +vfo2 = {x=0, y=0, width=50, height=50} +vfo3 = {x=0, y=0, width=50, height=50} +vfo4 = {x=0, y=0, width=50, height=50} +vfr1 = {x=0, y=0, width=50, height=50} +vfr2 = {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") +vsi1.sprite = love.graphics.newImage("img/valla-sup-ida.png") +vsd1.sprite = love.graphics.newImage("img/valla-sup-dta.png") +vii1.sprite = love.graphics.newImage("img/valla-inf-ida.png") +vid1.sprite = love.graphics.newImage("img/valla-inf-dta.png") +vli1.sprite = love.graphics.newImage("img/valla-lat-ida.png") +vli2.sprite = love.graphics.newImage("img/valla-lat-ida.png") +vld1.sprite = love.graphics.newImage("img/valla-lat-dta.png") +vld2.sprite = love.graphics.newImage("img/valla-lat-dta.png") +vfo1.sprite = love.graphics.newImage("img/valla-fondo.png") +vfo2.sprite = love.graphics.newImage("img/valla-fondo.png") +vfo3.sprite = love.graphics.newImage("img/valla-fondo.png") +vfo4.sprite = love.graphics.newImage("img/valla-fondo.png") +vfr1.sprite = love.graphics.newImage("img/valla-frente.png") +vfr2.sprite = love.graphics.newImage("img/valla-frente.png") function Cercado:new(x, y) -- 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}, + {vsi1,vfo1,vfo2,vfo3,vfo4,vsd1}, + {vli1, 'x', 'x', 'x', 'x',vld1}, + {vli2, 'x', 'x', 'x', 'x',vld2}, + {vii1,vfr1, 'x', 'x',vfr2,vid1}, } -- self.width = self.sprite:getWidth() -- self.height = self.sprite:getHeight() @@ -40,24 +52,41 @@ function Cercado:new(x, y) self.y = y-self.height/2 self.speed = 2 - return self -end - -function Cercado:draw() + -- Xenera as físicas para cada tile do cercado Y = self.y blockSize = 50 - for y = 1,self.filas do + for f = 1,table.getn(self.tiles) 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) + for c = 1,table.getn(self.tiles[f]) do + if self.tiles[f][c].sprite then + -- actualiza coordenadas + self.tiles[f][c].x = X + self.tiles[f][c].y = Y + -- asocia físicas + self.tiles[f][c].body = love.physics.newBody(world, X+25, Y+25) + self.tiles[f][c].shape = love.physics.newRectangleShape(30,30) + self.tiles[f][c].fixture = love.physics.newFixture(self.tiles[f][c].body, self.tiles[f][c].shape) end X = X + blockSize end Y = Y + blockSize end + + return self +end + +function Cercado:draw() + -- recorrese a tabla dos tiles + for f = 1,table.getn(self.tiles) do + for c = 1,table.getn(self.tiles[f]) do + if self.tiles[f][c].sprite then + -- dibuxa + love.graphics.polygon("fill", + self.tiles[f][c].body:getWorldPoints( + self.tiles[f][c].shape:getPoints() + )) + love.graphics.draw(self.tiles[f][c].sprite, self.tiles[f][c].x, self.tiles[f][c].y) + end + end + end end diff --git a/entidades/pastor.lua b/entidades/pastor.lua index 69800b7..55e2aac 100644 --- a/entidades/pastor.lua +++ b/entidades/pastor.lua @@ -8,6 +8,11 @@ function Pastor:new(x, y) self.y = y-self.height/2 self.speed = 2 + local width, height, flags = love.window.getMode(); + self.body = love.physics.newBody(world, width/2, height/2, "dynamic") + self.shape = love.physics.newCircleShape(self.width/2+5) + self.fixture = love.physics.newFixture(self.body, self.shape, 1) + return self end @@ -16,20 +21,22 @@ function Pastor:update(dt) width = width - self.width height = height - self.height - if love.keyboard.isDown("up") and self.y > 0 then + if (love.keyboard.isDown("up") or love.keyboard.isDown("k")) and self.y > 0 then self.y = self.y - self.speed end - if love.keyboard.isDown("down") and self.y < height then + if (love.keyboard.isDown("down") or love.keyboard.isDown("j")) and self.y < height then self.y = self.y + self.speed end - if love.keyboard.isDown("left") and self.x > 0 then + if (love.keyboard.isDown("left") or love.keyboard.isDown("h"))and self.x > 0 then self.x = self.x - self.speed end - if love.keyboard.isDown("right") and self.x < width then + if (love.keyboard.isDown("right") or love.keyboard.isDown("l")) and self.x < width then self.x = self.x + self.speed end end function Pastor:draw() + -- love.graphics.circle("fill", self.body:getX(), self.body:getY(), self.shape:getRadius()) + love.graphics.circle("fill", self.x+self.width/2, self.y+self.height/2, self.shape:getRadius()) love.graphics.draw(self.sprite, self.x, self.y) end diff --git a/main.lua b/main.lua index 20f181c..8b45293 100644 --- a/main.lua +++ b/main.lua @@ -14,13 +14,14 @@ end -- globales ovellas = {} +world = love.physics.newWorld(0,0,true) -- inicia os recursos, execútase ó cargar o xogo function love.load() local width, height, flags = love.window.getMode(); local BORDER = 100; -- pixeles de borde para non xenerar ovellas nos marxes - -- imaxes + -- imaxes background = love.graphics.newImage("img/fondo.png") -- fonte @@ -69,6 +70,9 @@ function love.draw() end function love.update(dt) + -- update world + world:update(dt) + --- update player object player:update(dt, cercado) -- update ovella object @@ -76,4 +80,8 @@ function love.update(dt) ovellas[i]:update(player, cercado) end + -- QUIT! + if love.keyboard.isDown("q") then + love.event.quit() + end end