From dc6fd557ed955fc5f177608c186768e29c7778c6 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 18:32:34 +0200 Subject: Cloud clean-up, moving methods from World to CloudGenerator --- not/Cloud.lua | 84 ++++++++++++++++++++----------------------------- not/CloudGenerator.lua | 54 ++++++++++++++++++++++++++++++-- not/World.lua | 85 ++++++++++++++++++-------------------------------- 3 files changed, 117 insertions(+), 106 deletions(-) (limited to 'not') diff --git a/not/Cloud.lua b/not/Cloud.lua index 25169c0..4851042 100644 --- a/not/Cloud.lua +++ b/not/Cloud.lua @@ -1,61 +1,45 @@ -require "not.Decoration" - --- `Cloud` --- That white thing moving in the background. --- TODO: extends variables names to be readable. -Cloud = Decoration:extends() -Cloud.t = 1 -- type (sprite number) -Cloud.v = 13 -- velocity - --- TODO: allow maps to use other quads and sprites for clouds --- TODO: you know this isn't right, don't you? -local animations = { - default = { - [1] = love.graphics.newQuad( 1, 1, 158,47, 478,49), - frames = 1, - repeated = true - }, - default2 = { - [1] = love.graphics.newQuad(160, 1, 158,47, 478,49), - frames = 1, - repeated = true - }, - default3 = { - [1] = love.graphics.newQuad(319, 1, 158,47, 478,49), - frames = 1, - repeated = true - } -} - --- Constructor of `Cloud`. -function Cloud:new (x, y, t, v, world) - if self:getImage() == nil then - self:setImage(Sprite.newImage("assets/clouds.png")) - end - Cloud.__super.new(self, x, y, world, nil) - self:setAnimationsList(animations) - self:setVelocity(v) - self:setType(t) +-- Moving decorations with limited lifespan. +Cloud = require "not.Decoration":extends() + +function Cloud:new (x, y, world, imagePath) + Cloud.__super.new(self, x, y, world, imagePath) + self.velocity_x = 0 + self.velocity_y = 0 + self.boundary_x = 0 + self.boundary_y = 0 end --- Setters for cloud type and velocity. -function Cloud:setType (type) - local animation = "default" - if type > 1 then - animation = animation .. type - end - self:setAnimation(animation) - self.t = type +function Cloud:setVelocity (x, y) + self.velocity_x = x + self.velocity_y = y +end + +function Cloud:setBoundary (x, y) + self.boundary_x = x + self.boundary_y = y end -function Cloud:setVelocity (velocity) - self.v = velocity + +function Cloud:setStyle (style) + self:setAnimation(style) +end + +function Cloud:getStyle () + return self:getAnimation() +end + +function Cloud:testPosition () + if self.x > self.boundary_x or self.y > self.boundary_y then + return true + end end --- Update of `Cloud`, returns x for world to delete cloud after reaching right corner. +-- Cloud will get deleted if this function returns true. function Cloud:update (dt) Cloud.__super.update(self, dt) - self.x = self.x + self.v*dt - return self.x + self.x = self.x + self.velocity_x * dt + self.y = self.y + self.velocity_y * dt + return self:testPosition() end return Cloud diff --git a/not/CloudGenerator.lua b/not/CloudGenerator.lua index 0620cdd..a26cc77 100644 --- a/not/CloudGenerator.lua +++ b/not/CloudGenerator.lua @@ -1,12 +1,62 @@ --- Generates clouds over time with randomized positions and styles. +-- Also used as factory for Clouds. CloudGenerator = require "not.Object":extends() -function CloudGenerator:new (world, styles) +require "not.Cloud" + +-- TODO: Allow map config to modify cloud styles. +local animations = { + default = { + [1] = love.graphics.newQuad( 1, 1, 158,47, 478,49), + frames = 1, + repeated = true + }, + default2 = { + [1] = love.graphics.newQuad(160, 1, 158,47, 478,49), + frames = 1, + repeated = true + }, + default3 = { + [1] = love.graphics.newQuad(319, 1, 158,47, 478,49), + frames = 1, + repeated = true + } +} + +function CloudGenerator:new (world) self.world = world + self.atlas = "assets/clouds.png" + self.quads = animations +end + +function CloudGenerator:createCloud (x, y) + local cloud = Cloud(x, y, self.world, self.atlas) + cloud:setAnimationsList(self.quads) + cloud:setVelocity(13, 0) + cloud:setBoundary(340, 320) + return cloud +end + +function CloudGenerator:randomize (outside) + if outside == nil then + outside = true + else + outside = outside + end + local x,y,t,v + local m = self.map + if outside then + x = m.center.x-m.width*1.2+love.math.random(-50,20) + else + x = love.math.random(m.center.x-m.width/2,m.center.x+m.width/2) + end + y = love.math.random(m.center.y-m.height/2, m.center.y+m.height/2) + t = love.math.random(1,3) + v = love.math.random(8,18) end function CloudGenerator:update (dt) - + local count = self.world:getCloudsCount() end return CloudGenerator diff --git a/not/World.lua b/not/World.lua index c4606dd..b97a5b5 100644 --- a/not/World.lua +++ b/not/World.lua @@ -5,10 +5,10 @@ World = require "not.Scene":extends() require "not.Platform" require "not.Player" -require "not.Cloud" require "not.Effect" require "not.Decoration" require "not.Ray" +require "not.CloudGenerator" --- ZA WARUDO! -- TODO: Missing documentation on most of World's methods. @@ -16,7 +16,7 @@ function World:new (map, nauts) love.physics.setMeter(64) self.world = love.physics.newWorld(0, 9.81*64, true) self.world:setCallbacks(self.beginContact, self.endContact) - -- Tables for entities. + -- TODO: Move all entities into single table. self.lastNaut = false self.Nauts = {} @@ -28,6 +28,7 @@ function World:new (map, nauts) self.map = map self:buildMap() + self:initClouds() self:spawnNauts(nauts) self.camera = Camera:new(self) @@ -65,11 +66,12 @@ function World:buildMap () self:createDecoration(x, y, op.background) -- TODO: Decoration does not allow Image instead of filePath! end end - +end + +-- TODO: Spawn some clouds after cloudGenerator has been initialized. +function World:initClouds () if self.map.clouds then - for i=1,6 do - self:randomizeCloud(false) - end + self.cloudGenerator = CloudGenerator(self) end end @@ -103,11 +105,6 @@ function World:createDecoration (x, y, sprite) table.insert(self.Decorations, Decoration(x, y, self, sprite)) end --- TODO: Extend names of variables related to Clouds to provide better readability. See also: `not/Cloud`. -function World:createCloud (x, y, t, v) - table.insert(self.Clouds, Cloud(x, y, t, v, self)) -end - function World:createEffect (name, x, y) table.insert(self.Effects, Effect(name, x, y, self)) end @@ -116,24 +113,15 @@ function World:createRay (naut) table.insert(self.Rays, Ray(naut, self)) end --- Randomize Cloud creation -function World:randomizeCloud (outside) - if outside == nil then - outside = true - else - outside = outside - end - local x,y,t,v - local m = self.map - if outside then - x = m.center.x-m.width*1.2+love.math.random(-50,20) - else - x = love.math.random(m.center.x-m.width/2,m.center.x+m.width/2) - end - y = love.math.random(m.center.y-m.height/2, m.center.y+m.height/2) - t = love.math.random(1,3) - v = love.math.random(8,18) - self:createCloud(x, y, t, v) +-- TODO: Sprites' in general don't take actual Image in constructor. That is not only case of Decoration. +-- TODO: Once entities are stored inside single table create single `insertEntity` method for World. +function World:insertCloud (cloud) + table.insert(self.Clouds, cloud) + return cloud +end + +function World:getCloudsCount () + return #self.Clouds end -- get Nauts functions @@ -187,7 +175,11 @@ end function World:update (dt) self.world:update(dt) self.camera:update(dt) - -- Engine world: Nauts, Grounds (kek) and Decorations - all Animateds (top kek) + + if self.cloudGenerator then + self.cloudGenerator:update(dt) + end + for _,naut in pairs(self.Nauts) do naut:update(dt) end @@ -197,38 +189,23 @@ function World:update (dt) for _,decoration in pairs(self.Decorations) do decoration:update(dt) end - -- Clouds - -- TODO: possibly create new class for Clouds generation. Do it along with Cloud cleaning. - if self.map.clouds then - -- generator - local n = table.getn(self.Clouds) - self.clouds_delay = self.clouds_delay - dt - if self.clouds_delay < 0 and - n < 18 - then - self:randomizeCloud() - self.clouds_delay = self.clouds_delay + World.clouds_delay -- World.clouds_delay is initial - end - -- movement - for _,cloud in pairs(self.Clouds) do - if cloud:update(dt) > 340 then - table.remove(self.Clouds, _) - end + for key,effect in pairs(self.Effects) do + if effect:update(dt) then + table.remove(self.Effects, key) end end - -- Effects - for _,effect in pairs(self.Effects) do - if effect:update(dt) then - table.remove(self.Effects, _) + for key,cloud in pairs(self.Clouds) do + if cloud:update(dt) then + table.remove(self.Clouds, key) end end - -- Rays - for _,ray in pairs(self.Rays) do + for key,ray in pairs(self.Rays) do if ray:update(dt) then - table.remove(self.Rays, _) + table.remove(self.Rays, key) end end end + -- Draw function World:draw () -- Camera stuff -- cgit v1.1