From eb8b36111c308deed093c1eda6794cb52fcbe227 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 17 May 2016 00:55:02 +0200 Subject: Effects added --- assets/effects.png | Bin 0 -> 1440 bytes effect.lua | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 5 ++- player.lua | 2 ++ world.lua | 46 ++++++++++++++++++++++--- 5 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 assets/effects.png create mode 100644 effect.lua diff --git a/assets/effects.png b/assets/effects.png new file mode 100644 index 0000000..90bc456 Binary files /dev/null and b/assets/effects.png differ diff --git a/effect.lua b/effect.lua new file mode 100644 index 0000000..6651916 --- /dev/null +++ b/effect.lua @@ -0,0 +1,97 @@ +-- `Effect` +-- Short animation with graphics that plays in various situation. + +-- Metatable of `Effect` +-- nils initialized in constructor +Effect = { + x = 0, + y = 0, + delay = 0.08, + initial = nil, + frame = 1, + animation = nil, + sprite = love.graphics.newImage("assets/effects.png"), + quads = { + jump = { + [1] = love.graphics.newQuad( 0, 0, 24,24, 168,120), + [2] = love.graphics.newQuad( 24, 0, 24,24, 168,120), + [3] = love.graphics.newQuad( 48, 0, 24,24, 168,120), + [4] = love.graphics.newQuad( 72, 0, 24,24, 168,120), + frames = 4 + }, + doublejump = { + [1] = love.graphics.newQuad( 0, 24, 24,24, 168,120), + [2] = love.graphics.newQuad( 24, 24, 24,24, 168,120), + [3] = love.graphics.newQuad( 48, 24, 24,24, 168,120), + [4] = love.graphics.newQuad( 72, 24, 24,24, 168,120), + frames = 4 + }, + land = { + [1] = love.graphics.newQuad( 0, 48, 24,24, 168,120), + [2] = love.graphics.newQuad( 24, 48, 24,24, 168,120), + [3] = love.graphics.newQuad( 48, 48, 24,24, 168,120), + [4] = love.graphics.newQuad( 72, 48, 24,24, 168,120), + [5] = love.graphics.newQuad( 96, 48, 24,24, 168,120), + frames = 5 + }, + respawn = {}, + clash = {}, + trail = {}, + hit = {} + } +} + +-- NAME :POSITION :SIZE :FRAMES +-- jump :x 0 y 0: 24px: 4 +-- doublejump:x 0 y 24: 24px: 4 +-- land :x 0 y 48: 24px: 5 +-- respawn :x 0 y 72: 24px: 7 +-- clash :x 0 y 96: 24px: 6 +-- trail :x104 y 0: 16px: 4 +-- hit :x106 y 18: 16px: 3 + +-- Construct of `Effect` +function Effect:new(name, x, y) + -- Meta + local o = {} + setmetatable(o, self) + self.__index = self + -- Set filter + local min, mag = self.sprite:getFilter() + if min ~= "nearest" or + mag ~= "nearest" then + self.sprite:setFilter("nearest", "nearest") + end + -- Init + o.initial = o.delay + o.animation = name + o.x = x or self.x + o.y = y or self.y + return o +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) + -- defaults + local offset_x = offset_x or 0 + local offset_y = offset_y or 0 + local scale = scale or 1 + -- draw + love.graphics.setColor(255,255,255,255) + love.graphics.draw(self.sprite, self.quads[self.animation][self.frame], (self.x+offset_x)*scale, (self.y+offset_y)*scale, 0, scale, scale) +end \ No newline at end of file diff --git a/main.lua b/main.lua index 35e9147..4302cb9 100644 --- a/main.lua +++ b/main.lua @@ -6,6 +6,7 @@ require "ground" require "player" require "camera" require "cloud" +require "effect" -- Temporary debug debug = false @@ -24,7 +25,6 @@ function love.load () w:createPlatform(290/2, 180/2-50, {-17,1, 17,1, 17,17, -17,17}, "assets/platform_top.png") w:createNaut(290/2-10, 180/2 - 80, "assets/leon.png") w:createNaut(290/2+10, 180/2 - 80, "assets/lonestar.png") - -- Temporary settings for second player w.Nauts[2].name = "Player2" @@ -58,4 +58,7 @@ end -- Draw function love.draw () w:draw() + if debug then + love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10) + end end \ No newline at end of file diff --git a/player.lua b/player.lua index fcbe83f..ab7e739 100644 --- a/player.lua +++ b/player.lua @@ -135,6 +135,7 @@ function Player:keypressed (key) -- Jumping if key == self.key_jump then if not self.inAir then + w:createEffectBottom("jump", self.body:getX()-12, self.body:getY()-15) self.jumpactive = true if (self.current == self.animations.attack) or (self.current == self.animations.attack_up) or @@ -142,6 +143,7 @@ function Player:keypressed (key) self:changeAnimation("idle") end elseif self.jumpdouble then + w:createEffectBottom("doublejump", self.body:getX()-12, self.body:getY()-15) self.jumpactive = true self.jumpdouble = false end diff --git a/world.lua b/world.lua index 1d1c6d4..733f9aa 100644 --- a/world.lua +++ b/world.lua @@ -11,6 +11,8 @@ World = { Nauts = nil, Platforms = nil, Clouds = nil, + EffectsBottom = nil, + EffectsTop = nil, camera = nil, -- cloud generator clouds_delay = 6, @@ -30,10 +32,14 @@ function World:new() -- Empty tables for objects local n = {} o.Nauts = n - local p = {} + local p = {} o.Platforms = {} - local c = {} - o.Clouds = c + local c = {} + o.Clouds = c + local eb = {} + local et = {} + o.EffectsBottom = eb + o.EffectsTop = et -- Random init math.randomseed(os.time()) -- Create camera @@ -80,6 +86,16 @@ function World:randomizeCloud(outside) self:createCloud(x, y, t, v) end +-- Add an effect behind nauts +function World:createEffectBottom(name, x, y) + table.insert(self.EffectsBottom, Effect:new(name, x, y)) +end + +-- Add an effect behind nauts +function World:createEffectTop(name, x, y) + table.insert(self.EffectsTop, Effect:new(name, x, y)) +end + -- Update ZU WARUDO function World:update(dt) -- Physical world @@ -106,6 +122,17 @@ function World:update(dt) table.remove(self.Clouds, _) end end + -- Effects + for _,effect in pairs(self.EffectsBottom) do + if effect:update(dt) then + table.remove(self.EffectsBottom, _) + end + end + for _,effect in pairs(self.EffectsTop) do + if effect:update(dt) then + table.remove(self.EffectsTop, _) + end + end end -- Keypressed @@ -136,7 +163,12 @@ function World:draw() -- Draw clouds for _,cloud in pairs(self.Clouds) do - local foo = cloud:draw(offset_x, offset_y, scale) + cloud:draw(offset_x, offset_y, scale) + end + + -- Draw effects bottom + for _,effect in pairs(self.EffectsBottom) do + effect:draw(offset_x,offset_y, scale) end -- Draw ground @@ -148,6 +180,11 @@ function World:draw() for _,naut in pairs(self.Nauts) do naut:draw(offset_x, offset_y, scale, debug) end + + -- Draw effects top + for _,effect in pairs(self.EffectsTop) do + effect:draw(offset_x,offset_y, scale) + end end -- beginContact @@ -157,6 +194,7 @@ function World.beginContact(a, b, coll) print(b:getUserData().name .. " is not in air") b:getUserData().inAir = false b:getUserData().jumpdouble = true + w:createEffectBottom("land", b:getBody():getX()-12, b:getBody():getY()-15) end end -- cgit v1.1