From afac3fdc7ce03e03304eec448149f346099cab94 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 15:30:27 +0200 Subject: Added Object.lua to lib --- lib/Object.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 lib/Object.lua diff --git a/lib/Object.lua b/lib/Object.lua new file mode 100644 index 0000000..b241bb2 --- /dev/null +++ b/lib/Object.lua @@ -0,0 +1,77 @@ +--- You may not believe me but we are not returning this one. +-- This table is used as metatable for classes e.g. for `Object`. +local Class = {} + +--- Metamethod for classes. +-- Creates new instance of class calling `new()` method (constructor) with all parameters passed. +Class.__call = function (self, ...) + local o = setmetatable({}, self) + self.new(o, ...) + return o +end + +--- Metamethod for classes. +-- First checks if `__index` (instance prototype) have field we are looking for. Then it tries to find it in super class. +Class.__index = function (self, key) + if rawget(self, "__index") ~= nil then + if rawget(self, "__index")[key] ~= nil then + return rawget(self, "__index")[key] + end + end + if rawget(self, "__super") ~= nil then + if rawget(self, "__super")[key] ~= nil then + return rawget(self, "__super")[key] + end + end + return nil +end +--- Metamethod for classes. +-- Redirects creating new properties to class'es `__index` which is used as a prototype for instances of class. +-- Only `new` method and metamethods are allowed to be written to class'es table directly. +Class.__newindex = function(self, key, value) + if key == "new" or key:sub(1, 2) == "__" then + rawset(self, key, value) + else + self.__index[key] = value + end +end + +--- Creates new class from parent class. +-- New class will call parent's constructor unless new constructor will be defined. +-- @param parent super class of new class +local extends = function (parent) + local self = setmetatable({}, Class) + rawset(self, "__index", {}) + if parent then + setmetatable(self.__index, {__index = parent.__index}) + end + rawset(self, "__super", parent) + return self +end + +--- Almost empty class. +-- Used to create new classes via `extend()` method: +-- `Child = Object:extend()` +-- Contains `is()` and `new()` methods. Later one isn't available from inside of instances. +local Object = extends(nil) +rawset(Object, "extends", extends) +Object.new = function (self) end + +--- Checks if class or instance of class is a child of class passed through parameter. +-- @param class table we want to test against (preferably class table) +-- @return boolean which is false if tested sample is not child of passed class +function Object:is (class) + if not class then return false end + if self == class or getmetatable(self) == class then + return true + end + if self.__super then + return self.__super:is(class) + end + if getmetatable(self).__super then + return getmetatable(self).__super:is(class) + end + return false +end + +return Object -- cgit v1.1 From 62b67be7882dffebd6de0c8241d253d806a6905c Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 19:14:27 +0200 Subject: Halfway through with moving to new OOP module --- not/Entity.lua | 16 +++++++++ not/Hero.lua | 92 +++++++++++++++++++++++----------------------------- not/Object.lua | 3 ++ not/PhysicalBody.lua | 33 +++++++------------ not/Player.lua | 34 ++++++------------- not/Sprite.lua | 72 ++++++++++++++++------------------------ not/World.lua | 12 +++---- 7 files changed, 115 insertions(+), 147 deletions(-) create mode 100644 not/Entity.lua create mode 100644 not/Object.lua diff --git a/not/Entity.lua b/not/Entity.lua new file mode 100644 index 0000000..6ba1606 --- /dev/null +++ b/not/Entity.lua @@ -0,0 +1,16 @@ +require "not.Sprite" + +--- `Entity` +-- Basic, visible object to be used within World instance. Can be anything. Represented as `Sprite`. +-- We still need to keep old, global way of using modules but there are `returns` on the end at least. +Entity = Sprite:extends() + +Entity.world =--[[not.World]]nil + +-- Simple constructor for `Entity`. +function Entity:new (world, imagePath) + Entity.__super.new(self, imagePath) + self.world = world +end + +return Entity diff --git a/not/Hero.lua b/not/Hero.lua index feb61da..1f7cd8b 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -1,60 +1,48 @@ +require "not.PhysicalBody" + --- `Hero` -- Hero (often referred to as: "naut") entity that exists in a game world. -- Collision category: [2] -Hero = { - -- General and physics - name = "empty", - angle = 0, - facing = 1, - max_velocity = 105, - world = --[[not.World]]nil, - group = nil, - -- Combat - combo = 0, - lives = 3, - spawntimer = 2, - isAlive = true, - punchCooldown = 0.25, - punchdir = 0, -- a really bad thing - -- Movement - inAir = true, - salto = false, - isJumping = false, - isWalking = false, - jumpTimer = 0.16, - jumpCounter = 2, - -- Statics - portrait_sprite = nil, - portrait_frame = nil, - portrait_sheet = getNautsIconsList(), - portrait_box = love.graphics.newQuad( 0, 15, 32,32, 80,130), - sfx = require "config.sounds", -} +Hero = PhysicalBody:extends() --- `Hero` is a child of `PhysicalBody`. -require "not.PhysicalBody" -Hero.__index = Hero -setmetatable(Hero, PhysicalBody) +Hero.name = "empty" +Hero.angle = 0 +Hero.facing = 1 +Hero.max_velocity = 105 +Hero.group = nil +-- Combat +Hero.combo = 0 +Hero.lives = 3 +Hero.spawntimer = 2 +Hero.isAlive = true +Hero.punchCooldown = 0.25 +Hero.punchdir = 0 -- a really bad thing +-- Movement +Hero.inAir = true +Hero.salto = false +Hero.isJumping = false +Hero.isWalking = false +Hero.jumpTimer = 0.16 +Hero.jumpCounter = 2 +-- Statics +Hero.portrait_sprite = nil +Hero.portrait_frame = nil +Hero.portrait_sheet = getNautsIconsList() +Hero.portrait_box = love.graphics.newQuad(0, 15, 32,32, 80,130) +Hero.sfx = require "config.sounds" -- Constructor of `Hero`. -function Hero:new (game, world, x, y, name) - local o = setmetatable({}, self) - o:init(name, game, x, y) - -- Load portraits statically. - if self.portrait_sprite == nil then - self.portrait_sprite = love.graphics.newImage("assets/portraits.png") - self.portrait_frame = love.graphics.newImage("assets/menu.png") +function Hero:new (name, x, y, world) + -- TODO: Statics moved temporarily here. Should be moved to e.g. `load()`. + if Hero.portrait_sprite == nil then + Hero.portrait_sprite = love.graphics.newImage("assets/portraits.png") + Hero.portrait_frame = love.graphics.newImage("assets/menu.png") end - return o -end - --- Initializer of `Hero`. -function Hero:init (name, world, x, y) -- Find imagePath based on hero name. - local fileName = name or Hero.name -- INITIAL from metatable + local fileName = name or Hero.name -- INITIAL from prototype local imagePath = string.format("assets/nauts/%s.png", fileName) -- `PhysicalBody` initialization. - PhysicalBody.init(self, world, x, y, imagePath) + Hero.__super.new(self, x, y, world, imagePath) self:setBodyType("dynamic") self:setBodyFixedRotation(true) self.group = -1-#world.Nauts @@ -74,7 +62,7 @@ end -- Update callback of `Hero` function Hero:update (dt) - PhysicalBody.update(self, dt) + Hero.__super.update(self, dt) if self.body:isDestroyed() then return end -- Salto @@ -163,7 +151,7 @@ end -- Draw of `Hero` function Hero:draw (offset_x, offset_y, scale, debug) if not self.isAlive then return end - PhysicalBody.draw(self, offset_x, offset_y, scale, debug) + Hero.__super.draw(self, offset_x, offset_y, scale, debug) end -- Draw HUD of `Hero` @@ -209,7 +197,7 @@ end -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. -- TODO: Magic numbers present in `not.Hero.punch`. function Hero:punch (direction) - self.punchCooldown = Hero.punchCooldown -- INITIAL from metatable + self.punchCooldown = Hero.punchCooldown -- INITIAL from prototype -- Choose shape based on punch direction. local shape if direction == "left" then shape = {-2,-6, -20,-6, -20,6, -2,6} end @@ -256,10 +244,10 @@ end -- DIE function Hero:die () self:playSound(1) - self.combo = Hero.combo -- INITIAL from metatable + self.combo = Hero.combo -- INITIAL from prototype self.lives = self.lives - 1 self.isAlive = false - self.spawntimer = Hero.spawntimer -- INITIAL from metatable + self.spawntimer = Hero.spawntimer -- INITIAL from prototype self:setBodyActive(false) self.world:onNautKilled(self) end diff --git a/not/Object.lua b/not/Object.lua new file mode 100644 index 0000000..fbc41e5 --- /dev/null +++ b/not/Object.lua @@ -0,0 +1,3 @@ +-- Wrapping library to game's hierarchy in a shameless way. +Object = require "lib.Object" +return Object diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index e9625fa..fd92f89 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -1,25 +1,15 @@ +require "not.Entity" + --- `PhysicalBody` -- Abstract class for drawable entity existing in `not.World`. -PhysicalBody = { - body =--[[love.physics.newBody]]nil, -} - --- `PhysicalBody` is a child of `Sprite`. -require "not.Sprite" -PhysicalBody.__index = PhysicalBody -setmetatable(PhysicalBody, Sprite) +PhysicalBody = Entity:extends() ---[[ Constructor of `PhysicalBody`. -function PhysicalBody:new (world, x, y, imagePath) - local o = setmetatable({}, self) - o:init(world, x, y, imagePath) - return o -end -]] +PhysicalBody.body =--[[love.physics.newBody]]nil --- Initializer of `PhysicalBody`. -function PhysicalBody:init (world, x, y, imagePath) - Sprite.init(self, imagePath) +-- Constructor of `PhysicalBody`. +-- `world` and `imagePath` are passed to parent's constructor (`Entity`). +function PhysicalBody:new (x, y, world, imagePath) + PhysicalBody.__super.new(self, world, imagePath) self.body = love.physics.newBody(world.world, x, y) end @@ -68,12 +58,12 @@ end -- Update of `PhysicalBody`. function PhysicalBody:update (dt) - Sprite.update(self, dt) + PhysicalBody.__super.update(self, dt) end -- Draw of `PhysicalBody`. function PhysicalBody:draw (offset_x, offset_y, scale, debug) - Sprite.draw(self, offset_x, offset_y, scale) + PhysicalBody.__super.draw(self, offset_x, offset_y, scale) if debug then for _,fixture in pairs(self.body:getFixtureList()) do local category = fixture:getCategory() @@ -86,8 +76,9 @@ function PhysicalBody:draw (offset_x, offset_y, scale, debug) if category == 3 then love.graphics.setColor(137, 0, 255, 40) end - -- TODO: `world` is not a member of `PhysicalBody` or its instance normally. love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(fixture:getShape():getPoints()))) end end end + +return PhysicalBody diff --git a/not/Player.lua b/not/Player.lua index 2a4b2e6..5d108ce 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -1,31 +1,15 @@ +require "not.Hero" + --- `Player` -- Special `not.Hero` controllable by a player. -Player = { - -- TODO: move functions and properties related to controls from `not.Hero`. - controllerSet = --[[Controller.sets.*]]nil, -} +-- TODO: move functions and properties related to controls from `not.Hero`. +Player = Hero:extends() --- `Player` is a child of `Hero`. -require "not.Hero" -Player.__index = Player -setmetatable(Player, Hero) +Player.controllerSet =--[[Controller.sets.*]]nil -- Constructor of `Player`. -function Player:new (name, game, x, y) - local o = setmetatable({}, self) - o:init(name, game, x, y) - -- Load portraits statically to `not.Hero`. - -- TODO: this is heresy, put it into `load` method or something similar. - if Hero.portrait_sprite == nil then - Hero.portrait_sprite = love.graphics.newImage("assets/portraits.png") - Hero.portrait_frame = love.graphics.newImage("assets/menu.png") - end - return o -end - --- Initializer of `Player`. -function Player:init (...) - Hero.init(self, ...) +function Player:new (name, x, y, world) + Player.__super.new(self, name, x, y, world) end -- Controller set manipulation. @@ -43,7 +27,7 @@ end -- Update of `Player`. function Player:update (dt) - Hero.update(self, dt) -- TODO: It would be probably a good idea to add return to update functions to terminate if something goes badly in parent's update. + Player.__super.update(self, dt) -- TODO: It would be probably a good idea to add return to update functions to terminate if something goes badly in parent's update. if self.body:isDestroyed() then return end local x, y = self:getLinearVelocity() -- Jumping. @@ -157,3 +141,5 @@ function Player:controlreleased (set, action, key) end end end + +return Player diff --git a/not/Sprite.lua b/not/Sprite.lua index 25d85f1..3951e6e 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -1,34 +1,27 @@ +require "not.Object" + --- `Sprite` -- Abstract class for drawable animated entities. -Sprite = { - animations =--[[table with animations]]nil, - current =--[[animations.default]]nil, - image =--[[love.graphics.newImage]]nil, - frame = 1, - delay = .1, -} -Sprite.__index = Sprite - ---[[ Constructor of `Sprite`. +Sprite = Object:extends() + +Sprite.animations =--[[table with animations]]nil +Sprite.current =--[[animations.default]]nil +Sprite.image =--[[love.graphics.newImage]]nil +Sprite.frame = 1 +Sprite.delay = .1 + +-- Constructor of `Sprite`. function Sprite:new (imagePath) - local o = setmetatable({}, self) - o:init(imagePath) - return o + if type(imagePath) == "string" then + self:setImage(Sprite.newImage(imagePath)) + end end -]] -- Cleans up reference to image on deletion. function Sprite:delete () self.image = nil end --- Initializes new Sprite instance. -function Sprite:init (imagePath) - if type(imagePath) == "string" then - self:setImage(Sprite.newImage(imagePath)) - end -end - -- Creates new Image object from path. Key-colours two shades of green. Static. function Sprite.newImage (path) local imagedata = love.image.newImageData(path) @@ -64,7 +57,7 @@ end -- Sets current animation by table key. function Sprite:setAnimation (animation) self.frame = 1 - self.delay = Sprite.delay -- INITIAL from metatable + self.delay = Sprite.delay -- INITIAL from prototype self.current = self.animations[animation] end -- Returns current animation table. @@ -79,26 +72,15 @@ function Sprite:getQuad () end end --- TODO: Following five methods are stupid, do something about them! --- Sprite can't be moved by itself. Positioning should be handled by children's methods. -function Sprite:getPosition () - return 0,0 -end --- Sprite can't be rotated by itself. Rotation should be handled by children's methods. -function Sprite:getAngle () - return 0 -end --- Sprite can't be mirrored by itself. Mirroring should be handled by children's methods. -function Sprite:getHorizontalMirror () - return 1 -end -function Sprite:getVerticalMirror () - return 1 -end --- Sprite can't be offset by itself. Offsetting should be handled by children's methods. -function Sprite:getOffset () - return 0,0 -end +-- Sprite's position. Can be overriden to add functionality. +function Sprite:getPosition () return 0,0 end +-- Sprite's angle. Can be overriden to add functionality. +function Sprite:getAngle () return 0 end +-- Sprite's horizontal and vertical mirrors. Can be overriden to add functionality. +function Sprite:getHorizontalMirror () return 1 end +function Sprite:getVerticalMirror () return 1 end +-- Sprite's drawing offset from position. Can be overriden to add functionality. +function Sprite:getOffset () return 0,0 end -- Drawing self to LOVE2D buffer. -- If there is no Quad, it will draw entire image. It won't draw anything if there is no image. @@ -136,7 +118,7 @@ function Sprite:update (dt) if self.animations and self.current then self.delay = self.delay - dt if self.delay < 0 then - self.delay = self.delay + Sprite.delay -- INITIAL from metatable + self.delay = self.delay + Sprite.delay -- INITIAL from prototype self:goToNextFrame() end end @@ -149,4 +131,6 @@ function Sprite:goToNextFrame () else self:setAnimation("default") end -end \ No newline at end of file +end + +return Sprite diff --git a/not/World.lua b/not/World.lua index bbceec4..b4ad4fa 100644 --- a/not/World.lua +++ b/not/World.lua @@ -117,13 +117,13 @@ end -- Add new platform to the world -- TODO: it would be nice if function parameters would be same as `not.Platform.new`. function World:createPlatform (x, y, polygon, sprite, animations) - table.insert(self.Platforms, Platform:new(animations, polygon, self, x, y, sprite)) + table.insert(self.Platforms, Platform(animations, polygon, self, x, y, sprite)) end -- Add new naut to the world -- TODO: separate two methods for `not.Hero` and `not.Player`. function World:createNaut (x, y, name) - local naut = Player:new(name, self, x, y) + local naut = Player(name, x, y, self) table.insert(self.Nauts, naut) return naut end @@ -131,14 +131,14 @@ end -- Add new decoration to the world -- TODO: `not.World.create*` functions often have different naming for parameters. It is not ground-breaking but it makes reading code harder for no good reason. function World:createDecoration (x, y, sprite) - table.insert(self.Decorations, Decoration:new(x, y, sprite)) + table.insert(self.Decorations, Decoration(x, y, sprite)) end -- Add new cloud to the world -- TODO: extend variables names to provide better readability. -- TODO: follow new parameters in `not.Cloud.new` based on `not.Cloud.init`. function World:createCloud (x, y, t, v) - table.insert(self.Clouds, Cloud:new(x, y, t, v)) + table.insert(self.Clouds, Cloud(x, y, t, v)) end -- Randomize Cloud creation @@ -165,12 +165,12 @@ end -- TODO: follow new parameters in `not.Effect.new` based on `not.Effect.init`. -- TODO: along with `createRay` move this nearer reast of `create*` methods for readability. function World:createEffect (name, x, y) - table.insert(self.Effects, Effect:new(name, x, y)) + table.insert(self.Effects, Effect(name, x, y)) end -- Add a ray function World:createRay (naut) - table.insert(self.Rays, Ray:new(naut, self)) + table.insert(self.Rays, Ray(naut, self)) end -- get Nauts functions -- cgit v1.1 From 81e72006a4ca0578f0bbc31d0788a6e0223fdb63 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 19:53:20 +0200 Subject: Random changed to use love lib --- not/World.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/not/World.lua b/not/World.lua index b4ad4fa..38639b7 100644 --- a/not/World.lua +++ b/not/World.lua @@ -53,8 +53,6 @@ function World:init (map, nauts) self.Effects = {} self.Decorations = {} self.Rays = {} - -- Random init; TODO: use LOVE2D's random. - math.randomseed(os.time()) -- Map and misc. local map = map or "default" self:loadMap(map) @@ -110,7 +108,7 @@ end -- Get respawn location function World:getSpawnPosition () - local n = math.random(1, #self.map.respawns) + local n = love.math.random(1, #self.map.respawns) return self.map.respawns[n].x, self.map.respawns[n].y end @@ -151,13 +149,13 @@ function World:randomizeCloud (outside) local x,y,t,v local m = self.map if outside then - x = m.center_x-m.width*1.2+math.random(-50,20) + x = m.center_x-m.width*1.2+love.math.random(-50,20) else - x = math.random(m.center_x-m.width/2,m.center_x+m.width/2) + x = love.math.random(m.center_x-m.width/2,m.center_x+m.width/2) end - y = math.random(m.center_y-m.height/2, m.center_y+m.height/2) - t = math.random(1,3) - v = math.random(8,18) + 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) end -- cgit v1.1 From b14d4608dc921f882067c43c71fcf04db7b2f794 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 22:44:00 +0200 Subject: Rest of entities moved to new oop module; tested --- not/Cloud.lua | 30 ++++++++++-------------------- not/Decoration.lua | 29 ++++++++++------------------- not/Effect.lua | 32 ++++++++++++-------------------- not/Platform.lua | 29 ++++++++--------------------- not/World.lua | 8 ++++---- 5 files changed, 44 insertions(+), 84 deletions(-) diff --git a/not/Cloud.lua b/not/Cloud.lua index 3bc5377..25169c0 100644 --- a/not/Cloud.lua +++ b/not/Cloud.lua @@ -1,10 +1,11 @@ +require "not.Decoration" + --- `Cloud` -- That white thing moving in the background. -- TODO: extends variables names to be readable. -Cloud = { - t = 1, -- type (sprite number) - v = 13 -- velocity -} +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? @@ -26,25 +27,12 @@ local animations = { } } --- `Cloud` is a child of `Decoration`. -require "not.Decoration" -Cloud.__index = Cloud -setmetatable(Cloud, Decoration) - -- Constructor of `Cloud`. -function Cloud:new (x, y, t, v) - local o = setmetatable({}, self) - o:init(x, y, t, v) - -- Load spritesheet statically. +function Cloud:new (x, y, t, v, world) if self:getImage() == nil then self:setImage(Sprite.newImage("assets/clouds.png")) end - return o -end - --- Initializer of `Cloud`. -function Cloud:init (x, y, t, v) - Decoration.init(self, x, y, nil) + Cloud.__super.new(self, x, y, world, nil) self:setAnimationsList(animations) self:setVelocity(v) self:setType(t) @@ -65,7 +53,9 @@ end -- Update of `Cloud`, returns x for world to delete cloud after reaching right corner. function Cloud:update (dt) - Decoration.update(self, dt) + Cloud.__super.update(self, dt) self.x = self.x + self.v*dt return self.x end + +return Cloud diff --git a/not/Decoration.lua b/not/Decoration.lua index 9dc2bdd..97524f5 100644 --- a/not/Decoration.lua +++ b/not/Decoration.lua @@ -1,26 +1,15 @@ +require "not.Entity" + --- `Decoration` -- Positioned sprite used to decorate maps with additional graphics. -Decoration = { - world = --[[not.World]]nil, - x = 0, - y = 0 -} +Decoration = Entity:extends() --- `Decoration` is a child of `Sprite`. -require "not.Sprite" -Decoration.__index = Decoration -setmetatable(Decoration, Sprite) +Decoration.x = 0 +Decoration.y = 0 -- Constructor of `Decoration`. -function Decoration:new (x, y, imagePath) - local o = setmetatable({}, self) - o:init(x, y, imagePath) - return o -end - --- Initializer of `Decoration`. -function Decoration:init (x, y, imagePath) - Sprite.init(self, imagePath) +function Decoration:new (x, y, world, imagePath) + Decoration.__super.new(self, world, imagePath) self:setPosition(x, y) end @@ -30,4 +19,6 @@ function Decoration:getPosition () end function Decoration:setPosition (x, y) self.x, self.y = x, y -end \ No newline at end of file +end + +return Decoration diff --git a/not/Effect.lua b/not/Effect.lua index dd7570a..82d6819 100644 --- a/not/Effect.lua +++ b/not/Effect.lua @@ -1,29 +1,19 @@ +require "not.Decoration" + --- `Effect` -- Short animation with graphics that plays in various situation. -- TODO: animation is currently slower than it used to be, check if it is ok; if not then make it possible to change it to 0.06 delay. -Effect = { - finished = false, -} +Effect = Decoration:extends() --- `Effect` is a child of `Decoration`. -require "not.Decoration" -Effect.__index = Effect -setmetatable(Effect, Decoration) +Effect.finished = false -- Constructor of `Effect`. -function Effect:new (name, x, y) - local o = setmetatable({}, self) - o:init(name, x, y) - -- Load spritesheet statically. - if self:getImage() == nil then - self:setImage(Sprite.newImage("assets/effects.png")) +function Effect:new (name, x, y, world) + -- TODO: Load spritesheet statically. Put it to load or somewhere else within non-existent resource manager. + if Effect:getImage() == nil then + Effect:setImage(Sprite.newImage("assets/effects.png")) end - return o -end - --- Initializer of `Effect`. -function Effect:init (name, x, y) - Decoration.init(self, x, y, nil) + Effect.__super.new(self, x, y, world, nil) self:setAnimationsList(require("config.animations.effects")) self:setAnimation(name) end @@ -31,7 +21,7 @@ end -- Update of `Effect`. -- Returns true if animation is finished and effect is ready to be deleted. function Effect:update (dt) - Decoration.update(self, dt) + Effect.__super.update(self, dt) return self.finished end @@ -44,3 +34,5 @@ function Effect:goToNextFrame () self.finished = true end end + +return Effect diff --git a/not/Platform.lua b/not/Platform.lua index 3748c47..a4b3a59 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -1,29 +1,14 @@ +require "not.PhysicalBody" + --- `Platform` -- Static platform physical object with a sprite. `Players` can walk on it. -- Collision category: [1] --- TODO: reformat code to follow new code patterns --- TODO: comment uncovered code parts -Platform = { - world = --[[not.World]]nil, -} - --- `Platform` is a child of `PhysicalBody`. -require "not.PhysicalBody" -Platform.__index = Platform -setmetatable(Platform, PhysicalBody) +Platform = PhysicalBody:extends() -- Constructor of `Platform` -function Platform:new (animations, shape, game, x, y, sprite) - local o = setmetatable({}, self) - o:init(animations, shape, game, x, y, sprite) - return o -end - --- Initializer of `Platform`. -function Platform:init (animations, shape, world, x, y, imagePath) - PhysicalBody.init(self, world, x, y, imagePath) +function Platform:new (animations, shape, x, y, world, imagePath) + Platform.__super.new(self, x, y, world, imagePath) self:setAnimationsList(animations) - self.world = world -- Create table of shapes if single shape is passed. if type(shape[1]) == "number" then shape = {shape} @@ -34,4 +19,6 @@ function Platform:init (animations, shape, world, x, y, imagePath) fixture:setCategory(1) fixture:setFriction(0.2) end -end \ No newline at end of file +end + +return Platform diff --git a/not/World.lua b/not/World.lua index 38639b7..88be97a 100644 --- a/not/World.lua +++ b/not/World.lua @@ -115,7 +115,7 @@ end -- Add new platform to the world -- TODO: it would be nice if function parameters would be same as `not.Platform.new`. function World:createPlatform (x, y, polygon, sprite, animations) - table.insert(self.Platforms, Platform(animations, polygon, self, x, y, sprite)) + table.insert(self.Platforms, Platform(animations, polygon, x, y, self, sprite)) end -- Add new naut to the world @@ -129,14 +129,14 @@ end -- Add new decoration to the world -- TODO: `not.World.create*` functions often have different naming for parameters. It is not ground-breaking but it makes reading code harder for no good reason. function World:createDecoration (x, y, sprite) - table.insert(self.Decorations, Decoration(x, y, sprite)) + table.insert(self.Decorations, Decoration(x, y, self, sprite)) end -- Add new cloud to the world -- TODO: extend variables names to provide better readability. -- TODO: follow new parameters in `not.Cloud.new` based on `not.Cloud.init`. function World:createCloud (x, y, t, v) - table.insert(self.Clouds, Cloud(x, y, t, v)) + table.insert(self.Clouds, Cloud(x, y, t, v, self)) end -- Randomize Cloud creation @@ -163,7 +163,7 @@ end -- TODO: follow new parameters in `not.Effect.new` based on `not.Effect.init`. -- TODO: along with `createRay` move this nearer reast of `create*` methods for readability. function World:createEffect (name, x, y) - table.insert(self.Effects, Effect(name, x, y)) + table.insert(self.Effects, Effect(name, x, y, self)) end -- Add a ray -- cgit v1.1 From 5d744fdc20fefde18d4176e7e25273d27c217007 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 22:44:41 +0200 Subject: Reviewed Ray which was way older than rest of stuff; moved to new oop module --- not/Ray.lua | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/not/Ray.lua b/not/Ray.lua index bbe11c1..8edf51f 100644 --- a/not/Ray.lua +++ b/not/Ray.lua @@ -1,36 +1,32 @@ --- `Ray` +require "not.Object" + +--- `Ray` -- That awesome effect that blinks when player dies! +Ray = Object:extends() --- WHOLE CODE HAS FLAG OF "need a cleanup" +Ray.naut =--[[not.Hero]]nil +Ray.world =--[[not.World]]nil +Ray.canvas =--[[love.graphics.newCanvas]]nil +Ray.delay = 0.3 -Ray = { - naut = nil, - world = nil, - canvas = nil, - delay = 0.3 -} -function Ray:new(naut, world) - -- Meta - local o = {} - setmetatable(o, self) - self.__index = self - -- Init - o.naut = naut - o.world = world +function Ray:new (naut, world) + self.naut = naut + self.world = world -- Cavas, this is temporary, I believe. - local scale = o.world.camera.scale + local scale = self.world.camera.scale local w, h = love.graphics.getWidth(), love.graphics.getHeight() - o.canvas = love.graphics.newCanvas(w/scale, h/scale) - return o + self.canvas = love.graphics.newCanvas(w/scale, h/scale) end -function Ray:update(dt) + +function Ray:update (dt) self.delay = self.delay - dt if self.delay < 0 then return true -- delete end return false end -function Ray:draw(offset_x, offset_y, scale) + +function Ray:draw (offset_x, offset_y, scale) love.graphics.setCanvas(self.canvas) love.graphics.clear() love.graphics.setColor(255, 247, 228, 247) @@ -50,3 +46,5 @@ function Ray:draw(offset_x, offset_y, scale) -- draw on screen love.graphics.draw(self.canvas, 0, 0, 0, scale, scale) end + +return Ray -- cgit v1.1 From 8e7c7d2a4ca74fcb58f36424b05a96d358208e39 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 22:46:50 +0200 Subject: Comment on Entity --- not/Entity.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/not/Entity.lua b/not/Entity.lua index 6ba1606..0e21e48 100644 --- a/not/Entity.lua +++ b/not/Entity.lua @@ -3,6 +3,7 @@ require "not.Sprite" --- `Entity` -- Basic, visible object to be used within World instance. Can be anything. Represented as `Sprite`. -- We still need to keep old, global way of using modules but there are `returns` on the end at least. +-- It probably would be nice to move `World` dependency out of it but for now it should stay this way. Later `World` instance could be just passed to methods that need it. Entity = Sprite:extends() Entity.world =--[[not.World]]nil -- cgit v1.1 From 87f870c0f4eb81e92aee929439692ecc2b95226a Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 23:05:49 +0200 Subject: World updated to new oop module --- not/World.lua | 53 ++++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/not/World.lua b/not/World.lua index 88be97a..1ced718 100644 --- a/not/World.lua +++ b/not/World.lua @@ -1,29 +1,27 @@ +require "not.Object" + --- `World` -- Used to manage physical world and everything inside it: clouds, platforms, nauts, background etc. -- TODO: Possibly move common parts of `World` and `Menu` to abstract class `Scene`. -World = { - world = --[[love.physics.newWorld]]nil, - Nauts = --[[{not.Hero}]]nil, - Platforms = --[[{not.Platform}]]nil, - Clouds = --[[{not.Cloud}]]nil, - Decorations = --[[{not.Decoration}]]nil, - Effects = --[[{not.Effect}]]nil, - Rays = --[[{not.Ray}]]nil, - camera = --[[not.Camera]]nil, - -- cloud generator - clouds_delay = 5, - -- Map - map = nil, - background = nil, - -- Gameplay status - lastNaut = false, - -- "WINNER" - win_move = 0, - -- Music - music = nil -} +World = Object:extends() -World.__index = World +World.world =--[[love.physics.newWorld]]nil +World.Nauts =--[[{not.Hero}]]nil +World.Platforms =--[[{not.Platform}]]nil +World.Clouds =--[[{not.Cloud}]]nil +World.Decorations =--[[{not.Decoration}]]nil +World.Effects =--[[{not.Effect}]]nil +World.Rays =--[[{not.Ray}]]nil +World.camera =--[[not.Camera]]nil +World.music =--[[not.Music]]nil +-- cloud generator +World.clouds_delay = 5 +-- Map +World.map = nil +World.background = nil +-- Gameplay status +World.lastNaut = false +World.win_move = 0 -- "WINNER" require "not.Platform" require "not.Player" @@ -35,13 +33,6 @@ require "not.Music" -- Constructor of `World` ZA WARUDO! function World:new (map, nauts) - local o = setmetatable({}, self) - o:init(map, nauts) - return o -end - --- Init za warudo -function World:init (map, nauts) -- Box2D physical world. love.physics.setMeter(64) self.world = love.physics.newWorld(0, 9.81*64, true) @@ -401,9 +392,9 @@ function World:controlpressed (set, action, key) local map = self:getMapName() local nauts = {} for _,naut in pairs(self:getNautsAll()) do - table.insert(nauts, {naut.name, naut:getControlSet()}) + table.insert(nauts, {naut.name, naut:getControllerSet()}) end - local new = World:new(map, nauts) + local new = World(map, nauts) changeScene(new) end for k,naut in pairs(self:getNautsAll()) do -- cgit v1.1 From 65f338b78a26784e42a232f9b0f2fdab3054e3aa Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 26 May 2017 23:22:18 +0200 Subject: Properly construct world --- config/menus/select.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/menus/select.lua b/config/menus/select.lua index 804b4eb..fd897d2 100644 --- a/config/menus/select.lua +++ b/config/menus/select.lua @@ -36,7 +36,7 @@ return { :set("active", function (self) local nauts = naut_selector:getFullSelection(false) if #nauts > 1 then - changeScene(World:new(MAP, nauts)) + changeScene(World(MAP, nauts)) end end) , -- cgit v1.1 From 20190e7e7878c9217dc2633cbb912e405c19074f Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 15 Jun 2017 14:57:58 +0200 Subject: Pushing old edits World comments and main newline a eof --- main.lua | 2 +- not/World.lua | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/main.lua b/main.lua index 11e4d95..4a56c9d 100644 --- a/main.lua +++ b/main.lua @@ -82,4 +82,4 @@ end function Controller.controlreleased (set, action, key) Scene:controlreleased(set, action, key) -end \ No newline at end of file +end diff --git a/not/World.lua b/not/World.lua index 1ced718..99f50fa 100644 --- a/not/World.lua +++ b/not/World.lua @@ -14,12 +14,9 @@ World.Effects =--[[{not.Effect}]]nil World.Rays =--[[{not.Ray}]]nil World.camera =--[[not.Camera]]nil World.music =--[[not.Music]]nil --- cloud generator World.clouds_delay = 5 --- Map -World.map = nil -World.background = nil --- Gameplay status +World.map =--[[config.maps.*]]nil +World.background =--[[image?]]nil World.lastNaut = false World.win_move = 0 -- "WINNER" -- cgit v1.1 From de4650f205a7c5f6fc5ffd7ec995eb0721c34296 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 15 Jun 2017 17:19:11 +0200 Subject: No more global quit --- main.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/main.lua b/main.lua index 4a56c9d..ba1bfca 100644 --- a/main.lua +++ b/main.lua @@ -72,9 +72,6 @@ function love.keyreleased (key) Controller.keyreleased(key) end -- Controller callbacks function Controller.controlpressed (set, action, key) Scene:controlpressed(set, action, key) - if key == "escape" then - love.event.quit() - end if key == "f5" then debug = not debug end -- cgit v1.1 From a00ac10a0c3654dccf7c8c92da6d8666f2f7ac48 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 15 Jun 2017 17:19:34 +0200 Subject: Removed magic numbers in punching --- not/Hero.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index 1f7cd8b..cb205bf 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -31,6 +31,12 @@ Hero.portrait_sheet = getNautsIconsList() Hero.portrait_box = love.graphics.newQuad(0, 15, 32,32, 80,130) Hero.sfx = require "config.sounds" +Hero.PUNCH_FIXTURE_LIFETIME = 0.08 +Hero.PUNCH_LEFT = {-2,-6, -20,-6, -20,6, -2,6} +Hero.PUNCH_RIGHT = {2,-6, 20,-6, 20,6, 2,6} +Hero.PUNCH_UP = {-8,-4, -8,-20, 8,-20, 8,-4} +Hero.PUNCH_DOWN = {-8,4, -8,20, 8,20, 8,4} + -- Constructor of `Hero`. function Hero:new (name, x, y, world) -- TODO: Statics moved temporarily here. Should be moved to e.g. `load()`. @@ -39,7 +45,7 @@ function Hero:new (name, x, y, world) Hero.portrait_frame = love.graphics.newImage("assets/menu.png") end -- Find imagePath based on hero name. - local fileName = name or Hero.name -- INITIAL from prototype + local fileName = name or Hero.name -- INITIAL local imagePath = string.format("assets/nauts/%s.png", fileName) -- `PhysicalBody` initialization. Hero.__super.new(self, x, y, world, imagePath) @@ -195,22 +201,21 @@ end -- Creates temporary fixture for hero's body that acts as sensor. -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. --- TODO: Magic numbers present in `not.Hero.punch`. function Hero:punch (direction) - self.punchCooldown = Hero.punchCooldown -- INITIAL from prototype + self.punchCooldown = Hero.punchCooldown -- Choose shape based on punch direction. local shape - if direction == "left" then shape = {-2,-6, -20,-6, -20,6, -2,6} end - if direction == "right" then shape = {2,-6, 20,-6, 20,6, 2,6} end - if direction == "up" then shape = {-8,-4, -8,-20, 8,-20, 8,-4} end - if direction == "down" then shape = {-8,4, -8,20, 8,20, 8,4} end + if direction == "left" then shape = Hero.PUNCH_LEFT end + if direction == "right" then shape = Hero.PUNCH_RIGHT end + if direction == "up" then shape = Hero.PUNCH_UP end + if direction == "down" then shape = Hero.PUNCH_DOWN end -- Create and set sensor fixture. local fixture = self:addFixture(shape, 0) fixture:setSensor(true) fixture:setCategory(3) fixture:setMask(1,3) fixture:setGroupIndex(self.group) - fixture:setUserData({0.08, direction}) + fixture:setUserData({Hero.PUNCH_FIXTURE_LIFETIME, direction}) self:playSound(4) end @@ -270,3 +275,5 @@ function Hero:playSound (sfx, force) source:play() end end + +return Hero -- cgit v1.1 From 2c458f0c756ebe577083a7cc972a07e870cb0ba9 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 15 Jun 2017 17:21:02 +0200 Subject: Music renamed to MusicPlayer changed behaviour to use static table for sources need to change dependencies now --- not/Music.lua | 25 ------------------------- not/MusicPlayer.lua | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 25 deletions(-) delete mode 100644 not/Music.lua create mode 100644 not/MusicPlayer.lua diff --git a/not/Music.lua b/not/Music.lua deleted file mode 100644 index ee930f4..0000000 --- a/not/Music.lua +++ /dev/null @@ -1,25 +0,0 @@ ---- `Music` --- Simple music player object that plays and loops selected track in single Scene. -Music = { - source = --[[love.audio.newSource]]nil -} - -Music.__index = Music - -function Music:new (trackName) - local o = setmetatable({}, self) - o:init(trackName) - return o -end - --- TODO: trackName should be passed without file extension. -function Music:init (trackName) - self.source = love.audio.newSource("assets/music/" .. trackName) - self.source:setLooping(true) - self.source:setVolume(.7) - self.source:play() -end - -function Music:delete () - self.source:stop() -end \ No newline at end of file diff --git a/not/MusicPlayer.lua b/not/MusicPlayer.lua new file mode 100644 index 0000000..1e65e3b --- /dev/null +++ b/not/MusicPlayer.lua @@ -0,0 +1,34 @@ +require "not.object" + +--- `MusicPlayer` +-- Simple music player object that plays and loops selected track. +MusicPlayer = Object:extends() + +MusicPlayer.TRACKS = {} + +-- TODO: trackName should be passed without file extension. +function MusicPlayer:new (trackName) + self:setTrack(trackName) +end + +function MusicPlayer:delete () + self.source:stop() +end + +function MusicPlayer:setTrack (trackName) + if self.source then + self.source:stop() + end + if MusicPlayer.TRACKS[trackName] then + self.source = MusicPlayer.TRACKS[trackName] + else + local source = love.audio.newSource("assets/music" .. trackName) + source:setLooping(true) + source:setVolume(.7) + self.source = source + MusicPlayer.TRACKS[trackName] = source + end + self.source:play() +end + +return MusicPlayer -- cgit v1.1 From baf04a055dbc66b80c29a406907a0a595ac55f00 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 27 Jun 2017 09:02:03 +0200 Subject: Moved ui elements to use new oop lib --- config/menus/credits.lua | 8 ++++---- config/menus/host.lua | 16 +++++++-------- config/menus/main.lua | 20 +++++++++---------- config/menus/select.lua | 26 ++++++++++++------------ config/menus/settings.lua | 18 ++++++++--------- not/Button.lua | 29 ++++++++++----------------- not/Element.lua | 16 +++++++-------- not/Header.lua | 20 ++++++------------- not/Selector.lua | 50 ++++++++++++++++++++--------------------------- 9 files changed, 88 insertions(+), 115 deletions(-) diff --git a/config/menus/credits.lua b/config/menus/credits.lua index 77cba62..6a2ab50 100644 --- a/config/menus/credits.lua +++ b/config/menus/credits.lua @@ -1,20 +1,20 @@ local menu = ... -local button = require "not.Button" -local element = require "not.Element" +local Button = require "not.Button" +local Element = require "not.Element" local width, height = love.graphics.getWidth()/getRealScale(), love.graphics.getHeight()/getRealScale() local bx = width/2-29 return { - button:new(menu) + Button(menu) :setText("Go back") :setPosition(bx,144) :set("active", function (self) self.parent:open("main") end) , - element:new(menu) + Element(menu) :setPosition(width/2, 30) :set("draw", function (self, scale) local x,y = self:getPosition() diff --git a/config/menus/host.lua b/config/menus/host.lua index b318a5b..fff683c 100644 --- a/config/menus/host.lua +++ b/config/menus/host.lua @@ -1,18 +1,18 @@ local menu = ... -local button = require "not.Button" -local selector = require "not.Selector" +local Button = require "not.Button" +local Selector = require "not.Selector" local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale() local bx = width/2-29 -local map_selector = selector:new(menu) +local map_Selector = Selector(menu) require "iconsList" local icons, maps = getMapsIconsList() return { - map_selector + map_Selector :setPosition(width/2, 40) :setSize(80, 42) :setMargin(0) @@ -24,18 +24,18 @@ return { :set("shape", "panorama") :init() , - button:new(menu) + Button(menu) :setText("Next") :setPosition(bx,101) :set("isEnabled", function () - return map_selector:isLocked() + return map_Selector:isLocked() end) :set("active", function (self) - MAP = map_selector:getFullSelection(true)[1][1] -- please, don't kill me for this, kek + MAP = map_Selector:getFullSelection(true)[1][1] -- please, don't kill me for this, kek self.parent:open("select") end) , - button:new(menu) + Button(menu) :setText("Go back") :setPosition(bx,117) :set("active", function (self) diff --git a/config/menus/main.lua b/config/menus/main.lua index 236c011..d65c57d 100644 --- a/config/menus/main.lua +++ b/config/menus/main.lua @@ -1,8 +1,8 @@ local menu = ... -local button = require "not.Button" -local header = require "not.Header" -local element = require "not.Element" +local Button = require "not.Button" +local Header = require "not.Header" +local Element = require "not.Element" local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale() local bx = width/2-29 @@ -10,40 +10,40 @@ local bx = width/2-29 local awesometwo = love.graphics.newImage("assets/two.png") return { - button:new(menu) + Button(menu) :setText("Start") :setPosition(bx, 80) :set("active", function (self) self.parent:open("host") end) , - button:new(menu) + Button(menu) :setText("Join") :setPosition(bx, 96) :set("isEnabled", function (self) return false end) , - button:new(menu) + Button(menu) :setText("Settings") :setPosition(bx, 112) :set("active", function (self) self.parent:open("settings") end) , - button:new(menu) + Button(menu) :setText("Credits") :setPosition(bx, 128) :set("active", function (self) self.parent:open("credits") end) , - button:new(menu) + Button(menu) :setText("Exit") :setPosition(bx, 144) :set("active", love.event.quit) , - element:new(menu) + Element(menu) :setPosition(width/2, 15) :set("draw", function (self, scale) local x,y = self:getPosition() @@ -52,7 +52,7 @@ return { love.graphics.draw(awesometwo, x*scale, y*scale, 0, scale, scale, 35) end) , - header:new(menu) + Header(menu) :setText("Roflnauts") :setPosition(width/2,40) , diff --git a/config/menus/select.lua b/config/menus/select.lua index fd897d2..431e620 100644 --- a/config/menus/select.lua +++ b/config/menus/select.lua @@ -1,20 +1,20 @@ local menu = ... -local button = require "not.Button" -local selector = require "not.Selector" -local element = require "not.Element" +local Button = require "not.Button" +local Selector = require "not.Selector" +local Element = require "not.Element" local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale() local bx = width/2-29 -local naut_selector = selector:new(menu) -local start_button = button:new(menu) +local naut_Selector = Selector(menu) +local start_Button = Button(menu) require "iconsList" local nautsIcons, nautsList = getNautsIconsList() return { - naut_selector + naut_Selector :setPosition(width/2,60) :setMargin(8) :setSize(32, 32) @@ -24,30 +24,30 @@ return { :set("icons_q", nautsIcons) :init() , - start_button + start_Button :setText("Force start") :setPosition(bx,134) :set("isEnabled", function () - if #naut_selector:getFullSelection(false) > 1 then + if #naut_Selector:getFullSelection(false) > 1 then return true end return false end) :set("active", function (self) - local nauts = naut_selector:getFullSelection(false) + local nauts = naut_Selector:getFullSelection(false) if #nauts > 1 then changeScene(World(MAP, nauts)) end end) , - button:new(menu) + Button(menu) :setText("Go back") :setPosition(bx,150) :set("active", function (self) self.parent:open("host") end) , - element:new(menu) + Element(menu) :setPosition(bx, 101) :set("the_final_countdown", 9) :set("draw", function (self, scale) @@ -62,14 +62,14 @@ return { end end) :set("update", function (self, dt) - local total = #naut_selector:getFullSelection(false) + local total = #naut_Selector:getFullSelection(false) if total > 1 then self.the_final_countdown = self.the_final_countdown - dt else self.the_final_countdown = 9 end if self.the_final_countdown < 0 then - start_button:active() + start_Button:active() end end) , diff --git a/config/menus/settings.lua b/config/menus/settings.lua index ae0403d..a1c6507 100644 --- a/config/menus/settings.lua +++ b/config/menus/settings.lua @@ -1,15 +1,15 @@ local menu = ... -local button = require "not.Button" -local selector = require "not.Selector" -local element = require "not.Element" +local Button = require "not.Button" +local Selector = require "not.Selector" +local Element = require "not.Element" local width, height = love.graphics.getWidth()/getRealScale(), love.graphics.getHeight()/getRealScale() local bx = width/2-29 local keys = {"Left", "Right", "Up", "Down", "Attack", "Jump"} -local dimmer = element:new(menu) +local dimmer = Element(menu) :setPosition(width/2, 15) :set("visible", false) :set("currentControl", "Left") -- it actually means control that is being set CURRENTLY @@ -65,7 +65,7 @@ local controlreleased = function(self, set, action, key) end local a = { - button:new(menu) + Button(menu) :setText("Keyboard 1") :setPosition(bx,80) :set("setNumber", function () return 1 end) @@ -74,7 +74,7 @@ local a = { :set("stopChange", stopChange) :set("active", startChange) , - button:new(menu) + Button(menu) :setText("Keyboard 2") :setPosition(bx,96) :set("setNumber", function () return 2 end) @@ -83,7 +83,7 @@ local a = { :set("stopChange", stopChange) :set("active", startChange) , - button:new(menu) + Button(menu) :setText("Gamepad 1") :setPosition(bx,112) :set("setNumber", function () return 3 end) @@ -92,7 +92,7 @@ local a = { :set("stopChange", stopChange) :set("active", startChange) , - button:new(menu) + Button(menu) :setText("Gamepad 2") :setPosition(bx,128) :set("setNumber", function () return 4 end) @@ -101,7 +101,7 @@ local a = { :set("stopChange", stopChange) :set("active", startChange) , - button:new(menu) + Button(menu) :setText("Go back") :setPosition(bx,144) :set("active", function (self) diff --git a/not/Button.lua b/not/Button.lua index 91aca45..a2f7a19 100644 --- a/not/Button.lua +++ b/not/Button.lua @@ -1,27 +1,18 @@ +require "not.Element" + --- `Button` -- Menu element that can be activated by user. -Button = { - parent = --[[not.Menu]]nil, - x = 0, - y = 0, - text = "", - focused = false, - sprite, - quads, - delay = 2, - parent, -} +Button = Element:extends() --- `Button` is a child of `Element`. -require "not.Element" -Button.__index = Button -setmetatable(Button, Element) +Button.text = "" +Button.focused = false +Button.sprite = --[[]]nil +Button.quads = --[[]]nil +Button.delay = 2 function Button:new (parent) - local o = setmetatable({}, self) - o.parent = parent - o.sprite, o.quads = parent:getSheet() - return o + Button.__super.new(self, parent) + self.sprite, self.quads = parent:getSheet() end function Button:setText (text) diff --git a/not/Element.lua b/not/Element.lua index e6d91da..24576e6 100644 --- a/not/Element.lua +++ b/not/Element.lua @@ -1,17 +1,15 @@ +require "not.Object" + --- `Element` -- Empty element used inside `Menu`. -Element = { - parent = --[[not.Menu]]nil, - x = 0, - y = 0 -} +Element = Object:extends() -Element.__index = Element +Element.parent = --[[not.Menu]]nil +Element.x = 0 +Element.y = 0 function Element:new (parent) - local o = setmetatable({}, self) - o.parent = parent - return o + self.parent = parent end function Element:delete () end -- deletes Element diff --git a/not/Header.lua b/not/Header.lua index a563ab2..8b2ec0d 100644 --- a/not/Header.lua +++ b/not/Header.lua @@ -1,22 +1,14 @@ +require "not.Element" + --- `Header` -- Swinging title. -Header = { - parent = --[[not.Menu]]nil, - x = 0, - y = 0, - text = "", - bounce = 2, -} +Header = Element:extends() --- `Header` is a child of `Element`. -require "not.Element" -Header.__index = Header -setmetatable(Header, Element) +Header.text = "" +Header.bounce = 2 function Header:new (parent) - local o = setmetatable({}, self) - o.parent = parent - return o + Header.__super.new(self, parent) end function Header:setText (text) diff --git a/not/Selector.lua b/not/Selector.lua index 8e03457..ef78778 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -1,3 +1,5 @@ +require "not.Element" + --- `Selector` -- Used in Menu for selecting various things from list. Works for each Controller set or globally. --[[ @@ -12,39 +14,29 @@ selector:new(menu) :set("global", false) -- true: single selector; false: selector for each controller set present :init() ]] -Selector = { - parent = --[[not.Menu]]nil, - x = 0, - y = 0, - width = 0, - height = 0, - margin = 0, - focused = false, - global = false, - delay = 2, - first = false, - list, - sets, - locks, - selections, - shape = "portrait", - sprite, - quads, - icons_i, - icons_q -} +Selector = Element:extends() --- `Selector` is a child of `Element`. -require "not.Element" -Selector.__index = Selector -setmetatable(Selector, Element) +Selector.width = 0 +Selector.height = 0 +Selector.margin = 0 +Selector.focused = false +Selector.global = false +Selector.delay = 2 +Selector.first = false +Selector.list = --[[]]nil +Selector.sets = --[[]]nil +Selector.locks = --[[]]nil +Selector.selections = --[[]]nil +Selector.shape = "portrait" +Selector.sprite = --[[]]nil +Selector.quads = --[[]]nil +Selector.icons_i = --[[]]nil +Selector.icons_q = --[[]]nil -- Constructor function Selector:new (parent) - local o = setmetatable({}, self) - o.parent = parent - o.sprite, o.quads = parent:getSheet() - return o + Selector.__super.new(self, parent) + self.sprite, self.quads = parent:getSheet() end -- Size of single block -- cgit v1.1 From ae8ce1a11ca02297ff9fe05b3146c620a2485975 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 27 Jun 2017 09:05:16 +0200 Subject: Scenes now use MusicPlayer --- not/Menu.lua | 6 +++--- not/MusicPlayer.lua | 4 ++-- not/World.lua | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/not/Menu.lua b/not/Menu.lua index 8ef1861..cd6d963 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -38,7 +38,7 @@ Menu = { Menu.__index = Menu -require "not.Music" +require "not.MusicPlayer" function Menu:new (name) local o = setmetatable({}, self) @@ -54,7 +54,7 @@ function Menu:new (name) end function Menu:init (name) - self.music = Music:new("menu.ogg") + self.music = MusicPlayer("menu.ogg") self:open(name) end @@ -141,4 +141,4 @@ function Menu:controlreleased (set, action, key) for _,element in pairs(self.elements) do element:controlreleased(set, action, key) end -end \ No newline at end of file +end diff --git a/not/MusicPlayer.lua b/not/MusicPlayer.lua index 1e65e3b..84f5384 100644 --- a/not/MusicPlayer.lua +++ b/not/MusicPlayer.lua @@ -1,4 +1,4 @@ -require "not.object" +require "not.Object" --- `MusicPlayer` -- Simple music player object that plays and loops selected track. @@ -22,7 +22,7 @@ function MusicPlayer:setTrack (trackName) if MusicPlayer.TRACKS[trackName] then self.source = MusicPlayer.TRACKS[trackName] else - local source = love.audio.newSource("assets/music" .. trackName) + local source = love.audio.newSource("assets/music/" .. trackName) source:setLooping(true) source:setVolume(.7) self.source = source diff --git a/not/World.lua b/not/World.lua index 99f50fa..112846a 100644 --- a/not/World.lua +++ b/not/World.lua @@ -26,7 +26,7 @@ require "not.Cloud" require "not.Effect" require "not.Decoration" require "not.Ray" -require "not.Music" +require "not.MusicPlayer" -- Constructor of `World` ZA WARUDO! function World:new (map, nauts) @@ -46,7 +46,7 @@ function World:new (map, nauts) self:loadMap(map) self:spawnNauts(nauts) self.camera = Camera:new(self) - self.music = Music:new(self.map.theme) + self.music = MusicPlayer(self.map.theme) end -- The end of the world @@ -402,4 +402,4 @@ function World:controlreleased (set, action, key) for k,naut in pairs(self:getNautsAll()) do naut:controlreleased(set, action, key) end -end \ No newline at end of file +end -- cgit v1.1 From 75e30a649bd41029e48344969225e8ff329a86b4 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 27 Jun 2017 14:40:08 +0200 Subject: Indention in settings menu --- config/menus/settings.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/menus/settings.lua b/config/menus/settings.lua index a1c6507..96c12cd 100644 --- a/config/menus/settings.lua +++ b/config/menus/settings.lua @@ -74,7 +74,7 @@ local a = { :set("stopChange", stopChange) :set("active", startChange) , - Button(menu) + Button(menu) :setText("Keyboard 2") :setPosition(bx,96) :set("setNumber", function () return 2 end) @@ -83,7 +83,7 @@ local a = { :set("stopChange", stopChange) :set("active", startChange) , - Button(menu) + Button(menu) :setText("Gamepad 1") :setPosition(bx,112) :set("setNumber", function () return 3 end) -- cgit v1.1 From b5c3f635128845baa6fdc738df44c5fe88b3db82 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 28 Jun 2017 21:18:09 +0200 Subject: Fixed animation bug for Player on walking combination --- not/Player.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/not/Player.lua b/not/Player.lua index 5d108ce..34f57eb 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -134,10 +134,11 @@ function Player:controlreleased (set, action, key) end -- Walking if (action == "left" or action == "right") then - self.isWalking = false - if not (self:isControlDown("left") or self:isControlDown("right")) and - self.current == self.animations.walk then - self:setAnimation("default") + if not (self:isControlDown("left") or self:isControlDown("right")) then + self.isWalking = false + if self.current == self.animations.walk then + self:setAnimation("default") + end end end end -- cgit v1.1 From f525f386952506f7c14e241d087b7473f230235b Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 5 Jul 2017 17:00:07 +0200 Subject: Object lib added as git submodule --- .gitmodules | 3 +++ lib/Object.lua | 77 ---------------------------------------------------------- lib/object | 1 + not/Object.lua | 2 +- 4 files changed, 5 insertions(+), 78 deletions(-) create mode 100644 .gitmodules delete mode 100644 lib/Object.lua create mode 160000 lib/object diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2388b92 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/object"] + path = lib/object + url = https://github.com/nthirtyone/object.git diff --git a/lib/Object.lua b/lib/Object.lua deleted file mode 100644 index b241bb2..0000000 --- a/lib/Object.lua +++ /dev/null @@ -1,77 +0,0 @@ ---- You may not believe me but we are not returning this one. --- This table is used as metatable for classes e.g. for `Object`. -local Class = {} - ---- Metamethod for classes. --- Creates new instance of class calling `new()` method (constructor) with all parameters passed. -Class.__call = function (self, ...) - local o = setmetatable({}, self) - self.new(o, ...) - return o -end - ---- Metamethod for classes. --- First checks if `__index` (instance prototype) have field we are looking for. Then it tries to find it in super class. -Class.__index = function (self, key) - if rawget(self, "__index") ~= nil then - if rawget(self, "__index")[key] ~= nil then - return rawget(self, "__index")[key] - end - end - if rawget(self, "__super") ~= nil then - if rawget(self, "__super")[key] ~= nil then - return rawget(self, "__super")[key] - end - end - return nil -end ---- Metamethod for classes. --- Redirects creating new properties to class'es `__index` which is used as a prototype for instances of class. --- Only `new` method and metamethods are allowed to be written to class'es table directly. -Class.__newindex = function(self, key, value) - if key == "new" or key:sub(1, 2) == "__" then - rawset(self, key, value) - else - self.__index[key] = value - end -end - ---- Creates new class from parent class. --- New class will call parent's constructor unless new constructor will be defined. --- @param parent super class of new class -local extends = function (parent) - local self = setmetatable({}, Class) - rawset(self, "__index", {}) - if parent then - setmetatable(self.__index, {__index = parent.__index}) - end - rawset(self, "__super", parent) - return self -end - ---- Almost empty class. --- Used to create new classes via `extend()` method: --- `Child = Object:extend()` --- Contains `is()` and `new()` methods. Later one isn't available from inside of instances. -local Object = extends(nil) -rawset(Object, "extends", extends) -Object.new = function (self) end - ---- Checks if class or instance of class is a child of class passed through parameter. --- @param class table we want to test against (preferably class table) --- @return boolean which is false if tested sample is not child of passed class -function Object:is (class) - if not class then return false end - if self == class or getmetatable(self) == class then - return true - end - if self.__super then - return self.__super:is(class) - end - if getmetatable(self).__super then - return getmetatable(self).__super:is(class) - end - return false -end - -return Object diff --git a/lib/object b/lib/object new file mode 160000 index 0000000..eacf0f6 --- /dev/null +++ b/lib/object @@ -0,0 +1 @@ +Subproject commit eacf0f60c0752616407fbca6cd9d8ca9757ce624 diff --git a/not/Object.lua b/not/Object.lua index fbc41e5..30b91b5 100644 --- a/not/Object.lua +++ b/not/Object.lua @@ -1,3 +1,3 @@ -- Wrapping library to game's hierarchy in a shameless way. -Object = require "lib.Object" +Object = require "lib.object.Object" return Object -- cgit v1.1 From dd40241cc5a8a981db453304f91c7c3e1cbfbe5a Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 5 Jul 2017 17:05:19 +0200 Subject: Updated makefile for submodule --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 5f17a9c..ad749e8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ all: - zip not-nautz not/*.lua config/maps/*.lua config/menus/*.lua config/*.lua assets/*.png assets/sounds/*.ogg assets/platforms/*.png assets/nauts/*.png assets/music/*.ogg assets/decorations/*.png assets/backgrounds/*.png *.lua gamecontrollerdb.txt settings.default - mv not-nautz.zip ../not-nautz.love + zip not-nautz lib/object/Object.lua not/*.lua config/maps/*.lua config/menus/*.lua config/*.lua assets/*.png assets/sounds/*.ogg assets/platforms/*.png assets/nauts/*.png assets/music/*.ogg assets/decorations/*.png assets/backgrounds/*.png *.lua gamecontrollerdb.txt settings.default clean: rm ../not-nautz.love \ No newline at end of file -- cgit v1.1 From 0b3de32ee8b9d79d7b818a6742ab577ff47525e6 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 10 Jul 2017 21:16:35 +0200 Subject: MusicPlayer now don't share sources --- not/MusicPlayer.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/not/MusicPlayer.lua b/not/MusicPlayer.lua index 84f5384..d2a89c3 100644 --- a/not/MusicPlayer.lua +++ b/not/MusicPlayer.lua @@ -4,14 +4,13 @@ require "not.Object" -- Simple music player object that plays and loops selected track. MusicPlayer = Object:extends() -MusicPlayer.TRACKS = {} - --- TODO: trackName should be passed without file extension. function MusicPlayer:new (trackName) + self.tracks = {} self:setTrack(trackName) end function MusicPlayer:delete () + self.tracks = nil self.source:stop() end @@ -19,14 +18,14 @@ function MusicPlayer:setTrack (trackName) if self.source then self.source:stop() end - if MusicPlayer.TRACKS[trackName] then - self.source = MusicPlayer.TRACKS[trackName] + if self.tracks[trackName] then + self.source = self.tracks[trackName] else local source = love.audio.newSource("assets/music/" .. trackName) source:setLooping(true) source:setVolume(.7) self.source = source - MusicPlayer.TRACKS[trackName] = source + self.tracks[trackName] = source end self.source:play() end -- cgit v1.1 From 4e69182a6d36966847430f01b440a9cdac0e4cdc Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 11 Jul 2017 01:21:31 +0200 Subject: Moved scene managment to SceneManager (non-existant) --- main.lua | 12 +----------- not/SceneManager.lua | 10 ++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) create mode 100644 not/SceneManager.lua diff --git a/main.lua b/main.lua index ba1bfca..6028c84 100644 --- a/main.lua +++ b/main.lua @@ -1,17 +1,6 @@ --- Roflnauts 2 -- TODO: Any lua source file in root directory that is not `main` (this file), `conf` should be moved to a proper directory. Its name should be changed to show what it contains. --- Pretend you didn't see this --- This is work for scene manager --- TODO: Create SceneManager or similar class. -Scene = nil -function changeScene (scene) - if Scene ~= nil then - Scene:delete() - end - Scene = scene -end - -- Should be moved to scene/camera -- TODO: move following functions to `Camera`. function getScale () @@ -24,6 +13,7 @@ end -- Require require "iconsList" +require "not.SceneManager" require "not.World" require "not.Camera" require "not.Menu" diff --git a/not/SceneManager.lua b/not/SceneManager.lua new file mode 100644 index 0000000..0a6cb4b --- /dev/null +++ b/not/SceneManager.lua @@ -0,0 +1,10 @@ +-- Pretend you didn't see this +-- This is work for scene manager +-- TODO: Create SceneManager or similar class. +Scene = nil +function changeScene (scene) + if Scene ~= nil then + Scene:delete() + end + Scene = scene +end -- cgit v1.1 From 47d4e1c229adfffb70b3c984d00049bcebcfc183 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 12 Jul 2017 09:04:23 +0200 Subject: All music playing moved to single instance of MusicPlayer --- not/Menu.lua | 9 +++------ not/MusicPlayer.lua | 14 ++++++++++++-- not/SceneManager.lua | 2 ++ not/World.lua | 5 ++--- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/not/Menu.lua b/not/Menu.lua index cd6d963..4dcff5e 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -38,8 +38,6 @@ Menu = { Menu.__index = Menu -require "not.MusicPlayer" - function Menu:new (name) local o = setmetatable({}, self) -- Load statically. @@ -54,13 +52,12 @@ function Menu:new (name) end function Menu:init (name) - self.music = MusicPlayer("menu.ogg") + musicPlayer:setTrack("menu.ogg") + musicPlayer:play() self:open(name) end -function Menu:delete () - self.music:delete() -end +function Menu:delete () end function Menu:open (name) local name = name or "main" diff --git a/not/MusicPlayer.lua b/not/MusicPlayer.lua index d2a89c3..e838d96 100644 --- a/not/MusicPlayer.lua +++ b/not/MusicPlayer.lua @@ -6,12 +6,15 @@ MusicPlayer = Object:extends() function MusicPlayer:new (trackName) self.tracks = {} - self:setTrack(trackName) + if trackName then + self:setTrack(trackName) + self:play() + end end function MusicPlayer:delete () self.tracks = nil - self.source:stop() + self:stop() end function MusicPlayer:setTrack (trackName) @@ -27,7 +30,14 @@ function MusicPlayer:setTrack (trackName) self.source = source self.tracks[trackName] = source end +end + +function MusicPlayer:play () self.source:play() end +function MusicPlayer:stop () + self.source:stop() +end + return MusicPlayer diff --git a/not/SceneManager.lua b/not/SceneManager.lua index 0a6cb4b..755548a 100644 --- a/not/SceneManager.lua +++ b/not/SceneManager.lua @@ -2,6 +2,8 @@ -- This is work for scene manager -- TODO: Create SceneManager or similar class. Scene = nil +musicPlayer = require("not.MusicPlayer")() + function changeScene (scene) if Scene ~= nil then Scene:delete() diff --git a/not/World.lua b/not/World.lua index 112846a..ab94ff4 100644 --- a/not/World.lua +++ b/not/World.lua @@ -26,7 +26,6 @@ require "not.Cloud" require "not.Effect" require "not.Decoration" require "not.Ray" -require "not.MusicPlayer" -- Constructor of `World` ZA WARUDO! function World:new (map, nauts) @@ -46,7 +45,8 @@ function World:new (map, nauts) self:loadMap(map) self:spawnNauts(nauts) self.camera = Camera:new(self) - self.music = MusicPlayer(self.map.theme) + musicPlayer:setTrack(self.map.theme) + musicPlayer:play() end -- The end of the world @@ -57,7 +57,6 @@ function World:delete () for _,naut in pairs(self.Nauts) do naut:delete() end - self.music:delete() self.world:destroy() end -- cgit v1.1 From 094f9326d1e3ae6e451b21192b288220a8fab12e Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 12 Jul 2017 09:31:32 +0200 Subject: Basic SceneManager created and partialy replaced old scene managment system --- main.lua | 20 ++++++++++++++------ not/SceneManager.lua | 23 ++++++++++++++--------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/main.lua b/main.lua index 6028c84..45d495b 100644 --- a/main.lua +++ b/main.lua @@ -11,9 +11,17 @@ function getRealScale () return math.max(1, math.max(love.graphics.getWidth() / 320, love.graphics.getHeight() / 180)) end +-- TODO: They don't look nice like this; move them to some kind of core/game object. +musicPlayer = require "not.MusicPlayer"() +sceneManager = require "not.SceneManager"() + +-- TODO: This is a temporary wrapper! Remove all use cases of `changeScene()`. +function changeScene (scene) + sceneManager:changeScene(scene) +end + -- Require require "iconsList" -require "not.SceneManager" require "not.World" require "not.Camera" require "not.Menu" @@ -34,11 +42,11 @@ function love.load () love.graphics.setFont(Font) Controller.load() Settings.load() - Scene = Menu:new() + sceneManager:changeScene(Menu:new()) end function love.draw () - Scene:draw() + sceneManager:getScene():draw() if debug then local scale = getScale() love.graphics.setFont(Font) @@ -49,7 +57,7 @@ function love.draw () end end -function love.update (dt) Scene:update(dt) end +function love.update (dt) sceneManager:getScene():update(dt) end function love.quit () Settings.save() end -- Pass input to Controller @@ -61,12 +69,12 @@ function love.keyreleased (key) Controller.keyreleased(key) end -- Controller callbacks function Controller.controlpressed (set, action, key) - Scene:controlpressed(set, action, key) + sceneManager:getScene():controlpressed(set, action, key) if key == "f5" then debug = not debug end end function Controller.controlreleased (set, action, key) - Scene:controlreleased(set, action, key) + sceneManager:getScene():controlreleased(set, action, key) end diff --git a/not/SceneManager.lua b/not/SceneManager.lua index 755548a..367a4c0 100644 --- a/not/SceneManager.lua +++ b/not/SceneManager.lua @@ -1,12 +1,17 @@ --- Pretend you didn't see this --- This is work for scene manager --- TODO: Create SceneManager or similar class. -Scene = nil -musicPlayer = require("not.MusicPlayer")() +--- `SceneManager` +-- Used for changing single active scene. +-- TODO: Extend functionality for more than one active scene (eg. overlay menu). +SceneManager = require "not.Object":extends() -function changeScene (scene) - if Scene ~= nil then - Scene:delete() +function SceneManager:changeScene (scene) + if self.scene ~= nil then + self.scene:delete() end - Scene = scene + self.scene = scene end + +function SceneManager:getScene () + return self.scene +end + +return SceneManager -- cgit v1.1 From d161510d2dc33046459e874e716c02c76d49f7d5 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 12 Jul 2017 09:38:36 +0200 Subject: Menu moved to new OOP model --- main.lua | 2 +- not/Menu.lua | 86 +++++++++++++++++++++++++++-------------------------------- not/World.lua | 2 +- 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/main.lua b/main.lua index 45d495b..74c3571 100644 --- a/main.lua +++ b/main.lua @@ -42,7 +42,7 @@ function love.load () love.graphics.setFont(Font) Controller.load() Settings.load() - sceneManager:changeScene(Menu:new()) + sceneManager:changeScene(Menu()) end function love.draw () diff --git a/not/Menu.lua b/not/Menu.lua index 4dcff5e..1688166 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -1,57 +1,49 @@ --- `Menu` -- It creates single screen of a menu -- I do know that model I used here and in `World` loading configuration files is not flawless but I did not want to rewrite `World`s one but wanted to keep things similar at least in project scope. -Menu = { - scale = getScale(), - elements = --[[{not.Element}]]nil, - active = 1, - music = --[[not.Music]]nil, - sprite = --[[love.graphics.newImage]]nil, - background = --[[love.graphics.newImage]]nil, - asteroids = --[[love.graphics.newImage]]nil, - stars = --[[love.graphics.newImage]]nil, - asteroids_bounce = 0, - stars_frame = 1, - stars_delay = 0.8, - allowMove = true, - quads = { -- TODO: Could be moved to config file or perhaps QuadManager to manage all quads for animations etc. - button = { - normal = love.graphics.newQuad(0, 0, 58,15, 80,130), - active = love.graphics.newQuad(0, 0, 58,15, 80,130) - }, - portrait = { - normal = love.graphics.newQuad( 0, 15, 32,32, 80,130), - active = love.graphics.newQuad(32, 15, 32,32, 80,130) - }, - panorama = { - normal = love.graphics.newQuad(0,47, 80,42, 80,130), - active = love.graphics.newQuad(0,88, 80,42, 80,130) - }, - arrow_l = love.graphics.newQuad(68, 0, 6, 6, 80,130), - arrow_r = love.graphics.newQuad(74, 0, 6, 6, 80,130), - stars = { - love.graphics.newQuad( 0, 0, 320, 200, 640,200), - love.graphics.newQuad(320, 0, 320, 200, 640,200) - }, - } -} +Menu = require "not.Object":extends() -Menu.__index = Menu +Menu.scale = getScale() +Menu.elements = --[[{not.Element}]]nil +Menu.active = 1 +Menu.music = --[[not.Music]]nil +Menu.sprite = --[[love.graphics.newImage]]nil +Menu.background = --[[love.graphics.newImage]]nil +Menu.asteroids = --[[love.graphics.newImage]]nil +Menu.stars = --[[love.graphics.newImage]]nil +Menu.asteroids_bounce = 0 +Menu.stars_frame = 1 +Menu.stars_delay = 0.8 +Menu.allowMove = true +Menu.quads = { -- TODO: Could be moved to config file or perhaps QuadManager to manage all quads for animations etc. + button = { + normal = love.graphics.newQuad(0, 0, 58,15, 80,130), + active = love.graphics.newQuad(0, 0, 58,15, 80,130) + }, + portrait = { + normal = love.graphics.newQuad( 0, 15, 32,32, 80,130), + active = love.graphics.newQuad(32, 15, 32,32, 80,130) + }, + panorama = { + normal = love.graphics.newQuad(0,47, 80,42, 80,130), + active = love.graphics.newQuad(0,88, 80,42, 80,130) + }, + arrow_l = love.graphics.newQuad(68, 0, 6, 6, 80,130), + arrow_r = love.graphics.newQuad(74, 0, 6, 6, 80,130), + stars = { + love.graphics.newQuad( 0, 0, 320, 200, 640,200), + love.graphics.newQuad(320, 0, 320, 200, 640,200) + }, +} function Menu:new (name) - local o = setmetatable({}, self) -- Load statically. - if self.sprite == nil then - self.sprite = love.graphics.newImage("assets/menu.png") - self.background = love.graphics.newImage("assets/backgrounds/menu.png") - self.asteroids = love.graphics.newImage("assets/asteroids.png") - self.stars = love.graphics.newImage("assets/stars.png") + if Menu.sprite == nil then + Menu.sprite = love.graphics.newImage("assets/menu.png") + Menu.background = love.graphics.newImage("assets/backgrounds/menu.png") + Menu.asteroids = love.graphics.newImage("assets/asteroids.png") + Menu.stars = love.graphics.newImage("assets/stars.png") end - o:init(name) - return o -end - -function Menu:init (name) musicPlayer:setTrack("menu.ogg") musicPlayer:play() self:open(name) @@ -139,3 +131,5 @@ function Menu:controlreleased (set, action, key) element:controlreleased(set, action, key) end end + +return Menu diff --git a/not/World.lua b/not/World.lua index ab94ff4..be29557 100644 --- a/not/World.lua +++ b/not/World.lua @@ -195,7 +195,7 @@ function World:onNautKilled (naut) self:createRay(naut) local nauts = self:getNautsPlayable() if self.lastNaut then - changeScene(Menu:new()) + changeScene(Menu()) elseif #nauts < 2 then self.lastNaut = true naut:playSound(5, true) -- cgit v1.1 From ea694c3659c801effaaf3361d3713f20517de3f5 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 12 Jul 2017 09:52:06 +0200 Subject: SceneManager has Controller and Love2D callbacks --- main.lua | 8 ++++---- not/SceneManager.lua | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/main.lua b/main.lua index 74c3571..869cac8 100644 --- a/main.lua +++ b/main.lua @@ -46,7 +46,7 @@ function love.load () end function love.draw () - sceneManager:getScene():draw() + sceneManager:draw() if debug then local scale = getScale() love.graphics.setFont(Font) @@ -57,7 +57,7 @@ function love.draw () end end -function love.update (dt) sceneManager:getScene():update(dt) end +function love.update (dt) sceneManager:update(dt) end function love.quit () Settings.save() end -- Pass input to Controller @@ -69,12 +69,12 @@ function love.keyreleased (key) Controller.keyreleased(key) end -- Controller callbacks function Controller.controlpressed (set, action, key) - sceneManager:getScene():controlpressed(set, action, key) + sceneManager:controlpressed(set, action, key) if key == "f5" then debug = not debug end end function Controller.controlreleased (set, action, key) - sceneManager:getScene():controlreleased(set, action, key) + sceneManager:controlreleased(set, action, key) end diff --git a/not/SceneManager.lua b/not/SceneManager.lua index 367a4c0..2e429c2 100644 --- a/not/SceneManager.lua +++ b/not/SceneManager.lua @@ -14,4 +14,20 @@ function SceneManager:getScene () return self.scene end +function SceneManager:update (dt) + self:getScene():update(dt) +end + +function SceneManager:draw () + self:getScene():draw() +end + +function SceneManager:controlpressed (set, action, key) + self:getScene():controlpressed(set, action, key) +end + +function SceneManager:controlreleased (set, action, key) + self:getScene():controlreleased(set, action, key) +end + return SceneManager -- cgit v1.1 From 6c6e2fe6ec84ec57f6336fa9d6e4136c0e1c9513 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 12 Jul 2017 11:09:05 +0200 Subject: All changeScene calls replaced with proper SceneManager calls --- config/menus/select.lua | 4 ++-- main.lua | 5 ----- not/World.lua | 4 ++-- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/config/menus/select.lua b/config/menus/select.lua index 431e620..037b798 100644 --- a/config/menus/select.lua +++ b/config/menus/select.lua @@ -36,7 +36,7 @@ return { :set("active", function (self) local nauts = naut_Selector:getFullSelection(false) if #nauts > 1 then - changeScene(World(MAP, nauts)) + sceneManager:changeScene(World(MAP, nauts)) end end) , @@ -73,4 +73,4 @@ return { end end) , -} \ No newline at end of file +} diff --git a/main.lua b/main.lua index 869cac8..ac04383 100644 --- a/main.lua +++ b/main.lua @@ -15,11 +15,6 @@ end musicPlayer = require "not.MusicPlayer"() sceneManager = require "not.SceneManager"() --- TODO: This is a temporary wrapper! Remove all use cases of `changeScene()`. -function changeScene (scene) - sceneManager:changeScene(scene) -end - -- Require require "iconsList" require "not.World" diff --git a/not/World.lua b/not/World.lua index be29557..8108a8a 100644 --- a/not/World.lua +++ b/not/World.lua @@ -195,7 +195,7 @@ function World:onNautKilled (naut) self:createRay(naut) local nauts = self:getNautsPlayable() if self.lastNaut then - changeScene(Menu()) + sceneManager:changeScene(Menu()) elseif #nauts < 2 then self.lastNaut = true naut:playSound(5, true) @@ -391,7 +391,7 @@ function World:controlpressed (set, action, key) table.insert(nauts, {naut.name, naut:getControllerSet()}) end local new = World(map, nauts) - changeScene(new) + sceneManager:changeScene(new) end for k,naut in pairs(self:getNautsAll()) do naut:controlpressed(set, action, key) -- cgit v1.1 From 9af261cd204b999e6b2b5a44d5c5eb768f84c49d Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 12 Jul 2017 16:00:51 +0200 Subject: Created Scene class extended SceneManager a little bit to support multiple scenes --- not/Menu.lua | 2 +- not/Scene.lua | 32 ++++++++++++++++++++++++++++++++ not/SceneManager.lua | 44 ++++++++++++++++++++++++++++++++++---------- not/World.lua | 4 +--- 4 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 not/Scene.lua diff --git a/not/Menu.lua b/not/Menu.lua index 1688166..ea47993 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -1,7 +1,7 @@ --- `Menu` -- It creates single screen of a menu -- I do know that model I used here and in `World` loading configuration files is not flawless but I did not want to rewrite `World`s one but wanted to keep things similar at least in project scope. -Menu = require "not.Object":extends() +Menu = require "not.Scene":extends() Menu.scale = getScale() Menu.elements = --[[{not.Element}]]nil diff --git a/not/Scene.lua b/not/Scene.lua new file mode 100644 index 0000000..c770fe4 --- /dev/null +++ b/not/Scene.lua @@ -0,0 +1,32 @@ +--- `Scene` +Scene = require "not.Object":extends() + +function Scene:new () + self.sleeping = false + self.hidden = false + self.inputDisabled = false +end + +-- Following setters and getters are a little bit too much, I think. But they do follow general coding directions. +function Scene:setSleeping (sleeping) + self.sleeping = sleeping +end +function Scene:isSleeping () + return self.sleeping +end + +function Scene:setHidden (hidden) + self.hidden = hidden +end +function Scene:isHidden () + return self.hidden +end + +function Scene:setInputDisabled (inputDisabled) + self.inputDisabled = inputDisabled +end +function Scene:isInputDisabled () + return self.inputDisabled +end + +return Scene diff --git a/not/SceneManager.lua b/not/SceneManager.lua index 2e429c2..c9a8e20 100644 --- a/not/SceneManager.lua +++ b/not/SceneManager.lua @@ -3,31 +3,55 @@ -- TODO: Extend functionality for more than one active scene (eg. overlay menu). SceneManager = require "not.Object":extends() +function SceneManager:new () + self.scenes = {} +end + +-- This function should be removed when multiple scenes will be handled properly by SceneManager and other things. function SceneManager:changeScene (scene) - if self.scene ~= nil then - self.scene:delete() - end - self.scene = scene + table.remove(self.scenes, #self.scenes) + return self:addScene(scene) +end + +function SceneManager:addScene (scene) + table.insert(self.scenes, scene) + return scene end -function SceneManager:getScene () - return self.scene +function SceneManager:getAllScenes () + return self.scenes end function SceneManager:update (dt) - self:getScene():update(dt) + for _,scene in pairs(self:getAllScenes()) do + if not scene:isSleeping() then + scene:update(dt) + end + end end function SceneManager:draw () - self:getScene():draw() + for _,scene in pairs(self:getAllScenes()) do + if not scene:isHidden() then + scene:draw() + end + end end function SceneManager:controlpressed (set, action, key) - self:getScene():controlpressed(set, action, key) + for _,scene in pairs(self:getAllScenes()) do + if not scene:isInputDisabled() then + scene:controlpressed(set, action, key) + end + end end function SceneManager:controlreleased (set, action, key) - self:getScene():controlreleased(set, action, key) + for _,scene in pairs(self:getAllScenes()) do + if not scene:isInputDisabled() then + scene:controlreleased(set, action, key) + end + end end return SceneManager diff --git a/not/World.lua b/not/World.lua index 8108a8a..65ca59e 100644 --- a/not/World.lua +++ b/not/World.lua @@ -1,9 +1,7 @@ -require "not.Object" - --- `World` -- Used to manage physical world and everything inside it: clouds, platforms, nauts, background etc. -- TODO: Possibly move common parts of `World` and `Menu` to abstract class `Scene`. -World = Object:extends() +World = require "not.Scene":extends() World.world =--[[love.physics.newWorld]]nil World.Nauts =--[[{not.Hero}]]nil -- cgit v1.1 From 96fef88f56fdcdf95bc5783eb2b3b881ff435ba0 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 14 Jul 2017 21:54:46 +0200 Subject: Background in menu moved to separate class Weird noise added to menu configs to use single MenuBackground instance Initial pause menu added --- config/menus/credits.lua | 9 +++++++-- config/menus/host.lua | 9 +++++++-- config/menus/main.lua | 9 +++++++-- config/menus/pause.lua | 19 ++++++++++++++++++ config/menus/select.lua | 7 ++++++- config/menus/settings.lua | 9 +++++++-- not/Menu.lua | 35 ++++++--------------------------- not/MenuBackground.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++ not/Scene.lua | 4 ++++ 9 files changed, 112 insertions(+), 38 deletions(-) create mode 100644 config/menus/pause.lua create mode 100644 not/MenuBackground.lua diff --git a/config/menus/credits.lua b/config/menus/credits.lua index 6a2ab50..d59b560 100644 --- a/config/menus/credits.lua +++ b/config/menus/credits.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Element = require "not.Element" @@ -6,7 +6,12 @@ local Element = require "not.Element" local width, height = love.graphics.getWidth()/getRealScale(), love.graphics.getHeight()/getRealScale() local bx = width/2-29 +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, Button(menu) :setText("Go back") :setPosition(bx,144) @@ -21,4 +26,4 @@ return { love.graphics.printf("A game by the Awesomenauts community including:\nSeltzy, PlasmaWisp, ParaDoX, MilkingChicken, Burningdillo, Bronkey and Aki.\n\n04font was used.\n\nBased on a game by Jan Willem Nijman, Paul Veer and Bits_Beats XOXO.\n\nAwesomenauts is property of Ronimo Games.", (x-110)*scale, (y+10)*scale, 220, "left", 0, scale, scale) end) , -} \ No newline at end of file +} diff --git a/config/menus/host.lua b/config/menus/host.lua index fff683c..a180736 100644 --- a/config/menus/host.lua +++ b/config/menus/host.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Selector = require "not.Selector" @@ -11,7 +11,12 @@ local map_Selector = Selector(menu) require "iconsList" local icons, maps = getMapsIconsList() +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, map_Selector :setPosition(width/2, 40) :setSize(80, 42) @@ -42,4 +47,4 @@ return { self.parent:open("main") end) , -} \ No newline at end of file +} diff --git a/config/menus/main.lua b/config/menus/main.lua index d65c57d..85e926f 100644 --- a/config/menus/main.lua +++ b/config/menus/main.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Header = require "not.Header" @@ -9,7 +9,12 @@ local bx = width/2-29 local awesometwo = love.graphics.newImage("assets/two.png") +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, Button(menu) :setText("Start") :setPosition(bx, 80) @@ -56,4 +61,4 @@ return { :setText("Roflnauts") :setPosition(width/2,40) , -} \ No newline at end of file +} diff --git a/config/menus/pause.lua b/config/menus/pause.lua new file mode 100644 index 0000000..74dd043 --- /dev/null +++ b/config/menus/pause.lua @@ -0,0 +1,19 @@ +local menu = ... + +local Button = require "not.Button" + +local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale() +local bx = width/2-29 + +return { + Button(menu) + :setText("Unpause") + :setPosition(bx, height - 36) + :set("active", function () end) + , + Button(menu) + :setText("Exit") + :setPosition(bx, height - 20) + :set("active", function () end) + , +} diff --git a/config/menus/select.lua b/config/menus/select.lua index 037b798..23f9374 100644 --- a/config/menus/select.lua +++ b/config/menus/select.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Selector = require "not.Selector" @@ -13,7 +13,12 @@ local start_Button = Button(menu) require "iconsList" local nautsIcons, nautsList = getNautsIconsList() +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, naut_Selector :setPosition(width/2,60) :setMargin(8) diff --git a/config/menus/settings.lua b/config/menus/settings.lua index 96c12cd..59f9234 100644 --- a/config/menus/settings.lua +++ b/config/menus/settings.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Selector = require "not.Selector" @@ -64,7 +64,12 @@ local controlreleased = function(self, set, action, key) end end +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + local a = { + background, Button(menu) :setText("Keyboard 1") :setPosition(bx,80) @@ -111,4 +116,4 @@ local a = { dimmer } -return a \ No newline at end of file +return a diff --git a/not/Menu.lua b/not/Menu.lua index ea47993..a837045 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -8,12 +8,6 @@ Menu.elements = --[[{not.Element}]]nil Menu.active = 1 Menu.music = --[[not.Music]]nil Menu.sprite = --[[love.graphics.newImage]]nil -Menu.background = --[[love.graphics.newImage]]nil -Menu.asteroids = --[[love.graphics.newImage]]nil -Menu.stars = --[[love.graphics.newImage]]nil -Menu.asteroids_bounce = 0 -Menu.stars_frame = 1 -Menu.stars_delay = 0.8 Menu.allowMove = true Menu.quads = { -- TODO: Could be moved to config file or perhaps QuadManager to manage all quads for animations etc. button = { @@ -30,22 +24,16 @@ Menu.quads = { -- TODO: Could be moved to config file or perhaps QuadManager to }, arrow_l = love.graphics.newQuad(68, 0, 6, 6, 80,130), arrow_r = love.graphics.newQuad(74, 0, 6, 6, 80,130), - stars = { - love.graphics.newQuad( 0, 0, 320, 200, 640,200), - love.graphics.newQuad(320, 0, 320, 200, 640,200) - }, } function Menu:new (name) -- Load statically. if Menu.sprite == nil then Menu.sprite = love.graphics.newImage("assets/menu.png") - Menu.background = love.graphics.newImage("assets/backgrounds/menu.png") - Menu.asteroids = love.graphics.newImage("assets/asteroids.png") - Menu.stars = love.graphics.newImage("assets/stars.png") end musicPlayer:setTrack("menu.ogg") musicPlayer:play() + self.elements = {} self:open(name) end @@ -54,8 +42,11 @@ function Menu:delete () end function Menu:open (name) local name = name or "main" self.active = Menu.active --Menu.active is initial - self.elements = love.filesystem.load(string.format("config/menus/%s.lua", name))(self) - self.elements[self.active]:focus() + self.elements = love.filesystem.load(string.format("config/menus/%s.lua", name))(self, self.elements[1]) + -- Common with `next` method. + if not self.elements[self.active]:focus() then + self:next() + end end -- Return reference to quads table and menu sprite @@ -88,24 +79,10 @@ function Menu:update (dt) for _,element in pairs(self.elements) do element:update(dt) end - self.asteroids_bounce = self.asteroids_bounce + dt*0.1 - if self.asteroids_bounce > 2 then self.asteroids_bounce = self.asteroids_bounce - 2 end - self.stars_delay = self.stars_delay - dt - if self.stars_delay < 0 then - self.stars_delay = self.stars_delay + Menu.stars_delay --Menu.stars_delay is initial - if self.stars_frame == 2 then - self.stars_frame = 1 - else - self.stars_frame = 2 - end - end end function Menu:draw () local scale = self.scale local scaler = getRealScale() - love.graphics.draw(self.background, 0, 0, 0, scaler, scaler) - love.graphics.draw(self.stars, self.quads.stars[self.stars_frame], 0, 0, 0, scaler, scaler) - love.graphics.draw(self.asteroids, 0, math.floor(64+math.sin(self.asteroids_bounce*math.pi)*4)*scaler, 0, scaler, scaler) love.graphics.setFont(Font) for _,element in pairs(self.elements) do element:draw(scale) diff --git a/not/MenuBackground.lua b/not/MenuBackground.lua new file mode 100644 index 0000000..83b409c --- /dev/null +++ b/not/MenuBackground.lua @@ -0,0 +1,49 @@ +--- `MenuBackground` +-- Represented as space background with blinking stars and moving asteroids. +-- It might be too specific, but whatever. It is still better than hardcoded background in `Menu` class. +MenuBackground = require "not.Element":extends() + +MenuBackground.BASE_STARS_DELAY = .8 +MenuBackground.QUAD_STARS = { + love.graphics.newQuad( 0, 0, 320, 200, 640,200), + love.graphics.newQuad(320, 0, 320, 200, 640,200) +} + +function MenuBackground:new (parent) + MenuBackground.__super.new(parent) + self.starsFrame = 1 + self.starsDelay = self.BASE_STARS_DELAY + self.asteroidsBounce = 0 + -- Load statically. + if MenuBackground.IMAGE_BACKGROUND == nil then + MenuBackground.IMAGE_BACKGROUND = love.graphics.newImage("assets/backgrounds/menu.png") + MenuBackground.IMAGE_ASTEROIDS = love.graphics.newImage("assets/asteroids.png") + MenuBackground.IMAGE_STARS = love.graphics.newImage("assets/stars.png") + end +end + +function MenuBackground:update (dt) + self.asteroidsBounce = self.asteroidsBounce + dt*0.1 + if self.asteroidsBounce > 2 then + self.asteroidsBounce = self.asteroidsBounce - 2 + end + self.starsDelay = self.starsDelay - dt + if self.starsDelay < 0 then + self.starsDelay = self.starsDelay + self.BASE_STARS_DELAY + if self.starsFrame == 2 then + self.starsFrame = 1 + else + self.starsFrame = 2 + end + end +end + +function MenuBackground:draw () + local scale = self.scale + local scaler = getRealScale() + love.graphics.draw(self.IMAGE_BACKGROUND, 0, 0, 0, scaler, scaler) + love.graphics.draw(self.IMAGE_STARS, self.QUAD_STARS[self.starsFrame], 0, 0, 0, scaler, scaler) + love.graphics.draw(self.IMAGE_ASTEROIDS, 0, math.floor(64+math.sin(self.asteroidsBounce*math.pi)*4)*scaler, 0, scaler, scaler) +end + +return MenuBackground diff --git a/not/Scene.lua b/not/Scene.lua index c770fe4..3324284 100644 --- a/not/Scene.lua +++ b/not/Scene.lua @@ -7,6 +7,10 @@ function Scene:new () self.inputDisabled = false end +function Scene:delete () end +function Scene:update (dt) end +function Scene:draw () end + -- Following setters and getters are a little bit too much, I think. But they do follow general coding directions. function Scene:setSleeping (sleeping) self.sleeping = sleeping -- cgit v1.1 From 64adda89b9c1c53827d56391d5ccd9f0e9a4e93c Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 15 Jul 2017 01:00:06 +0200 Subject: Making new win screen based on Menu and SceneManager --- config/menus/win.lua | 22 ++++++++++++++++++++++ not/Menu.lua | 1 + 2 files changed, 23 insertions(+) create mode 100644 config/menus/win.lua diff --git a/config/menus/win.lua b/config/menus/win.lua new file mode 100644 index 0000000..1d0918d --- /dev/null +++ b/config/menus/win.lua @@ -0,0 +1,22 @@ +local menu = ... + +local Header = require "not.Header" +local Element = require "not.Element" + +local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale() + +return { + Element(menu) + :setPosition(width/2, 18) + :set("draw", function (self, scale) + local x,y = self:getPosition() + love.graphics.setColor(255, 255, 255, 255) + love.graphics.printf("rofl, now kill yourself", x*scale, y*scale, 160, "center", 0, scale, scale, 80, 3) + end) + :set("focus", function () return true end) + , + Header(menu) + :setText("WINNER") + :setPosition(width/2,40) + , +} diff --git a/not/Menu.lua b/not/Menu.lua index a837045..e0e4064 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -31,6 +31,7 @@ function Menu:new (name) if Menu.sprite == nil then Menu.sprite = love.graphics.newImage("assets/menu.png") end + -- musicPlayer calls should be moved to menu files; see issue with new win screen musicPlayer:setTrack("menu.ogg") musicPlayer:play() self.elements = {} -- cgit v1.1 From 8226ffafa0d24a0c6e80d5411ab8dea69dc29094 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 15 Jul 2017 01:12:42 +0200 Subject: Added missing empty functions in Scene --- not/Scene.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/not/Scene.lua b/not/Scene.lua index 3324284..f0e2e34 100644 --- a/not/Scene.lua +++ b/not/Scene.lua @@ -10,6 +10,8 @@ end function Scene:delete () end function Scene:update (dt) end function Scene:draw () end +function Scene:controlpressed (set, action, key) end +function Scene:controlreleased (set, action, key) end -- Following setters and getters are a little bit too much, I think. But they do follow general coding directions. function Scene:setSleeping (sleeping) -- cgit v1.1 From 1aa2ddfd920899e61295d876f3433dcab6dd6ed8 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 15 Jul 2017 01:21:01 +0200 Subject: Win screen is now handled by Scene Issue with MusicPlayer calls in Scene constructor (not solved) --- not/SceneManager.lua | 5 +++++ not/World.lua | 24 ++---------------------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/not/SceneManager.lua b/not/SceneManager.lua index c9a8e20..c076448 100644 --- a/not/SceneManager.lua +++ b/not/SceneManager.lua @@ -18,6 +18,11 @@ function SceneManager:addScene (scene) return scene end +-- Not nice, not nice. +function SceneManager:removeTopScene () + table.remove(self.scenes, #self.scenes) +end + function SceneManager:getAllScenes () return self.scenes end diff --git a/not/World.lua b/not/World.lua index 65ca59e..29c46cc 100644 --- a/not/World.lua +++ b/not/World.lua @@ -16,7 +16,6 @@ World.clouds_delay = 5 World.map =--[[config.maps.*]]nil World.background =--[[image?]]nil World.lastNaut = false -World.win_move = 0 -- "WINNER" require "not.Platform" require "not.Player" @@ -193,18 +192,15 @@ function World:onNautKilled (naut) self:createRay(naut) local nauts = self:getNautsPlayable() if self.lastNaut then + sceneManager:removeTopScene() sceneManager:changeScene(Menu()) elseif #nauts < 2 then self.lastNaut = true naut:playSound(5, true) + sceneManager:addScene(Menu("win")) end end -function World:getBounce (f) - local f = f or 1 - return math.sin(self.win_move*f*math.pi) -end - -- LÖVE2D callbacks -- Update ZU WARUDO function World:update (dt) @@ -250,11 +246,6 @@ function World:update (dt) table.remove(self.Rays, _) end end - -- Bounce `winner` - self.win_move = self.win_move + dt - if self.win_move > 2 then - self.win_move = self.win_move - 2 - end end -- Draw function World:draw () @@ -333,17 +324,6 @@ function World:draw () if _ < 3 then y, e = h-33, 0 end naut:drawHUD(1+(_%2)*(w-34), y, scale, e) end - - -- Draw winner - if self.lastNaut then - local w, h = love.graphics.getWidth()/scale, love.graphics.getHeight()/scale - local angle = self:getBounce(2) - local dy = self:getBounce()*3 - love.graphics.setFont(Bold) - love.graphics.printf("WINNER",(w/2)*scale,(42+dy)*scale,336,"center",(angle*5)*math.pi/180,scale,scale,168,12) - love.graphics.setFont(Font) - love.graphics.printf("rofl, now kill yourself", w/2*scale, 18*scale, 160, "center", 0, scale, scale, 80, 3) - end end -- Box2D callbacks -- cgit v1.1 From 73bca2d56b9a95b6b233da8ac466ba1d1f081c76 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 16 Jul 2017 17:20:03 +0200 Subject: Pause works now MusicPlayer bug still exists for pause and win screens --- config/menus/pause.lua | 24 ++++++++++++++++++++---- not/World.lua | 5 +++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/config/menus/pause.lua b/config/menus/pause.lua index 74dd043..60aa2fc 100644 --- a/config/menus/pause.lua +++ b/config/menus/pause.lua @@ -1,19 +1,35 @@ local menu = ... local Button = require "not.Button" +local Element = require "not.Element" local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale() local bx = width/2-29 return { + Element(menu) + :set("draw", function (self, scale) + love.graphics.setColor(0, 0, 0, 110) + local width, height = love.graphics.getWidth(), love.graphics.getHeight() + love.graphics.rectangle("fill", 0, 0, width, height) + end) + , Button(menu) :setText("Unpause") - :setPosition(bx, height - 36) - :set("active", function () end) + :setPosition(bx, height - 38) + :set("active", function (self) + sceneManager:removeTopScene() + local scene = sceneManager:getAllScenes()[1] + scene:setSleeping(false) + scene:setInputDisabled(false) + end) , Button(menu) :setText("Exit") - :setPosition(bx, height - 20) - :set("active", function () end) + :setPosition(bx, height - 22) + :set("active", function (self) + sceneManager:removeTopScene() + sceneManager:changeScene(Menu("main")) + end) , } diff --git a/not/World.lua b/not/World.lua index 29c46cc..c6b74a3 100644 --- a/not/World.lua +++ b/not/World.lua @@ -371,6 +371,11 @@ function World:controlpressed (set, action, key) local new = World(map, nauts) sceneManager:changeScene(new) end + if key == "escape" then + sceneManager:addScene(Menu("pause")) + self:setInputDisabled(true) + self:setSleeping(true) + end for k,naut in pairs(self:getNautsAll()) do naut:controlpressed(set, action, key) end -- cgit v1.1 From e39c00b6e26504ba0f5fc4f0129bfceb3afe331f Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 16 Jul 2017 17:24:09 +0200 Subject: Fixed MusicPlayer bug with win and pause menus --- config/menus/main.lua | 4 ++++ not/Menu.lua | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/config/menus/main.lua b/config/menus/main.lua index 85e926f..95e44ec 100644 --- a/config/menus/main.lua +++ b/config/menus/main.lua @@ -13,6 +13,10 @@ if background == nil or not background:is(require "not.MenuBackground") then background = require "not.MenuBackground"(menu) end +-- Wait, only here? +musicPlayer:setTrack("menu.ogg") +musicPlayer:play() + return { background, Button(menu) diff --git a/not/Menu.lua b/not/Menu.lua index e0e4064..b835777 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -31,9 +31,6 @@ function Menu:new (name) if Menu.sprite == nil then Menu.sprite = love.graphics.newImage("assets/menu.png") end - -- musicPlayer calls should be moved to menu files; see issue with new win screen - musicPlayer:setTrack("menu.ogg") - musicPlayer:play() self.elements = {} self:open(name) end -- cgit v1.1 From b2c2ef1ae1300a2a2610525bf19db227d6d5e54b Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 16 Jul 2017 20:20:50 +0200 Subject: Music doesn't stop on transition from any-menu to main-menu --- config/menus/main.lua | 6 ++++-- not/MusicPlayer.lua | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config/menus/main.lua b/config/menus/main.lua index 95e44ec..ae2cef2 100644 --- a/config/menus/main.lua +++ b/config/menus/main.lua @@ -14,8 +14,10 @@ if background == nil or not background:is(require "not.MenuBackground") then end -- Wait, only here? -musicPlayer:setTrack("menu.ogg") -musicPlayer:play() +if musicPlayer:getCurrentTrack() ~= "menu.ogg" then + musicPlayer:setTrack("menu.ogg") + musicPlayer:play() +end return { background, diff --git a/not/MusicPlayer.lua b/not/MusicPlayer.lua index e838d96..4634ed9 100644 --- a/not/MusicPlayer.lua +++ b/not/MusicPlayer.lua @@ -32,6 +32,14 @@ function MusicPlayer:setTrack (trackName) end end +function MusicPlayer:getCurrentTrack () + for key,track in pairs(self.tracks) do + if self.tracks[key] == self.source then + return key + end + end +end + function MusicPlayer:play () self.source:play() end -- cgit v1.1 From 0c70843b512c0e1633f77f5b0355cf5c2f80efe5 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 16 Jul 2017 22:10:07 +0200 Subject: Initial trial effect, it's too fun --- not/Effect.lua | 8 ++------ not/Hero.lua | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/not/Effect.lua b/not/Effect.lua index 82d6819..6c0dad0 100644 --- a/not/Effect.lua +++ b/not/Effect.lua @@ -1,11 +1,6 @@ -require "not.Decoration" - --- `Effect` -- Short animation with graphics that plays in various situation. --- TODO: animation is currently slower than it used to be, check if it is ok; if not then make it possible to change it to 0.06 delay. -Effect = Decoration:extends() - -Effect.finished = false +Effect = require "not.Decoration":extends() -- Constructor of `Effect`. function Effect:new (name, x, y, world) @@ -14,6 +9,7 @@ function Effect:new (name, x, y, world) Effect:setImage(Sprite.newImage("assets/effects.png")) end Effect.__super.new(self, x, y, world, nil) + self.finished = false self:setAnimationsList(require("config.animations.effects")) self:setAnimation(name) end diff --git a/not/Hero.lua b/not/Hero.lua index cb205bf..0406e51 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -1,9 +1,7 @@ -require "not.PhysicalBody" - --- `Hero` -- Hero (often referred to as: "naut") entity that exists in a game world. -- Collision category: [2] -Hero = PhysicalBody:extends() +Hero = require "not.PhysicalBody":extends() Hero.name = "empty" Hero.angle = 0 @@ -114,6 +112,12 @@ function Hero:update (dt) self:respawn() end + -- Trail spawner + if self.combo > 100 then + local dx, dy = love.math.random(-4, 4), love.math.random(-4, 4) + self:createEffect("trail", dx, dy) + end + -- # PUNCH -- Cooldown self.punchCooldown = self.punchCooldown - dt @@ -188,14 +192,18 @@ function Hero:goToNextFrame () end -- Spawn `Effect` relative to `Hero` -function Hero:createEffect (name) - if name == "trail" or name == "hit" then - -- 16px effect: -7 -7 - self.world:createEffect(name, self.body:getX()-8, self.body:getY()-8) - elseif name ~= nil then - -- 24px effect: -12 -15 - self.world:createEffect(name, self.body:getX()-12, self.body:getY()-15) +function Hero:createEffect (name, dx, dy) + local x, y = self.body:getX()-8, self.body:getY()-8 -- 16px effect: -7 -7 + if not (name == "trail") and not (name == "hit") then + x, y = x-4, y-7 -- 24px effect: -12 -15 + end + if dx then + x = x + dx + end + if dy then + y = y + dy end + self.world:createEffect(name, x, y) end -- Creates temporary fixture for hero's body that acts as sensor. -- cgit v1.1 From 539faff13d2ca690d94102abf6eef85572e9a287 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 16 Jul 2017 23:09:09 +0200 Subject: Additional condition for trail to appear Wider range for random position --- not/Hero.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index 0406e51..cb4d249 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -113,8 +113,8 @@ function Hero:update (dt) end -- Trail spawner - if self.combo > 100 then - local dx, dy = love.math.random(-4, 4), love.math.random(-4, 4) + if self.combo > 100 and self.punchCooldown > 0 then + local dx, dy = love.math.random(-5, 5), love.math.random(-5, 5) self:createEffect("trail", dx, dy) end -- cgit v1.1 From 25c3cdf7ee06a6415e7bb26418ffb64dda3a2151 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 16 Jul 2017 23:23:49 +0200 Subject: Exploding update and new methods of Hero --- not/Hero.lua | 78 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index cb4d249..b097767 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -37,26 +37,13 @@ Hero.PUNCH_DOWN = {-8,4, -8,20, 8,20, 8,4} -- Constructor of `Hero`. function Hero:new (name, x, y, world) - -- TODO: Statics moved temporarily here. Should be moved to e.g. `load()`. - if Hero.portrait_sprite == nil then - Hero.portrait_sprite = love.graphics.newImage("assets/portraits.png") - Hero.portrait_frame = love.graphics.newImage("assets/menu.png") - end - -- Find imagePath based on hero name. - local fileName = name or Hero.name -- INITIAL - local imagePath = string.format("assets/nauts/%s.png", fileName) - -- `PhysicalBody` initialization. + local imagePath = string.format("assets/nauts/%s.png", name) + Hero.load() Hero.__super.new(self, x, y, world, imagePath) self:setBodyType("dynamic") self:setBodyFixedRotation(true) self.group = -1-#world.Nauts - -- Main fixture initialization. - local fixture = self:addFixture({-5,-8, 5,-8, 5,8, -5,8}, 8) - fixture:setUserData(self) - fixture:setCategory(2) - fixture:setMask(2) - fixture:setGroupIndex(self.group) - -- Actual `Hero` initialization. + self:newFixture() self.world = world self.punchCooldown = 0 self.name = name @@ -64,11 +51,30 @@ function Hero:new (name, x, y, world) self:createEffect("respawn") end +-- TODO: This is temporarily called by constructor. +function Hero.load () + if Hero.portrait_sprite == nil then + Hero.portrait_sprite = love.graphics.newImage("assets/portraits.png") + Hero.portrait_frame = love.graphics.newImage("assets/menu.png") + end +end + +--- Creates hero's fixture and adds it to physical body. +function Hero:newFixture () + local fixture = self:addFixture({-5,-8, 5,-8, 5,8, -5,8}, 8) + fixture:setUserData(self) + fixture:setCategory(2) + fixture:setMask(2) + fixture:setGroupIndex(self.group) +end + -- Update callback of `Hero` function Hero:update (dt) Hero.__super.update(self, dt) - if self.body:isDestroyed() then return end - + if self.body:isDestroyed() then + return + end + self:dampVelocity(dt) -- Salto if self.salto and (self.current == self.animations.walk or self.current == self.animations.default) then self.angle = (self.angle + 17 * dt * self.facing) % 360 @@ -76,23 +82,6 @@ function Hero:update (dt) self.angle = 0 end - -- Custom linear damping. - if not self.isWalking then - local face = nil - local x, y = self:getLinearVelocity() - if x < -12 then - face = 1 - elseif x > 12 then - face = -1 - else - face = 0 - end - self:applyForce(40*face,0) - if not self.inAir then - self:applyForce(80*face,0) - end - end - -- Could you please die? -- TODO: World/Map function for testing if Point is inside playable area. local m = self.world.map @@ -147,6 +136,25 @@ function Hero:update (dt) end end +--- Damps linear velocity every frame by applying minor force to body. +function Hero:dampVelocity (dt) + if not self.isWalking then + local face + local x, y = self:getLinearVelocity() + if x < -12 then + face = 1 + elseif x > 12 then + face = -1 + else + face = 0 + end + self:applyForce(40*face,0) + if not self.inAir then + self:applyForce(80*face,0) + end + end +end + -- TODO: comment them and place them somewhere properly function Hero:getAngle () return self.angle -- cgit v1.1 From 113d648178755dd0fb9f4ea0e7726f173ecc5223 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 16 Jul 2017 23:45:35 +0200 Subject: Cleaning-up Hero and Player, this far w/o big changes --- not/Hero.lua | 32 +++++++++++++++++--------------- not/Player.lua | 4 ++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index b097767..d1621df 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -3,17 +3,7 @@ -- Collision category: [2] Hero = require "not.PhysicalBody":extends() -Hero.name = "empty" -Hero.angle = 0 -Hero.facing = 1 -Hero.max_velocity = 105 -Hero.group = nil -- Combat -Hero.combo = 0 -Hero.lives = 3 -Hero.spawntimer = 2 -Hero.isAlive = true -Hero.punchCooldown = 0.25 Hero.punchdir = 0 -- a really bad thing -- Movement Hero.inAir = true @@ -29,6 +19,9 @@ Hero.portrait_sheet = getNautsIconsList() Hero.portrait_box = love.graphics.newQuad(0, 15, 32,32, 80,130) Hero.sfx = require "config.sounds" +Hero.MAX_VELOCITY = 105 +Hero.RESPAWN_TIME = 2 +Hero.PUNCH_COOLDOWN = 0.25 Hero.PUNCH_FIXTURE_LIFETIME = 0.08 Hero.PUNCH_LEFT = {-2,-6, -20,-6, -20,6, -2,6} Hero.PUNCH_RIGHT = {2,-6, 20,-6, 20,6, 2,6} @@ -40,14 +33,23 @@ function Hero:new (name, x, y, world) local imagePath = string.format("assets/nauts/%s.png", name) Hero.load() Hero.__super.new(self, x, y, world, imagePath) + -- Physics + self.group = -1-#world.Nauts self:setBodyType("dynamic") self:setBodyFixedRotation(true) - self.group = -1-#world.Nauts self:newFixture() + -- General self.world = world - self.punchCooldown = 0 self.name = name + self.lives = 3 + self.angle = 0 + self.facing = 1 + self.combo = 0 + self.punchCooldown = 0 + self.spawntimer = 2 + self.isAlive = true self:setAnimationsList(require("config.animations.hero")) + -- Post-creation self:createEffect("respawn") end @@ -218,7 +220,7 @@ end -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. function Hero:punch (direction) - self.punchCooldown = Hero.punchCooldown + self.punchCooldown = Hero.PUNCH_COOLDOWN -- Choose shape based on punch direction. local shape if direction == "left" then shape = Hero.PUNCH_LEFT end @@ -265,10 +267,10 @@ end -- DIE function Hero:die () self:playSound(1) - self.combo = Hero.combo -- INITIAL from prototype + self.combo = 0 self.lives = self.lives - 1 self.isAlive = false - self.spawntimer = Hero.spawntimer -- INITIAL from prototype + self.spawntimer = Hero.RESPAWN_TIME self:setBodyActive(false) self.world:onNautKilled(self) end diff --git a/not/Player.lua b/not/Player.lua index 34f57eb..8e15da0 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -41,7 +41,7 @@ function Player:update (dt) self.facing = -1 self:applyForce(-250, 0) -- Controlled speed limit - if x < -self.max_velocity then + if x < -self.MAX_VELOCITY then self:applyForce(250, 0) end end @@ -49,7 +49,7 @@ function Player:update (dt) self.facing = 1 self:applyForce(250, 0) -- Controlled speed limit - if x > self.max_velocity then + if x > self.MAX_VELOCITY then self:applyForce(-250, 0) end end -- cgit v1.1 From c668460a615bc5f6a3e3f921f570269b1598bfc9 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 17 Jul 2017 10:36:25 +0200 Subject: More properties moved to constructor in Hero --- not/Hero.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index d1621df..6eb3978 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -6,19 +6,15 @@ Hero = require "not.PhysicalBody":extends() -- Combat Hero.punchdir = 0 -- a really bad thing -- Movement -Hero.inAir = true -Hero.salto = false -Hero.isJumping = false -Hero.isWalking = false Hero.jumpTimer = 0.16 Hero.jumpCounter = 2 -- Statics -Hero.portrait_sprite = nil -Hero.portrait_frame = nil Hero.portrait_sheet = getNautsIconsList() Hero.portrait_box = love.graphics.newQuad(0, 15, 32,32, 80,130) Hero.sfx = require "config.sounds" +Hero.IMAGE_PORTRAITS = nil +Hero.IMAGE_FRAME = nil Hero.MAX_VELOCITY = 105 Hero.RESPAWN_TIME = 2 Hero.PUNCH_COOLDOWN = 0.25 @@ -48,6 +44,10 @@ function Hero:new (name, x, y, world) self.punchCooldown = 0 self.spawntimer = 2 self.isAlive = true + self.inAir = true + self.isJumping = false + self.isWalking = false + self.salto = false self:setAnimationsList(require("config.animations.hero")) -- Post-creation self:createEffect("respawn") @@ -55,9 +55,9 @@ end -- TODO: This is temporarily called by constructor. function Hero.load () - if Hero.portrait_sprite == nil then - Hero.portrait_sprite = love.graphics.newImage("assets/portraits.png") - Hero.portrait_frame = love.graphics.newImage("assets/menu.png") + if Hero.IMAGE_PORTRAITS == nil then + Hero.IMAGE_PORTRAITS = love.graphics.newImage("assets/portraits.png") + Hero.IMAGE_FRAME = love.graphics.newImage("assets/menu.png") end end @@ -180,8 +180,8 @@ function Hero:drawHUD (x,y,scale,elevation) -- hud displays only if player is alive if self.isAlive then love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.portrait_frame, self.portrait_box, (x)*scale, (y)*scale, 0, scale, scale) - love.graphics.draw(self.portrait_sprite, self.portrait_sheet[self.name], (x+2)*scale, (y+3)*scale, 0, scale, scale) + love.graphics.draw(self.IMAGE_FRAME, self.portrait_box, (x)*scale, (y)*scale, 0, scale, scale) + love.graphics.draw(self.IMAGE_PORTRAITS, self.portrait_sheet[self.name], (x+2)*scale, (y+3)*scale, 0, scale, scale) local dy = 30 * elevation love.graphics.setFont(Font) love.graphics.print((self.combo).."%",(x+2)*scale,(y-3+dy)*scale,0,scale,scale) -- cgit v1.1 From 57d6d2def0674c135a659740265ec75cdebae6fa Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 17 Jul 2017 10:39:09 +0200 Subject: Changed order of properties in Hero's constructor --- not/Hero.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index 6eb3978..d92d003 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -37,17 +37,18 @@ function Hero:new (name, x, y, world) -- General self.world = world self.name = name - self.lives = 3 self.angle = 0 self.facing = 1 + -- Status self.combo = 0 - self.punchCooldown = 0 - self.spawntimer = 2 - self.isAlive = true + self.lives = 3 self.inAir = true - self.isJumping = false - self.isWalking = false self.salto = false + self.isAlive = true + self.isWalking = false + self.isJumping = false + self.spawntimer = 2 + self.punchCooldown = 0 self:setAnimationsList(require("config.animations.hero")) -- Post-creation self:createEffect("respawn") -- cgit v1.1 From b1885629faea4ea4c1c0694ef90e4ffef730b0ce Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 17 Jul 2017 10:42:50 +0200 Subject: KYS message is uppercase now --- config/menus/win.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/menus/win.lua b/config/menus/win.lua index 1d0918d..f7163dd 100644 --- a/config/menus/win.lua +++ b/config/menus/win.lua @@ -11,7 +11,7 @@ return { :set("draw", function (self, scale) local x,y = self:getPosition() love.graphics.setColor(255, 255, 255, 255) - love.graphics.printf("rofl, now kill yourself", x*scale, y*scale, 160, "center", 0, scale, scale, 80, 3) + love.graphics.printf("ROFL, NOW KILL YOURSELF", x*scale, y*scale, 160, "center", 0, scale, scale, 80, 3) end) :set("focus", function () return true end) , -- cgit v1.1 From fc43bcb81b5dbfc8ef8dd164193014aa0cf43c9f Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 17 Jul 2017 12:53:00 +0200 Subject: Trail now works mostly as intended --- not/Hero.lua | 7 ++++++- not/Player.lua | 1 + not/World.lua | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/not/Hero.lua b/not/Hero.lua index d92d003..014eeb2 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -44,6 +44,7 @@ function Hero:new (name, x, y, world) self.lives = 3 self.inAir = true self.salto = false + self.smoke = false self.isAlive = true self.isWalking = false self.isJumping = false @@ -105,7 +106,8 @@ function Hero:update (dt) end -- Trail spawner - if self.combo > 100 and self.punchCooldown > 0 then + -- TODO: lower the frequency of spawning - currently it is each frame. + if self.smoke and self.inAir then local dx, dy = love.math.random(-5, 5), love.math.random(-5, 5) self:createEffect("trail", dx, dy) end @@ -263,6 +265,9 @@ function Hero:damage (direction) self.combo = math.min(999, self.combo + 10) self.punchCooldown = 0.08 + self.combo*0.0006 self:playSound(2) + if self.combo > 80 then + self.smoke = true + end end -- DIE diff --git a/not/Player.lua b/not/Player.lua index 8e15da0..11461d3 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -58,6 +58,7 @@ end -- Controller callbacks. function Player:controlpressed (set, action, key) if set ~= self:getControllerSet() then return end + self.smoke = false -- TODO: temporary -- Jumping if action == "jump" then if self.jumpCounter > 0 then diff --git a/not/World.lua b/not/World.lua index c6b74a3..3a5b051 100644 --- a/not/World.lua +++ b/not/World.lua @@ -337,6 +337,7 @@ function World.beginContact (a, b, coll) b:getUserData().inAir = false b:getUserData().jumpCounter = 2 b:getUserData().salto = false + b:getUserData().smoke = false b:getUserData():createEffect("land") end local vx, vy = b:getUserData().body:getLinearVelocity() -- cgit v1.1 From 7ad366dcec68c703499e3f6f72b345d1adeac1da Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 17 Jul 2017 14:15:36 +0200 Subject: Moved landing method to Hero from World --- not/Hero.lua | 9 +++++++++ not/World.lua | 8 +------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index 014eeb2..ff07c36 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -219,6 +219,15 @@ function Hero:createEffect (name, dx, dy) self.world:createEffect(name, x, y) end +-- Called by World when Hero starts contact with Platform (lands). +function Hero:land () + self.inAir = false + self.jumpCounter = 2 + self.salto = false + self.smoke = false + self:createEffect("land") +end + -- Creates temporary fixture for hero's body that acts as sensor. -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. diff --git a/not/World.lua b/not/World.lua index 3a5b051..47a1237 100644 --- a/not/World.lua +++ b/not/World.lua @@ -332,13 +332,7 @@ function World.beginContact (a, b, coll) if a:getCategory() == 1 then local x,y = coll:getNormal() if y < -0.6 then - -- TODO: move landing to `not.Hero` - -- Move them to Hero - b:getUserData().inAir = false - b:getUserData().jumpCounter = 2 - b:getUserData().salto = false - b:getUserData().smoke = false - b:getUserData():createEffect("land") + b:getUserData():land() end local vx, vy = b:getUserData().body:getLinearVelocity() if math.abs(x) == 1 or (y < -0.6 and x == 0) then -- cgit v1.1 From b6aad58259b1e71913280d30e2c9dbf4ecf264c3 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 18 Jul 2017 06:39:05 +0200 Subject: Removed obsolete punchdir and reworked physical animations on punches --- not/Hero.lua | 19 +++++++------------ not/Player.lua | 1 - 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index ff07c36..d9b0636 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -3,8 +3,6 @@ -- Collision category: [2] Hero = require "not.PhysicalBody":extends() --- Combat -Hero.punchdir = 0 -- a really bad thing -- Movement Hero.jumpTimer = 0.16 Hero.jumpCounter = 2 @@ -127,17 +125,14 @@ function Hero:update (dt) end -- Stop vertical - local c,a = self.current, self.animations - if (c == a.attack_up or c == a.attack_down or c == a.attack) and self.frame < c.frames then - if self.punchdir == 0 then - self:setLinearVelocity(0,0) - else - self:setLinearVelocity(38*self.facing,0) + local currentAnimation = self:getAnimation() + if self.frame < currentAnimation.frames then + if currentAnimation == self.animations.attack_up or currentAnimation == self.animations.attack_down then + self:setLinearVelocity(0, 0) + end + if currentAnimation == self.animations.attack then + self:setLinearVelocity(38*self.facing, 0) end - end - - if self.punchCooldown <= 0 and self.punchdir == 1 then - self.punchdir = 0 end end diff --git a/not/Player.lua b/not/Player.lua index 11461d3..b0dac75 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -122,7 +122,6 @@ function Player:controlpressed (set, action, key) else self:punch("left") end - self.punchdir = 1 end end end -- cgit v1.1 From 0774fe93cb125e2b3d37904b2c32f9e490d432f7 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 18 Jul 2017 06:44:16 +0200 Subject: Renamed few more poperties in Hero which are used the same way as statics --- not/Hero.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index d9b0636..729826d 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -3,14 +3,13 @@ -- Collision category: [2] Hero = require "not.PhysicalBody":extends() --- Movement +-- Few are left... Hero.jumpTimer = 0.16 Hero.jumpCounter = 2 --- Statics -Hero.portrait_sheet = getNautsIconsList() -Hero.portrait_box = love.graphics.newQuad(0, 15, 32,32, 80,130) Hero.sfx = require "config.sounds" +Hero.QUAD_PORTRAITS = getNautsIconsList() +Hero.QUAD_FRAME = love.graphics.newQuad(0, 15, 32,32, 80,130) Hero.IMAGE_PORTRAITS = nil Hero.IMAGE_FRAME = nil Hero.MAX_VELOCITY = 105 @@ -178,8 +177,8 @@ function Hero:drawHUD (x,y,scale,elevation) -- hud displays only if player is alive if self.isAlive then love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.IMAGE_FRAME, self.portrait_box, (x)*scale, (y)*scale, 0, scale, scale) - love.graphics.draw(self.IMAGE_PORTRAITS, self.portrait_sheet[self.name], (x+2)*scale, (y+3)*scale, 0, scale, scale) + love.graphics.draw(self.IMAGE_FRAME, self.QUAD_FRAME, (x)*scale, (y)*scale, 0, scale, scale) + love.graphics.draw(self.IMAGE_PORTRAITS, self.QUAD_PORTRAITS[self.name], (x+2)*scale, (y+3)*scale, 0, scale, scale) local dy = 30 * elevation love.graphics.setFont(Font) love.graphics.print((self.combo).."%",(x+2)*scale,(y-3+dy)*scale,0,scale,scale) -- cgit v1.1 From c55bf67aa348c7f16e5fccf2f2153bc90910934c Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 18 Jul 2017 06:54:35 +0200 Subject: Respawning now resets salto and smoke flags in Hero's behaviour --- not/Hero.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/not/Hero.lua b/not/Hero.lua index 729826d..1c15588 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -287,6 +287,8 @@ end -- And then respawn. Like Jon Snow. function Hero:respawn () self.isAlive = true + self.salto = false + self.smoke = false self:setLinearVelocity(0,0) self:setPosition(self.world:getSpawnPosition()) -- TODO: I'm not convinced about getting new position like this. self:setBodyActive(true) -- cgit v1.1 From 9b75ea38f0194dfffc89c0144c1ab0efb8b4f392 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 18 Jul 2017 16:54:14 +0200 Subject: Resized 16px effects frames to 24px --- assets/effects.png | Bin 1440 -> 1456 bytes config/animations/effects.lua | 74 +++++++++++++++++++++--------------------- not/Hero.lua | 5 +-- 3 files changed, 38 insertions(+), 41 deletions(-) diff --git a/assets/effects.png b/assets/effects.png index 90bc456..1a80fc9 100644 Binary files a/assets/effects.png and b/assets/effects.png differ diff --git a/config/animations/effects.lua b/config/animations/effects.lua index dd6d55e..a0c9f47 100644 --- a/config/animations/effects.lua +++ b/config/animations/effects.lua @@ -1,5 +1,5 @@ -- Animations spritesheet array for `Effect` --- Size of sprie atlas is 168px x 120px +-- Size of sprie atlas is 168px x 144px -- NAME :POSITION :SIZE :FRAMES -- jump :x 0 y 0: 24px: 4 @@ -7,70 +7,70 @@ -- land :x 0 y 48: 24px: 5 -- respawn :x 0 y 72: 24px: 7 -- clash :x 0 y 96: 24px: 6 --- trail :x104 y 0: 16px: 4 --- hit :x106 y 18: 16px: 3 +-- trail :x 0 y120: 24px: 4 +-- hit :x 96 y 0: 24px: 3 local quads = { jump = { - [1] = love.graphics.newQuad( 0, 0, 24,24, 168,120), - [2] = love.graphics.newQuad( 24, 0, 24,24, 168,120), - [3] = love.graphics.newQuad( 48, 0, 24,24, 168,120), - [4] = love.graphics.newQuad( 72, 0, 24,24, 168,120), + [1] = love.graphics.newQuad( 0, 0, 24,24, 168,144), + [2] = love.graphics.newQuad( 24, 0, 24,24, 168,144), + [3] = love.graphics.newQuad( 48, 0, 24,24, 168,144), + [4] = love.graphics.newQuad( 72, 0, 24,24, 168,144), frames = 4, repeated = false }, doublejump = { - [1] = love.graphics.newQuad( 0, 24, 24,24, 168,120), - [2] = love.graphics.newQuad( 24, 24, 24,24, 168,120), - [3] = love.graphics.newQuad( 48, 24, 24,24, 168,120), - [4] = love.graphics.newQuad( 72, 24, 24,24, 168,120), + [1] = love.graphics.newQuad( 0, 24, 24,24, 168,144), + [2] = love.graphics.newQuad( 24, 24, 24,24, 168,144), + [3] = love.graphics.newQuad( 48, 24, 24,24, 168,144), + [4] = love.graphics.newQuad( 72, 24, 24,24, 168,144), frames = 4, repeated = false }, land = { - [1] = love.graphics.newQuad( 0, 48, 24,24, 168,120), - [2] = love.graphics.newQuad( 24, 48, 24,24, 168,120), - [3] = love.graphics.newQuad( 48, 48, 24,24, 168,120), - [4] = love.graphics.newQuad( 72, 48, 24,24, 168,120), - [5] = love.graphics.newQuad( 96, 48, 24,24, 168,120), + [1] = love.graphics.newQuad( 0, 48, 24,24, 168,144), + [2] = love.graphics.newQuad( 24, 48, 24,24, 168,144), + [3] = love.graphics.newQuad( 48, 48, 24,24, 168,144), + [4] = love.graphics.newQuad( 72, 48, 24,24, 168,144), + [5] = love.graphics.newQuad( 96, 48, 24,24, 168,144), frames = 5, repeated = false }, respawn = { - [1] = love.graphics.newQuad( 0, 72, 24,24, 168,120), - [2] = love.graphics.newQuad( 24, 72, 24,24, 168,120), - [3] = love.graphics.newQuad( 48, 72, 24,24, 168,120), - [4] = love.graphics.newQuad( 72, 72, 24,24, 168,120), - [5] = love.graphics.newQuad( 96, 72, 24,24, 168,120), - [6] = love.graphics.newQuad(120, 72, 24,24, 168,120), - [7] = love.graphics.newQuad(144, 72, 24,24, 168,120), + [1] = love.graphics.newQuad( 0, 72, 24,24, 168,144), + [2] = love.graphics.newQuad( 24, 72, 24,24, 168,144), + [3] = love.graphics.newQuad( 48, 72, 24,24, 168,144), + [4] = love.graphics.newQuad( 72, 72, 24,24, 168,144), + [5] = love.graphics.newQuad( 96, 72, 24,24, 168,144), + [6] = love.graphics.newQuad(120, 72, 24,24, 168,144), + [7] = love.graphics.newQuad(144, 72, 24,24, 168,144), frames = 7, repeated = false }, clash = { - [1] = love.graphics.newQuad( 0, 96, 24,24, 168,120), - [2] = love.graphics.newQuad( 24, 96, 24,24, 168,120), - [3] = love.graphics.newQuad( 48, 96, 24,24, 168,120), - [4] = love.graphics.newQuad( 72, 96, 24,24, 168,120), - [5] = love.graphics.newQuad( 96, 96, 24,24, 168,120), - [6] = love.graphics.newQuad(120, 96, 24,24, 168,120), + [1] = love.graphics.newQuad( 0, 96, 24,24, 168,144), + [2] = love.graphics.newQuad( 24, 96, 24,24, 168,144), + [3] = love.graphics.newQuad( 48, 96, 24,24, 168,144), + [4] = love.graphics.newQuad( 72, 96, 24,24, 168,144), + [5] = love.graphics.newQuad( 96, 96, 24,24, 168,144), + [6] = love.graphics.newQuad(120, 96, 24,24, 168,144), frames = 6, repeated = false }, trail = { - [1] = love.graphics.newQuad(104, 0, 16,16, 168,120), - [2] = love.graphics.newQuad(120, 0, 16,16, 168,120), - [3] = love.graphics.newQuad(136, 0, 16,16, 168,120), - [4] = love.graphics.newQuad(152, 0, 16,16, 168,120), + [1] = love.graphics.newQuad( 0,120, 24,24, 168,144), + [2] = love.graphics.newQuad( 24,120, 24,24, 168,144), + [3] = love.graphics.newQuad( 48,120, 24,24, 168,144), + [4] = love.graphics.newQuad( 72,120, 24,24, 168,144), frames = 4, repeated = false }, hit = { - [1] = love.graphics.newQuad(106, 18, 16,16, 168,120), - [2] = love.graphics.newQuad(122, 18, 16,16, 168,120), - [3] = love.graphics.newQuad(138, 18, 16,16, 168,120), + [1] = love.graphics.newQuad( 96, 0, 24,24, 168,144), + [2] = love.graphics.newQuad(120, 0, 24,24, 168,144), + [3] = love.graphics.newQuad(144, 0, 24,24, 168,144), frames = 3, repeated = false } } -return quads \ No newline at end of file +return quads diff --git a/not/Hero.lua b/not/Hero.lua index 1c15588..13ab3dd 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -200,10 +200,7 @@ end -- Spawn `Effect` relative to `Hero` function Hero:createEffect (name, dx, dy) - local x, y = self.body:getX()-8, self.body:getY()-8 -- 16px effect: -7 -7 - if not (name == "trail") and not (name == "hit") then - x, y = x-4, y-7 -- 24px effect: -12 -15 - end + local x, y = self.body:getX()-12, self.body:getY()-15 if dx then x = x + dx end -- cgit v1.1 From 657eb912abc89a71d16ea60516458ad6c72f6a4e Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 19 Jul 2017 14:50:25 +0200 Subject: Initial clash No additional effect, rusty mechanics for now --- not/Hero.lua | 2 +- not/PhysicalBody.lua | 1 + not/World.lua | 16 ++++++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index 13ab3dd..5c3d9f4 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -234,7 +234,7 @@ function Hero:punch (direction) local fixture = self:addFixture(shape, 0) fixture:setSensor(true) fixture:setCategory(3) - fixture:setMask(1,3) + fixture:setMask(1) fixture:setGroupIndex(self.group) fixture:setUserData({Hero.PUNCH_FIXTURE_LIFETIME, direction}) self:playSound(4) diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index fd92f89..804c706 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -11,6 +11,7 @@ PhysicalBody.body =--[[love.physics.newBody]]nil function PhysicalBody:new (x, y, world, imagePath) PhysicalBody.__super.new(self, world, imagePath) self.body = love.physics.newBody(world.world, x, y) + self.body:setUserData(self) end -- Add new fixture to body. diff --git a/not/World.lua b/not/World.lua index 47a1237..92b71ea 100644 --- a/not/World.lua +++ b/not/World.lua @@ -329,6 +329,10 @@ end -- Box2D callbacks -- beginContact function World.beginContact (a, b, coll) + -- TODO: Stop using magical numbers: + -- [1] -> Platform + -- [2] -> Hero + -- [3] -> Punch sensor if a:getCategory() == 1 then local x,y = coll:getNormal() if y < -0.6 then @@ -340,10 +344,18 @@ function World.beginContact (a, b, coll) end end if a:getCategory() == 3 then - b:getUserData():damage(a:getUserData()[2]) + if b:getCategory() == 2 then + b:getUserData():damage(a:getUserData()[2]) + end + if b:getCategory() == 3 then + a:getBody():getUserData():damage(b:getUserData()[2]) + b:getBody():getUserData():damage(a:getUserData()[2]) + end end if b:getCategory() == 3 then - a:getUserData():damage(b:getUserData()[2]) + if a:getCategory() == 2 then + a:getUserData():damage(b:getUserData()[2]) + end end end -- endContact -- cgit v1.1 From 53e8d75edc401d613a1fee9795532cc6d98f7132 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 19 Jul 2017 15:12:10 +0200 Subject: Added clash effect for clashes --- not/World.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/not/World.lua b/not/World.lua index 92b71ea..992eef8 100644 --- a/not/World.lua +++ b/not/World.lua @@ -350,6 +350,11 @@ function World.beginContact (a, b, coll) if b:getCategory() == 3 then a:getBody():getUserData():damage(b:getUserData()[2]) b:getBody():getUserData():damage(a:getUserData()[2]) + local x1,y1 = b:getBody():getUserData():getPosition() + local x2,y2 = a:getBody():getUserData():getPosition() + local x = (x2 - x1) / 2 + x1 - 12 + local y = (y2 - y1) / 2 + y1 - 15 + a:getBody():getUserData().world:createEffect("clash", x, y) end end if b:getCategory() == 3 then -- cgit v1.1 From ca6b363b70117198266022316a4ebfc9c8d1cda8 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 19 Jul 2017 15:14:32 +0200 Subject: Cleaned-up Contact functions --- not/World.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/not/World.lua b/not/World.lua index 992eef8..86b1560 100644 --- a/not/World.lua +++ b/not/World.lua @@ -327,12 +327,13 @@ function World:draw () end -- Box2D callbacks --- beginContact +-- TODO: Rather than here, these contacts should be in `Hero` (most likely). +-- TODO: Explode these into more functions.\ +-- TODO: Stop using magical numbers: +-- [1] -> Platform +-- [2] -> Hero +-- [3] -> Punch sensor function World.beginContact (a, b, coll) - -- TODO: Stop using magical numbers: - -- [1] -> Platform - -- [2] -> Hero - -- [3] -> Punch sensor if a:getCategory() == 1 then local x,y = coll:getNormal() if y < -0.6 then @@ -363,10 +364,8 @@ function World.beginContact (a, b, coll) end end end --- endContact function World.endContact (a, b, coll) if a:getCategory() == 1 then - -- Move them to Hero b:getUserData().inAir = true end end -- cgit v1.1 From cc352d320bf87f2d9e4ca77497fb452958bab231 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 20 Jul 2017 16:18:30 +0200 Subject: Added initial name tags above Heroes --- not/Hero.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/not/Hero.lua b/not/Hero.lua index 5c3d9f4..e8fa206 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -169,6 +169,9 @@ end function Hero:draw (offset_x, offset_y, scale, debug) if not self.isAlive then return end Hero.__super.draw(self, offset_x, offset_y, scale, debug) + love.graphics.setFont(Font) + local x,y = self:getPosition() + love.graphics.printf(string.format("Player %d", math.abs(self.group)), (math.floor(x)+offset_x)*scale, (math.floor(y)+offset_y-26)*scale,100,'center',0,scale,scale,50,0) end -- Draw HUD of `Hero` -- cgit v1.1 From 02aba07e03465205b45c41df7aec6894d4e89909 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 20 Jul 2017 19:51:30 +0200 Subject: Moved name-tag drawing to separate function; moved up overall in drawing functions --- not/Hero.lua | 5 ++++- not/World.lua | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/not/Hero.lua b/not/Hero.lua index e8fa206..039aeb8 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -169,8 +169,11 @@ end function Hero:draw (offset_x, offset_y, scale, debug) if not self.isAlive then return end Hero.__super.draw(self, offset_x, offset_y, scale, debug) - love.graphics.setFont(Font) +end + +function Hero:drawTag (offset_x, offset_y, scale) local x,y = self:getPosition() + love.graphics.setFont(Font) love.graphics.printf(string.format("Player %d", math.abs(self.group)), (math.floor(x)+offset_x)*scale, (math.floor(y)+offset_y-26)*scale,100,'center',0,scale,scale,50,0) end diff --git a/not/World.lua b/not/World.lua index 86b1560..7317617 100644 --- a/not/World.lua +++ b/not/World.lua @@ -315,6 +315,10 @@ function World:draw () love.graphics.line(x1,y1,x2,y2) end + for _,naut in pairs(self.Nauts) do + naut:drawTag(offset_x, offset_y, scale) + end + -- Draw HUDs for _,naut in pairs(self.Nauts) do -- I have no idea where to place them T_T -- cgit v1.1 From 29310cc9a4b29b4f758fb23d6cd963e2caa549b3 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 31 Jul 2017 19:31:17 +0200 Subject: New settings format that allows something else than controller sets --- not/Settings.lua | 29 ++++++++++++++++++++--------- settings.default | 11 +++++++---- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/not/Settings.lua b/not/Settings.lua index e3316f9..ff28e6a 100644 --- a/not/Settings.lua +++ b/not/Settings.lua @@ -4,7 +4,7 @@ Settings = { current = {} } -function Settings.load() +function Settings.load () if Controller then if not love.filesystem.exists("settings") then local def = love.filesystem.newFile("settings.default") @@ -15,9 +15,12 @@ function Settings.load() end local getSettings = love.filesystem.load("settings") Settings.current = getSettings() + if not Settings.current.sets then + Settings.current = Settings.convertToNew() + end Controller.reset() local joysticksList = love.joystick.getJoysticks() -- local list for editing - for _,set in pairs(Settings.current) do + for _,set in pairs(Settings.current.sets) do local isJoystick = set[7] local joystick if isJoystick then @@ -32,12 +35,18 @@ function Settings.load() end end -function Settings.save() +-- Converts from old settings format to the one after `02aba07e03465205b45c41df7aec6894d4e89909`. +function Settings.convertToNew () + local old = Settings.current + return {sets = old, display = "fullscreen"} +end + +function Settings.save () local new = love.filesystem.newFile("settings") - local sets = Settings.current - local string = "return {\n" + local sets = Settings.current.sets + local string = "return {\n\tsets = {\n" for i,set in pairs(sets) do - string = string .. "\t{" + string = string .. "\t\t{" for j,word in pairs(set) do if j ~= 7 then string = string .. "\"" .. word .. "\", " @@ -51,13 +60,15 @@ function Settings.save() end string = string .. "},\n" end - string = string .. "}" + string = string .. "\t},\n" + string = string .. "\tdisplay = \"fullscreen\",\n" + string = string .. "}\n" new:open("w") new:write(string) new:close() end -function Settings.change(n, left, right, up, down, attack, jump, joystick) +function Settings.change (n, left, right, up, down, attack, jump, joystick) local bool if joystick then bool = true @@ -65,7 +76,7 @@ function Settings.change(n, left, right, up, down, attack, jump, joystick) bool = false end -- Save current settings - Settings.current[n] = {left, right, up, down, attack, jump, bool} + Settings.current.sets[n] = {left, right, up, down, attack, jump, bool} Settings.save() -- Load settings Settings.load() diff --git a/settings.default b/settings.default index 6816116..3a5229b 100644 --- a/settings.default +++ b/settings.default @@ -1,6 +1,9 @@ return { - {"left", "right", "up", "down", "return", "rshift", false}, - {"a", "d", "w", "s", "g", "h", false}, - {"axis:leftx-", "axis:leftx+", "axis:lefty-", "axis:lefty+", "a", "b", true}, - {"axis:leftx-", "axis:leftx+", "axis:lefty-", "axis:lefty+", "a", "b", true}, + display = "fullscreen", + sets = { + {"left", "right", "up", "down", "return", "rshift", false}, + {"a", "d", "w", "s", "g", "h", false}, + {"axis:leftx-", "axis:leftx+", "axis:lefty-", "axis:lefty+", "a", "b", true}, + {"axis:leftx-", "axis:leftx+", "axis:lefty-", "axis:lefty+", "a", "b", true}, + }, } -- cgit v1.1 From a5dfe50a4f4dd64ef0f54e1caaef8a2672ff8c76 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 31 Jul 2017 22:17:24 +0200 Subject: For testing purpose - static change of display settings --- conf.lua | 8 ++++---- not/Settings.lua | 55 +++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/conf.lua b/conf.lua index bb1da32..032f41b 100644 --- a/conf.lua +++ b/conf.lua @@ -2,11 +2,11 @@ function love.conf(t) t.title = "Roflnauts 2" t.version = "0.10.2" - -- t.window.width = 320*3 - -- t.window.height = 180*3 + t.window.width = 320 + t.window.height = 180 -- t.window.borderless = true t.identity = "not-nautz" - t.window.fullscreentype = "desktop" - t.window.fullscreen = true + -- t.window.fullscreentype = "desktop" + -- t.window.fullscreen = true t.console = false end diff --git a/not/Settings.lua b/not/Settings.lua index ff28e6a..f2919ea 100644 --- a/not/Settings.lua +++ b/not/Settings.lua @@ -4,22 +4,34 @@ Settings = { current = {} } -function Settings.load () +-- Converts from old settings format to the one after `02aba07e03465205b45c41df7aec6894d4e89909`. +local function convertToNew (old) + return {sets = old, display = "fullscreen"} +end + +local function filePrepare () + if not love.filesystem.exists("settings") then + local def = love.filesystem.newFile("settings.default") + local new = love.filesystem.newFile("settings") + new:open("w") def:open("r") + new:write(def:read()) + new:close() def:close() + end +end + +local function fileLoad () + local getSettings = love.filesystem.load("settings") + local settings = getSettings() + if not settings.sets then + settings = convertToNew(settings) + end + Settings.current = settings +end + +local function controllerLoad () if Controller then - if not love.filesystem.exists("settings") then - local def = love.filesystem.newFile("settings.default") - local new = love.filesystem.newFile("settings") - new:open("w") def:open("r") - new:write(def:read()) - new:close() def:close() - end - local getSettings = love.filesystem.load("settings") - Settings.current = getSettings() - if not Settings.current.sets then - Settings.current = Settings.convertToNew() - end Controller.reset() - local joysticksList = love.joystick.getJoysticks() -- local list for editing + local joysticksList = love.joystick.getJoysticks() for _,set in pairs(Settings.current.sets) do local isJoystick = set[7] local joystick @@ -35,10 +47,17 @@ function Settings.load () end end --- Converts from old settings format to the one after `02aba07e03465205b45c41df7aec6894d4e89909`. -function Settings.convertToNew () - local old = Settings.current - return {sets = old, display = "fullscreen"} +local function displayLoad () + local width, height = 320, 180 + love.window.setFullscreen(false) + love.window.setMode(width*2, height*2) +end + +function Settings.load () + filePrepare() + fileLoad() + controllerLoad() + displayLoad() end function Settings.save () -- cgit v1.1 From 9aa820bf100845da2024bf3af9e802c90b3a6b51 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 31 Jul 2017 22:20:41 +0200 Subject: Fixed scaling issues on display change --- not/Camera.lua | 14 ++++++-------- not/Menu.lua | 3 +-- not/World.lua | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/not/Camera.lua b/not/Camera.lua index 63489f3..01d5962 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -5,8 +5,6 @@ Camera = { y = 0, dest_x = 0, dest_y = 0, - scale = getScale(), - scaler = getRealScale(), shake = 0, timer = 0, delay = 0, @@ -45,7 +43,7 @@ function Camera:getPosition () end function Camera:getPositionScaled () - return self.x*self.scale, self.y*self.scale + return self.x*getScale(), self.y*getScale() end -- Destination @@ -63,7 +61,7 @@ end function Camera:translatePosition (x, y) local x = x or 0 local y = y or 0 - return (x-self.x)*self.scale, (y-self.y)*self.scale + return (x-self.x)*getScale().scale, (y-self.y)*getScale() end function Camera:translatePoints(...) @@ -72,9 +70,9 @@ function Camera:translatePoints(...) local x,y = self:getOffsets() for k,v in pairs(a) do if k%2 == 1 then - table.insert(r, (v + x) * self.scale) + table.insert(r, (v + x) * getScale()) else - table.insert(r, (v + y) * self.scale) + table.insert(r, (v + y) * getScale()) end end return r @@ -120,8 +118,8 @@ function Camera:follow () sum_y = naut_y + sum_y end end - local x = sum_x / i - love.graphics.getWidth()/self.scale/2 - local y = sum_y / i - love.graphics.getHeight()/self.scale/2 + 4*self.scale -- hotfix + local x = sum_x / i - love.graphics.getWidth()/getScale()/2 + local y = sum_y / i - love.graphics.getHeight()/getScale()/2 + 4*getScale() -- hotfix return x,y end diff --git a/not/Menu.lua b/not/Menu.lua index b835777..b7fc7ed 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -3,7 +3,6 @@ -- I do know that model I used here and in `World` loading configuration files is not flawless but I did not want to rewrite `World`s one but wanted to keep things similar at least in project scope. Menu = require "not.Scene":extends() -Menu.scale = getScale() Menu.elements = --[[{not.Element}]]nil Menu.active = 1 Menu.music = --[[not.Music]]nil @@ -79,7 +78,7 @@ function Menu:update (dt) end end function Menu:draw () - local scale = self.scale + local scale = getScale() local scaler = getRealScale() love.graphics.setFont(Font) for _,element in pairs(self.elements) do diff --git a/not/World.lua b/not/World.lua index 7317617..c73a3da 100644 --- a/not/World.lua +++ b/not/World.lua @@ -251,8 +251,8 @@ end function World:draw () -- Camera stuff local offset_x, offset_y = self.camera:getOffsets() - local scale = self.camera.scale - local scaler = self.camera.scaler + local scale = getScale() + local scaler = getRealScale() -- Background love.graphics.draw(self.background, 0, 0, 0, scaler, scaler) -- cgit v1.1 From b5a35c0300d9a3c4b97836a313cc17815f1db75c Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 02:34:36 +0200 Subject: displayLoads now correctly handles fullscreen or windowed modes --- not/Settings.lua | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/not/Settings.lua b/not/Settings.lua index f2919ea..358aa79 100644 --- a/not/Settings.lua +++ b/not/Settings.lua @@ -48,9 +48,21 @@ local function controllerLoad () end local function displayLoad () - local width, height = 320, 180 - love.window.setFullscreen(false) - love.window.setMode(width*2, height*2) + local width, height, flags = love.window.getMode() + if Settings.current.display == "fullscreen" then + if not flags.fullscreen then + love.window.setFullscreen(true, "desktop") + end + else + local scale = tonumber(Settings.current.display) or 1 + local expectedWidth, expectedHeight = 320 * scale, 180 * scale + if flags.fullscreen then + love.window.setFullscreen(false) + end + if width ~= expectedWidth or height ~= expectedHeight then + love.window.setMode(expectedWidth, expectedHeight) + end + end end function Settings.load () -- cgit v1.1 From 264a773d7313cffab491c9fbdb14a14480c8f73f Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 02:35:57 +0200 Subject: Remove joysticks pop-like --- not/Settings.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/not/Settings.lua b/not/Settings.lua index 358aa79..7d5f489 100644 --- a/not/Settings.lua +++ b/not/Settings.lua @@ -36,9 +36,7 @@ local function controllerLoad () local isJoystick = set[7] local joystick if isJoystick then - -- take and remove first joystick from list - joystick = joysticksList[1] - table.remove(joysticksList, 1) + joystick = table.remove(joysticksList, 1) end if not isJoystick or joystick then Controller.registerSet(set[1], set[2], set[3], set[4], set[5], set[6], joystick) -- cgit v1.1 From e44eefe628e27a14ba7523a2951014cde782cf7f Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 02:37:11 +0200 Subject: Saves display settings properly --- not/Settings.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/not/Settings.lua b/not/Settings.lua index 7d5f489..ea4c428 100644 --- a/not/Settings.lua +++ b/not/Settings.lua @@ -90,7 +90,7 @@ function Settings.save () string = string .. "},\n" end string = string .. "\t},\n" - string = string .. "\tdisplay = \"fullscreen\",\n" + string = string .. "\tdisplay = \"" .. Settings.current.display .. "\",\n" string = string .. "}\n" new:open("w") new:write(string) -- cgit v1.1 From 0dbb0a6ef0ee24bd4fbd293cdf40a3f066887bb6 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 03:55:17 +0200 Subject: Settings reload moved to separate method --- not/Settings.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/not/Settings.lua b/not/Settings.lua index ea4c428..ca429eb 100644 --- a/not/Settings.lua +++ b/not/Settings.lua @@ -104,9 +104,11 @@ function Settings.change (n, left, right, up, down, attack, jump, joystick) else bool = false end - -- Save current settings Settings.current.sets[n] = {left, right, up, down, attack, jump, bool} + Settings.reload() +end + +function Settings.reload () Settings.save() - -- Load settings Settings.load() end -- cgit v1.1 From bc625807322a7fc7f16a2c228670c85d292e878f Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 04:09:45 +0200 Subject: Added short input from taking input --- not/Menu.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/not/Menu.lua b/not/Menu.lua index b7fc7ed..f6da354 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -71,11 +71,22 @@ function Menu:previous () end end +-- @Override +function Menu:isInputDisabled () + if self.inputBreakTimer then + return self.inputDisabled or self.inputBreakTimer > 0 + end + return self.inputDisabled +end + -- LÖVE2D callbacks function Menu:update (dt) for _,element in pairs(self.elements) do element:update(dt) end + if self.inputBreakTimer and self.inputBreakTimer > 0 then + self.inputBreakTimer = self.inputBreakTimer - dt + end end function Menu:draw () local scale = getScale() -- cgit v1.1 From a6810abd1c8cc9956cb4b8cc2f00258780eee3a8 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 04:10:11 +0200 Subject: Ingame settings now support changing display modes --- config/menus/settings.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/config/menus/settings.lua b/config/menus/settings.lua index 59f9234..7102bea 100644 --- a/config/menus/settings.lua +++ b/config/menus/settings.lua @@ -68,8 +68,29 @@ if background == nil or not background:is(require "not.MenuBackground") then background = require "not.MenuBackground"(menu) end +local displayTypes = {["fullscreen"] = "fullscreen", ["1"] = "1x", ["2"] = "2x", ["3"] = "3x", ["4"] = "4x", ["5"] = "5x"} +local displayButton = Button(menu) +:set("types", displayTypes) +:setText(displayTypes[Settings.current.display]) +:setPosition(bx,64) +:set("enabled", true) +:set("isEnabled", function (self) return self.enabled end) +:set("active", function (self) + self.parent.inputBreakTimer = 0.2 + if Settings.current.display == "fullscreen" then + Settings.current.display = "1" + elseif Settings.current.display == "5" then + Settings.current.display = "fullscreen" + else + Settings.current.display = tostring(tonumber(Settings.current.display) + 1) + end + self:setText(self.types[Settings.current.display]) + Settings.reload() +end) + local a = { background, + displayButton, Button(menu) :setText("Keyboard 1") :setPosition(bx,80) -- cgit v1.1 From cfeab5b477697061d46a9298d4fa33b3263a7403 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 04:27:22 +0200 Subject: Ray now uses getScale directly --- not/Ray.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/not/Ray.lua b/not/Ray.lua index 8edf51f..16a9bee 100644 --- a/not/Ray.lua +++ b/not/Ray.lua @@ -13,7 +13,7 @@ function Ray:new (naut, world) self.naut = naut self.world = world -- Cavas, this is temporary, I believe. - local scale = self.world.camera.scale + local scale = getScale() local w, h = love.graphics.getWidth(), love.graphics.getHeight() self.canvas = love.graphics.newCanvas(w/scale, h/scale) end -- cgit v1.1 From ed62b573417bdc85bec616f6016846b02de4c906 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 1 Aug 2017 04:30:14 +0200 Subject: Fixed leftover from mass replace --- not/Camera.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/not/Camera.lua b/not/Camera.lua index 01d5962..aa4df5b 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -61,7 +61,7 @@ end function Camera:translatePosition (x, y) local x = x or 0 local y = y or 0 - return (x-self.x)*getScale().scale, (y-self.y)*getScale() + return (x-self.x)*getScale(), (y-self.y)*getScale() end function Camera:translatePoints(...) -- cgit v1.1