Compare commits

...

2 Commits

Author SHA1 Message Date
serxoz
bb2e13d34d Solucionado que apareceran as ovellas enriba do cercado 2022-07-04 16:34:51 +02:00
serxoz
8135d6a454 por fin dibuxa todo o cercado 2022-06-27 23:48:45 +02:00
4 changed files with 106 additions and 42 deletions

View File

@ -5,32 +5,44 @@ Cercado.filas = 4
Cercado.columnas = 6 Cercado.columnas = 6
--inicialización tablas para os tiles do cercado --inicialización tablas para os tiles do cercado
vsi = {x=0, y=0, width=50, height=50} vsi1 = {x=0, y=0, width=50, height=50}
vsd = {x=0, y=0, width=50, height=50} vsd1 = {x=0, y=0, width=50, height=50}
vii = {x=0, y=0, width=50, height=50} vii1 = {x=0, y=0, width=50, height=50}
vid = {x=0, y=0, width=50, height=50} vid1 = {x=0, y=0, width=50, height=50}
vli = {x=0, y=0, width=50, height=50} vli1 = {x=0, y=0, width=50, height=50}
vld = {x=0, y=0, width=50, height=50} vli2 = {x=0, y=0, width=50, height=50}
vfo = {x=0, y=0, width=50, height=50} vld1 = {x=0, y=0, width=50, height=50}
vfr = {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 -- imaxen de cada tile
vsi.sprite = love.graphics.newImage("img/valla-sup-ida.png") vsi1.sprite = love.graphics.newImage("img/valla-sup-ida.png")
vsd.sprite = love.graphics.newImage("img/valla-sup-dta.png") vsd1.sprite = love.graphics.newImage("img/valla-sup-dta.png")
vii.sprite = love.graphics.newImage("img/valla-inf-ida.png") vii1.sprite = love.graphics.newImage("img/valla-inf-ida.png")
vid.sprite = love.graphics.newImage("img/valla-inf-dta.png") vid1.sprite = love.graphics.newImage("img/valla-inf-dta.png")
vli.sprite = love.graphics.newImage("img/valla-lat-ida.png") vli1.sprite = love.graphics.newImage("img/valla-lat-ida.png")
vld.sprite = love.graphics.newImage("img/valla-lat-dta.png") vli2.sprite = love.graphics.newImage("img/valla-lat-ida.png")
vfo.sprite = love.graphics.newImage("img/valla-fondo.png") vld1.sprite = love.graphics.newImage("img/valla-lat-dta.png")
vfr.sprite = love.graphics.newImage("img/valla-frente.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) function Cercado:new(x, y)
-- self.sprite = love.graphics.newImage("img/cercado.png") -- self.sprite = love.graphics.newImage("img/cercado.png")
self.tiles = { self.tiles = {
{vsi,vfo,vfo,vfo,vfo,vsd}, {vsi1,vfo1,vfo2,vfo3,vfo4,vsd1},
{vli,'x','x','x','x',vld}, {vli1, 'x', 'x', 'x', 'x',vld1},
{vli,'x','x','x','x',vld}, {vli2, 'x', 'x', 'x', 'x',vld2},
{vii,vfr,'x','x',vfr,vid}, {vii1,vfr1, 'x', 'x',vfr2,vid1},
} }
-- self.width = self.sprite:getWidth() -- self.width = self.sprite:getWidth()
-- self.height = self.sprite:getHeight() -- self.height = self.sprite:getHeight()
@ -40,24 +52,41 @@ function Cercado:new(x, y)
self.y = y-self.height/2 self.y = y-self.height/2
self.speed = 2 self.speed = 2
return self -- Xenera as físicas para cada tile do cercado
end
function Cercado:draw()
Y = self.y Y = self.y
blockSize = 50 blockSize = 50
for y = 1,self.filas do for f = 1,table.getn(self.tiles) do
X = self.x X = self.x
for x = 1,self.columnas do for c = 1,table.getn(self.tiles[f]) do
if self.tiles[y][x] ~= 'x' then if self.tiles[f][c].sprite then
-- acutaliza coordenadas -- actualiza coordenadas
self.tiles[y][x].x = X self.tiles[f][c].x = X
self.tiles[y][x].y = Y self.tiles[f][c].y = Y
-- dibuxa -- asocia físicas
love.graphics.draw(self.tiles[y][x].sprite, X, Y) 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 end
X = X + blockSize X = X + blockSize
end end
Y = Y + blockSize Y = Y + blockSize
end 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 end

View File

@ -77,8 +77,28 @@ function Ovella:update(pastor, cercado)
end end
function Ovella:draw() function Ovella:draw(cercado)
love.graphics.draw(self.sprite, self.x, self.y) -- love.graphics.draw(self.sprite, self.x, self.y)
local solapase = false
for y = 1,cercado.filas do
for x = 1,cercado.columnas do
if cercado.tiles[y][x].sprite 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],self) then
solapase = true
-- FIXME movo a ovella en diagonal para quitala de enriba
-- do cercado
self.x = self.x+50
self.y = self.y+50
end
end
end
end
if not solapase then
love.graphics.draw(self.sprite, self.x, self.y)
end
end end
return Ovella return Ovella

View File

@ -8,6 +8,11 @@ function Pastor:new(x, y)
self.y = y-self.height/2 self.y = y-self.height/2
self.speed = 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 return self
end end
@ -16,20 +21,22 @@ function Pastor:update(dt)
width = width - self.width width = width - self.width
height = height - self.height 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 self.y = self.y - self.speed
end 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 self.y = self.y + self.speed
end 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 self.x = self.x - self.speed
end 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 self.x = self.x + self.speed
end end
end end
function Pastor:draw() 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) love.graphics.draw(self.sprite, self.x, self.y)
end end

View File

@ -14,13 +14,14 @@ end
-- globales -- globales
ovellas = {} ovellas = {}
world = love.physics.newWorld(0,0,true)
-- inicia os recursos, execútase ó cargar o xogo -- inicia os recursos, execútase ó cargar o xogo
function love.load() function love.load()
local width, height, flags = love.window.getMode(); local width, height, flags = love.window.getMode();
local BORDER = 100; -- pixeles de borde para non xenerar ovellas nos marxes local BORDER = 100; -- pixeles de borde para non xenerar ovellas nos marxes
-- imaxes -- imaxes
background = love.graphics.newImage("img/fondo.png") background = love.graphics.newImage("img/fondo.png")
-- fonte -- fonte
@ -62,18 +63,25 @@ function love.draw()
-- ovellas -- ovellas
for i = 1, #ovellas,1 do --itera sobre cada ovella na tabla for i = 1, #ovellas,1 do --itera sobre cada ovella na tabla
-- print(inspect(ovellas[i])) -- print(inspect(ovellas[i]))
ovellas[i]:draw() ovellas[i]:draw(cercado)
end end
love.graphics.print("Ovellas recollidas: 0", 10, 10) love.graphics.print("Ovellas recollidas: 0", 10, 10)
end end
function love.update(dt) function love.update(dt)
-- update world
world:update(dt)
--- update player object --- update player object
player:update(dt, cercado) 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)
end end
-- QUIT!
if love.keyboard.isDown("q") then
love.event.quit()
end
end end