Compare commits

...

4 Commits

6 changed files with 79 additions and 47 deletions

View File

@@ -2,10 +2,10 @@
-- Imports -- Imports
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
local love = require 'love' local love = require 'love'
local game_states = require 'src.states' local game_states = require 'src.states'
local settings = require 'src.utils.settings' local settings = require 'src.utils.settings'
local Fader = require 'src.utils.fader' local Fader = require 'src.utils.fader'
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@@ -13,9 +13,9 @@ local Fader = require 'src.utils.fader'
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Current_state = 1 Current_state = 1
Fade = Fader() Fade = Fader()
Change_state = false Change_state = false
Debug = false Debug = false
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@@ -37,7 +37,7 @@ end
function love.load(args) function love.load(args)
parse_args(args) parse_args(args)
-- Obtain the settigns for the game. -- Obtain the settings for the game.
settings:load_settings() settings:load_settings()
-- Load the assets of the intro game state. -- Load the assets of the intro game state.
@@ -67,7 +67,7 @@ function love.update(dt)
love.event.quit() love.event.quit()
-- Save the settings before quitting just in case. -- Save the settings before quitting just in case.
settings:load_settings() settings:save_settings()
end end
else else
-- If the new state exists then unload it's data and set the new state. -- If the new state exists then unload it's data and set the new state.
@@ -89,11 +89,12 @@ function love.draw()
if Debug then if Debug then
love.graphics.setColor(0, 0, 0) love.graphics.setColor(0, 0, 0)
love.graphics.rectangle('fill', 0, 0, 140, 40) love.graphics.rectangle('fill', 0, 0, 140, 50)
love.graphics.setColor() love.graphics.setColor()
love.graphics.print(string.format('OS: %s', love.system.getOS()), 5, 5) love.graphics.print(string.format('OS: %s', love.system.getOS()), 5, 5)
love.graphics.print(string.format('Love version: %s', love.getVersion()), 5, 15) love.graphics.print(string.format('Love version: %s', love.getVersion()), 5, 15)
love.graphics.print(string.format('Memory usage: %s KiB', love.system.getMemUsage()), 5, 25) love.graphics.print(string.format('Memory usage: %s KiB', love.system.getMemUsage()), 5, 25)
love.graphics.print(string.format('FPS: %.2f', 1.0 / love.timer.getAverageDelta()), 5, 35)
end end
end end

View File

@@ -17,7 +17,7 @@ local GameState = make_class()
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
function GameState:_init(name, index) function GameState:_init(name, index)
self.name = name self.name = name
self.index = index self.index = index
self.valid = false self.valid = false
end end

View File

