From 84278ed41f61c586dbb38dd99c45ee33e2f58c77 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 16 Mar 2017 19:05:50 +0100 Subject: Moved ? -> not.?; Renamed Player -> Hero --- not/Cloud.lua | 63 ++++++++ not/Decoration.lua | 34 ++++ not/Effect.lua | 68 ++++++++ not/Hero.lua | 449 +++++++++++++++++++++++++++++++++++++++++++++++++++++ not/Platform.lua | 73 +++++++++ not/Sprite.lua | 81 ++++++++++ not/World.lua | 424 ++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 1192 insertions(+) create mode 100644 not/Cloud.lua create mode 100644 not/Decoration.lua create mode 100644 not/Effect.lua create mode 100644 not/Hero.lua create mode 100644 not/Platform.lua create mode 100644 not/Sprite.lua create mode 100644 not/World.lua (limited to 'not') diff --git a/not/Cloud.lua b/not/Cloud.lua new file mode 100644 index 0000000..c12e236 --- /dev/null +++ b/not/Cloud.lua @@ -0,0 +1,63 @@ +-- `Cloud` +-- That white thing moving in the background. + +-- WHOLE CODE HAS FLAG OF "need a cleanup" + +-- Metatable of `Cloud` +-- nils initialized in constructor +Cloud = { + x = 0, -- position horizontal + y = 0, -- position vertical + t = 1, -- type (sprite number) + v = 13, -- velocity + sprite = nil, + quads = { + [1] = love.graphics.newQuad( 1, 1, 158,47, 478,49), + [2] = love.graphics.newQuad(160, 1, 158,47, 478,49), + [3] = love.graphics.newQuad(319, 1, 158,47, 478,49) + } +} + +-- Constructor of `Cloud` +function Cloud:new(x, y, t, v) + -- Meta + local o = {} + setmetatable(o, self) + self.__index = self + -- Load spritesheet to metatable if not yet loaded + if self.sprite == nil then + self.sprite = love.graphics.newImage("assets/clouds.png") + end + -- Init + o.x = x or self.x + o.y = y or self.y + o.t = t or self.t + o.v = v or self.v + return o +end + +-- Position +function Cloud:getPosition() + return self.x, self.y +end + +-- Update of `Cloud`, returns x for world to delete cloud after reaching right corner +function Cloud:update(dt) + self.x = self.x + self.v*dt + return self.x +end + +-- Draw `Cloud` +function Cloud:draw(offset_x, offset_y, scale) + -- locals + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local scale = scale or 1 + local x, y = self:getPosition() + -- pixel grid + local draw_x = (math.floor(x) + offset_x) * scale + local draw_y = (math.floor(y) + offset_y) * scale + -- draw + love.graphics.setColor(255,255,255,255) + love.graphics.draw(self.sprite, self.quads[self.t], draw_x, draw_y, 0, scale, scale) +end \ No newline at end of file diff --git a/not/Decoration.lua b/not/Decoration.lua new file mode 100644 index 0000000..7f020e1 --- /dev/null +++ b/not/Decoration.lua @@ -0,0 +1,34 @@ +require "not.Sprite" +Decoration = { + world = nil, + sprite = nil, + x = 0, + y = 0 +} +Decoration.__index = Decoration +setmetatable(Decoration, Sprite) +function Decoration:new(x, y, sprite) + local o = {} + setmetatable(o, self) + o:setImage(love.graphics.newImage(sprite)) + o:setPosition(x,y) + return o +end +function Decoration:setPosition(x, y) + self.x, self.y = x, y +end +function Decoration:getPosition() + return self.x, self.y +end +function Decoration:draw(offset_x, offset_y, scale) + -- locals + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local scale = scale or 1 + local x, y = self:getPosition() + -- pixel grid + local draw_x = (math.floor(x) + offset_x) * scale + local draw_y = (math.floor(y) + offset_y) * scale + -- draw + Sprite.draw(self, draw_x, draw_y, 0, scale, scale) +end \ No newline at end of file diff --git a/not/Effect.lua b/not/Effect.lua new file mode 100644 index 0000000..4e327a1 --- /dev/null +++ b/not/Effect.lua @@ -0,0 +1,68 @@ +-- `Effect` +-- Short animation with graphics that plays in various situation. + +-- Metatable of `Effect` +-- nils initialized in constructor +Effect = { + x = 0, + y = 0, + delay = 0.06, + initial = nil, + frame = 1, + animation = nil, + sprite = nil, + quads = require "effects" +} + +-- Construct of `Effect` +function Effect:new(name, x, y) + -- Meta + local o = {} + setmetatable(o, self) + self.__index = self + -- Load spritesheet to metatable if not yet loaded + if self.sprite == nil then + self.sprite = love.graphics.newImage("assets/effects.png") + end + -- Init + o.initial = o.delay + o.animation = name + o.x = x or self.x + o.y = y or self.y + return o +end + +-- Position +function Effect:getPosition() + return self.x, self.y +end + +-- Animation and return flag for deletion after completion +-- returns true if completed and ready to delete +function Effect:update(dt) + self.delay = self.delay - dt + if self.delay < 0 then + if self.frame < self.quads[self.animation].frames then + self.frame = self.frame + 1 + self.delay = self.delay + self.initial + else + return true -- delete + end + end + return false +end + +-- Draw me with scale and offsets, senpai +function Effect:draw(offset_x, offset_y, scale) + -- locals + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local scale = scale or 1 + local x, y = self:getPosition() + -- pixel grid + local draw_x = (math.floor(x) + offset_x) * scale + local draw_y = (math.floor(y) + offset_y) * scale + -- draw + love.graphics.setColor(255,255,255,255) + love.graphics.draw(self.sprite, self.quads[self.animation][self.frame], draw_x, draw_y, 0, scale, scale) +end \ No newline at end of file diff --git a/not/Hero.lua b/not/Hero.lua new file mode 100644 index 0000000..7ddc724 --- /dev/null +++ b/not/Hero.lua @@ -0,0 +1,449 @@ +-- `Hero` +-- Entity controlled by a player. It has a physical body and a sprite. Can play animations and interact with other instances of the same class. +-- Collision category: [2] + +-- WHOLE CODE HAS FLAG OF "need a cleanup" +require "not.Sprite" + +-- Metatable of `Hero` +-- nils initialized in constructor +Hero = { + -- General and physics + name = "empty", + body = nil, + shape = nil, + fixture = nil, + sprite = nil, + rotate = 0, -- "angle" would sound better + facing = 1, + max_velocity = 105, + world = nil, -- game world + -- Combat + combo = 0, + lives = 3, + spawntimer = 2, + alive = true, + punchcd = 0.25, + punchdir = 0, -- a really bad thing + -- Movement + inAir = true, + salto = false, + jumpactive = false, + jumpdouble = true, + jumptimer = 0.16, + jumpnumber = 2, + -- Keys + controlset = nil, + -- HUD + portrait_sprite = nil, + portrait_frame = nil, + portrait_sheet = require "nautsicons", + portrait_box = love.graphics.newQuad( 0, 15, 32,32, 80,130), + -- Sounds + sfx = require "sounds", + -- Animations table + animations = require "animations" +} +Hero.__index = Hero +setmetatable(Hero, Sprite) + +-- Constructor of `Hero` +function Hero:new (game, world, x, y, name) + -- Meta + local o = {} + setmetatable(o, self) + -- Physics + local group = -1-#game.Nauts + o.body = love.physics.newBody(world, x, y, "dynamic") + o.shape = love.physics.newRectangleShape(10, 16) + o.fixture = love.physics.newFixture(o.body, o.shape, 8) + o.fixture:setUserData(o) + o.fixture:setCategory(2) + o.fixture:setMask(2) + o.fixture:setGroupIndex(group) + o.body:setFixedRotation(true) + -- Misc + o.name = name or "empty" + o:setImage(newImage("assets/nauts/"..o.name..".png")) + o.world = game + o.punchcd = 0 + -- Animation + o.current = o.animations.default + o:createEffect("respawn") + -- Portrait load for first object created + if self.portrait_sprite == nil then + self.portrait_sprite = love.graphics.newImage("assets/portraits.png") + self.portrait_frame = love.graphics.newImage("assets/menu.png") + end + return o +end + +-- Control set managment +function Hero:assignControlSet(set) + self.controlset = set +end +function Hero:getControlSet() + return self.controlset +end + +-- Update callback of `Hero` +function Hero:update(dt) + -- hotfix? for destroyed bodies + if self.body:isDestroyed() then return end + -- locals + local x, y = self.body:getLinearVelocity() + local isDown = Controller.isDown + local controlset = self:getControlSet() + + -- # VERTICAL MOVEMENT + -- Jumping + if self.jumpactive and self.jumptimer > 0 then + self.body:setLinearVelocity(x,-160) + self.jumptimer = self.jumptimer - dt + end + + -- Salto + if self.salto and (self.current == self.animations.walk or self.current == self.animations.default) then + self.rotate = (self.rotate + 17 * dt * self.facing) % 360 + elseif self.rotate ~= 0 then + self.rotate = 0 + end + + -- # HORIZONTAL MOVEMENT + -- Walking + if isDown(controlset, "left") then + self.facing = -1 + self.body:applyForce(-250, 0) + -- Controlled speed limit + if x < -self.max_velocity then + self.body:applyForce(250, 0) + end + end + if isDown(controlset, "right") then + self.facing = 1 + self.body:applyForce(250, 0) + -- Controlled speed limit + if x > self.max_velocity then + self.body:applyForce(-250, 0) + end + end + + -- Custom linear damping + if not isDown(controlset, "left") and + not isDown(controlset, "right") + then + local face = nil + if x < -12 then + face = 1 + elseif x > 12 then + face = -1 + else + face = 0 + end + self.body:applyForce(40*face,0) + if not self.inAir then + self.body:applyForce(80*face,0) + end + end + + Sprite.update(self, dt) + + -- # DEATH + -- We all die in the end. + local m = self.world.map + if (self.body:getX() < m.center_x - m.width*1.5 or self.body:getX() > m.center_x + m.width*1.5 or + self.body:getY() < m.center_y - m.height*1.5 or self.body:getY() > m.center_y + m.height*1.5) and + self.alive + then + self:die() + end + + -- respawn + if self.spawntimer > 0 then + self.spawntimer = self.spawntimer - dt + end + if self.spawntimer <= 0 and not self.alive and self.lives >= 0 then + self:respawn() + end + + -- # PUNCH + -- Cooldown + self.punchcd = self.punchcd - dt + if not self.body:isDestroyed() then -- This is weird + for _,fixture in pairs(self.body:getFixtureList()) do + if fixture:getUserData() ~= self then + fixture:setUserData({fixture:getUserData()[1] - dt, fixture:getUserData()[2]}) + if fixture:getUserData()[1] < 0 then + fixture:destroy() + end + end + end + 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.body:setLinearVelocity(0,0) + else + self.body:setLinearVelocity(38*self.facing,0) + end + end + + if self.punchcd <= 0 and self.punchdir == 1 then + self.punchdir = 0 + end +end + +-- Controller callbacks +function Hero:controlpressed(set, action, key) + if set ~= self:getControlSet() then return end + local isDown = Controller.isDown + local controlset = self:getControlSet() + -- Jumping + if action == "jump" then + if self.jumpnumber > 0 then + -- General jump logics + self.jumpactive = true + --self:playSound(6) + -- Spawn proper effect + if not self.inAir then + self:createEffect("jump") + else + self:createEffect("doublejump") + end + -- Start salto if last jump + if self.jumpnumber == 1 then + self.salto = true + end + -- Animation clear + if (self.current == self.animations.attack) or + (self.current == self.animations.attack_up) or + (self.current == self.animations.attack_down) then + self:setAnimation("default") + end + -- Remove jump + self.jumpnumber = self.jumpnumber - 1 + end + end + + -- Walking + if (action == "left" or action == "right") and + (self.current ~= self.animations.attack) and + (self.current ~= self.animations.attack_up) and + (self.current ~= self.animations.attack_down) then + self:setAnimation("walk") + end + + -- Punching + if action == "attack" and self.punchcd <= 0 then + local f = self.facing + self.salto = false + if isDown(controlset, "up") then + -- Punch up + if self.current ~= self.animations.damage then + self:setAnimation("attack_up") + end + self:hit("up") + elseif isDown(controlset, "down") then + -- Punch down + if self.current ~= self.animations.damage then + self:setAnimation("attack_down") + end + self:hit("down") + else + -- Punch horizontal + if self.current ~= self.animations.damage then + self:setAnimation("attack") + end + if f == 1 then + self:hit("right") + else + self:hit("left") + end + self.punchdir = 1 + end + end +end +function Hero:controlreleased(set, action, key) + if set ~= self:getControlSet() then return end + local isDown = Controller.isDown + local controlset = self:getControlSet() + -- Jumping + if action == "jump" then + self.jumpactive = false + self.jumptimer = Hero.jumptimer -- take initial from metatable + end + -- Walking + if (action == "left" or action == "right") and not + (isDown(controlset, "left") or isDown(controlset, "right")) and + self.current == self.animations.walk + then + self:setAnimation("default") + end +end + +-- Draw of `Hero` +function Hero:draw(offset_x, offset_y, scale, debug) + -- draw only alive + if not self.alive then return end + -- locals + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local scale = scale or 1 + local debug = debug or false + local x, y = self:getPosition() + -- pixel grid ; `approx` selected to prevent floating characters on certain conditions + local approx = math.floor + if (y - math.floor(y)) > 0.5 then approx = math.ceil end + local draw_y = (approx(y) + offset_y) * scale + local draw_x = (math.floor(x) + offset_x) * scale + -- sprite draw + Sprite.draw(self, draw_x, draw_y, self.rotate, self.facing*scale, scale, 12, 15) + -- debug draw + if debug then + for _,fixture in pairs(self.body:getFixtureList()) do + if fixture:getCategory() == 2 then + love.graphics.setColor(137, 255, 0, 120) + else + love.graphics.setColor(137, 0, 255, 40) + end + love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(fixture:getShape():getPoints()))) + end + for _,contact in pairs(self.body:getContactList()) do + love.graphics.setColor(255, 0, 0, 255) + love.graphics.setPointSize(scale) + love.graphics.points(self.world.camera:translatePoints(contact:getPositions())) + end + end +end + +-- getPosition +function Hero:getPosition() + return self.body:getPosition() +end + +-- Draw HUD of `Hero` +-- elevation: 1 bottom, 0 top +function Hero:drawHUD(x,y,scale,elevation) + -- hud displays only if player is alive + if self.alive 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) + local dy = 30 * elevation + love.graphics.setFont(Font) + love.graphics.print((self.combo*10).."%",(x+2)*scale,(y-3+dy)*scale,0,scale,scale) + love.graphics.print(math.max(0, self.lives),(x+24)*scale,(y-3+dy)*scale,0,scale,scale) + end +end + +-- Change animation of `Hero` +-- default, walk, attack, attack_up, attack_down, damage +function Hero:nextFrame() + local isDown = Controller.isDown + local controlset = self:getControlSet() + if self.current.repeated or not (self.frame == self.current.frames) then + self.frame = (self.frame % self.current.frames) + 1 + elseif isDown(controlset, "right") or isDown(controlset, "left") then + -- If nonrepeatable animation is finished and player is walking + self:setAnimation("walk") + elseif self.current == self.animations.damage then + self:setAnimation("default") + end +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) + end +end + +-- Punch of `Hero` +-- direction: left, right, up, down +-- creates temporary fixture for player's body that acts as sensor; fixture is deleted after time set in UserData[1]; deleted by Hero:update(dt) +function Hero:hit(direction) + -- start cooldown + self.punchcd = Hero.punchcd -- INITIAL from metatable + -- actual punch + local fixture + if direction == "left" then + fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-2,-6, -20,-6, -20,6, -2,6), 0) + end + if direction == "right" then + fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(2,-6, 20,-6, 20,6, 2,6), 0) + end + if direction == "up" then + fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-8,-4, -8,-20, 8,-20, 8,-4), 0) + end + if direction == "down" then + fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-8,4, -8,20, 8,20, 8,4), 0) + end + fixture:setSensor(true) + fixture:setCategory(3) + fixture:setMask(1,3) + fixture:setGroupIndex(self.fixture:getGroupIndex()) + fixture:setUserData({0.08, direction}) + -- sound + self:playSound(4) +end + +-- Taking damage of `Hero` by successful hit test +-- currently called from World's startContact +function Hero:damage(direction) + local horizontal, vertical = 0, 0 + if direction == "left" then + horizontal = -1 + end + if direction == "right" then + horizontal = 1 + end + if direction == "up" then + vertical = -1 + end + if direction == "down" then + vertical = 1 + end + self:createEffect("hit") + local x,y = self.body:getLinearVelocity() + self.body:setLinearVelocity(x,0) + self.body:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) + self:setAnimation("damage") + self.combo = math.min(27, self.combo + 1) + self.punchcd = 0.08 + self.combo*0.006 + self:playSound(2) +end + +-- DIE +function Hero:die() + self:playSound(1) + self.combo = Hero.combo -- INITIAL from metatable + self.lives = self.lives - 1 + self.alive = false + self.spawntimer = Hero.spawntimer -- INITIAL from metatable + self.body:setActive(false) + self.world:onNautKilled(self) +end + +-- And then respawn. Like Jon Snow. +function Hero:respawn() + self.alive = true + self.body:setLinearVelocity(0,0) + self.body:setPosition(self.world:getSpawnPosition()) + self.body:setActive(true) + self:createEffect("respawn") + self:playSound(7) +end + +-- Sounds +function Hero:playSound(sfx, force) + if self.alive or force then + local source = love.audio.newSource(self.sfx[sfx]) + source:play() + end +end diff --git a/not/Platform.lua b/not/Platform.lua new file mode 100644 index 0000000..9b7b03c --- /dev/null +++ b/not/Platform.lua @@ -0,0 +1,73 @@ +-- `Platform` +-- Static platform physical object with a sprite. `Players` can walk on it. +-- Collision category: [1] + +-- WHOLE CODE HAS FLAG OF "need a cleanup" +require "not.Sprite" + +-- Metatable of `Platform` +-- nils initialized in constructor +Platform = { + body = nil, + shape = nil, + fixture = nil, + world = nil, +} +Platform.__index = Platform +setmetatable(Platform, Sprite) + +-- Constructor of `Platform` +function Platform:new (game, world, x, y, shape, sprite, animations) + local o = {} + setmetatable(o, self) + o.body = love.physics.newBody(world, x, y) + -- MULTIPLE SHAPES NEED TO BE REWRITED! + o.shape = {} + if type(shape[1]) == "number" then + local poly = love.physics.newPolygonShape(shape) + table.insert(o.shape, poly) + o.fixture = love.physics.newFixture(o.body, poly) + o.fixture:setCategory(1) + o.fixture:setFriction(0.2) + else + for i,v in pairs(shape) do + local poly = love.physics.newPolygonShape(v) + table.insert(o.shape, poly) + local fixture = love.physics.newFixture(o.body, poly) + fixture:setCategory(1) + fixture:setFriction(0.2) + end + end + -- END HERE + o:setImage(love.graphics.newImage(sprite)) + o:setAnimationsList(animations) + o.world = game + return o +end + +-- Position +function Platform:getPosition() + return self.body:getPosition() +end + +-- Draw of `Platform` +function Platform:draw (offset_x, offset_y, scale, debug) + -- locals + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local scale = scale or 1 + local debug = debug or false + local x, y = self:getPosition() + -- pixel grid + local draw_x = (math.floor(x) + offset_x) * scale + local draw_y = (math.floor(y) + offset_y) * scale + -- sprite draw + Sprite.draw(self, draw_x, draw_y, 0, scale, scale) + -- debug draw + if debug then + love.graphics.setColor(255, 69, 0, 140) + for i,v in pairs(self.shape) do + love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(v:getPoints()))) + end + end +end \ No newline at end of file diff --git a/not/Sprite.lua b/not/Sprite.lua new file mode 100644 index 0000000..1cc46f7 --- /dev/null +++ b/not/Sprite.lua @@ -0,0 +1,81 @@ +-- `Sprite` +-- Abstract class for drawable animated entities. + +-- Metatable +Sprite = { + animations--[[table with animations]], + current--[[animations.default]], + image--[[love.graphics.newImage()]], + frame = 1, + delay = .1, +} +Sprite.__index = Sprite + +-- Cleans up reference to image on deletion. +function Sprite:delete() + self.image = nil +end + +-- Sets an Image as a image. +function Sprite:setImage(image) + self.image = image +end +-- Returns current image Image. +function Sprite:getImage() + return self.image +end + +-- Sets new animations list. +function Sprite:setAnimationsList(t) + if t then + self.animations = t + self:setAnimation("default") + end +end + +-- Sets current animation by table key. +function Sprite:setAnimation(animation) + self.frame = 1 + self.delay = Sprite.delay -- INITIAL from metatable + self.current = self.animations[animation] +end +-- Returns current animation table. +function Sprite:getAnimation() + return self.current +end + +-- Get frame quad for drawing. +function Sprite:getQuad() + if self.animations and self.current then + return self.current[self.frame] + end +end + +-- Drawing self to LOVE2D buffer. +-- If there is no Quad, it will draw entire image. +function Sprite:draw(...) + local s, q = self:getImage(), self:getQuad() + if s then + love.graphics.setColor(255,255,255,255) + if q then love.graphics.draw(s, q, ...) + else love.graphics.draw(s, ...) end + end +end +-- Animation updating. +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:nextFrame() + end + end +end +-- Moving to the next frame. +function Sprite:nextFrame() + if self.current.repeated or not (self.frame == self.current.frames) then + self.frame = (self.frame % self.current.frames) + 1 + else + self:setAnimation("default") + end +end \ No newline at end of file diff --git a/not/World.lua b/not/World.lua new file mode 100644 index 0000000..5afc5bf --- /dev/null +++ b/not/World.lua @@ -0,0 +1,424 @@ +-- `World` +-- Used to manage physical world and everything inside it: clouds, platforms, nauts, background etc. + +-- WHOLE CODE HAS FLAG OF "need a cleanup" + +require "not.Platform" +require "not.Hero" +require "not.Cloud" +require "not.Effect" +require "not.Decoration" +require "ray" + +-- Metatable of `World` +-- nils initialized in constructor +World = { + -- inside + world = nil, + Nauts = nil, + Platforms = nil, + Clouds = nil, + Decorations = nil, + Effects = nil, + Rays = nil, + camera = nil, + -- cloud generator + clouds_delay = 5, + -- Map + map = nil, + background = nil, + -- Gameplay status + lastNaut = false, + -- "WINNER" + win_move = 0, + -- Music + music = nil +} + +-- Constructor of `World` ZA WARUDO! +function World:new(map, nauts) + -- Meta + local o = {} + setmetatable(o, self) + self.__index = self + -- Physical world initialization + love.physics.setMeter(64) + o.world = love.physics.newWorld(0, 9.81*64, true) + o.world:setCallbacks(o.beginContact, o.endContact) + -- Empty tables for objects + local n = {} + o.Nauts = n + local p = {} + o.Platforms = {} + local c = {} + o.Clouds = c + local e = {} + o.Effects = e + local d = {} + o.Decorations = d + local r = {} + o.Rays = r + -- Random init + math.randomseed(os.time()) + -- Map + local map = map or "default" + o:loadMap(map) + -- Nauts + o:spawnNauts(nauts) + -- Create camera + o.camera = Camera:new(o) + -- Play music + o.music = Music:new(o.map.theme) + return o +end + +-- The end of the world +function World:delete() + self.world:destroy() + for _,platform in pairs(self.Platforms) do + platform:delete() + end + for _,naut in pairs(self.Nauts) do + naut:delete() + end + self.music:delete() + self = nil +end + +-- Load map from file +function World:loadMap(name) + local name = name or "default" + name = "maps/" .. name .. ".lua" + local map = love.filesystem.load(name) + self.map = map() + -- Platforms + for _,platform in pairs(self.map.platforms) do + self:createPlatform(platform.x, platform.y, platform.shape, platform.sprite, platform.animations) + end + -- Decorations + for _,decoration in pairs(self.map.decorations) do + self:createDecoration(decoration.x, decoration.y, decoration.sprite) + end + -- Background + self.background = love.graphics.newImage(self.map.background) + -- Clouds + if self.map.clouds then + for i=1,6 do + self:randomizeCloud(false) + end + end +end + +-- Spawn all the nauts for the round +function World:spawnNauts(nauts) + for _,naut in pairs(nauts) do + local x,y = self:getSpawnPosition() + local spawn = self:createNaut(x, y, naut[1]) + spawn:assignControlSet(naut[2]) + end +end + +-- Get respawn location +function World:getSpawnPosition() + local n = math.random(1, #self.map.respawns) + return self.map.respawns[n].x, self.map.respawns[n].y +end + +-- Add new platform to the world +function World:createPlatform(x, y, polygon, sprite, animations) + table.insert(self.Platforms, Platform:new(self, self.world, x, y, polygon, sprite, animations)) +end + +-- Add new naut to the world +function World:createNaut(x, y, name) + local naut = Hero:new(self, self.world, x, y, name) + table.insert(self.Nauts, naut) + return naut +end + +-- Add new decoration to the world +function World:createDecoration(x, y, sprite) + table.insert(self.Decorations, Decoration:new(x, y, sprite)) +end + +-- Add new cloud to the world +function World:createCloud(x, y, t, v) + table.insert(self.Clouds, Cloud:new(x, y, t, v)) +end + +-- Randomize Cloud creation +function World:randomizeCloud(outside) + if outside == nil then + outside = true + else + outside = outside + end + local x,y,t,v + local m = self.map + if outside then + x = m.center_x-m.width*1.2+math.random(-50,20) + else + x = 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) + self:createCloud(x, y, t, v) +end + +-- Add an effect behind nauts +function World:createEffect(name, x, y) + table.insert(self.Effects, Effect:new(name, x, y)) +end + +-- Add a ray +function World:createRay(naut) + table.insert(self.Rays, Ray:new(naut, self)) +end + +-- get Nauts functions +-- more than -1 lives +function World:getNautsPlayable() + local nauts = {} + for _,naut in pairs(self.Nauts) do + if naut.lives > -1 then + table.insert(nauts, naut) + end + end + return nauts +end +-- are alive +function World:getNautsAlive() + local nauts = {} + for _,naut in self.Nauts do + if naut.alive then + table.insert(nauts, naut) + end + end + return nauts +end +-- all of them +function World:getNautsAll() + return self.Nauts +end + +-- get Map name +function World:getMapName() + return self.map.name +end + +-- Event: when player is killed +function World:onNautKilled(naut) + self.camera:startShake() + self:createRay(naut) + local nauts = self:getNautsPlayable() + if self.lastNaut then + changeScene(Menu:new()) + elseif #nauts < 2 then + self.lastNaut = true + naut:playSound(5, true) + 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) + -- Physical world + self.world:update(dt) + -- Camera + self.camera:update(dt) + -- Engine world: Nauts, Grounds (kek) and Decorations - all Animateds (top kek) + for _,naut in pairs(self.Nauts) do + naut:update(dt) + end + for _,platform in pairs(self.Platforms) do + platform:update(dt) + end + for _,decoration in pairs(self.Decorations) do + decoration:update(dt) + end + -- Clouds + if self.map.clouds then + -- generator + local n = table.getn(self.Clouds) + self.clouds_delay = self.clouds_delay - dt + if self.clouds_delay < 0 and + n < 18 + then + self:randomizeCloud() + self.clouds_delay = self.clouds_delay + World.clouds_delay -- World.clouds_delay is initial + end + -- movement + for _,cloud in pairs(self.Clouds) do + if cloud:update(dt) > 340 then + table.remove(self.Clouds, _) + end + end + end + -- Effects + for _,effect in pairs(self.Effects) do + if effect:update(dt) then + table.remove(self.Effects, _) + end + end + -- Rays + for _,ray in pairs(self.Rays) do + if ray:update(dt) then + 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() + -- Camera stuff + local offset_x, offset_y = self.camera:getOffsets() + local scale = self.camera.scale + local scaler = self.camera.scaler + + -- Background + love.graphics.draw(self.background, 0, 0, 0, scaler, scaler) + + -- This needs to be reworked! + -- Draw clouds + for _,cloud in pairs(self.Clouds) do + cloud:draw(offset_x, offset_y, scale) + end + + -- Draw decorations + for _,decoration in pairs(self.Decorations) do + decoration:draw(offset_x, offset_y, scale) + end + + -- Draw effects + for _,effect in pairs(self.Effects) do + effect:draw(offset_x,offset_y, scale) + end + + -- Draw player + for _,naut in pairs(self.Nauts) do + naut:draw(offset_x, offset_y, scale, debug) + end + + -- Draw ground + for _,platform in pairs(self.Platforms) do + platform:draw(offset_x, offset_y, scale, debug) + end + + -- Draw rays + for _,ray in pairs(self.Rays) do + ray:draw(offset_x, offset_y, scale) + end + + -- draw center + if debug then + local c = self.camera + local w, h = love.graphics.getWidth(), love.graphics.getHeight() + -- draw map center + love.graphics.setColor(130,130,130) + love.graphics.setLineWidth(1) + love.graphics.setLineStyle("rough") + local cx, cy = c:getPositionScaled() + local x1, y1 = c:translatePosition(self.map.center_x, cy) + local x2, y2 = c:translatePosition(self.map.center_x, cy+h) + love.graphics.line(x1,y1,x2,y2) + local x1, y1 = c:translatePosition(cx, self.map.center_y) + local x2, y2 = c:translatePosition(cx+w, self.map.center_y) + love.graphics.line(x1,y1,x2,y2) + -- draw ox, oy + love.graphics.setColor(200,200,200) + love.graphics.setLineStyle("rough") + local cx, cy = c:getPositionScaled() + local x1, y1 = c:translatePosition(0, cy) + local x2, y2 = c:translatePosition(0, cy+h) + love.graphics.line(x1,y1,x2,y2) + local x1, y1 = c:translatePosition(cx, 0) + local x2, y2 = c:translatePosition(cx+w, 0) + love.graphics.line(x1,y1,x2,y2) + end + + -- Draw HUDs + for _,naut in pairs(self.Nauts) do + -- I have no idea where to place them T_T + -- let's do: bottom-left, bottom-right, top-left, top-right + local w, h = love.graphics.getWidth()/scale, love.graphics.getHeight()/scale + local y, e = 1, 1 + 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 +-- beginContact +function World.beginContact(a, b, coll) + if a:getCategory() == 1 then + local x,y = coll:getNormal() + if y < -0.6 then + print(b:getUserData().name .. " is not in air") + -- Move them to Hero + b:getUserData().inAir = false + b:getUserData().jumpnumber = 2 + b:getUserData().salto = false + b:getUserData():createEffect("land") + end + local vx, vy = b:getUserData().body:getLinearVelocity() + if math.abs(x) == 1 or (y < -0.6 and x == 0) then + b:getUserData():playSound(3) + end + end + if a:getCategory() == 3 then + b:getUserData():damage(a:getUserData()[2]) + end + if b:getCategory() == 3 then + a:getUserData():damage(b:getUserData()[2]) + end +end +-- endContact +function World.endContact(a, b, coll) + if a:getCategory() == 1 then + print(b:getUserData().name .. " is in air") + -- Move them to Hero + b:getUserData().inAir = true + end +end + +-- Controller callbacks +function World:controlpressed(set, action, key) + if key == "f6" and debug then + local map = self:getMapName() + local nauts = {} + for _,naut in pairs(self:getNautsAll()) do + table.insert(nauts, {naut.name, naut:getControlSet()}) + end + local new = World:new(map, nauts) + changeScene(new) + end + for k,naut in pairs(self:getNautsAll()) do + naut:controlpressed(set, action, key) + end +end +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 -- cgit v1.1 From c16c1206f5884614157b8f5049e601ff77478d7f Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 17 Mar 2017 18:35:54 +0100 Subject: Ray renamed and moved --- not/Ray.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ not/World.lua | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 not/Ray.lua (limited to 'not') diff --git a/not/Ray.lua b/not/Ray.lua new file mode 100644 index 0000000..bbe11c1 --- /dev/null +++ b/not/Ray.lua @@ -0,0 +1,52 @@ +-- `Ray` +-- That awesome effect that blinks when player dies! + +-- WHOLE CODE HAS FLAG OF "need a cleanup" + +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 + -- Cavas, this is temporary, I believe. + local scale = o.world.camera.scale + local w, h = love.graphics.getWidth(), love.graphics.getHeight() + o.canvas = love.graphics.newCanvas(w/scale, h/scale) + return o +end +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) + love.graphics.setCanvas(self.canvas) + love.graphics.clear() + love.graphics.setColor(255, 247, 228, 247) + love.graphics.setLineStyle("rough") + love.graphics.setLineWidth(self.delay*160) + local x, y = self.naut:getPosition() + local m = self.world.map + local dy = m.height + if y > m.center_y then + dy = -dy + end + love.graphics.line(-x+offset_x,-y+offset_y-dy*0.7,x+offset_x,y+dy*0.7+offset_y) + -- reset + love.graphics.setCanvas() + love.graphics.setLineWidth(1) + love.graphics.setColor(255,255,255,255) + -- draw on screen + love.graphics.draw(self.canvas, 0, 0, 0, scale, scale) +end diff --git a/not/World.lua b/not/World.lua index 5afc5bf..6d81918 100644 --- a/not/World.lua +++ b/not/World.lua @@ -8,7 +8,7 @@ require "not.Hero" require "not.Cloud" require "not.Effect" require "not.Decoration" -require "ray" +require "not.Ray" -- Metatable of `World` -- nils initialized in constructor -- cgit v1.1 From 340a3a4b92de5495b47e8e1e102178edfd97514f Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 00:21:02 +0100 Subject: Night commit, added PhysicalBody, newImage to sprite --- not/Hero.lua | 19 +++++++-------- not/PhysicalBody.lua | 16 +++++++++++++ not/Sprite.lua | 66 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 not/PhysicalBody.lua (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 7ddc724..8d226ac 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -1,12 +1,6 @@ --- `Hero` --- Entity controlled by a player. It has a physical body and a sprite. Can play animations and interact with other instances of the same class. +--- `Hero` +-- Hero (naut) entity that exists in a game world. -- Collision category: [2] - --- WHOLE CODE HAS FLAG OF "need a cleanup" -require "not.Sprite" - --- Metatable of `Hero` --- nils initialized in constructor Hero = { -- General and physics name = "empty", @@ -44,10 +38,13 @@ Hero = { -- Animations table animations = require "animations" } + +-- `Hero` is a child of `PhysicalBody`. +require "not.PhysicalBody" Hero.__index = Hero -setmetatable(Hero, Sprite) +setmetatable(Hero, PhysicalBody) --- Constructor of `Hero` +-- Constructor of `Hero`. function Hero:new (game, world, x, y, name) -- Meta local o = {} @@ -64,7 +61,7 @@ function Hero:new (game, world, x, y, name) o.body:setFixedRotation(true) -- Misc o.name = name or "empty" - o:setImage(newImage("assets/nauts/"..o.name..".png")) + o:setImage(Sprite.newImage("assets/nauts/"..o.name..".png")) o.world = game o.punchcd = 0 -- Animation diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua new file mode 100644 index 0000000..6e6a8a6 --- /dev/null +++ b/not/PhysicalBody.lua @@ -0,0 +1,16 @@ +--- `PhysicalBody` +-- Abstract class for drawable body existing in Box2D's physical world. +PhysicalBody = { + body =--[[love.physics.newBody]]nil, +} + +-- `PhysicalBody` is a child of `Sprite`. +require "not.Sprite" +PhysicalBody.__index = PhysicalBody +setmetatable(PhysicalBody, Sprite) + +-- Constructor of `PhysicalBody`. +function PhysicalBody:new (world, x, y, imagePath) + local o = Sprite:new(imagePath) + return o +end \ No newline at end of file diff --git a/not/Sprite.lua b/not/Sprite.lua index 1cc46f7..905816b 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -1,32 +1,54 @@ -- `Sprite` -- Abstract class for drawable animated entities. - --- Metatable Sprite = { - animations--[[table with animations]], - current--[[animations.default]], - image--[[love.graphics.newImage()]], + animations =--[[table with animations]]nil, + current =--[[animations.default]]nil, + image =--[[love.graphics.newImage]]nil, frame = 1, delay = .1, } Sprite.__index = Sprite +-- Constructor of `Sprite`. +function Sprite:new (imagePath) + local o = setmetatable({}, self) + if type(imagePath) == "string" then + o:setImage(self.newImage(imagePath)) + end + return o +end + -- Cleans up reference to image on deletion. -function Sprite:delete() +function Sprite:delete () self.image = nil end --- Sets an Image as a image. -function Sprite:setImage(image) +-- Creates new Image object from path. Key-colours two shades of green. Static. +function Sprite.newImage (path) + local imagedata = love.image.newImageData(path) + local transparency = function(x, y, r, g, b, a) + if (r == 0 and g == 128 and b == 64) or + (r == 0 and g == 240 and b == 6) then + a = 0 + end + return r, g, b, a + end + imagedata:mapPixel(transparency) + local image = love.graphics.newImage(imagedata) + return image +end + +-- Sets an Image as an image. +function Sprite:setImage (image) self.image = image end -- Returns current image Image. -function Sprite:getImage() +function Sprite:getImage () return self.image end -- Sets new animations list. -function Sprite:setAnimationsList(t) +function Sprite:setAnimationsList (t) if t then self.animations = t self:setAnimation("default") @@ -34,45 +56,45 @@ function Sprite:setAnimationsList(t) end -- Sets current animation by table key. -function Sprite:setAnimation(animation) +function Sprite:setAnimation (animation) self.frame = 1 self.delay = Sprite.delay -- INITIAL from metatable self.current = self.animations[animation] end -- Returns current animation table. -function Sprite:getAnimation() +function Sprite:getAnimation () return self.current end -- Get frame quad for drawing. -function Sprite:getQuad() +function Sprite:getQuad () if self.animations and self.current then return self.current[self.frame] end end -- Drawing self to LOVE2D buffer. --- If there is no Quad, it will draw entire image. -function Sprite:draw(...) - local s, q = self:getImage(), self:getQuad() - if s then +-- If there is no Quad, it will draw entire image. It won't draw anything if there is no image. +function Sprite:draw (...) + local i, q = self:getImage(), self:getQuad() + if i then love.graphics.setColor(255,255,255,255) - if q then love.graphics.draw(s, q, ...) - else love.graphics.draw(s, ...) end + if q then love.graphics.draw(i, q, ...) + else love.graphics.draw(i, ...) end end end -- Animation updating. -function Sprite:update(dt) +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:nextFrame() + self:goToNextFrame() end end end -- Moving to the next frame. -function Sprite:nextFrame() +function Sprite:goToNextFrame () if self.current.repeated or not (self.frame == self.current.frames) then self.frame = (self.frame % self.current.frames) + 1 else -- cgit v1.1 From b262cb3eec1a797832d168f9e6d144468fb48c1d Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 01:35:43 +0100 Subject: Initializers in PhysicalBody, Hero and Sprite --- not/Hero.lua | 52 +++++++++++++++++++++++++++++----------------------- not/PhysicalBody.lua | 8 +++++++- not/Sprite.lua | 11 ++++++++--- 3 files changed, 44 insertions(+), 27 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 8d226ac..eee7a83 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -36,7 +36,7 @@ Hero = { -- Sounds sfx = require "sounds", -- Animations table - animations = require "animations" + animations = nil } -- `Hero` is a child of `PhysicalBody`. @@ -46,28 +46,9 @@ setmetatable(Hero, PhysicalBody) -- Constructor of `Hero`. function Hero:new (game, world, x, y, name) - -- Meta - local o = {} - setmetatable(o, self) - -- Physics - local group = -1-#game.Nauts - o.body = love.physics.newBody(world, x, y, "dynamic") - o.shape = love.physics.newRectangleShape(10, 16) - o.fixture = love.physics.newFixture(o.body, o.shape, 8) - o.fixture:setUserData(o) - o.fixture:setCategory(2) - o.fixture:setMask(2) - o.fixture:setGroupIndex(group) - o.body:setFixedRotation(true) - -- Misc - o.name = name or "empty" - o:setImage(Sprite.newImage("assets/nauts/"..o.name..".png")) - o.world = game - o.punchcd = 0 - -- Animation - o.current = o.animations.default - o:createEffect("respawn") - -- Portrait load for first object created + 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") @@ -75,6 +56,31 @@ function Hero:new (game, world, x, y, name) return o end +-- Initializator of `Hero`. +function Hero:init (name, world, x, y) + -- Find imagePath basing on hero name and call super initializator. + local fileName = name or Hero.name -- INITIAL from metatable + local imagePath = string.format("assets/nauts/%s.png", fileName) + PhysicalBody.init(self, world, x, y, imagePath) + -- To be removed or heavily changed. + self.world = world + self.punchcd = 0 + -- To be moved to PhysicalBody abstract. + local group = -1-#world.Nauts + self.body = love.physics.newBody(world.world, x, y, "dynamic") + self.shape = love.physics.newRectangleShape(10, 16) + self.fixture = love.physics.newFixture(self.body, self.shape, 8) + self.fixture:setUserData(self) + self.fixture:setCategory(2) + self.fixture:setMask(2) + self.fixture:setGroupIndex(group) + self.body:setFixedRotation(true) + -- Actual `Hero` initialization. + self.name = name + self:setAnimationsList(require("animations")) + self:createEffect("respawn") +end + -- Control set managment function Hero:assignControlSet(set) self.controlset = set diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 6e6a8a6..1f91faf 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -11,6 +11,12 @@ setmetatable(PhysicalBody, Sprite) -- Constructor of `PhysicalBody`. function PhysicalBody:new (world, x, y, imagePath) - local o = Sprite:new(imagePath) + local o = setmetatable({}, self) + o:init(world, x, y, imagePath) return o +end + +-- Initializator of `PhysicalBody`. +function PhysicalBody:init (world, x, y, imagePath) + Sprite.init(self, imagePath) end \ No newline at end of file diff --git a/not/Sprite.lua b/not/Sprite.lua index 905816b..6342f60 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -12,9 +12,7 @@ Sprite.__index = Sprite -- Constructor of `Sprite`. function Sprite:new (imagePath) local o = setmetatable({}, self) - if type(imagePath) == "string" then - o:setImage(self.newImage(imagePath)) - end + o:init(imagePath) return o end @@ -23,6 +21,13 @@ 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) -- cgit v1.1 From 4652d69cc88e3d0137cac118344d21853c6a7605 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 02:45:17 +0100 Subject: Small changes, comments --- not/Hero.lua | 25 +++++++++---------------- not/PhysicalBody.lua | 2 +- not/Sprite.lua | 3 ++- 3 files changed, 12 insertions(+), 18 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index eee7a83..362ebf9 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -4,14 +4,10 @@ Hero = { -- General and physics name = "empty", - body = nil, - shape = nil, - fixture = nil, - sprite = nil, - rotate = 0, -- "angle" would sound better + angle = 0, facing = 1, max_velocity = 105, - world = nil, -- game world + world = --[[not.World]]nil, -- Combat combo = 0, lives = 3, @@ -28,15 +24,12 @@ Hero = { jumpnumber = 2, -- Keys controlset = nil, - -- HUD + -- Statics portrait_sprite = nil, portrait_frame = nil, portrait_sheet = require "nautsicons", portrait_box = love.graphics.newQuad( 0, 15, 32,32, 80,130), - -- Sounds sfx = require "sounds", - -- Animations table - animations = nil } -- `Hero` is a child of `PhysicalBody`. @@ -62,10 +55,10 @@ function Hero:init (name, world, x, y) local fileName = name or Hero.name -- INITIAL from metatable local imagePath = string.format("assets/nauts/%s.png", fileName) PhysicalBody.init(self, world, x, y, imagePath) - -- To be removed or heavily changed. + -- TODO: probably should be removed or heavily changed. self.world = world self.punchcd = 0 - -- To be moved to PhysicalBody abstract. + -- TODO: move following lines to PhysicalBody, cut if not needed, refectorize to subfunctions in target class. local group = -1-#world.Nauts self.body = love.physics.newBody(world.world, x, y, "dynamic") self.shape = love.physics.newRectangleShape(10, 16) @@ -107,9 +100,9 @@ function Hero:update(dt) -- Salto if self.salto and (self.current == self.animations.walk or self.current == self.animations.default) then - self.rotate = (self.rotate + 17 * dt * self.facing) % 360 - elseif self.rotate ~= 0 then - self.rotate = 0 + self.angle = (self.angle + 17 * dt * self.facing) % 360 + elseif self.angle ~= 0 then + self.angle = 0 end -- # HORIZONTAL MOVEMENT @@ -302,7 +295,7 @@ function Hero:draw(offset_x, offset_y, scale, debug) local draw_y = (approx(y) + offset_y) * scale local draw_x = (math.floor(x) + offset_x) * scale -- sprite draw - Sprite.draw(self, draw_x, draw_y, self.rotate, self.facing*scale, scale, 12, 15) + Sprite.draw(self, draw_x, draw_y, self.angle, self.facing*scale, scale, 12, 15) -- debug draw if debug then for _,fixture in pairs(self.body:getFixtureList()) do diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 1f91faf..e726ea3 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -19,4 +19,4 @@ end -- Initializator of `PhysicalBody`. function PhysicalBody:init (world, x, y, imagePath) Sprite.init(self, imagePath) -end \ No newline at end of file +end diff --git a/not/Sprite.lua b/not/Sprite.lua index 6342f60..d4cde59 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -1,4 +1,4 @@ --- `Sprite` +--- `Sprite` -- Abstract class for drawable animated entities. Sprite = { animations =--[[table with animations]]nil, @@ -80,6 +80,7 @@ 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. +-- TODO: it doesn't follow same pattern as `not.Hero.draw`. It should implement so it can be called from `not.World`. function Sprite:draw (...) local i, q = self:getImage(), self:getQuad() if i then -- cgit v1.1 From a8551f5da9549453381a0f2daa0ff4aefd06272f Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 02:50:40 +0100 Subject: Added tempalte for not.Player --- not/Player.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 not/Player.lua (limited to 'not') diff --git a/not/Player.lua b/not/Player.lua new file mode 100644 index 0000000..bcd72bc --- /dev/null +++ b/not/Player.lua @@ -0,0 +1,22 @@ +--- `Player` +-- Special `not.Hero` controllable by a player. +Player = { + -- TODO: move functions and properties related to controls from `not.Hero`. +} + +-- `Player` is a child of `Hero`. +require "not.Hero" +Player.__index = Player +setmetatable(Player, Hero) + +-- Constructor of `Player`. +function Player:new (...) + local o = setmetatable({}, self) + o:init(...) + return o +end + +-- Initializator of `Player`. +function Player:init (...) + Hero.init(self, ...) +end \ No newline at end of file -- cgit v1.1 From b10988b7e8a0c52148b1d3828c3739fb0eeff24e Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 03:09:36 +0100 Subject: No constructors for abstracts --- not/PhysicalBody.lua | 15 +++++++++++++-- not/Sprite.lua | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'not') diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index e726ea3..52e9357 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -1,5 +1,5 @@ --- `PhysicalBody` --- Abstract class for drawable body existing in Box2D's physical world. +-- Abstract class for drawable entity existing in `not.World`. PhysicalBody = { body =--[[love.physics.newBody]]nil, } @@ -9,14 +9,25 @@ require "not.Sprite" PhysicalBody.__index = PhysicalBody setmetatable(PhysicalBody, Sprite) --- Constructor of `PhysicalBody`. +--[[ Constructor of `PhysicalBody`. function PhysicalBody:new (world, x, y, imagePath) local o = setmetatable({}, self) o:init(world, x, y, imagePath) return o end +]] -- Initializator of `PhysicalBody`. function PhysicalBody:init (world, x, y, imagePath) Sprite.init(self, imagePath) end + +-- Update of `PhysicalBody`. +function PhysicalBody:update (dt) + Sprite.update(self, dt) +end + +-- Draw of `PhysicalBody`. +function PhysicalBody:draw (offset_x, offset_y, scale, debug) + -- TODO: Move debug part here from `not.Hero.draw`. +end diff --git a/not/Sprite.lua b/not/Sprite.lua index d4cde59..e9eb387 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -9,12 +9,13 @@ Sprite = { } Sprite.__index = Sprite --- Constructor of `Sprite`. +--[[ Constructor of `Sprite`. function Sprite:new (imagePath) local o = setmetatable({}, self) o:init(imagePath) return o end +]] -- Cleans up reference to image on deletion. function Sprite:delete () @@ -89,6 +90,7 @@ function Sprite:draw (...) else love.graphics.draw(i, ...) end end end + -- Animation updating. function Sprite:update (dt) if self.animations and self.current then @@ -99,6 +101,7 @@ function Sprite:update (dt) end end end + -- Moving to the next frame. function Sprite:goToNextFrame () if self.current.repeated or not (self.frame == self.current.frames) then -- cgit v1.1 From 19e6728c339cb029ab9a6b7762d60abe790b772a Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 03:13:34 +0100 Subject: Spaces! --- not/Hero.lua | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 362ebf9..bd8c55c 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -75,15 +75,15 @@ function Hero:init (name, world, x, y) end -- Control set managment -function Hero:assignControlSet(set) +function Hero:assignControlSet (set) self.controlset = set end -function Hero:getControlSet() +function Hero:getControlSet () return self.controlset end -- Update callback of `Hero` -function Hero:update(dt) +function Hero:update (dt) -- hotfix? for destroyed bodies if self.body:isDestroyed() then return end -- locals @@ -192,7 +192,7 @@ function Hero:update(dt) end -- Controller callbacks -function Hero:controlpressed(set, action, key) +function Hero:controlpressed (set, action, key) if set ~= self:getControlSet() then return end local isDown = Controller.isDown local controlset = self:getControlSet() @@ -261,7 +261,7 @@ function Hero:controlpressed(set, action, key) end end end -function Hero:controlreleased(set, action, key) +function Hero:controlreleased (set, action, key) if set ~= self:getControlSet() then return end local isDown = Controller.isDown local controlset = self:getControlSet() @@ -280,7 +280,7 @@ function Hero:controlreleased(set, action, key) end -- Draw of `Hero` -function Hero:draw(offset_x, offset_y, scale, debug) +function Hero:draw (offset_x, offset_y, scale, debug) -- draw only alive if not self.alive then return end -- locals @@ -315,13 +315,13 @@ function Hero:draw(offset_x, offset_y, scale, debug) end -- getPosition -function Hero:getPosition() +function Hero:getPosition () return self.body:getPosition() end -- Draw HUD of `Hero` -- elevation: 1 bottom, 0 top -function Hero:drawHUD(x,y,scale,elevation) +function Hero:drawHUD (x,y,scale,elevation) -- hud displays only if player is alive if self.alive then love.graphics.setColor(255,255,255,255) @@ -336,7 +336,7 @@ end -- Change animation of `Hero` -- default, walk, attack, attack_up, attack_down, damage -function Hero:nextFrame() +function Hero:nextFrame () local isDown = Controller.isDown local controlset = self:getControlSet() if self.current.repeated or not (self.frame == self.current.frames) then @@ -350,7 +350,7 @@ function Hero:nextFrame() end -- Spawn `Effect` relative to `Hero` -function Hero:createEffect(name) +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) @@ -363,7 +363,7 @@ end -- Punch of `Hero` -- direction: left, right, up, down -- creates temporary fixture for player's body that acts as sensor; fixture is deleted after time set in UserData[1]; deleted by Hero:update(dt) -function Hero:hit(direction) +function Hero:hit (direction) -- start cooldown self.punchcd = Hero.punchcd -- INITIAL from metatable -- actual punch @@ -391,7 +391,7 @@ end -- Taking damage of `Hero` by successful hit test -- currently called from World's startContact -function Hero:damage(direction) +function Hero:damage (direction) local horizontal, vertical = 0, 0 if direction == "left" then horizontal = -1 @@ -416,7 +416,7 @@ function Hero:damage(direction) end -- DIE -function Hero:die() +function Hero:die () self:playSound(1) self.combo = Hero.combo -- INITIAL from metatable self.lives = self.lives - 1 @@ -427,7 +427,7 @@ function Hero:die() end -- And then respawn. Like Jon Snow. -function Hero:respawn() +function Hero:respawn () self.alive = true self.body:setLinearVelocity(0,0) self.body:setPosition(self.world:getSpawnPosition()) @@ -437,7 +437,7 @@ function Hero:respawn() end -- Sounds -function Hero:playSound(sfx, force) +function Hero:playSound (sfx, force) if self.alive or force then local source = love.audio.newSource(self.sfx[sfx]) source:play() -- cgit v1.1 From 2e352657813b37d17c2215b85189f18a50c099f9 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 03:29:32 +0100 Subject: Comments, todos, pretty useless --- not/Cloud.lua | 1 + not/Decoration.lua | 2 ++ not/Effect.lua | 2 ++ not/Hero.lua | 2 ++ not/Platform.lua | 3 +++ not/Sprite.lua | 1 + 6 files changed, 11 insertions(+) (limited to 'not') diff --git a/not/Cloud.lua b/not/Cloud.lua index c12e236..c9899d5 100644 --- a/not/Cloud.lua +++ b/not/Cloud.lua @@ -5,6 +5,7 @@ -- Metatable of `Cloud` -- nils initialized in constructor +-- TODO: inherit from `not.Decoration` or `not.Sprite`, depending on final result of `not.Sprite`. Cloud = { x = 0, -- position horizontal y = 0, -- position vertical diff --git a/not/Decoration.lua b/not/Decoration.lua index 7f020e1..5bfc328 100644 --- a/not/Decoration.lua +++ b/not/Decoration.lua @@ -1,3 +1,5 @@ +-- TODO: follow new code template +-- TODO: add comments require "not.Sprite" Decoration = { world = nil, diff --git a/not/Effect.lua b/not/Effect.lua index 4e327a1..7010946 100644 --- a/not/Effect.lua +++ b/not/Effect.lua @@ -3,6 +3,8 @@ -- Metatable of `Effect` -- nils initialized in constructor +-- TODO: inherit from `not.Sprite`. +-- TODO: clean-up and reformat code, see newer code for reference. Effect = { x = 0, y = 0, diff --git a/not/Hero.lua b/not/Hero.lua index bd8c55c..a6facd9 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -83,6 +83,7 @@ function Hero:getControlSet () end -- Update callback of `Hero` +-- TODO: Explode this function (method, kek), move controler-related parts to `not.Player`, physics parts to `not.PhysicalBody`. function Hero:update (dt) -- hotfix? for destroyed bodies if self.body:isDestroyed() then return end @@ -280,6 +281,7 @@ function Hero:controlreleased (set, action, key) end -- Draw of `Hero` +-- TODO: see `not.PhysicalBody.draw` and `not.Sprite.draw`. function Hero:draw (offset_x, offset_y, scale, debug) -- draw only alive if not self.alive then return end diff --git a/not/Platform.lua b/not/Platform.lua index 9b7b03c..ecf0377 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -1,6 +1,8 @@ -- `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 -- WHOLE CODE HAS FLAG OF "need a cleanup" require "not.Sprite" @@ -51,6 +53,7 @@ function Platform:getPosition() end -- Draw of `Platform` +-- TODO: see todos in `not.Sprite.draw`. function Platform:draw (offset_x, offset_y, scale, debug) -- locals local offset_x = offset_x or 0 diff --git a/not/Sprite.lua b/not/Sprite.lua index e9eb387..f9a9cb4 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -82,6 +82,7 @@ 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. -- TODO: it doesn't follow same pattern as `not.Hero.draw`. It should implement so it can be called from `not.World`. +-- TODO: change children if above changes are in effect: `not.Platform`, `not.Decoration`. function Sprite:draw (...) local i, q = self:getImage(), self:getQuad() if i then -- cgit v1.1 From cca4d9c1bf4033c79e4bd61a257c6ea02557524c Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 04:19:59 +0100 Subject: Moving draw away to abstract classes --- not/Decoration.lua | 11 +---------- not/Hero.lua | 27 +++++++++++++-------------- not/PhysicalBody.lua | 1 + not/Platform.lua | 13 ++----------- not/Sprite.lua | 46 +++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 60 insertions(+), 38 deletions(-) (limited to 'not') diff --git a/not/Decoration.lua b/not/Decoration.lua index 5bfc328..a57c143 100644 --- a/not/Decoration.lua +++ b/not/Decoration.lua @@ -23,14 +23,5 @@ function Decoration:getPosition() return self.x, self.y end function Decoration:draw(offset_x, offset_y, scale) - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local x, y = self:getPosition() - -- pixel grid - local draw_x = (math.floor(x) + offset_x) * scale - local draw_y = (math.floor(y) + offset_y) * scale - -- draw - Sprite.draw(self, draw_x, draw_y, 0, scale, scale) + Sprite.draw(self, offset_x, offset_y, scale) end \ No newline at end of file diff --git a/not/Hero.lua b/not/Hero.lua index a6facd9..7be3104 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -280,24 +280,23 @@ function Hero:controlreleased (set, action, key) end end +-- TODO: comment them and place them somewhere properly +function Hero:getAngle () + return self.angle +end +function Hero:getHorizontalMirror() + return self.facing +end +function Hero:getOffset () + return 12,15 -- TODO: WHY? How about creating body as polygon and using 0,0 instead. LIKE EVERYWHERE ELSE? Make it obsolete both in here and in `not.Sprite`. +end + -- Draw of `Hero` -- TODO: see `not.PhysicalBody.draw` and `not.Sprite.draw`. function Hero:draw (offset_x, offset_y, scale, debug) - -- draw only alive if not self.alive then return end - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local debug = debug or false - local x, y = self:getPosition() - -- pixel grid ; `approx` selected to prevent floating characters on certain conditions - local approx = math.floor - if (y - math.floor(y)) > 0.5 then approx = math.ceil end - local draw_y = (approx(y) + offset_y) * scale - local draw_x = (math.floor(x) + offset_x) * scale - -- sprite draw - Sprite.draw(self, draw_x, draw_y, self.angle, self.facing*scale, scale, 12, 15) + PhysicalBody.draw(self, offset_x, offset_y, scale, debug) + -- debug draw if debug then for _,fixture in pairs(self.body:getFixtureList()) do diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 52e9357..bbea3ac 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -30,4 +30,5 @@ end -- Draw of `PhysicalBody`. function PhysicalBody:draw (offset_x, offset_y, scale, debug) -- TODO: Move debug part here from `not.Hero.draw`. + Sprite.draw(self, offset_x, offset_y, scale) end diff --git a/not/Platform.lua b/not/Platform.lua index ecf0377..7dcea6c 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -55,17 +55,8 @@ end -- Draw of `Platform` -- TODO: see todos in `not.Sprite.draw`. function Platform:draw (offset_x, offset_y, scale, debug) - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local debug = debug or false - local x, y = self:getPosition() - -- pixel grid - local draw_x = (math.floor(x) + offset_x) * scale - local draw_y = (math.floor(y) + offset_y) * scale - -- sprite draw - Sprite.draw(self, draw_x, draw_y, 0, scale, scale) + Sprite.draw(self, offset_x, offset_y, scale) + -- debug draw if debug then love.graphics.setColor(255, 69, 0, 140) diff --git a/not/Sprite.lua b/not/Sprite.lua index f9a9cb4..a8785d3 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -79,16 +79,56 @@ 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 + -- 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. -- TODO: it doesn't follow same pattern as `not.Hero.draw`. It should implement so it can be called from `not.World`. -- TODO: change children if above changes are in effect: `not.Platform`, `not.Decoration`. -function Sprite:draw (...) +function Sprite:draw (offset_x, offset_y, scale, debug) + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local debug = debug or false + local i, q = self:getImage(), self:getQuad() + local x, y = self:getPosition() + local angle = self:getAngle() + + local scaleX = self:getHorizontalMirror()*(scale or 1) + local scaleY = self:getVerticalMirror()*(scale or 1) + + -- pixel grid ; `approx` selected to prevent floating characters on certain conditions + local approx = math.floor + if (y - math.floor(y)) > 0.5 then approx = math.ceil end + local draw_y = (approx(y) + offset_y) * scale + local draw_x = (math.floor(x) + offset_x) * scale + if i then love.graphics.setColor(255,255,255,255) - if q then love.graphics.draw(i, q, ...) - else love.graphics.draw(i, ...) end + if q then + love.graphics.draw(i, q, draw_x, draw_y, angle, scaleX, scaleY, self:getOffset()) + else + love.graphics.draw(i, draw_x, draw_y, angle, scaleX, scaleY, self:getOffset()) + end end end -- cgit v1.1 From 94f84647255e6d18f1e20ac0a1b8c8c9860b918e Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Mar 2017 04:20:46 +0100 Subject: Debug is obsolete in not.Sprite --- not/Sprite.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'not') diff --git a/not/Sprite.lua b/not/Sprite.lua index a8785d3..25d85f1 100644 --- a/not/Sprite.lua +++ b/not/Sprite.lua @@ -104,10 +104,9 @@ end -- If there is no Quad, it will draw entire image. It won't draw anything if there is no image. -- TODO: it doesn't follow same pattern as `not.Hero.draw`. It should implement so it can be called from `not.World`. -- TODO: change children if above changes are in effect: `not.Platform`, `not.Decoration`. -function Sprite:draw (offset_x, offset_y, scale, debug) +function Sprite:draw (offset_x, offset_y, scale) local offset_x = offset_x or 0 local offset_y = offset_y or 0 - local debug = debug or false local i, q = self:getImage(), self:getQuad() local x, y = self:getPosition() -- cgit v1.1 From fcc6dc19b495fbf21a803f10f7c4cec41d4f8a49 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 20 Mar 2017 18:20:49 +0100 Subject: Hero.nextFrame -> Hero.goToNextFrame --- not/Hero.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 7be3104..c9250e0 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -337,7 +337,7 @@ end -- Change animation of `Hero` -- default, walk, attack, attack_up, attack_down, damage -function Hero:nextFrame () +function Hero:goToNextFrame () local isDown = Controller.isDown local controlset = self:getControlSet() if self.current.repeated or not (self.frame == self.current.frames) then -- cgit v1.1 From bda0f791d64178904e655f74efce24a2f3fc2f96 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 17:20:55 +0200 Subject: Position, Fixture, Body init moved from Hero to PhysicalBody --- not/Hero.lua | 21 +++++++-------------- not/PhysicalBody.lua | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 14 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index c9250e0..4654b33 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -55,19 +55,18 @@ function Hero:init (name, world, x, y) local fileName = name or Hero.name -- INITIAL from metatable local imagePath = string.format("assets/nauts/%s.png", fileName) PhysicalBody.init(self, world, x, y, imagePath) + self:setBodyType("dynamic") + self:setBodyFixedRotation(true) -- TODO: probably should be removed or heavily changed. self.world = world self.punchcd = 0 -- TODO: move following lines to PhysicalBody, cut if not needed, refectorize to subfunctions in target class. local group = -1-#world.Nauts - self.body = love.physics.newBody(world.world, x, y, "dynamic") - self.shape = love.physics.newRectangleShape(10, 16) - self.fixture = love.physics.newFixture(self.body, self.shape, 8) - self.fixture:setUserData(self) - self.fixture:setCategory(2) - self.fixture:setMask(2) - self.fixture:setGroupIndex(group) - self.body:setFixedRotation(true) + local fixture = self:addFixture({-5,-8, 5,-8, 5,8, -5,8}, 8) + fixture:setUserData(self) + fixture:setCategory(2) + fixture:setMask(2) + fixture:setGroupIndex(group) -- Actual `Hero` initialization. self.name = name self:setAnimationsList(require("animations")) @@ -296,7 +295,6 @@ end function Hero:draw (offset_x, offset_y, scale, debug) if not self.alive then return end PhysicalBody.draw(self, offset_x, offset_y, scale, debug) - -- debug draw if debug then for _,fixture in pairs(self.body:getFixtureList()) do @@ -315,11 +313,6 @@ function Hero:draw (offset_x, offset_y, scale, debug) end end --- getPosition -function Hero:getPosition () - return self.body:getPosition() -end - -- Draw HUD of `Hero` -- elevation: 1 bottom, 0 top function Hero:drawHUD (x,y,scale,elevation) diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index bbea3ac..54d334f 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -20,6 +20,31 @@ end -- Initializator of `PhysicalBody`. function PhysicalBody:init (world, x, y, imagePath) Sprite.init(self, imagePath) + self.body = love.physics.newBody(world.world, x, y) +end + +-- Add new fixture to body. +function PhysicalBody:addFixture (shape, density) + local shape = love.physics.newPolygonShape(shape) + local fixture = love.physics.newFixture(self.body, shape, density) + return fixture +end + +-- Position-related methods. +function PhysicalBody:getPosition () + return self.body:getPosition() +end +function PhysicalBody:setPosition (x, y) + self.body:setPosition(x, y) +end + +-- Various setters from Body. +-- type: BodyType ("static", "dynamic", "kinematic") +function PhysicalBody:setBodyType (type) + self.body:setType(type) +end +function PhysicalBody:setBodyFixedRotation (bool) + self.body:setFixedRotation(bool) end -- Update of `PhysicalBody`. -- cgit v1.1 From 9af03e90acde019820021d0bc2fe546d10c25ed4 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 17:34:03 +0200 Subject: Setting group differently, moved debug draw to PhysicalBody from Hero --- not/Hero.lua | 31 ++++++++----------------------- not/PhysicalBody.lua | 12 +++++++++++- 2 files changed, 19 insertions(+), 24 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 4654b33..e81f10c 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -8,6 +8,7 @@ Hero = { facing = 1, max_velocity = 105, world = --[[not.World]]nil, + group = nil, -- Combat combo = 0, lives = 3, @@ -54,20 +55,20 @@ function Hero:init (name, world, x, y) -- Find imagePath basing on hero name and call super initializator. local fileName = name or Hero.name -- INITIAL from metatable local imagePath = string.format("assets/nauts/%s.png", fileName) + -- `PhysicalBody` initialization. PhysicalBody.init(self, world, x, y, imagePath) self:setBodyType("dynamic") self:setBodyFixedRotation(true) - -- TODO: probably should be removed or heavily changed. - self.world = world - self.punchcd = 0 - -- TODO: move following lines to PhysicalBody, cut if not needed, refectorize to subfunctions in target class. - local group = -1-#world.Nauts + 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(group) + fixture:setGroupIndex(self.group) -- Actual `Hero` initialization. + self.world = world + self.punchcd = 0 self.name = name self:setAnimationsList(require("animations")) self:createEffect("respawn") @@ -295,22 +296,6 @@ end function Hero:draw (offset_x, offset_y, scale, debug) if not self.alive then return end PhysicalBody.draw(self, offset_x, offset_y, scale, debug) - -- debug draw - if debug then - for _,fixture in pairs(self.body:getFixtureList()) do - if fixture:getCategory() == 2 then - love.graphics.setColor(137, 255, 0, 120) - else - love.graphics.setColor(137, 0, 255, 40) - end - love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(fixture:getShape():getPoints()))) - end - for _,contact in pairs(self.body:getContactList()) do - love.graphics.setColor(255, 0, 0, 255) - love.graphics.setPointSize(scale) - love.graphics.points(self.world.camera:translatePoints(contact:getPositions())) - end - end end -- Draw HUD of `Hero` @@ -377,7 +362,7 @@ function Hero:hit (direction) fixture:setSensor(true) fixture:setCategory(3) fixture:setMask(1,3) - fixture:setGroupIndex(self.fixture:getGroupIndex()) + fixture:setGroupIndex(self.group) fixture:setUserData({0.08, direction}) -- sound self:playSound(4) diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 54d334f..24e87c8 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -54,6 +54,16 @@ end -- Draw of `PhysicalBody`. function PhysicalBody:draw (offset_x, offset_y, scale, debug) - -- TODO: Move debug part here from `not.Hero.draw`. Sprite.draw(self, offset_x, offset_y, scale) + if debug then + for _,fixture in pairs(self.body:getFixtureList()) do + if fixture:getCategory() == 2 then + love.graphics.setColor(137, 255, 0, 120) + else + 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 -- cgit v1.1 From 8fa7ec07a1a9b4307ed9b221770c91a242cd68d9 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 18:16:23 +0200 Subject: Platform is now child of PhysicalBody --- not/PhysicalBody.lua | 9 +++++-- not/Platform.lua | 74 ++++++++++++++++------------------------------------ 2 files changed, 29 insertions(+), 54 deletions(-) (limited to 'not') diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 24e87c8..7c05262 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -57,9 +57,14 @@ function PhysicalBody:draw (offset_x, offset_y, scale, debug) Sprite.draw(self, offset_x, offset_y, scale) if debug then for _,fixture in pairs(self.body:getFixtureList()) do - if fixture:getCategory() == 2 then + local category = fixture:getCategory() + if category == 1 then + love.graphics.setColor(255, 69, 0, 140) + end + if category == 2 then love.graphics.setColor(137, 255, 0, 120) - else + end + if category == 3 then love.graphics.setColor(137, 0, 255, 40) end -- TODO: `world` is not a member of `PhysicalBody` or its instance normally. diff --git a/not/Platform.lua b/not/Platform.lua index 7dcea6c..0c03e73 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -1,67 +1,37 @@ --- `Platform` +--- `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 - --- WHOLE CODE HAS FLAG OF "need a cleanup" -require "not.Sprite" - --- Metatable of `Platform` --- nils initialized in constructor Platform = { - body = nil, - shape = nil, - fixture = nil, - world = nil, + world = --[[not.World]]nil, } + +-- `Platform` is a child of `PhysicalBody`. +require "not.PhysicalBody" Platform.__index = Platform -setmetatable(Platform, Sprite) +setmetatable(Platform, PhysicalBody) -- Constructor of `Platform` function Platform:new (game, world, x, y, shape, sprite, animations) - local o = {} - setmetatable(o, self) - o.body = love.physics.newBody(world, x, y) - -- MULTIPLE SHAPES NEED TO BE REWRITED! - o.shape = {} - if type(shape[1]) == "number" then - local poly = love.physics.newPolygonShape(shape) - table.insert(o.shape, poly) - o.fixture = love.physics.newFixture(o.body, poly) - o.fixture:setCategory(1) - o.fixture:setFriction(0.2) - else - for i,v in pairs(shape) do - local poly = love.physics.newPolygonShape(v) - table.insert(o.shape, poly) - local fixture = love.physics.newFixture(o.body, poly) - fixture:setCategory(1) - fixture:setFriction(0.2) - end - end - -- END HERE - o:setImage(love.graphics.newImage(sprite)) - o:setAnimationsList(animations) - o.world = game + local o = setmetatable({}, self) + o:init(animations, shape, game, x, y, sprite) return o end --- Position -function Platform:getPosition() - return self.body:getPosition() -end - --- Draw of `Platform` --- TODO: see todos in `not.Sprite.draw`. -function Platform:draw (offset_x, offset_y, scale, debug) - Sprite.draw(self, offset_x, offset_y, scale) - - -- debug draw - if debug then - love.graphics.setColor(255, 69, 0, 140) - for i,v in pairs(self.shape) do - love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(v:getPoints()))) - end +-- Initializator of `Platform`. +function Platform:init (animations, shape, world, x, y, imagePath) + PhysicalBody.init(self, world, x, y, imagePath) + self:setAnimationsList(animations) + self.world = world + -- Create table of shapes if single shape is passed. + if type(shape[1]) == "number" then + shape = {shape} + end + -- Add all shapes from as fixtures to body. + for _,single in pairs(shape) do + local fixture = self:addFixture(single) + fixture:setCategory(1) + fixture:setFriction(0.2) end end \ No newline at end of file -- cgit v1.1 From 93e91c1821087b05f235ae356b7af4d8cfc3cae7 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 18:32:59 +0200 Subject: Decoration properly inherits from Sprite --- not/Decoration.lua | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'not') diff --git a/not/Decoration.lua b/not/Decoration.lua index a57c143..80f6653 100644 --- a/not/Decoration.lua +++ b/not/Decoration.lua @@ -1,27 +1,33 @@ --- TODO: follow new code template --- TODO: add comments -require "not.Sprite" +--- `Decoration` +-- Positioned sprite used to decorate maps with additional graphics. Decoration = { - world = nil, - sprite = nil, + world = --[[not.World]]nil, x = 0, y = 0 } + +-- `Decoration` is a child of `Sprite`. +require "not.Sprite" Decoration.__index = Decoration setmetatable(Decoration, Sprite) -function Decoration:new(x, y, sprite) - local o = {} - setmetatable(o, self) - o:setImage(love.graphics.newImage(sprite)) - o:setPosition(x,y) + +-- Constructor of `Decoration`. +function Decoration:new (x, y, imagePath) + local o = setmetatable({}, self) + o:init(x, y, imagePath) return o end -function Decoration:setPosition(x, y) - self.x, self.y = x, y + +-- Initializator of `Decoration`. +function Decoration:init (x, y, imagePath) + Sprite.init(self, imagePath) + self:setPosition(x, y) end -function Decoration:getPosition() + +-- Position-related methods. +function Decoration:getPosition () return self.x, self.y end -function Decoration:draw(offset_x, offset_y, scale) - Sprite.draw(self, offset_x, offset_y, scale) +function Decoration:setPosition (x, y) + self.x, self.y = x, y end \ No newline at end of file -- cgit v1.1 From e2e4164cda3b4536fcba96f5c5283eb125d2b6b1 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 18:51:51 +0200 Subject: Cloud is now child of Decoration; reworked majority of its content --- not/Cloud.lua | 105 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 49 deletions(-) (limited to 'not') diff --git a/not/Cloud.lua b/not/Cloud.lua index c9899d5..2af7d4b 100644 --- a/not/Cloud.lua +++ b/not/Cloud.lua @@ -1,64 +1,71 @@ --- `Cloud` --- That white thing moving in the background. - --- WHOLE CODE HAS FLAG OF "need a cleanup" - --- Metatable of `Cloud` --- nils initialized in constructor --- TODO: inherit from `not.Decoration` or `not.Sprite`, depending on final result of `not.Sprite`. +--- `Cloud` +-- That white thing moving in the background. +-- TODO: extends variables names to be readable. Cloud = { - x = 0, -- position horizontal - y = 0, -- position vertical t = 1, -- type (sprite number) - v = 13, -- velocity - sprite = nil, - quads = { + v = 13 -- velocity +} + +-- TODO: allow maps to use other quads and sprites for clouds +-- TODO: you know this isn't right, don't you? +local animations = { + default = { [1] = love.graphics.newQuad( 1, 1, 158,47, 478,49), - [2] = love.graphics.newQuad(160, 1, 158,47, 478,49), - [3] = love.graphics.newQuad(319, 1, 158,47, 478,49) + frames = 1, + repeated = true + }, + default2 = { + [1] = love.graphics.newQuad(160, 1, 158,47, 478,49), + frames = 1, + repeated = true + }, + default3 = { + [1] = love.graphics.newQuad(319, 1, 158,47, 478,49), + frames = 1, + repeated = true } } --- Constructor of `Cloud` -function Cloud:new(x, y, t, v) - -- Meta - local o = {} - setmetatable(o, self) - self.__index = self - -- Load spritesheet to metatable if not yet loaded - if self.sprite == nil then - self.sprite = love.graphics.newImage("assets/clouds.png") +-- `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. + if self:getImage() == nil then + self:setImage(Sprite.newImage("assets/clouds.png")) end - -- Init - o.x = x or self.x - o.y = y or self.y - o.t = t or self.t - o.v = v or self.v return o end --- Position -function Cloud:getPosition() - return self.x, self.y +-- Initializator of `Cloud`. +function Cloud:init (x, y, t, v) + Decoration.init(self, x, y, nil) + self:setAnimationsList(animations) + self:setVelocity(v) + self:setType(t) end --- Update of `Cloud`, returns x for world to delete cloud after reaching right corner -function Cloud:update(dt) +-- Setters for cloud type and velocity. +function Cloud:setType (type) + local animation = "default" + if type > 1 then + animation = animation .. type + end + self:setAnimation(animation) + self.t = type +end +function Cloud:setVelocity (velocity) + self.v = velocity +end + +-- Update of `Cloud`, returns x for world to delete cloud after reaching right corner. +function Cloud:update (dt) + Decoration.update(self, dt) self.x = self.x + self.v*dt return self.x end - --- Draw `Cloud` -function Cloud:draw(offset_x, offset_y, scale) - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local x, y = self:getPosition() - -- pixel grid - local draw_x = (math.floor(x) + offset_x) * scale - local draw_y = (math.floor(y) + offset_y) * scale - -- draw - love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.sprite, self.quads[self.t], draw_x, draw_y, 0, scale, scale) -end \ No newline at end of file -- cgit v1.1 From 33865425828c92bb1cf97dee8d0b0f51f9b01042 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 19:16:42 +0200 Subject: Effect is now child of Decoration. Uses Sprite methods and Decoration positioning --- not/Effect.lua | 92 ++++++++++++++++++++++------------------------------------ 1 file changed, 34 insertions(+), 58 deletions(-) (limited to 'not') diff --git a/not/Effect.lua b/not/Effect.lua index 7010946..1bf9ed7 100644 --- a/not/Effect.lua +++ b/not/Effect.lua @@ -1,70 +1,46 @@ --- `Effect` +--- `Effect` -- Short animation with graphics that plays in various situation. - --- Metatable of `Effect` --- nils initialized in constructor --- TODO: inherit from `not.Sprite`. --- TODO: clean-up and reformat code, see newer code for reference. +-- 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 = { - x = 0, - y = 0, - delay = 0.06, - initial = nil, - frame = 1, - animation = nil, - sprite = nil, - quads = require "effects" + finished = false, } --- Construct of `Effect` -function Effect:new(name, x, y) - -- Meta - local o = {} - setmetatable(o, self) - self.__index = self - -- Load spritesheet to metatable if not yet loaded - if self.sprite == nil then - self.sprite = love.graphics.newImage("assets/effects.png") +-- `Effect` is a child of `Decoration`. +require "not.Decoration" +Effect.__index = Effect +setmetatable(Effect, Decoration) + +-- 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")) end - -- Init - o.initial = o.delay - o.animation = name - o.x = x or self.x - o.y = y or self.y return o end --- Position -function Effect:getPosition() - return self.x, self.y +-- Initializator of `Effect`. +function Effect:init (name, x, y) + Decoration.init(self, x, y, nil) + self:setAnimationsList(require("effects")) + self:setAnimation(name) end --- Animation and return flag for deletion after completion --- returns true if completed and ready to delete -function Effect:update(dt) - self.delay = self.delay - dt - if self.delay < 0 then - if self.frame < self.quads[self.animation].frames then - self.frame = self.frame + 1 - self.delay = self.delay + self.initial - else - return true -- delete - end - end - return false +-- Update of `Effect`. +-- Returns true if animation is finished and effect is ready to be deleted. +function Effect:update (dt) + Decoration.update(self, dt) + return self.finished end --- Draw me with scale and offsets, senpai -function Effect:draw(offset_x, offset_y, scale) - -- locals - local offset_x = offset_x or 0 - local offset_y = offset_y or 0 - local scale = scale or 1 - local x, y = self:getPosition() - -- pixel grid - local draw_x = (math.floor(x) + offset_x) * scale - local draw_y = (math.floor(y) + offset_y) * scale - -- draw - love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.sprite, self.quads[self.animation][self.frame], draw_x, draw_y, 0, scale, scale) -end \ No newline at end of file +-- Overridden from `not.Sprite`. +-- Sets finished flag if reached last frame of played animation. +function Effect:goToNextFrame () + if not (self.frame == self.current.frames) then + self.frame = (self.frame % self.current.frames) + 1 + else + self.finished = true + end +end -- cgit v1.1 From eed0553c2f005c439a028d56d82756828e75c2ac Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 19:38:59 +0200 Subject: Hero use direct parent's update rather than distant relative's one --- not/Hero.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index e81f10c..f77e5cb 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -52,7 +52,7 @@ end -- Initializator of `Hero`. function Hero:init (name, world, x, y) - -- Find imagePath basing on hero name and call super initializator. + -- Find imagePath based on hero name. local fileName = name or Hero.name -- INITIAL from metatable local imagePath = string.format("assets/nauts/%s.png", fileName) -- `PhysicalBody` initialization. @@ -87,6 +87,7 @@ end function Hero:update (dt) -- hotfix? for destroyed bodies if self.body:isDestroyed() then return end + PhysicalBody.update(self, dt) -- locals local x, y = self.body:getLinearVelocity() local isDown = Controller.isDown @@ -143,8 +144,6 @@ function Hero:update (dt) end end - Sprite.update(self, dt) - -- # DEATH -- We all die in the end. local m = self.world.map -- cgit v1.1 From b4389dfb590862b50cc6c9ce59d3fcef9bd046b3 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 20:11:21 +0200 Subject: World comments, other comments, todos --- not/Cloud.lua | 2 +- not/Decoration.lua | 2 +- not/Effect.lua | 2 +- not/Hero.lua | 2 +- not/PhysicalBody.lua | 2 +- not/Platform.lua | 2 +- not/Player.lua | 2 +- not/World.lua | 17 +++++++++++++++-- 8 files changed, 22 insertions(+), 9 deletions(-) (limited to 'not') diff --git a/not/Cloud.lua b/not/Cloud.lua index 2af7d4b..3bc5377 100644 --- a/not/Cloud.lua +++ b/not/Cloud.lua @@ -42,7 +42,7 @@ function Cloud:new (x, y, t, v) return o end --- Initializator of `Cloud`. +-- Initializer of `Cloud`. function Cloud:init (x, y, t, v) Decoration.init(self, x, y, nil) self:setAnimationsList(animations) diff --git a/not/Decoration.lua b/not/Decoration.lua index 80f6653..9dc2bdd 100644 --- a/not/Decoration.lua +++ b/not/Decoration.lua @@ -18,7 +18,7 @@ function Decoration:new (x, y, imagePath) return o end --- Initializator of `Decoration`. +-- Initializer of `Decoration`. function Decoration:init (x, y, imagePath) Sprite.init(self, imagePath) self:setPosition(x, y) diff --git a/not/Effect.lua b/not/Effect.lua index 1bf9ed7..4051b92 100644 --- a/not/Effect.lua +++ b/not/Effect.lua @@ -21,7 +21,7 @@ function Effect:new (name, x, y) return o end --- Initializator of `Effect`. +-- Initializer of `Effect`. function Effect:init (name, x, y) Decoration.init(self, x, y, nil) self:setAnimationsList(require("effects")) diff --git a/not/Hero.lua b/not/Hero.lua index f77e5cb..47c6e33 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -50,7 +50,7 @@ function Hero:new (game, world, x, y, name) return o end --- Initializator of `Hero`. +-- 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 diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 7c05262..7b774f2 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -17,7 +17,7 @@ function PhysicalBody:new (world, x, y, imagePath) end ]] --- Initializator of `PhysicalBody`. +-- Initializer of `PhysicalBody`. function PhysicalBody:init (world, x, y, imagePath) Sprite.init(self, imagePath) self.body = love.physics.newBody(world.world, x, y) diff --git a/not/Platform.lua b/not/Platform.lua index 0c03e73..5ee2dd7 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -19,7 +19,7 @@ function Platform:new (game, world, x, y, shape, sprite, animations) return o end --- Initializator of `Platform`. +-- Initializer of `Platform`. function Platform:init (animations, shape, world, x, y, imagePath) PhysicalBody.init(self, world, x, y, imagePath) self:setAnimationsList(animations) diff --git a/not/Player.lua b/not/Player.lua index bcd72bc..fd1613c 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -16,7 +16,7 @@ function Player:new (...) return o end --- Initializator of `Player`. +-- Initializer of `Player`. function Player:init (...) Hero.init(self, ...) end \ No newline at end of file diff --git a/not/World.lua b/not/World.lua index 6d81918..216f9dd 100644 --- a/not/World.lua +++ b/not/World.lua @@ -1,5 +1,6 @@ -- `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`. -- WHOLE CODE HAS FLAG OF "need a cleanup" @@ -36,6 +37,7 @@ World = { } -- Constructor of `World` ZA WARUDO! +-- TODO: push stuff to initialization method. function World:new(map, nauts) -- Meta local o = {} @@ -46,6 +48,7 @@ function World:new(map, nauts) o.world = love.physics.newWorld(0, 9.81*64, true) o.world:setCallbacks(o.beginContact, o.endContact) -- Empty tables for objects + -- TODO: DEAR DEER, do you see it? local n = {} o.Nauts = n local p = {} @@ -58,7 +61,7 @@ function World:new(map, nauts) o.Decorations = d local r = {} o.Rays = r - -- Random init + -- Random init; TODO: use LOVE2D's random. math.randomseed(os.time()) -- Map local map = map or "default" @@ -86,6 +89,7 @@ function World:delete() end -- Load map from file +-- TODO: Change current map model to function-based one. function World:loadMap(name) local name = name or "default" name = "maps/" .. name .. ".lua" @@ -125,11 +129,14 @@ function World:getSpawnPosition() end -- Add new platform to the world +-- TODO: follow new parameters in `not.Platform.new` based on `not.Platform.init`. function World:createPlatform(x, y, polygon, sprite, animations) table.insert(self.Platforms, Platform:new(self, self.world, x, y, polygon, sprite, animations)) end -- Add new naut to the world +-- TODO: separate two methods for `not.Hero` and `not.Player`. +-- TODO: follow new parameters in `not.Player.new` based on `not.Player.init`. function World:createNaut(x, y, name) local naut = Hero:new(self, self.world, x, y, name) table.insert(self.Nauts, naut) @@ -137,11 +144,14 @@ function World:createNaut(x, y, name) end -- Add new decoration to the world +-- TODO: follow new parameters in `not.Decoration.new` based on `not.Decoration.init`. function World:createDecoration(x, y, sprite) table.insert(self.Decorations, Decoration:new(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)) end @@ -167,6 +177,8 @@ function World:randomizeCloud(outside) end -- Add an effect behind nauts +-- 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)) end @@ -288,7 +300,7 @@ function World:draw() -- Background love.graphics.draw(self.background, 0, 0, 0, scaler, scaler) - -- This needs to be reworked! + -- TODO: this needs to be reworked! -- Draw clouds for _,cloud in pairs(self.Clouds) do cloud:draw(offset_x, offset_y, scale) @@ -403,6 +415,7 @@ function World.endContact(a, b, coll) end -- Controller callbacks +-- TODO: names of this methods don't follow naming patterns in this project. See `Controller` and change it. function World:controlpressed(set, action, key) if key == "f6" and debug then local map = self:getMapName() -- cgit v1.1 From be64dffe9e3354fda6220a1b25251e1664cd71cc Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 21:50:40 +0200 Subject: Testing if body isn't destroyed in PhysicalBody's update --- not/Hero.lua | 2 -- not/PhysicalBody.lua | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 47c6e33..1b84b1c 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -85,8 +85,6 @@ end -- Update callback of `Hero` -- TODO: Explode this function (method, kek), move controler-related parts to `not.Player`, physics parts to `not.PhysicalBody`. function Hero:update (dt) - -- hotfix? for destroyed bodies - if self.body:isDestroyed() then return end PhysicalBody.update(self, dt) -- locals local x, y = self.body:getLinearVelocity() diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index 7b774f2..a7abcc0 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -50,6 +50,7 @@ end -- Update of `PhysicalBody`. function PhysicalBody:update (dt) Sprite.update(self, dt) + if self.body:isDestroyed() then return end end -- Draw of `PhysicalBody`. -- cgit v1.1 From 603fac3ce18ff7df7b8d2f74d5e57cc728c0abc2 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 22:07:32 +0200 Subject: Created physics functions to influence PhysicalBody; changed Hero to use them. Comments. --- not/Hero.lua | 43 +++++++++++++++++++++++-------------------- not/PhysicalBody.lua | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+), 20 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 1b84b1c..1cb5e93 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -75,6 +75,7 @@ function Hero:init (name, world, x, y) end -- Control set managment +-- TODO: move these two to `not.Player`. function Hero:assignControlSet (set) self.controlset = set end @@ -87,14 +88,14 @@ end function Hero:update (dt) PhysicalBody.update(self, dt) -- locals - local x, y = self.body:getLinearVelocity() + local x, y = self:getLinearVelocity() local isDown = Controller.isDown local controlset = self:getControlSet() -- # VERTICAL MOVEMENT -- Jumping if self.jumpactive and self.jumptimer > 0 then - self.body:setLinearVelocity(x,-160) + self:setLinearVelocity(x,-160) self.jumptimer = self.jumptimer - dt end @@ -109,18 +110,18 @@ function Hero:update (dt) -- Walking if isDown(controlset, "left") then self.facing = -1 - self.body:applyForce(-250, 0) + self:applyForce(-250, 0) -- Controlled speed limit if x < -self.max_velocity then - self.body:applyForce(250, 0) + self:applyForce(250, 0) end end if isDown(controlset, "right") then self.facing = 1 - self.body:applyForce(250, 0) + self:applyForce(250, 0) -- Controlled speed limit if x > self.max_velocity then - self.body:applyForce(-250, 0) + self:applyForce(-250, 0) end end @@ -136,9 +137,9 @@ function Hero:update (dt) else face = 0 end - self.body:applyForce(40*face,0) + self:applyForce(40*face,0) if not self.inAir then - self.body:applyForce(80*face,0) + self:applyForce(80*face,0) end end @@ -163,8 +164,8 @@ function Hero:update (dt) -- # PUNCH -- Cooldown self.punchcd = self.punchcd - dt - if not self.body:isDestroyed() then -- This is weird - for _,fixture in pairs(self.body:getFixtureList()) do + if not self.body:isDestroyed() then -- TODO: This is weird + for _,fixture in pairs(self.body:getFixtureList()) do -- TODO: getFixtures from `PhysicalBody` or similar. if fixture:getUserData() ~= self then fixture:setUserData({fixture:getUserData()[1] - dt, fixture:getUserData()[2]}) if fixture:getUserData()[1] < 0 then @@ -178,9 +179,9 @@ function Hero:update (dt) 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.body:setLinearVelocity(0,0) + self:setLinearVelocity(0,0) else - self.body:setLinearVelocity(38*self.facing,0) + self:setLinearVelocity(38*self.facing,0) end end @@ -281,7 +282,7 @@ end function Hero:getAngle () return self.angle end -function Hero:getHorizontalMirror() +function Hero:getHorizontalMirror () return self.facing end function Hero:getOffset () @@ -289,7 +290,6 @@ function Hero:getOffset () end -- Draw of `Hero` --- TODO: see `not.PhysicalBody.draw` and `not.Sprite.draw`. function Hero:draw (offset_x, offset_y, scale, debug) if not self.alive then return end PhysicalBody.draw(self, offset_x, offset_y, scale, debug) @@ -339,10 +339,12 @@ end -- Punch of `Hero` -- direction: left, right, up, down -- creates temporary fixture for player's body that acts as sensor; fixture is deleted after time set in UserData[1]; deleted by Hero:update(dt) +-- TODO: attack functions needs to be renamed, because even I have problems understanding them. function Hero:hit (direction) -- start cooldown self.punchcd = Hero.punchcd -- INITIAL from metatable -- actual punch + -- TODO: use `PhysicalBody.addFixture`. local fixture if direction == "left" then fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-2,-6, -20,-6, -20,6, -2,6), 0) @@ -382,8 +384,8 @@ function Hero:damage (direction) vertical = 1 end self:createEffect("hit") - local x,y = self.body:getLinearVelocity() - self.body:setLinearVelocity(x,0) + local x,y = self:getLinearVelocity() + self:setLinearVelocity(x,0) self.body:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) self:setAnimation("damage") self.combo = math.min(27, self.combo + 1) @@ -398,21 +400,22 @@ function Hero:die () self.lives = self.lives - 1 self.alive = false self.spawntimer = Hero.spawntimer -- INITIAL from metatable - self.body:setActive(false) + self:setBodyActive(false) self.world:onNautKilled(self) end -- And then respawn. Like Jon Snow. function Hero:respawn () self.alive = true - self.body:setLinearVelocity(0,0) - self.body:setPosition(self.world:getSpawnPosition()) - self.body:setActive(true) + self:setLinearVelocity(0,0) + self:setPosition(self.world:getSpawnPosition()) -- TODO: I'm not convinced about getting new position like this. + self:setBodyActive(true) self:createEffect("respawn") self:playSound(7) end -- Sounds +-- TODO: Possibly export to nonexistent SoundEmitter class. Can be used by World (Stage), too. function Hero:playSound (sfx, force) if self.alive or force then local source = love.audio.newSource(self.sfx[sfx]) diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index a7abcc0..a9ac63b 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -38,6 +38,14 @@ function PhysicalBody:setPosition (x, y) self.body:setPosition(x, y) end +-- Velocity-related methods. +function PhysicalBody:setLinearVelocity (x, y) + self.body:setLinearVelocity(x, y) +end +function PhysicalBody:getLinearVelocity () + return self.body:getLinearVelocity() +end + -- Various setters from Body. -- type: BodyType ("static", "dynamic", "kinematic") function PhysicalBody:setBodyType (type) @@ -46,6 +54,17 @@ end function PhysicalBody:setBodyFixedRotation (bool) self.body:setFixedRotation(bool) end +function PhysicalBody:setBodyActive (bool) + self.body:setActive(bool) +end + +-- Physical influence methods. +function PhysicalBody:applyLinearImpulse (x, y) + self.body:applyLinearImpulse(x, y) +end +function PhysicalBody:applyForce (x, y) + self.body:applyForce(x, y) +end -- Update of `PhysicalBody`. function PhysicalBody:update (dt) -- cgit v1.1 From cedc95538ded6ca0845f13e8ec2fdbf2b8c0c8ed Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 22:15:55 +0200 Subject: Missed applyLinearImpulse in Hero --- not/Hero.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 1cb5e93..ec7f39f 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -386,7 +386,7 @@ function Hero:damage (direction) self:createEffect("hit") local x,y = self:getLinearVelocity() self:setLinearVelocity(x,0) - self.body:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) + self:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) self:setAnimation("damage") self.combo = math.min(27, self.combo + 1) self.punchcd = 0.08 + self.combo*0.006 -- cgit v1.1 From 8552aee09a280f14dd84c64a0e13e3b0cb67d965 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 23:24:04 +0200 Subject: Maximum combo increased to 990 --- not/Hero.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index ec7f39f..68329ad 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -126,8 +126,8 @@ function Hero:update (dt) end -- Custom linear damping - if not isDown(controlset, "left") and - not isDown(controlset, "right") + if not isDown(controlset, "left") and + not isDown(controlset, "right") then local face = nil if x < -12 then @@ -388,7 +388,7 @@ function Hero:damage (direction) self:setLinearVelocity(x,0) self:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) self:setAnimation("damage") - self.combo = math.min(27, self.combo + 1) + self.combo = math.min(99, self.combo + 1) self.punchcd = 0.08 + self.combo*0.006 self:playSound(2) end -- cgit v1.1 From cd025014eac8d1adf70bc3238de5c034dea80c78 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 3 Apr 2017 23:56:03 +0200 Subject: Renamed jumping-related variables, removed obsoletes. --- not/Hero.lua | 37 ++++++++++++++++++------------------- not/World.lua | 4 +++- 2 files changed, 21 insertions(+), 20 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 68329ad..daed8fc 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -14,15 +14,14 @@ Hero = { lives = 3, spawntimer = 2, alive = true, - punchcd = 0.25, + punchCooldown = 0.25, punchdir = 0, -- a really bad thing -- Movement inAir = true, salto = false, - jumpactive = false, - jumpdouble = true, - jumptimer = 0.16, - jumpnumber = 2, + isJumping = false, + jumpTimer = 0.16, + jumpCounter = 2, -- Keys controlset = nil, -- Statics @@ -68,7 +67,7 @@ function Hero:init (name, world, x, y) fixture:setGroupIndex(self.group) -- Actual `Hero` initialization. self.world = world - self.punchcd = 0 + self.punchCooldown = 0 self.name = name self:setAnimationsList(require("animations")) self:createEffect("respawn") @@ -94,9 +93,9 @@ function Hero:update (dt) -- # VERTICAL MOVEMENT -- Jumping - if self.jumpactive and self.jumptimer > 0 then + if self.isJumping and self.jumpTimer > 0 then self:setLinearVelocity(x,-160) - self.jumptimer = self.jumptimer - dt + self.jumpTimer = self.jumpTimer - dt end -- Salto @@ -163,7 +162,7 @@ function Hero:update (dt) -- # PUNCH -- Cooldown - self.punchcd = self.punchcd - dt + self.punchCooldown = self.punchCooldown - dt if not self.body:isDestroyed() then -- TODO: This is weird for _,fixture in pairs(self.body:getFixtureList()) do -- TODO: getFixtures from `PhysicalBody` or similar. if fixture:getUserData() ~= self then @@ -185,7 +184,7 @@ function Hero:update (dt) end end - if self.punchcd <= 0 and self.punchdir == 1 then + if self.punchCooldown <= 0 and self.punchdir == 1 then self.punchdir = 0 end end @@ -197,9 +196,9 @@ function Hero:controlpressed (set, action, key) local controlset = self:getControlSet() -- Jumping if action == "jump" then - if self.jumpnumber > 0 then + if self.jumpCounter > 0 then -- General jump logics - self.jumpactive = true + self.isJumping = true --self:playSound(6) -- Spawn proper effect if not self.inAir then @@ -208,7 +207,7 @@ function Hero:controlpressed (set, action, key) self:createEffect("doublejump") end -- Start salto if last jump - if self.jumpnumber == 1 then + if self.jumpCounter == 1 then self.salto = true end -- Animation clear @@ -218,7 +217,7 @@ function Hero:controlpressed (set, action, key) self:setAnimation("default") end -- Remove jump - self.jumpnumber = self.jumpnumber - 1 + self.jumpCounter = self.jumpCounter - 1 end end @@ -231,7 +230,7 @@ function Hero:controlpressed (set, action, key) end -- Punching - if action == "attack" and self.punchcd <= 0 then + if action == "attack" and self.punchCooldown <= 0 then local f = self.facing self.salto = false if isDown(controlset, "up") then @@ -266,8 +265,8 @@ function Hero:controlreleased (set, action, key) local controlset = self:getControlSet() -- Jumping if action == "jump" then - self.jumpactive = false - self.jumptimer = Hero.jumptimer -- take initial from metatable + self.isJumping = false + self.jumpTimer = Hero.jumpTimer -- take initial from metatable end -- Walking if (action == "left" or action == "right") and not @@ -342,7 +341,7 @@ end -- TODO: attack functions needs to be renamed, because even I have problems understanding them. function Hero:hit (direction) -- start cooldown - self.punchcd = Hero.punchcd -- INITIAL from metatable + self.punchCooldown = Hero.punchCooldown -- INITIAL from metatable -- actual punch -- TODO: use `PhysicalBody.addFixture`. local fixture @@ -389,7 +388,7 @@ function Hero:damage (direction) self:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) self:setAnimation("damage") self.combo = math.min(99, self.combo + 1) - self.punchcd = 0.08 + self.combo*0.006 + self.punchCooldown = 0.08 + self.combo*0.006 self:playSound(2) end diff --git a/not/World.lua b/not/World.lua index 216f9dd..b7bd48c 100644 --- a/not/World.lua +++ b/not/World.lua @@ -386,10 +386,12 @@ function World.beginContact(a, b, coll) if a:getCategory() == 1 then local x,y = coll:getNormal() if y < -0.6 then + -- TODO: remove debug messages + -- TODO: move landing to `not.Hero` print(b:getUserData().name .. " is not in air") -- Move them to Hero b:getUserData().inAir = false - b:getUserData().jumpnumber = 2 + b:getUserData().jumpCounter = 2 b:getUserData().salto = false b:getUserData():createEffect("land") end -- cgit v1.1 From 94fe36185b24315abccc55477c92961adf4d409c Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 4 Apr 2017 12:49:51 +0200 Subject: Punching method renamed, updated to use new stuff --- not/Hero.lua | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index daed8fc..e28c2ef 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -238,22 +238,22 @@ function Hero:controlpressed (set, action, key) if self.current ~= self.animations.damage then self:setAnimation("attack_up") end - self:hit("up") + self:punch("up") elseif isDown(controlset, "down") then -- Punch down if self.current ~= self.animations.damage then self:setAnimation("attack_down") end - self:hit("down") + self:punch("down") else -- Punch horizontal if self.current ~= self.animations.damage then self:setAnimation("attack") end if f == 1 then - self:hit("right") + self:punch("right") else - self:hit("left") + self:punch("left") end self.punchdir = 1 end @@ -335,39 +335,31 @@ function Hero:createEffect (name) end end --- Punch of `Hero` --- direction: left, right, up, down --- creates temporary fixture for player's body that acts as sensor; fixture is deleted after time set in UserData[1]; deleted by Hero:update(dt) --- TODO: attack functions needs to be renamed, because even I have problems understanding them. -function Hero:hit (direction) - -- start cooldown +-- 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 metatable - -- actual punch - -- TODO: use `PhysicalBody.addFixture`. - local fixture - if direction == "left" then - fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-2,-6, -20,-6, -20,6, -2,6), 0) - end - if direction == "right" then - fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(2,-6, 20,-6, 20,6, 2,6), 0) - end - if direction == "up" then - fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-8,-4, -8,-20, 8,-20, 8,-4), 0) - end - if direction == "down" then - fixture = love.physics.newFixture(self.body, love.physics.newPolygonShape(-8,4, -8,20, 8,20, 8,4), 0) - end + -- 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 + -- 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}) - -- sound self:playSound(4) end -- Taking damage of `Hero` by successful hit test -- currently called from World's startContact +-- TODO: attack functions needs to be renamed, because even I have problems understanding them. function Hero:damage (direction) local horizontal, vertical = 0, 0 if direction == "left" then -- cgit v1.1 From 7a560cee7e7f0620656a63e91f05fbe898919246 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 4 Apr 2017 13:11:33 +0200 Subject: Kek, never commit late night. Fail statement back to not.Hero.update --- not/Hero.lua | 1 + not/PhysicalBody.lua | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index e28c2ef..cddf864 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -86,6 +86,7 @@ end -- TODO: Explode this function (method, kek), move controler-related parts to `not.Player`, physics parts to `not.PhysicalBody`. function Hero:update (dt) PhysicalBody.update(self, dt) + if self.body:isDestroyed() then return end -- locals local x, y = self:getLinearVelocity() local isDown = Controller.isDown diff --git a/not/PhysicalBody.lua b/not/PhysicalBody.lua index a9ac63b..e9625fa 100644 --- a/not/PhysicalBody.lua +++ b/not/PhysicalBody.lua @@ -69,7 +69,6 @@ end -- Update of `PhysicalBody`. function PhysicalBody:update (dt) Sprite.update(self, dt) - if self.body:isDestroyed() then return end end -- Draw of `PhysicalBody`. -- cgit v1.1 From 10db7a170cf28db93740448bba76f7a9cd452664 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 4 Apr 2017 13:31:31 +0200 Subject: Combo is held the same way as shown now --- not/Hero.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index cddf864..7b968ab 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -305,7 +305,7 @@ function Hero:drawHUD (x,y,scale,elevation) love.graphics.draw(self.portrait_sprite, 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*10).."%",(x+2)*scale,(y-3+dy)*scale,0,scale,scale) + love.graphics.print((self.combo).."%",(x+2)*scale,(y-3+dy)*scale,0,scale,scale) love.graphics.print(math.max(0, self.lives),(x+24)*scale,(y-3+dy)*scale,0,scale,scale) end end @@ -378,10 +378,10 @@ function Hero:damage (direction) self:createEffect("hit") local x,y = self:getLinearVelocity() self:setLinearVelocity(x,0) - self:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) + self:applyLinearImpulse((42+self.combo)*horizontal, (68+self.combo)*vertical + 15) self:setAnimation("damage") - self.combo = math.min(99, self.combo + 1) - self.punchCooldown = 0.08 + self.combo*0.006 + self.combo = math.min(999, self.combo + 10) + self.punchCooldown = 0.08 + self.combo*0.0006 self:playSound(2) end -- cgit v1.1 From 5c51b6e4d39bc55887f94dc65e58fd1765d86c1c Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 4 Apr 2017 14:30:15 +0200 Subject: First steps to move player-input logics into Player from Hero --- not/Hero.lua | 13 +------------ not/Player.lua | 22 +++++++++++++++++++--- not/World.lua | 6 +++--- 3 files changed, 23 insertions(+), 18 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 7b968ab..a0758ee 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -1,5 +1,5 @@ --- `Hero` --- Hero (naut) entity that exists in a game world. +-- Hero (often referred to as: "naut") entity that exists in a game world. -- Collision category: [2] Hero = { -- General and physics @@ -22,8 +22,6 @@ Hero = { isJumping = false, jumpTimer = 0.16, jumpCounter = 2, - -- Keys - controlset = nil, -- Statics portrait_sprite = nil, portrait_frame = nil, @@ -73,15 +71,6 @@ function Hero:init (name, world, x, y) self:createEffect("respawn") end --- Control set managment --- TODO: move these two to `not.Player`. -function Hero:assignControlSet (set) - self.controlset = set -end -function Hero:getControlSet () - return self.controlset -end - -- Update callback of `Hero` -- TODO: Explode this function (method, kek), move controler-related parts to `not.Player`, physics parts to `not.PhysicalBody`. function Hero:update (dt) diff --git a/not/Player.lua b/not/Player.lua index fd1613c..373505d 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -2,6 +2,7 @@ -- Special `not.Hero` controllable by a player. Player = { -- TODO: move functions and properties related to controls from `not.Hero`. + controlSet = --[[Controller.sets.*]]nil, } -- `Player` is a child of `Hero`. @@ -10,13 +11,28 @@ Player.__index = Player setmetatable(Player, Hero) -- Constructor of `Player`. -function Player:new (...) +-- TODO: I'm sure it is a duplicate, but `not.World.create*` methods need to pass proper parameters. +function Player:new (game, world, x, y, name) local o = setmetatable({}, self) - o:init(...) + 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, ...) -end \ No newline at end of file +end + +-- Controller set manipulation. +function Player:assignControlSet (set) + self.controlset = set +end +function Player:getControlSet () + return self.controlset +end diff --git a/not/World.lua b/not/World.lua index b7bd48c..44923bb 100644 --- a/not/World.lua +++ b/not/World.lua @@ -5,7 +5,7 @@ -- WHOLE CODE HAS FLAG OF "need a cleanup" require "not.Platform" -require "not.Hero" +require "not.Player" require "not.Cloud" require "not.Effect" require "not.Decoration" @@ -136,9 +136,9 @@ end -- Add new naut to the world -- TODO: separate two methods for `not.Hero` and `not.Player`. --- TODO: follow new parameters in `not.Player.new` based on `not.Player.init`. +-- TODO: follow new parameters in `not.(Player/Hero).new` based on `not.(Player/Hero).init`. function World:createNaut(x, y, name) - local naut = Hero:new(self, self.world, x, y, name) + local naut = Player:new(self, self.world, x, y, name) table.insert(self.Nauts, naut) return naut end -- cgit v1.1 From 8e11bf89f1abe547c30f7d5ac39bf0d7ed555f7e Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 5 Apr 2017 18:43:14 +0200 Subject: Some of World's create methods are now following new parameter orders of entities constructors --- not/Platform.lua | 2 +- not/Player.lua | 2 +- not/World.lua | 57 ++++++++++++++++++++++++++++---------------------------- 3 files changed, 30 insertions(+), 31 deletions(-) (limited to 'not') diff --git a/not/Platform.lua b/not/Platform.lua index 5ee2dd7..3748c47 100644 --- a/not/Platform.lua +++ b/not/Platform.lua @@ -13,7 +13,7 @@ Platform.__index = Platform setmetatable(Platform, PhysicalBody) -- Constructor of `Platform` -function Platform:new (game, world, x, y, shape, sprite, animations) +function Platform:new (animations, shape, game, x, y, sprite) local o = setmetatable({}, self) o:init(animations, shape, game, x, y, sprite) return o diff --git a/not/Player.lua b/not/Player.lua index 373505d..5fc2adc 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -12,7 +12,7 @@ setmetatable(Player, Hero) -- Constructor of `Player`. -- TODO: I'm sure it is a duplicate, but `not.World.create*` methods need to pass proper parameters. -function Player:new (game, world, x, y, name) +function Player:new (name, game, x, y) local o = setmetatable({}, self) o:init(name, game, x, y) -- Load portraits statically to `not.Hero`. diff --git a/not/World.lua b/not/World.lua index 44923bb..fc5e229 100644 --- a/not/World.lua +++ b/not/World.lua @@ -38,7 +38,7 @@ World = { -- Constructor of `World` ZA WARUDO! -- TODO: push stuff to initialization method. -function World:new(map, nauts) +function World:new (map, nauts) -- Meta local o = {} setmetatable(o, self) @@ -76,7 +76,7 @@ function World:new(map, nauts) end -- The end of the world -function World:delete() +function World:delete () self.world:destroy() for _,platform in pairs(self.Platforms) do platform:delete() @@ -90,7 +90,7 @@ end -- Load map from file -- TODO: Change current map model to function-based one. -function World:loadMap(name) +function World:loadMap (name) local name = name or "default" name = "maps/" .. name .. ".lua" local map = love.filesystem.load(name) @@ -114,7 +114,7 @@ function World:loadMap(name) end -- Spawn all the nauts for the round -function World:spawnNauts(nauts) +function World:spawnNauts (nauts) for _,naut in pairs(nauts) do local x,y = self:getSpawnPosition() local spawn = self:createNaut(x, y, naut[1]) @@ -123,41 +123,40 @@ function World:spawnNauts(nauts) end -- Get respawn location -function World:getSpawnPosition() +function World:getSpawnPosition () local n = math.random(1, #self.map.respawns) return self.map.respawns[n].x, self.map.respawns[n].y end -- Add new platform to the world --- TODO: follow new parameters in `not.Platform.new` based on `not.Platform.init`. -function World:createPlatform(x, y, polygon, sprite, animations) - table.insert(self.Platforms, Platform:new(self, self.world, x, y, polygon, sprite, animations)) +-- 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)) end -- Add new naut to the world -- TODO: separate two methods for `not.Hero` and `not.Player`. --- TODO: follow new parameters in `not.(Player/Hero).new` based on `not.(Player/Hero).init`. -function World:createNaut(x, y, name) - local naut = Player:new(self, self.world, x, y, name) +function World:createNaut (x, y, name) + local naut = Player:new(name, self, x, y) table.insert(self.Nauts, naut) return naut end -- Add new decoration to the world --- TODO: follow new parameters in `not.Decoration.new` based on `not.Decoration.init`. -function World:createDecoration(x, y, sprite) +-- 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)) 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) +function World:createCloud (x, y, t, v) table.insert(self.Clouds, Cloud:new(x, y, t, v)) end -- Randomize Cloud creation -function World:randomizeCloud(outside) +function World:randomizeCloud (outside) if outside == nil then outside = true else @@ -179,18 +178,18 @@ end -- Add an effect behind nauts -- 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) +function World:createEffect (name, x, y) table.insert(self.Effects, Effect:new(name, x, y)) end -- Add a ray -function World:createRay(naut) +function World:createRay (naut) table.insert(self.Rays, Ray:new(naut, self)) end -- get Nauts functions -- more than -1 lives -function World:getNautsPlayable() +function World:getNautsPlayable () local nauts = {} for _,naut in pairs(self.Nauts) do if naut.lives > -1 then @@ -200,7 +199,7 @@ function World:getNautsPlayable() return nauts end -- are alive -function World:getNautsAlive() +function World:getNautsAlive () local nauts = {} for _,naut in self.Nauts do if naut.alive then @@ -210,17 +209,17 @@ function World:getNautsAlive() return nauts end -- all of them -function World:getNautsAll() +function World:getNautsAll () return self.Nauts end -- get Map name -function World:getMapName() +function World:getMapName () return self.map.name end -- Event: when player is killed -function World:onNautKilled(naut) +function World:onNautKilled (naut) self.camera:startShake() self:createRay(naut) local nauts = self:getNautsPlayable() @@ -232,14 +231,14 @@ function World:onNautKilled(naut) end end -function World:getBounce(f) +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) +function World:update (dt) -- Physical world self.world:update(dt) -- Camera @@ -291,7 +290,7 @@ function World:update(dt) end end -- Draw -function World:draw() +function World:draw () -- Camera stuff local offset_x, offset_y = self.camera:getOffsets() local scale = self.camera.scale @@ -382,7 +381,7 @@ end -- Box2D callbacks -- beginContact -function World.beginContact(a, b, coll) +function World.beginContact (a, b, coll) if a:getCategory() == 1 then local x,y = coll:getNormal() if y < -0.6 then @@ -408,7 +407,7 @@ function World.beginContact(a, b, coll) end end -- endContact -function World.endContact(a, b, coll) +function World.endContact (a, b, coll) if a:getCategory() == 1 then print(b:getUserData().name .. " is in air") -- Move them to Hero @@ -418,7 +417,7 @@ end -- Controller callbacks -- TODO: names of this methods don't follow naming patterns in this project. See `Controller` and change it. -function World:controlpressed(set, action, key) +function World:controlpressed (set, action, key) if key == "f6" and debug then local map = self:getMapName() local nauts = {} @@ -432,7 +431,7 @@ function World:controlpressed(set, action, key) naut:controlpressed(set, action, key) end end -function World:controlreleased(set, action, key) +function World:controlreleased (set, action, key) for k,naut in pairs(self:getNautsAll()) do naut:controlreleased(set, action, key) end -- cgit v1.1 From 527901f599c79047ab9a12fbc065a93faa8f872e Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 5 Apr 2017 19:32:26 +0200 Subject: Moved most player-input-related methods from Hero to Player --- not/Hero.lua | 150 +++------------------------------------------------------ not/Player.lua | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 148 insertions(+), 144 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index a0758ee..760a881 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -72,21 +72,9 @@ function Hero:init (name, world, x, y) end -- Update callback of `Hero` --- TODO: Explode this function (method, kek), move controler-related parts to `not.Player`, physics parts to `not.PhysicalBody`. function Hero:update (dt) PhysicalBody.update(self, dt) if self.body:isDestroyed() then return end - -- locals - local x, y = self:getLinearVelocity() - local isDown = Controller.isDown - local controlset = self:getControlSet() - - -- # VERTICAL MOVEMENT - -- Jumping - if self.isJumping and self.jumpTimer > 0 then - self:setLinearVelocity(x,-160) - self.jumpTimer = self.jumpTimer - dt - end -- Salto if self.salto and (self.current == self.animations.walk or self.current == self.animations.default) then @@ -95,54 +83,18 @@ function Hero:update (dt) self.angle = 0 end - -- # HORIZONTAL MOVEMENT - -- Walking - if isDown(controlset, "left") then - self.facing = -1 - self:applyForce(-250, 0) - -- Controlled speed limit - if x < -self.max_velocity then - self:applyForce(250, 0) - end - end - if isDown(controlset, "right") then - self.facing = 1 - self:applyForce(250, 0) - -- Controlled speed limit - if x > self.max_velocity then - self:applyForce(-250, 0) - end - end - - -- Custom linear damping - if not isDown(controlset, "left") and - not isDown(controlset, "right") - then - local face = nil - 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 - - -- # DEATH - -- We all die in the end. + -- Could you please die? + -- TODO: World/Map function for testing if Point is inside playable area. local m = self.world.map - if (self.body:getX() < m.center_x - m.width*1.5 or self.body:getX() > m.center_x + m.width*1.5 or - self.body:getY() < m.center_y - m.height*1.5 or self.body:getY() > m.center_y + m.height*1.5) and + local x, y = self:getPosition() + if (x < m.center_x - m.width*1.5 or x > m.center_x + m.width*1.5 or + y < m.center_y - m.height*1.5 or y > m.center_y + m.height*1.5) and self.alive then self:die() end - -- respawn + -- Respawn timer. if self.spawntimer > 0 then self.spawntimer = self.spawntimer - dt end @@ -179,94 +131,6 @@ function Hero:update (dt) end end --- Controller callbacks -function Hero:controlpressed (set, action, key) - if set ~= self:getControlSet() then return end - local isDown = Controller.isDown - local controlset = self:getControlSet() - -- Jumping - if action == "jump" then - if self.jumpCounter > 0 then - -- General jump logics - self.isJumping = true - --self:playSound(6) - -- Spawn proper effect - if not self.inAir then - self:createEffect("jump") - else - self:createEffect("doublejump") - end - -- Start salto if last jump - if self.jumpCounter == 1 then - self.salto = true - end - -- Animation clear - if (self.current == self.animations.attack) or - (self.current == self.animations.attack_up) or - (self.current == self.animations.attack_down) then - self:setAnimation("default") - end - -- Remove jump - self.jumpCounter = self.jumpCounter - 1 - end - end - - -- Walking - if (action == "left" or action == "right") and - (self.current ~= self.animations.attack) and - (self.current ~= self.animations.attack_up) and - (self.current ~= self.animations.attack_down) then - self:setAnimation("walk") - end - - -- Punching - if action == "attack" and self.punchCooldown <= 0 then - local f = self.facing - self.salto = false - if isDown(controlset, "up") then - -- Punch up - if self.current ~= self.animations.damage then - self:setAnimation("attack_up") - end - self:punch("up") - elseif isDown(controlset, "down") then - -- Punch down - if self.current ~= self.animations.damage then - self:setAnimation("attack_down") - end - self:punch("down") - else - -- Punch horizontal - if self.current ~= self.animations.damage then - self:setAnimation("attack") - end - if f == 1 then - self:punch("right") - else - self:punch("left") - end - self.punchdir = 1 - end - end -end -function Hero:controlreleased (set, action, key) - if set ~= self:getControlSet() then return end - local isDown = Controller.isDown - local controlset = self:getControlSet() - -- Jumping - if action == "jump" then - self.isJumping = false - self.jumpTimer = Hero.jumpTimer -- take initial from metatable - end - -- Walking - if (action == "left" or action == "right") and not - (isDown(controlset, "left") or isDown(controlset, "right")) and - self.current == self.animations.walk - then - self:setAnimation("default") - end -end - -- TODO: comment them and place them somewhere properly function Hero:getAngle () return self.angle @@ -275,7 +139,7 @@ function Hero:getHorizontalMirror () return self.facing end function Hero:getOffset () - return 12,15 -- TODO: WHY? How about creating body as polygon and using 0,0 instead. LIKE EVERYWHERE ELSE? Make it obsolete both in here and in `not.Sprite`. + return 12,15 end -- Draw of `Hero` diff --git a/not/Player.lua b/not/Player.lua index 5fc2adc..2bee903 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -11,7 +11,6 @@ Player.__index = Player setmetatable(Player, Hero) -- Constructor of `Player`. --- TODO: I'm sure it is a duplicate, but `not.World.create*` methods need to pass proper parameters. function Player:new (name, game, x, y) local o = setmetatable({}, self) o:init(name, game, x, y) @@ -36,3 +35,144 @@ end function Player:getControlSet () return self.controlset end + +-- Check if control of assigned controller is pressed. +function Player:isControlDown (control) + return Controller.isDown(self:getControlSet(), control) +end + +-- Update of `Player`. +function Player:update (dt) + local x, y = self:getLinearVelocity() + 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. + + -- Jumping. + if self.isJumping and self.jumpTimer > 0 then + self:setLinearVelocity(x,-160) + self.jumpTimer = self.jumpTimer - dt + end + + -- Walking. + if self:isControlDown("left") then + self.facing = -1 + self:applyForce(-250, 0) + -- Controlled speed limit + if x < -self.max_velocity then + self:applyForce(250, 0) + end + end + if self:isControlDown("right") then + self.facing = 1 + self:applyForce(250, 0) + -- Controlled speed limit + if x > self.max_velocity then + self:applyForce(-250, 0) + end + end + + -- Limiting walking speed. + if not self:isControlDown("left") and + not self:isControlDown("right") + then + local face = nil + 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 + +-- Controller callbacks. +function Player:controlpressed (set, action, key) + if set ~= self:getControlSet() then return end + local isDown = Controller.isDown + local controlset = self:getControlSet() + -- Jumping + if action == "jump" then + if self.jumpCounter > 0 then + -- General jump logics + self.isJumping = true + --self:playSound(6) + -- Spawn proper effect + if not self.inAir then + self:createEffect("jump") + else + self:createEffect("doublejump") + end + -- Start salto if last jump + if self.jumpCounter == 1 then + self.salto = true + end + -- Animation clear + if (self.current == self.animations.attack) or + (self.current == self.animations.attack_up) or + (self.current == self.animations.attack_down) then + self:setAnimation("default") + end + -- Remove jump + self.jumpCounter = self.jumpCounter - 1 + end + end + + -- Walking + if (action == "left" or action == "right") and + (self.current ~= self.animations.attack) and + (self.current ~= self.animations.attack_up) and + (self.current ~= self.animations.attack_down) then + self:setAnimation("walk") + end + + -- Punching + if action == "attack" and self.punchCooldown <= 0 then + local f = self.facing + self.salto = false + if isDown(controlset, "up") then + -- Punch up + if self.current ~= self.animations.damage then + self:setAnimation("attack_up") + end + self:punch("up") + elseif isDown(controlset, "down") then + -- Punch down + if self.current ~= self.animations.damage then + self:setAnimation("attack_down") + end + self:punch("down") + else + -- Punch horizontal + if self.current ~= self.animations.damage then + self:setAnimation("attack") + end + if f == 1 then + self:punch("right") + else + self:punch("left") + end + self.punchdir = 1 + end + end +end +function Player:controlreleased (set, action, key) + if set ~= self:getControlSet() then return end + local isDown = Controller.isDown + local controlset = self:getControlSet() + -- Jumping + if action == "jump" then + self.isJumping = false + self.jumpTimer = Hero.jumpTimer -- take initial from metatable + end + -- Walking + if (action == "left" or action == "right") and not + (isDown(controlset, "left") or isDown(controlset, "right")) and + self.current == self.animations.walk + then + self:setAnimation("default") + end +end -- cgit v1.1 From a46bdb0673461a69fe4467f794f0ddaab9449f85 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 5 Apr 2017 19:45:59 +0200 Subject: Hero no longer uses controler-related functions/methods --- not/Hero.lua | 6 ++---- not/Player.lua | 31 +++++++++++++++---------------- 2 files changed, 17 insertions(+), 20 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 760a881..6d0a202 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -20,6 +20,7 @@ Hero = { inAir = true, salto = false, isJumping = false, + isWalking = false, jumpTimer = 0.16, jumpCounter = 2, -- Statics @@ -166,12 +167,9 @@ end -- Change animation of `Hero` -- default, walk, attack, attack_up, attack_down, damage function Hero:goToNextFrame () - local isDown = Controller.isDown - local controlset = self:getControlSet() if self.current.repeated or not (self.frame == self.current.frames) then self.frame = (self.frame % self.current.frames) + 1 - elseif isDown(controlset, "right") or isDown(controlset, "left") then - -- If nonrepeatable animation is finished and player is walking + elseif self.isWalking then self:setAnimation("walk") elseif self.current == self.animations.damage then self:setAnimation("default") diff --git a/not/Player.lua b/not/Player.lua index 2bee903..515684f 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -92,8 +92,6 @@ end -- Controller callbacks. function Player:controlpressed (set, action, key) if set ~= self:getControlSet() then return end - local isDown = Controller.isDown - local controlset = self:getControlSet() -- Jumping if action == "jump" then if self.jumpCounter > 0 then @@ -122,24 +120,26 @@ function Player:controlpressed (set, action, key) end -- Walking - if (action == "left" or action == "right") and - (self.current ~= self.animations.attack) and - (self.current ~= self.animations.attack_up) and - (self.current ~= self.animations.attack_down) then - self:setAnimation("walk") + if (action == "left" or action == "right") then + self.isWalking = true + if (self.current ~= self.animations.attack) and + (self.current ~= self.animations.attack_up) and + (self.current ~= self.animations.attack_down) then + self:setAnimation("walk") + end end -- Punching if action == "attack" and self.punchCooldown <= 0 then local f = self.facing self.salto = false - if isDown(controlset, "up") then + if self:isControlDown("up") then -- Punch up if self.current ~= self.animations.damage then self:setAnimation("attack_up") end self:punch("up") - elseif isDown(controlset, "down") then + elseif self:isControlDown("down") then -- Punch down if self.current ~= self.animations.damage then self:setAnimation("attack_down") @@ -161,18 +161,17 @@ function Player:controlpressed (set, action, key) end function Player:controlreleased (set, action, key) if set ~= self:getControlSet() then return end - local isDown = Controller.isDown - local controlset = self:getControlSet() -- Jumping if action == "jump" then self.isJumping = false self.jumpTimer = Hero.jumpTimer -- take initial from metatable end -- Walking - if (action == "left" or action == "right") and not - (isDown(controlset, "left") or isDown(controlset, "right")) and - self.current == self.animations.walk - then - self:setAnimation("default") + 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") + end end end -- cgit v1.1 From 5ed018c810a5433851fefc2c45792de1da7e7ca8 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 01:01:07 +0200 Subject: Controller-related methods and variables in Player renamed --- not/Player.lua | 16 ++++++++-------- not/World.lua | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'not') diff --git a/not/Player.lua b/not/Player.lua index 515684f..719b77e 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -2,7 +2,7 @@ -- Special `not.Hero` controllable by a player. Player = { -- TODO: move functions and properties related to controls from `not.Hero`. - controlSet = --[[Controller.sets.*]]nil, + controllerSet = --[[Controller.sets.*]]nil, } -- `Player` is a child of `Hero`. @@ -29,16 +29,16 @@ function Player:init (...) end -- Controller set manipulation. -function Player:assignControlSet (set) - self.controlset = set +function Player:assignControllerSet (set) + self.controllerSet = set end -function Player:getControlSet () - return self.controlset +function Player:getControllerSet () + return self.controllerSet end -- Check if control of assigned controller is pressed. function Player:isControlDown (control) - return Controller.isDown(self:getControlSet(), control) + return Controller.isDown(self:getControllerSet(), control) end -- Update of `Player`. @@ -91,7 +91,7 @@ end -- Controller callbacks. function Player:controlpressed (set, action, key) - if set ~= self:getControlSet() then return end + if set ~= self:getControllerSet() then return end -- Jumping if action == "jump" then if self.jumpCounter > 0 then @@ -160,7 +160,7 @@ function Player:controlpressed (set, action, key) end end function Player:controlreleased (set, action, key) - if set ~= self:getControlSet() then return end + if set ~= self:getControllerSet() then return end -- Jumping if action == "jump" then self.isJumping = false diff --git a/not/World.lua b/not/World.lua index fc5e229..8aa9d28 100644 --- a/not/World.lua +++ b/not/World.lua @@ -118,7 +118,7 @@ function World:spawnNauts (nauts) for _,naut in pairs(nauts) do local x,y = self:getSpawnPosition() local spawn = self:createNaut(x, y, naut[1]) - spawn:assignControlSet(naut[2]) + spawn:assignControllerSet(naut[2]) end end -- cgit v1.1 From 2508af1856d4b30678e11fa610c0449bf2135ed0 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 16:09:50 +0200 Subject: Hero.alive -> Hero.isAlive; World.init method --- not/Hero.lua | 16 ++++++------ not/World.lua | 81 ++++++++++++++++++++++++++--------------------------------- 2 files changed, 44 insertions(+), 53 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 6d0a202..7c2555a 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -13,7 +13,7 @@ Hero = { combo = 0, lives = 3, spawntimer = 2, - alive = true, + isAlive = true, punchCooldown = 0.25, punchdir = 0, -- a really bad thing -- Movement @@ -90,7 +90,7 @@ function Hero:update (dt) local x, y = self:getPosition() if (x < m.center_x - m.width*1.5 or x > m.center_x + m.width*1.5 or y < m.center_y - m.height*1.5 or y > m.center_y + m.height*1.5) and - self.alive + self.isAlive then self:die() end @@ -99,7 +99,7 @@ function Hero:update (dt) if self.spawntimer > 0 then self.spawntimer = self.spawntimer - dt end - if self.spawntimer <= 0 and not self.alive and self.lives >= 0 then + if self.spawntimer <= 0 and not self.isAlive and self.lives >= 0 then self:respawn() end @@ -145,7 +145,7 @@ end -- Draw of `Hero` function Hero:draw (offset_x, offset_y, scale, debug) - if not self.alive then return end + if not self.isAlive then return end PhysicalBody.draw(self, offset_x, offset_y, scale, debug) end @@ -153,7 +153,7 @@ end -- elevation: 1 bottom, 0 top function Hero:drawHUD (x,y,scale,elevation) -- hud displays only if player is alive - if self.alive then + 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) @@ -241,7 +241,7 @@ function Hero:die () self:playSound(1) self.combo = Hero.combo -- INITIAL from metatable self.lives = self.lives - 1 - self.alive = false + self.isAlive = false self.spawntimer = Hero.spawntimer -- INITIAL from metatable self:setBodyActive(false) self.world:onNautKilled(self) @@ -249,7 +249,7 @@ end -- And then respawn. Like Jon Snow. function Hero:respawn () - self.alive = true + self.isAlive = true self:setLinearVelocity(0,0) self:setPosition(self.world:getSpawnPosition()) -- TODO: I'm not convinced about getting new position like this. self:setBodyActive(true) @@ -260,7 +260,7 @@ end -- Sounds -- TODO: Possibly export to nonexistent SoundEmitter class. Can be used by World (Stage), too. function Hero:playSound (sfx, force) - if self.alive or force then + if self.isAlive or force then local source = love.audio.newSource(self.sfx[sfx]) source:play() end diff --git a/not/World.lua b/not/World.lua index 8aa9d28..6e57ac1 100644 --- a/not/World.lua +++ b/not/World.lua @@ -1,18 +1,6 @@ --- `World` +--- `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`. - --- WHOLE CODE HAS FLAG OF "need a cleanup" - -require "not.Platform" -require "not.Player" -require "not.Cloud" -require "not.Effect" -require "not.Decoration" -require "not.Ray" - --- Metatable of `World` --- nils initialized in constructor World = { -- inside world = nil, @@ -23,6 +11,7 @@ World = { Effects = nil, Rays = nil, camera = nil, + isActive = true, -- cloud generator clouds_delay = 5, -- Map @@ -36,48 +25,54 @@ World = { music = nil } +World.__index = World + +require "not.Platform" +require "not.Player" +require "not.Cloud" +require "not.Effect" +require "not.Decoration" +require "not.Ray" + -- Constructor of `World` ZA WARUDO! -- TODO: push stuff to initialization method. function World:new (map, nauts) - -- Meta - local o = {} - setmetatable(o, self) - self.__index = self - -- Physical world initialization + 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) - o.world = love.physics.newWorld(0, 9.81*64, true) - o.world:setCallbacks(o.beginContact, o.endContact) - -- Empty tables for objects - -- TODO: DEAR DEER, do you see it? + self.world = love.physics.newWorld(0, 9.81*64, true) + self.world:setCallbacks(self.beginContact, self.endContact) + -- Tables for entities. TODO: DEAR DEER, do you see it? local n = {} - o.Nauts = n + self.Nauts = n local p = {} - o.Platforms = {} + self.Platforms = {} local c = {} - o.Clouds = c + self.Clouds = c local e = {} - o.Effects = e + self.Effects = e local d = {} - o.Decorations = d + self.Decorations = d local r = {} - o.Rays = r + self.Rays = r -- Random init; TODO: use LOVE2D's random. math.randomseed(os.time()) - -- Map + -- Map and misc. local map = map or "default" - o:loadMap(map) - -- Nauts - o:spawnNauts(nauts) - -- Create camera - o.camera = Camera:new(o) - -- Play music - o.music = Music:new(o.map.theme) - return o + self:loadMap(map) + self:spawnNauts(nauts) + self.camera = Camera:new(self) + self.music = Music:new(self.map.theme) end -- The end of the world function World:delete () - self.world:destroy() for _,platform in pairs(self.Platforms) do platform:delete() end @@ -85,7 +80,7 @@ function World:delete () naut:delete() end self.music:delete() - self = nil + self.world:destroy() end -- Load map from file @@ -202,7 +197,7 @@ end function World:getNautsAlive () local nauts = {} for _,naut in self.Nauts do - if naut.alive then + if naut.isAlive then table.insert(nauts, naut) end end @@ -239,9 +234,8 @@ end -- LÖVE2D callbacks -- Update ZU WARUDO function World:update (dt) - -- Physical world + self.world:update(dt) - -- Camera self.camera:update(dt) -- Engine world: Nauts, Grounds (kek) and Decorations - all Animateds (top kek) for _,naut in pairs(self.Nauts) do @@ -385,9 +379,7 @@ function World.beginContact (a, b, coll) if a:getCategory() == 1 then local x,y = coll:getNormal() if y < -0.6 then - -- TODO: remove debug messages -- TODO: move landing to `not.Hero` - print(b:getUserData().name .. " is not in air") -- Move them to Hero b:getUserData().inAir = false b:getUserData().jumpCounter = 2 @@ -409,7 +401,6 @@ end -- endContact function World.endContact (a, b, coll) if a:getCategory() == 1 then - print(b:getUserData().name .. " is in air") -- Move them to Hero b:getUserData().inAir = true end -- cgit v1.1 From 970655737ce7f497f0725d28605fc296c9923a64 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 16:16:05 +0200 Subject: Hotfix for destroyed body crash --- not/Player.lua | 4 ++-- not/World.lua | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'not') diff --git a/not/Player.lua b/not/Player.lua index 719b77e..30057bb 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -43,9 +43,9 @@ end -- Update of `Player`. function Player:update (dt) - local x, y = self:getLinearVelocity() 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. - + if self.body:isDestroyed() then return end + local x, y = self:getLinearVelocity() -- Jumping. if self.isJumping and self.jumpTimer > 0 then self:setLinearVelocity(x,-160) diff --git a/not/World.lua b/not/World.lua index 6e57ac1..d234413 100644 --- a/not/World.lua +++ b/not/World.lua @@ -11,7 +11,6 @@ World = { Effects = nil, Rays = nil, camera = nil, - isActive = true, -- cloud generator clouds_delay = 5, -- Map @@ -234,7 +233,6 @@ end -- LÖVE2D callbacks -- Update ZU WARUDO function World:update (dt) - self.world:update(dt) self.camera:update(dt) -- Engine world: Nauts, Grounds (kek) and Decorations - all Animateds (top kek) -- cgit v1.1 From eff39f1f49d5223cb27b5874f8ac29ce8a2ef85f Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 16:28:21 +0200 Subject: Moved damping back to Hero from Player --- not/Hero.lua | 17 +++++++++++++++++ not/Player.lua | 18 ------------------ 2 files changed, 17 insertions(+), 18 deletions(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index 7c2555a..a22cc2e 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -84,6 +84,23 @@ 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 diff --git a/not/Player.lua b/not/Player.lua index 30057bb..2a4b2e6 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -69,24 +69,6 @@ function Player:update (dt) self:applyForce(-250, 0) end end - - -- Limiting walking speed. - if not self:isControlDown("left") and - not self:isControlDown("right") - then - local face = nil - 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 -- Controller callbacks. -- cgit v1.1 From 9a5b629d8c1719af62667235c59950862b46b771 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 18:30:42 +0200 Subject: World clean-up init and table --- not/World.lua | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'not') diff --git a/not/World.lua b/not/World.lua index d234413..579fff2 100644 --- a/not/World.lua +++ b/not/World.lua @@ -2,15 +2,14 @@ -- 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 = { - -- inside - world = nil, - Nauts = nil, - Platforms = nil, - Clouds = nil, - Decorations = nil, - Effects = nil, - Rays = nil, - camera = nil, + 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 @@ -34,7 +33,6 @@ require "not.Decoration" require "not.Ray" -- Constructor of `World` ZA WARUDO! --- TODO: push stuff to initialization method. function World:new (map, nauts) local o = setmetatable({}, self) o:init(map, nauts) @@ -47,19 +45,13 @@ function World:init (map, nauts) love.physics.setMeter(64) self.world = love.physics.newWorld(0, 9.81*64, true) self.world:setCallbacks(self.beginContact, self.endContact) - -- Tables for entities. TODO: DEAR DEER, do you see it? - local n = {} - self.Nauts = n - local p = {} + -- Tables for entities. TODO: It is still pretty bad! + self.Nauts = {} self.Platforms = {} - local c = {} - self.Clouds = c - local e = {} - self.Effects = e - local d = {} - self.Decorations = d - local r = {} - self.Rays = r + self.Clouds = {} + self.Effects = {} + self.Decorations = {} + self.Rays = {} -- Random init; TODO: use LOVE2D's random. math.randomseed(os.time()) -- Map and misc. -- cgit v1.1 From 4cf1755d05a63452feca03d2e380b149fcaa76c2 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 18:40:27 +0200 Subject: Moved music.lua, cleaned-up its code; Fixed requires for main, World and Menu --- not/Music.lua | 25 +++++++++++++++++++++++++ not/World.lua | 1 + 2 files changed, 26 insertions(+) create mode 100644 not/Music.lua (limited to 'not') diff --git a/not/Music.lua b/not/Music.lua new file mode 100644 index 0000000..ee930f4 --- /dev/null +++ b/not/Music.lua @@ -0,0 +1,25 @@ +--- `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/World.lua b/not/World.lua index 579fff2..2a6106c 100644 --- a/not/World.lua +++ b/not/World.lua @@ -31,6 +31,7 @@ require "not.Cloud" require "not.Effect" require "not.Decoration" require "not.Ray" +require "not.Music" -- Constructor of `World` ZA WARUDO! function World:new (map, nauts) -- cgit v1.1 From 93d3257ff23331c308fb32940557911a29ca608b Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 19:22:26 +0200 Subject: Menu cleaned-up and moved to /not/ Menu is now similar to new code Menu.load => Menu.open --- not/Menu.lua | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 not/Menu.lua (limited to 'not') diff --git a/not/Menu.lua b/not/Menu.lua new file mode 100644 index 0000000..1be6f00 --- /dev/null +++ b/not/Menu.lua @@ -0,0 +1,144 @@ +--- `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.__index = Menu + +require "not.Music" + +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") + end + o:init(name) + return o +end + +function Menu:init (name) + self.music = Music:new("menu.ogg") + self:open(name) +end + +function Menu:delete () + self.music:delete() +end + +function Menu:open (name) + local name = name or "menumain" + self.active = Menu.active --Menu.active is initial + self.elements = love.filesystem.load(string.format("config/%s.lua", name))(self) + self.elements[self.active]:focus() +end + +-- Return reference to quads table and menu sprite +function Menu:getSheet () + return self.sprite, self.quads +end + +-- Cycle elements +function Menu:next () + self.elements[self.active]:blur() + self.active = (self.active%#self.elements)+1 + if not self.elements[self.active]:focus() then + self:next() + end +end +function Menu:previous () + self.elements[self.active]:blur() + if self.active == 1 then + self.active = #self.elements + else + self.active = self.active - 1 + end + if not self.elements[self.active]:focus() then + self:previous() + end +end + +-- LÖVE2D callbacks +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) + end +end + +-- Controller callbacks +function Menu:controlpressed (set, action, key) + if self.allowMove then + if action == "down" then + self:next() + end + if action == "up" then + self:previous() + end + end + for _,element in pairs(self.elements) do + element:controlpressed(set, action, key) + end +end +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 -- cgit v1.1 From a013d70115171764ce0941fbbe18a5447447a7eb Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 19:59:51 +0200 Subject: Settings moved to /not/, cleaned-up a bit --- not/Settings.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 not/Settings.lua (limited to 'not') diff --git a/not/Settings.lua b/not/Settings.lua new file mode 100644 index 0000000..e3316f9 --- /dev/null +++ b/not/Settings.lua @@ -0,0 +1,72 @@ +--- `Settings` +-- Stores, loads, saves and changes game settings including Controller sets. +Settings = { + current = {} +} + +function Settings.load() + 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() + Controller.reset() + local joysticksList = love.joystick.getJoysticks() -- local list for editing + for _,set in pairs(Settings.current) do + local isJoystick = set[7] + local joystick + if isJoystick then + -- take and remove first joystick from list + joystick = joysticksList[1] + 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) + end + end + end +end + +function Settings.save() + local new = love.filesystem.newFile("settings") + local sets = Settings.current + local string = "return {\n" + for i,set in pairs(sets) do + string = string .. "\t{" + for j,word in pairs(set) do + if j ~= 7 then + string = string .. "\"" .. word .. "\", " + else + if word then + string = string .. "true" + else + string = string .. "false" + end + end + end + string = string .. "},\n" + end + string = string .. "}" + new:open("w") + new:write(string) + new:close() +end + +function Settings.change(n, left, right, up, down, attack, jump, joystick) + local bool + if joystick then + bool = true + else + bool = false + end + -- Save current settings + Settings.current[n] = {left, right, up, down, attack, jump, bool} + Settings.save() + -- Load settings + Settings.load() +end -- cgit v1.1 From 33b3a93138a9eee40727e9f56373b434ceab1cef Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 20:03:17 +0200 Subject: Controller moved to /not/ and cleaned-up a bit. --- not/Controller.lua | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 not/Controller.lua (limited to 'not') diff --git a/not/Controller.lua b/not/Controller.lua new file mode 100644 index 0000000..8a2a863 --- /dev/null +++ b/not/Controller.lua @@ -0,0 +1,201 @@ +--- `Controller` +-- Module to manage player input. +-- It uses `love.keypressed`, `love.keyreleased`, `love.gamepadreleased`, `love.gamepadpressed`, `love.joystickadded`, so be sure not to use them by yourself. +-- Rather than that use functions provided by this module: `Controller.controlpressed` and `Controller.controlreleased`. +Controller = { + sets = {}, + axes = {}, + deadzone = .3 +} + +-- Declared to avoid calling nil. Be sure to define yours after this line is performed. +function Controller.controlpressed(set, action, key) end +function Controller.controlreleased(set, action, key) end + +-- Create new controls set. +function Controller.registerSet(left, right, up, down, attack, jump, joystick) + if not Controller.isJoystickUnique(joystick) then return end + local set = {} + set.left = left or "left" + set.right = right or "right" + set.up = up or "up" + set.down = down or "down" + set.attack = attack or "return" + set.jump = jump or "rshift" + set.joystick = joystick + table.insert(Controller.sets, set) + print(set, left, right, up, down, attack, jump, joystick) + return set +end + +-- Reset table of controls sets. +function Controller.reset() + local t = {} + Controller.sets = t +end + +-- Get table of controls sets. +function Controller.getSets() + return Controller.sets +end + +-- Checks if given joystick is unique in current set of Controller sets +function Controller.isJoystickUnique(joystick) + if joystick ~= nil then + for _,set in pairs(Controller.sets) do + if set.joystick == joystick then return false end + end + end + return true +end + +-- Tests all sets if they have control assigned to given key and joystick. +function Controller.testSets(key, joystick) + for i,set in pairs(Controller.sets) do + local action = Controller.testControl(set, key, joystick) + if action ~= nil then + return set, action + end + end + return nil, nil +end + +-- Tests given set if it has controll assigned to given key and joystick. +function Controller.testControl(set, key, joystick) + -- First test if it is joystick and if it is correct one + if joystick == set.joystick then + if key == set.left then + return "left" + elseif key == set.right then + return "right" + elseif key == set.up then + return "up" + elseif key == set.down then + return "down" + elseif key == set.attack then + return "attack" + elseif key == set.jump then + return "jump" + end + end +end + +-- Checks if given action of given set is down +function Controller.isDown(set, action) + if set ~= nil then + if set.joystick == nil then + return love.keyboard.isDown(set[action]) + else + if not Controller.isAxis(set[action]) then + return set.joystick:isGamepadDown(set[action]) + else + return Controller.getAxisState(set.joystick, set[action]) + end + end + end +end + +-- Return key name from given axis and value +function Controller.createAxisName(axis, value) + local key = "axis:"..axis + if value == 0 then + key = key.."0" + elseif value > 0 then + key = key.."+" + else + key = key.."-" + end + return key +end + +-- Checks if given key is an axis +function Controller.isAxis(key) + if string.find(key, "axis:") then + return true + else + return false + end +end + +-- Checks state of key assigned to axis of given joystick +function Controller.getAxisState(joystick, key) + if Controller.axes[joystick] then + local state = Controller.axes[joystick][key] + if state ~= nil then + return state + else + return false + end + end +end + +-- Sets state of key assigned to axis of given joystick +function Controller.setAxisState(joystick, key, state) + if Controller.axes[joystick] == nil then + Controller.axes[joystick] = {} + end + Controller.axes[joystick][key] = state +end + +-- Simulate pressing key on an axis +function Controller.axisPress(joystick, axis, value) + local key = Controller.createAxisName(axis, value) + local set, action = Controller.testSets(key, joystick) + local state = Controller.getAxisState(joystick, key) + if not state then + print(joystick, set, action, key) + Controller.setAxisState(joystick, key, true) + Controller.controlpressed(set, action, key) + end +end + +-- Simulate releasing key on an axis +function Controller.axisRelease(joystick, axis, value) + local key = Controller.createAxisName(axis, value) + local set, action = Controller.testSets(key, joystick) + local state = Controller.getAxisState(joystick, key) + if state then + Controller.setAxisState(joystick, key,false) + Controller.controlreleased(set, action, key) + end +end + +-- Callbacks from LÖVE2D +-- Load gamepad mappings from db file and init module +function Controller.load() + love.joystick.loadGamepadMappings("gamecontrollerdb.txt") +end + +-- Gamepad input callbacks +function Controller.gamepadaxis(joystick, axis, value) + if value ~= 0 then + if math.abs(value) > Controller.deadzone then + Controller.axisPress(joystick, axis, value) + else + Controller.axisRelease(joystick, axis, value) + end + else + Controller.axisRelease(joystick, axis, 1) + Controller.axisRelease(joystick, axis, -1) + end +end +function Controller.gamepadpressed(joystick, key) + local set, action = Controller.testSets(key, joystick) + print(joystick, set, action, key) + Controller.controlpressed(set, action, key) +end +function Controller.gamepadreleased(joystick, key) + local set, action = Controller.testSets(key, joystick) + Controller.controlreleased(set, action, key) +end + +-- Keyboard input callbacks +function Controller.keypressed(key) + local set, action = Controller.testSets(key, nil) + print(nil, set, action, key) + Controller.controlpressed(set, action, key) +end +function Controller.keyreleased(key) + local set, action = Controller.testSets(key, nil) + Controller.controlreleased(set, action, key) +end \ No newline at end of file -- cgit v1.1 From e9396447537e0c6dedfb55e9a7ffc1e99575d860 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 20:06:12 +0200 Subject: Camera moved to /not/ and cleaned-up a bit --- not/Camera.lua | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 not/Camera.lua (limited to 'not') diff --git a/not/Camera.lua b/not/Camera.lua new file mode 100644 index 0000000..63489f3 --- /dev/null +++ b/not/Camera.lua @@ -0,0 +1,145 @@ +--- `Camera` +-- Used in drawing. +Camera = { + x = 0, + y = 0, + dest_x = 0, + dest_y = 0, + scale = getScale(), + scaler = getRealScale(), + shake = 0, + timer = 0, + delay = 0, + origin_x = 0, + origin_y = 0, + shake_x = 0, + shake_y = 0, + world = --[[not.World]]nil, +} + +-- Constructor of `Camera` +function Camera:new (world) + local o = {} + setmetatable(o, self) + self.__index = self + o.world = world + o:setPosition(o:follow()) + o:setDestination(o:follow()) + return o +end + +-- Drawing offsets +function Camera:getOffsets () + return -self.x,-self.y +end + +-- Position +function Camera:setPosition (x, y) + local x = x or 0 + local y = y or 0 + self.x, self.y = x, y +end + +function Camera:getPosition () + return self.x, self.y +end + +function Camera:getPositionScaled () + return self.x*self.scale, self.y*self.scale +end + +-- Destination +function Camera:setDestination (x, y) + local x = x or 0 + local y = y or 0 + self.dest_x, self.dest_y = x, y +end + +function Camera:getDestination () + return self.dest_x, self.dest_y +end + +-- Translate points +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 +end + +function Camera:translatePoints(...) + local a = {...} + local r = {} + local x,y = self:getOffsets() + for k,v in pairs(a) do + if k%2 == 1 then + table.insert(r, (v + x) * self.scale) + else + table.insert(r, (v + y) * self.scale) + end + end + return r +end + +-- Shake it +-- Really bad script, but for now it works +function Camera:shake () + if self.shake_x == 0 then + self.shake_x = math.random(-10, 10) * 2 + elseif self.shake_x > 0 then + self.shake_x = math.random(-10, -1) * 2 + elseif self.shake_x < 0 then + self.shake_x = math.random(10, 1) * 2 + end + if self.shake_y == 0 then + self.shake_y = math.random(-10, 10) * 2 + elseif self.shake_y > 0 then + self.shake_y = math.random(-10, -1) * 2 + elseif self.shake_y < 0 then + self.shake_y = math.random(10, 1) * 2 + end + local x = self.origin_x + self.shake_x + local y = self.origin_y + self.shake_y + self:setDestination(x, y) +end + +function Camera:startShake () + self.timer = 0.3 + self.origin_x, self.origin_y = self:getPosition() +end + +-- Move follow +function Camera:follow () + local map = self.world.map + local sum_x,sum_y,i = map.center_x, map.center_y, 1 + for k,naut in pairs(self.world.Nauts) do + local naut_x,naut_y = naut:getPosition() + if math.abs(naut_x - map.center_x) < map.width/2 and + math.abs(naut_y - map.center_y) < map.height/2 then + i = i + 1 + sum_x = naut_x + sum_x + 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 + return x,y +end + +-- Update +function Camera:update (dt) + if self.timer > 0 then + self.timer = self.timer - dt + if self.delay <= 0 then + self:shake() + self.delay = 0.02 + else + self.delay = self.delay - dt + end + else + self:setDestination(self:follow()) + end + local dx, dy = self:getDestination() + dx = (dx - self.x) * 6 * dt + dy = (dy - self.y) * 6 * dt + self:setPosition(self.x + dx, self.y + dy) +end -- cgit v1.1 From 37767d6c54ea6af0b4631b5ce838ee584603895e Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 7 Apr 2017 02:53:36 +0200 Subject: Moved menu configs to subdirectory --- not/Menu.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'not') diff --git a/not/Menu.lua b/not/Menu.lua index 1be6f00..8ef1861 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -63,9 +63,9 @@ function Menu:delete () end function Menu:open (name) - local name = name or "menumain" + local name = name or "main" self.active = Menu.active --Menu.active is initial - self.elements = love.filesystem.load(string.format("config/%s.lua", name))(self) + self.elements = love.filesystem.load(string.format("config/menus/%s.lua", name))(self) self.elements[self.active]:focus() end -- cgit v1.1 From 54e85dd188af15cd5f3f5e08f5d3e69088a909b1 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 7 Apr 2017 03:06:26 +0200 Subject: Moved map configs to config directory --- not/World.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'not') diff --git a/not/World.lua b/not/World.lua index 2a6106c..bbceec4 100644 --- a/not/World.lua +++ b/not/World.lua @@ -79,8 +79,7 @@ end -- TODO: Change current map model to function-based one. function World:loadMap (name) local name = name or "default" - name = "maps/" .. name .. ".lua" - local map = love.filesystem.load(name) + local map = love.filesystem.load(string.format("config/maps/%s.lua", name)) self.map = map() -- Platforms for _,platform in pairs(self.map.platforms) do -- cgit v1.1 From d1a19fea50aefc9d7fb52568a5bdcfb56d75eccf Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 7 Apr 2017 03:24:40 +0200 Subject: Moved menu elements to /not/ --- not/Button.lua | 76 +++++++++++++++ not/Element.lua | 50 ++++++++++ not/Header.lua | 48 +++++++++ not/Selector.lua | 290 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 464 insertions(+) create mode 100644 not/Button.lua create mode 100644 not/Element.lua create mode 100644 not/Header.lua create mode 100644 not/Selector.lua (limited to 'not') diff --git a/not/Button.lua b/not/Button.lua new file mode 100644 index 0000000..91aca45 --- /dev/null +++ b/not/Button.lua @@ -0,0 +1,76 @@ +--- `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` is a child of `Element`. +require "not.Element" +Button.__index = Button +setmetatable(Button, Element) + +function Button:new (parent) + local o = setmetatable({}, self) + o.parent = parent + o.sprite, o.quads = parent:getSheet() + return o +end + +function Button:setText (text) + self.text = text or "" + return self +end + +function Button:focus(next) + self.focused = true + return true +end +function Button:blur () + self.focused = false +end + +function Button:active () end +function Button:isEnabled () + return true +end + +function Button:draw (scale) + local x,y = self:getPosition() + local quad = self.quads + local sprite = self.sprite + if self:isEnabled() then + love.graphics.setColor(255, 255, 255, 255) + else + love.graphics.setColor(140, 140, 140, 255) + end + love.graphics.draw(sprite, quad.button.normal, x*scale, y*scale, 0, scale, scale) + if self.focused then + love.graphics.draw(sprite, quad.arrow_l, (x+54+math.floor(self.delay))*scale, (y+5)*scale, 0, scale, scale) + love.graphics.draw(sprite, quad.arrow_r, (x-2-math.floor(self.delay))*scale, (y+5)*scale, 0, scale, scale) + end + love.graphics.setFont(Font) + love.graphics.printf(self.text, (x+2)*scale, (y+4)*scale, 54, "center", 0, scale, scale) +end + +function Button:update (dt) + self.delay = self.delay + dt + if self.delay > Button.delay then -- Button.delay is initial + self.delay = self.delay - Button.delay + end +end + +function Button:controlpressed (set, action, key) + if action == "attack" and self.focused and self:isEnabled() then + self:active() + end +end + +return Button diff --git a/not/Element.lua b/not/Element.lua new file mode 100644 index 0000000..e6d91da --- /dev/null +++ b/not/Element.lua @@ -0,0 +1,50 @@ +--- `Element` +-- Empty element used inside `Menu`. +Element = { + parent = --[[not.Menu]]nil, + x = 0, + y = 0 +} + +Element.__index = Element + +function Element:new (parent) + local o = setmetatable({}, self) + o.parent = parent + return o +end + +function Element:delete () end -- deletes Element + +function Element:getPosition () + return self.x, self.y +end +function Element:setPosition (x, y) + self.x = x or 0 + self.y = y or 0 + return self +end + +function Element:set (name, func) + if type(name) == "string" and func ~= nil then + self[name] = func + end + return self +end + +-- Called when menu tries to focus on this element. +-- If it will return false then menu will skip element and go to next in list. +function Element:focus () + return false +end +function Element:blur () end -- Called when Element loses focus. + +-- LÖVE2D callbacks +function Element:draw (scale) end +function Element:update (dt) end + +-- Controller callbacks +function Element:controlpressed (set, action, key) end +function Element:controlreleased (set, action, key) end + +return Element diff --git a/not/Header.lua b/not/Header.lua new file mode 100644 index 0000000..a563ab2 --- /dev/null +++ b/not/Header.lua @@ -0,0 +1,48 @@ +--- `Header` +-- Swinging title. +Header = { + parent = --[[not.Menu]]nil, + x = 0, + y = 0, + text = "", + bounce = 2, +} + +-- `Header` is a child of `Element`. +require "not.Element" +Header.__index = Header +setmetatable(Header, Element) + +function Header:new (parent) + local o = setmetatable({}, self) + o.parent = parent + return o +end + +function Header:setText (text) + self.text = text or "" + return self +end + +function Header:getBounce (f) + local f = f or 1 + return math.sin(self.bounce*f*math.pi) +end + +-- LÖVE2D callbacks +function Header:draw (scale) + local angle = self:getBounce(2) + local dy = self:getBounce()*4 + local x,y = self:getPosition() + love.graphics.setColor(255,255,255,255) + love.graphics.setFont(Bold) + love.graphics.printf(string.upper(self.text),x*scale,(y+dy)*scale,400,"center",(angle*5)*math.pi/180,scale,scale,200,12) +end +function Header:update (dt) + self.bounce = self.bounce + dt*0.7 + if self.bounce > Header.bounce then -- Header.bounce is initial + self.bounce = self.bounce - Header.bounce + end +end + +return Header diff --git a/not/Selector.lua b/not/Selector.lua new file mode 100644 index 0000000..8e03457 --- /dev/null +++ b/not/Selector.lua @@ -0,0 +1,290 @@ +--- `Selector` +-- Used in Menu for selecting various things from list. Works for each Controller set or globally. +--[[ +How to use `Selector` in `Menu` config file? +selector:new(menu) + :setPosition(x, y) + :setMargin(8) -- each block has marigin on both sides; they do stack + :setSize(32, 32) -- size of single graphics frame + :set("list", require "nautslist") + :set("icons_i", love.graphics.newImage("assets/portraits.png")) + :set("icons_q", require "portraits") + :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` is a child of `Element`. +require "not.Element" +Selector.__index = Selector +setmetatable(Selector, Element) + +-- Constructor +function Selector:new (parent) + local o = setmetatable({}, self) + o.parent = parent + o.sprite, o.quads = parent:getSheet() + return o +end + +-- Size of single block +function Selector:getSize () + return self.width, self.height +end +function Selector:setSize (width, height) + self.width, self.height = width, height + return self +end + +-- Spacing between two blocks +function Selector:getMargin () + return self.margin +end +function Selector:setMargin (margin) + self.margin = margin + return self +end + +-- Initialize Selector with current settings. +function Selector:init () + -- Make sure that there is list present + if self.list == nil then + self.list = {} + end + -- Initialize global Selector + if self.global then + self.sets = {} + self.locks = {false} + self.selections = {1} + -- Initialize Selector for Controllers + else + self.sets = Controller.getSets() + self.locks = {} + self.selections = {} + for n=1,#self.sets do + self.locks[n] = false + self.selections[n] = 1 + end + end + return self +end + +-- Cycle through list on given number +function Selector:next (n) + local current = self.selections[n] + self:setSelection(n, current + 1) +end +function Selector:previous (n) + local current = self.selections[n] + self:setSelection(n, current - 1) +end + +-- Get number associated with a given set +function Selector:checkNumber (set) + if self.global then return 1 end -- For global Selector + for n,check in pairs(self.sets) do + if check == set then return n end + end +end + +-- Check if given number is locked +function Selector:isLocked (n) + local n = n or 1 + return self.locks[n] +end + +-- Sets value of selection of given number. Returns old. +function Selector:setSelection (n, new) + -- Functception. It sounds like fun but it isn't. + local function limit(new, total) + if new > total then + return limit(new - total, total) + elseif new < 1 then + return limit(total + new, total) + else + return new + end + end + local n = n or 1 + local old = self.selections[n] + self.selections[n] = limit(new, #self.list) + return old +end + +-- Get value of selection of given number +function Selector:getSelection (n) + local n = n or 1 + return self.selections[n] +end + +-- Get value from list by selection +function Selector:getListValue (i) + return self.list[i] +end + +-- Checks if selection of given number is unique within Selector scope. +function Selector:isUnique (n) + local selection = self:getSelection(n) + for fn,v in pairs(self.selections) do + if fn ~= n and self:isLocked(fn) and v == selection then + return false + end + end + return true +end + +-- Get list of selections, checks if not locked are allowed. +function Selector:getFullSelection (allowed) + local allowed = allowed + if allowed == nil then allowed = false end + local t = {} + for n,v in pairs(self.selections) do + local name = self:getListValue(self:getSelection(n)) + local locked = self:isLocked(n) + if locked or allowed then + local a = {name} + if self.sets[n] then table.insert(a, self.sets[n]) end + table.insert(t, a) + end + end + return t +end + +-- Rolls and returns random selection from list that is not locked. +function Selector:rollRandom (avoids) + -- Me: You should make it simpler. + -- Inner me: Nah, it works. Leave it. + -- Me: Ok, let's leave it as it is. + local avoids = avoids or {} + local total = #self.list + local random = love.math.random(1, total) + local eligible = true + for _,avoid in ipairs(avoids) do + if random == avoid then + eligible = false + break + end + end + if not eligible or self:isLocked(random) then + table.insert(avoids, random) + return self:rollRandom(avoid) + else + return random + end +end + +-- Draw single block of Selector +function Selector:drawBlock (n, x, y, scale) + if self.quads == nil or self.sprite == nil then return end + local x, y = x or 0, y or 0 + local name = self:getListValue(self:getSelection(n)) + local locked = self:isLocked(n) + local sprite = self.sprite + local quad = self.quads + local icon = self.icons_i + local iconq = self.icons_q[name] + local w,h = self:getSize() + local unique = self:isUnique(n) + if unique then + love.graphics.setColor(255, 255, 255, 255) + else + love.graphics.setColor(140, 140, 140, 255) + end + if not locked then + love.graphics.draw(sprite, quad[self.shape].normal, x*scale, y*scale, 0, scale, scale) + else + love.graphics.draw(sprite, quad[self.shape].active, x*scale, y*scale, 0, scale, scale) + end + love.graphics.draw(icon, iconq, (x+2)*scale, (y+3)*scale, 0, scale, scale) + if self.focused then + local dy = (h-6)/2 + if not locked then + love.graphics.draw(sprite, quad.arrow_l, (x+0-2-math.floor(self.delay))* scale, (y+dy)*scale, 0, scale, scale) + love.graphics.draw(sprite, quad.arrow_r, (x+w-4+math.floor(self.delay))*scale, (y+dy)*scale, 0, scale, scale) + else + love.graphics.draw(sprite, quad.arrow_r, (x+0-2-math.floor(self.delay))* scale, (y+dy)*scale, 0, scale, scale) + love.graphics.draw(sprite, quad.arrow_l, (x+w-4+math.floor(self.delay))*scale, (y+dy)*scale, 0, scale, scale) + end + end + if (self:getSelection(n) ~= 1 or self.first) then + love.graphics.setFont(Font) + love.graphics.setColor(255, 255, 255, 255) + love.graphics.printf(string.upper(name), (x-w)*scale, (y+h+1)*scale, w*3, "center", 0, scale, scale) + end +end + +-- Menu callbacks +function Selector:focus () -- Called when Element gains focus + self.focused = true + return true +end +function Selector:blur () -- Called when Element loses focus + self.focused = false +end + +-- LÖVE2D callbacks +function Selector:draw (scale) + local x,y = self:getPosition() + local margin = self:getMargin() + local width = self:getSize() + x = x - #self.selections*0.5*(margin+margin+width) + for n=1,#self.selections do + self:drawBlock(n, x+(margin+width)*(n-1)+margin*n, y, scale) + end +end +function Selector:update (dt) + self.delay = self.delay + dt + if self.delay > Selector.delay then -- Selector.delay is initial + self.delay = self.delay - Selector.delay + end +end + +-- Controller callbacks +-- TODO: Add action to perform when key is pressed and selector is locked in e.g. to move into character selection from map selection. +function Selector:controlpressed (set, action, key) + if set and self.focused then + local n = self:checkNumber(set) + local locked = self:isLocked(n) + if action == "left" and not locked then self:previous(n) end + if action == "right" and not locked then self:next(n) end + if action == "attack" then + local name = self:getListValue(self:getSelection(n)) + if name == "random" then + self:setSelection(n, self:rollRandom({1,2})) -- avoid empty naut + self.locks[n] = true + else + -- If not empty or if first is allowed. Additionaly must be unique selection. + if (self:getSelection(n) ~= 1 or self.first) and self:isUnique(n) then + self.locks[n] = true + end + end + end + if action == "jump" then + if locked then + self.locks[n] = false + end + end + end +end + +return Selector -- cgit v1.1 From 1c3040de93f9d5a164ccb06194eacc28eead0234 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 7 Apr 2017 21:05:41 +0200 Subject: Maps and nauts list moved to config Implemented functions to create icons list for them Changed so game uses these functions now --- not/Hero.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'not') diff --git a/not/Hero.lua b/not/Hero.lua index a22cc2e..4150024 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -26,7 +26,7 @@ Hero = { -- Statics portrait_sprite = nil, portrait_frame = nil, - portrait_sheet = require "nautsicons", + portrait_sheet = getNautsIconsList(), portrait_box = love.graphics.newQuad( 0, 15, 32,32, 80,130), sfx = require "sounds", } -- cgit v1.1 From a03c1125f10fbbad253a0efc4727072fcbd55345 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 8 Apr 2017 20:38:45 +0200 Subject: Moved configs from root directory to config directory --- not/Effect.lua | 2 +- not/Hero.lua | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'not') diff --git a/not/Effect.lua b/not/Effect.lua index 4051b92..dd7570a 100644 --- a/not/Effect.lua +++ b/not/Effect.lua @@ -24,7 +24,7 @@ end -- Initializer of `Effect`. function Effect:init (name, x, y) Decoration.init(self, x, y, nil) - self:setAnimationsList(require("effects")) + self:setAnimationsList(require("config.animations.effects")) self:setAnimation(name) end diff --git a/not/Hero.lua b/not/Hero.lua index 4150024..feb61da 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -28,7 +28,7 @@ Hero = { portrait_frame = nil, portrait_sheet = getNautsIconsList(), portrait_box = love.graphics.newQuad( 0, 15, 32,32, 80,130), - sfx = require "sounds", + sfx = require "config.sounds", } -- `Hero` is a child of `PhysicalBody`. @@ -68,7 +68,7 @@ function Hero:init (name, world, x, y) self.world = world self.punchCooldown = 0 self.name = name - self:setAnimationsList(require("animations")) + self:setAnimationsList(require("config.animations.hero")) self:createEffect("respawn") end -- cgit v1.1