Reorganized code and assets.
This commit is contained in:
58
src/graphics/drawable.lua
Normal file
58
src/graphics/drawable.lua
Normal file
@@ -0,0 +1,58 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local make_class = require 'src.utils.classes'
|
||||
local Asset = require 'src.utils.asset'
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local Drawable = make_class(Asset)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Drawable:_init()
|
||||
end
|
||||
|
||||
|
||||
function Drawable:update(_)
|
||||
end
|
||||
|
||||
|
||||
function Drawable:draw()
|
||||
end
|
||||
|
||||
|
||||
function Drawable:keypressed(_, _ , _)
|
||||
end
|
||||
|
||||
|
||||
function Drawable:textinput(_)
|
||||
end
|
||||
|
||||
|
||||
function Drawable:keyreleased(_, _)
|
||||
end
|
||||
|
||||
|
||||
function Drawable:mousemoved(_, _, _, _)
|
||||
end
|
||||
|
||||
|
||||
function Drawable:mousepressed(_, _, _)
|
||||
end
|
||||
|
||||
|
||||
function Drawable:mousereleased(_, _, _)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return Drawable
|
||||
112
src/graphics/fader.lua
Normal file
112
src/graphics/fader.lua
Normal file
@@ -0,0 +1,112 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local make_class = require 'src.utils.classes'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local Fader = make_class()
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Fader:_init()
|
||||
self.t = 0.0
|
||||
self.step = 0
|
||||
self.done = true
|
||||
self.reverse = false
|
||||
self.callback = nil
|
||||
end
|
||||
|
||||
|
||||
function Fader:_reset()
|
||||
self.t = 0
|
||||
self.step = 0
|
||||
self.done = false
|
||||
end
|
||||
|
||||
|
||||
function Fader:fade_in(callback)
|
||||
self:_reset()
|
||||
self.reverse = true
|
||||
self.callback = callback
|
||||
end
|
||||
|
||||
|
||||
function Fader:fade_out(callback)
|
||||
self:_reset()
|
||||
self.reverse = false
|
||||
self.callback = callback
|
||||
end
|
||||
|
||||
|
||||
function Fader:update(dt)
|
||||
-- While the fader is active.
|
||||
if not self.done then
|
||||
-- Update the internal timer.
|
||||
self.t = self.t + dt
|
||||
|
||||
-- Advance the fade if enough time has passed.
|
||||
if self.t >= 0.009 then
|
||||
self.step = self.step + 1
|
||||
self.t = 0.0
|
||||
end
|
||||
|
||||
-- Mark as done when all fade tiles have been shown/hidden.
|
||||
if self.step > 175 then
|
||||
self.done = true
|
||||
|
||||
-- Execute the callback if any.
|
||||
if self.callback ~= nil then
|
||||
self.callback()
|
||||
self.callback = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Fader:draw()
|
||||
local c = 1
|
||||
|
||||
-- Set the render color to black.
|
||||
love.graphics.setColor(0, 0, 0)
|
||||
|
||||
-- For every fader tile.
|
||||
for i = 0, 16 do
|
||||
for j = 0, 10 do
|
||||
-- Check if this is a fade in or fade out.
|
||||
if not self.reverse then
|
||||
-- If it's a fade out then draw black tiles until c.
|
||||
if c <= self.step then
|
||||
love.graphics.rectangle('fill', 20 * i, 20 * j, 20, 20)
|
||||
end
|
||||
else
|
||||
-- If it's a fade in then draw black tiles with indices higher than c.
|
||||
if c > self.step then
|
||||
love.graphics.rectangle('fill', 20 * i, 20 * j, 20, 20)
|
||||
end
|
||||
end
|
||||
|
||||
-- Advance to the next tile to show/hide.
|
||||
c = c + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- Reset the render color to white.
|
||||
love.graphics.setColor()
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return Fader
|
||||
48
src/graphics/sprite.lua
Normal file
48
src/graphics/sprite.lua
Normal file
@@ -0,0 +1,48 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local make_class = require 'src.utils.classes'
|
||||
local Drawable = require 'src.graphics.drawable'
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local Sprite = make_class(Drawable)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Sprite:_init(sprite_name, x, y)
|
||||
self.sprite_name = string.format('assets/%s', sprite_name)
|
||||
self.x = (x ~= nil and x) or 0
|
||||
self.y = (y ~= nil and y) or 0
|
||||
end
|
||||
|
||||
|
||||
function Sprite:load()
|
||||
self.sprite = love.graphics.newImage(self.sprite_name)
|
||||
end
|
||||
|
||||
|
||||
function Sprite:draw()
|
||||
if self.sprite ~= nil then
|
||||
love.graphics.draw(self.sprite, self.x, self.y)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Sprite:unload()
|
||||
self.sprite = nil
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return Sprite
|
||||
Reference in New Issue
Block a user