@@ -2,10 +2,10 @@
-- Imports -- Imports
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
local love = require 'love' local love = require 'love'
local make_class = require 'src.utils.classes' local make_class = require 'src.utils.classes'
local GameState = require 'src.gstates.gstate' local GameState = require 'src.gstates.gstate'
local Fader = require 'src.utils.fader' local Fader = require 'src.utils.fader'
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@@ -21,21 +21,21 @@ local Intro = make_class(GameState)
function Intro:_init(name, index) function Intro:_init(name, index)
GameState._init(self, name, index) GameState._init(self, name, index)
self.skip = false self.skip = false
self.fade = Fader() self.fade = Fader()
self.stage = 0 self.stage = 0
self.timer = 0 self.timer = 0
end end
function Intro:load() function Intro:load()
self.splash = love.graphics.newImage('imgs/splash.png') self.splash = love.graphics.newImage('imgs/splash.png')
self.author = love.graphics.newImage('imgs/wally.png') self.author = love.graphics.newImage('imgs/wally.png')
self.title = love.graphics.newImage('imgs/title.png') self.title = love.graphics.newImage('imgs/title.png')
self.splash_snd = love.audio.newSource('snd/jachiev.wav') self.splash_snd = love.audio.newSource('snd/jachiev.wav')
self.author_snd = love.audio.newSource('snd/alang.wav') self.author_snd = love.audio.newSource('snd/alang.wav')
self.title_snd = love.audio.newSource('snd/ablhole.wav') self.title_snd = love.audio.newSource('snd/ablhole.wav')
self.valid = true self.valid = true
self.splash_snd:play() self.splash_snd:play()
end end
@@ -44,21 +44,25 @@ end
function Intro:update(dt) function Intro:update(dt)
local total_time local total_time
-- Set time to wait for each intro stage.
if self.stage == 0 then if self.stage == 0 then
total_time = 4.5 total_time = 6.5
elseif self.stage == 1 then elseif self.stage == 1 then
total_time = 3.0 total_time = 5.0
else else
total_time = 8.0 total_time = 8.0
end end
-- Start counting the stage time when the fade in is done.
if self.fade.done then if self.fade.done then
self.timer = self.timer + dt self.timer = self.timer + dt
end end
-- Check if it's time to change stage.
if self.timer >= total_time then if self.timer >= total_time then
self.fade:fade_out( self.fade:fade_out(
function () function ()
-- Change the stage and play the corresponding sound.
self.stage = self.stage + 1 self.stage = self.stage + 1
if self.stage == 1 then if self.stage == 1 then
@@ -67,41 +71,49 @@ function Intro:update(dt)
self.title_snd:play() self.title_snd:play()
end end
-- Request a fade in.
self.fade:fade_in() self.fade:fade_in()
end end
) )
-- Reset the stage timer.
self.timer = 0.0 self.timer = 0.0
end end
-- Update the fader.
self.fade:update(dt) self.fade:update(dt)
-- Move on to the next game state if the user skipped the intro or all stages are complete.
if not self.skip and self.stage < 3 then return self.index else return 2 end if not self.skip and self.stage < 3 then return self.index else return 2 end
end end
function Intro:draw() function Intro:draw()
if self.valid then if self.valid then
if self.stage == 0 then love.graphics.draw(self.splash, 0, 0) -- If there is data to draw, then draw the current stage's backdrop.
if self.stage == 0 then love.graphics.draw(self.splash, 0, 0)
elseif self.stage == 1 then love.graphics.draw(self.author, 0, 0) elseif self.stage == 1 then love.graphics.draw(self.author, 0, 0)
elseif self.stage == 2 then love.graphics.draw(self.title, 0, 0) end elseif self.stage == 2 then love.graphics.draw(self.title, 0, 0) end
end end
-- Draw the fader if needed.
self.fade:draw() self.fade:draw()
end end
function Intro:unload() function Intro:unload()
self.splash = nil self.splash = nil
self.author = nil self.author = nil
self.title = nil self.title = nil
self.splash_snd = nil self.splash_snd = nil
self.author_snd = nil self.author_snd = nil
self.title_snd = nil self.title_snd = nil
self.valid = false self.valid = false
end end
function Intro:keypressed(_) function Intro:keypressed(_)
-- Skip the intro on any key press.
self.skip = true self.skip = true
end end

View File

@@ -2,7 +2,7 @@
-- Imports -- Imports
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
local love = require 'love' local love = require 'love'
local make_class = require 'src.utils.classes' local make_class = require 'src.utils.classes'
@@ -18,16 +18,16 @@ local Fader = make_class()
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
function Fader:_init() function Fader:_init()
self.t = 0.0 self.t = 0.0
self.step = 0 self.step = 0
self.done = true self.done = true
self.reverse = false self.reverse = false
self.callback = nil self.callback = nil
end end
function Fader:_reset() function Fader:_reset()
self.t = 0 self.t = 0
self.step = 0 self.step = 0
self.done = false self.done = false
end end
@@ -35,14 +35,14 @@ end
function Fader:fade_in(callback) function Fader:fade_in(callback)
self:_reset() self:_reset()
self.reverse = true self.reverse = true
self.callback = callback self.callback = callback
end end
function Fader:fade_out(callback) function Fader:fade_out(callback)
self:_reset() self:_reset()
self.reverse = false self.reverse = false
self.callback = callback self.callback = callback
end end
@@ -53,11 +53,12 @@ function Fader:update(dt)
if self.t >= 0.009 then if self.t >= 0.009 then
self.step = self.step + 1 self.step = self.step + 1
self.t = 0.0 self.t = 0.0
end end
if self.step > 175 then if self.step > 175 then
self.done = true self.done = true
if self.callback ~= nil then if self.callback ~= nil then
self.callback() self.callback()
self.callback = nil self.callback = nil

View File

@@ -3,6 +3,7 @@
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- From https://stackoverflow.com/a/19327160 -- From https://stackoverflow.com/a/19327160
-- Cargo cult programming ahoy!!
local function magiclines( str ) local function magiclines( str )
local pos local pos
pos = 1 pos = 1

