Code formatting and comments across the board.

This commit is contained in:
2025-10-04 05:02:39 -04:00
parent 845c696899
commit c0d25f1d47
6 changed files with 74 additions and 43 deletions

View File

@@ -37,7 +37,7 @@ end
function love.load(args)
parse_args(args)
-- Obtain the settigns for the game.
-- Obtain the settings for the game.
settings:load_settings()
-- Load the assets of the intro game state.

View File

@@ -44,6 +44,7 @@ end
function Intro:update(dt)
local total_time
-- Set time to wait for each intro stage.
if self.stage == 0 then
total_time = 6.5
elseif self.stage == 1 then
@@ -52,13 +53,16 @@ function Intro:update(dt)
total_time = 8.0
end
-- Start counting the stage time when the fade in is done.
if self.fade.done then
self.timer = self.timer + dt
end
-- Check if it's time to change stage.
if self.timer >= total_time then
self.fade:fade_out(
function ()
-- Change the stage and play the corresponding sound.
self.stage = self.stage + 1
if self.stage == 1 then
@@ -67,25 +71,32 @@ function Intro:update(dt)
self.title_snd:play()
end
-- Request a fade in.
self.fade:fade_in()
end
)
-- Reset the stage timer.
self.timer = 0.0
end
-- Update the fader.
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
end
function Intro:draw()
if self.valid then
-- 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 == 2 then love.graphics.draw(self.title, 0, 0) end
end
-- Draw the fader if needed.
self.fade:draw()
end
@@ -102,6 +113,7 @@ end
function Intro:keypressed(_)
-- Skip the intro on any key press.
self.skip = true
end

View File

@@ -58,6 +58,7 @@ function Fader:update(dt)
if self.step > 175 then
self.done = true
if self.callback ~= nil then
self.callback()
self.callback = nil

View File

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

View File

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