From 32a4ebfa5a8af65bd763dfaeef0a0217e94cceeb Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 15 May 2016 23:06:51 +0200 Subject: Clouds :cloud: :cloud: :cloud: --- cloud.lua | 21 ++++++++++++--------- main.lua | 4 ++++ world.lua | 27 ++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/cloud.lua b/cloud.lua index d745ba7..c039c95 100644 --- a/cloud.lua +++ b/cloud.lua @@ -5,19 +5,20 @@ -- Metatable of `Cloud` Cloud = { - x = 0, - y = 0, - t = 1, -- Type of the cloud (quad number) + x = 0, -- position horizontal + y = 0, -- position vertical + t = 1, -- type (sprite number) + v = 6, -- velocity sprite = love.graphics.newImage("assets/clouds.png"), quads = { - [1] = love.graphics.newQuad( 1,159, 158,47, 480,49), - [2] = love.graphics.newQuad(161,319, 158,47, 480,49), - [3] = love.graphics.newQuad(321,479, 158,47, 480,49) + [1] = love.graphics.newQuad( 1, 1, 158,47, 480,49), + [2] = love.graphics.newQuad(161, 1, 158,47, 480,49), + [3] = love.graphics.newQuad(321, 1, 158,47, 480,49) } } -- Constructor of `Cloud` -function Cloud:new(x, y, t) +function Cloud:new(x, y, t, v) -- Meta local o = {} setmetatable(o, self) @@ -26,12 +27,13 @@ function Cloud:new(x, y, t) o.x = x or self.x o.y = y or self.y o.t = t or self.t + o.v = v or self.v return o end -- Update of `Cloud`, returns x for world to delete cloud after reaching right corner function Cloud:update(dt) - self.x = self.x + 5*dt + self.x = self.x + self.v*dt return self.x end @@ -40,8 +42,9 @@ function Cloud:draw(offset_x, offset_y, scale) -- defaults local offset_x = offset_x or 0 local offset_y = offset_y or 0 + local scale = scale or 1 local debug = debug or false -- draw love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.sprite, self.quads[t], (self.x+offset_x)*scale, (self.y+offset_y)*scale, 0, scale, scale) + love.graphics.draw(self.sprite, self.quads[self.t], (self.x+offset_x)*scale, (self.y+offset_y)*scale, 0, scale, scale) end \ No newline at end of file diff --git a/main.lua b/main.lua index c450734..e393d0c 100644 --- a/main.lua +++ b/main.lua @@ -25,6 +25,10 @@ function love.load () w:createNaut(290/2-10, 180/2 - 80, "assets/leon.png") w:createNaut(290/2+10, 180/2 - 80, "assets/lonestar.png") + w:randomizeCloud() + w:randomizeCloud() + w:randomizeCloud() + -- Temporary settings for second player w.Nauts[2].name = "Player2" w.Nauts[2].key_left = "a" diff --git a/world.lua b/world.lua index 098e268..c692ceb 100644 --- a/world.lua +++ b/world.lua @@ -29,7 +29,9 @@ function World:new() local p = {} o.Platforms = {} local c = {} - o.Clouds = c + o.Clouds = c + -- Random init + math.randomseed(os.time()) -- Create camera o.camera = Camera:new() return o @@ -46,8 +48,18 @@ function World:createNaut(x, y, sprite) end -- Add new cloud to the world -function World:createCloud(x, y, t) - table.insert(self.Clouds, Cloud:new(x, y, t)) +function World:createCloud(x, y, t, v) + table.insert(self.Clouds, Cloud:new(x, y, t, v)) +end + +-- Randomize Cloud creation +function World:randomizeCloud() + local x,y,t,v + x = -200+math.random(-20,20) + y = math.random(0, 160) + t = math.random(1,3) + v = math.random(4,14) + self:createCloud(x, y, t, v) end -- Update ZU WARUDO @@ -60,6 +72,10 @@ function World:update(dt) for _,naut in pairs(self.Nauts) do naut:update(dt) end + -- Clouds + for _,cloud in pairs(self.Clouds) do + cloud:update(dt) + end end -- Keypressed @@ -88,6 +104,11 @@ function World:draw() local offset_x, offset_y = self.camera:getOffsets() local scale = self.camera.scale + -- Draw clouds + for _,cloud in pairs(self.Clouds) do + local foo = cloud:draw(offset_x, offset_y, scale) + end + -- Draw ground for _,platform in pairs(self.Platforms) do platform:draw(offset_x, offset_y, scale, debug) -- cgit v1.1