View File

@@ -2,7 +2,7 @@
-- Imports -- Imports
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
local love = require 'love' local love = require 'love'
local magiclines = require 'src.utils.mgclines' local magiclines = require 'src.utils.mgclines'
@@ -18,15 +18,16 @@ local SETTINGS_PATH = 'settings.ini'
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
local settings = { local settings = {
permadeath=true, -- Default setting values.
forward='w', permadeath = true,
backward='s', forward = 'w',
stepleft='a', backward = 's',
stepright='d', stepleft = 'a',
turnleft='q', stepright = 'd',
turnright='e', turnleft = 'q',
musicvol=75, turnright = 'e',
soundvol=100, musicvol = 75,
soundvol = 100,
} }
@@ -35,11 +36,16 @@ local settings = {
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
function settings:_parse_settings_str(s) function settings:_parse_settings_str(s)
-- For each line in the data string.
for line in magiclines(s) do for line in magiclines(s) do
-- Check if the line matches a key=value pair
local k, v = string.match(line, '(%w+)=(.+)') local k, v = string.match(line, '(%w+)=(.+)')
if k and v then if k and v then
-- If the line matches then check if it has a known settings key.
if self[k] ~= nil then if self[k] ~= nil then
-- If the key is valid then cast the value to it's expected data type and store it.
-- Fail if the cast is not doable.
if v == 'true' then if v == 'true' then
if type(self[k]) ~= 'boolean' then error(string.format('Invalid value for %s in settings', k)) end if type(self[k]) ~= 'boolean' then error(string.format('Invalid value for %s in settings', k)) end
self[k] = true self[k] = true
@@ -53,6 +59,7 @@ function settings:_parse_settings_str(s)
self[k] = v self[k] = v
end end
else else
-- Fail on an unknown key.
error(string.format('Unknown settings key "%s"', k)) error(string.format('Unknown settings key "%s"', k))
end end
end end
@@ -61,10 +68,14 @@ end
function settings:_get_settings_str() function settings:_get_settings_str()
-- Start with the settings category.
local s = '[settings]\n' local s = '[settings]\n'
-- For each value in the settings table.
for k, v in pairs(self) do for k, v in pairs(self) do
-- If the value isn't a function then it is a setting.
if type(v) ~= 'function' then if type(v) ~= 'function' then
-- Format the value as key=value.
s = s .. string.format('%s=%s\n', k, v) s = s .. string.format('%s=%s\n', k, v)
end end
end end
@@ -75,13 +86,16 @@ end
function settings:_open_or_create() function settings:_open_or_create()
if not love.filesystem.exists(SETTINGS_PATH) then if not love.filesystem.exists(SETTINGS_PATH) then
-- If the settings file doesn't exist then create it with the default values.
local setts = self:_get_settings_str() local setts = self:_get_settings_str()
love.filesystem.write(SETTINGS_PATH, setts) love.filesystem.write(SETTINGS_PATH, setts)
return setts return setts
else else
if not love.filesystem.isFile(SETTINGS_PATH) then if not love.filesystem.isFile(SETTINGS_PATH) then
-- Fail if the settings path points to a directory.
error ("Settings path exists and isn't a file") error ("Settings path exists and isn't a file")
else else
-- Else read the settings.
return love.filesystem.read(SETTINGS_PATH) return love.filesystem.read(SETTINGS_PATH)
end end
end end
@@ -93,6 +107,7 @@ end
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
function settings:load_settings() function settings:load_settings()
-- Obtain the settings data and parse it.
local s = self:_open_or_create() local s = self:_open_or_create()
self:_parse_settings_str(s) self:_parse_settings_str(s)
end end
@@ -100,8 +115,10 @@ end
function settings:save_settings() function settings:save_settings()
if not love.filesystem.exists(SETTINGS_PATH) or love.filesystem.isFile(SETTINGS_PATH) then if not love.filesystem.exists(SETTINGS_PATH) or love.filesystem.isFile(SETTINGS_PATH) then
-- If the settings file doesn't exist or it exists and is a file then save the current data.
love.filesystem.write(SETTINGS_PATH, self:_get_settings_str()) love.filesystem.write(SETTINGS_PATH, self:_get_settings_str())
else else
-- Fail if the settings path points to a directory.
error("Settings path exists and isn't a file") error("Settings path exists and isn't a file")
end end
end end