diff --git a/sonic-radiance.love/core/utils/init.lua b/sonic-radiance.love/core/utils/init.lua index cc9257c..0235851 100644 --- a/sonic-radiance.love/core/utils/init.lua +++ b/sonic-radiance.love/core/utils/init.lua @@ -29,5 +29,6 @@ return { graphics = require(cwd .. "graphics"), filesystem = require(cwd .. "filesystem"), table = require(cwd .. "table"), - string = require(cwd .. "string") + string = require(cwd .. "string"), + time = require(cwd .. "time"), } diff --git a/sonic-radiance.love/core/utils/time.lua b/sonic-radiance.love/core/utils/time.lua new file mode 100644 index 0000000..38660ae --- /dev/null +++ b/sonic-radiance.love/core/utils/time.lua @@ -0,0 +1,28 @@ +local Math = require "core.utils.math" + +Time = {} + +Time.HOURS = 3600 +Time.MINUTES = 60 +Time.SEPARATOR = ":" + +function Time.timestamp(hour, minute, seconds) + return (hour * Time.HOURS) + (minute * Time.MINUTES) + seconds +end + +function Time.getFields(timestamp) + local hours = math.floor(timestamp / Time.HOURS) + local minutes = math.floor((timestamp % Time.HOURS) / Time.MINUTES) + local seconds = math.floor((timestamp % Time.MINUTES)) + return hours, minutes, seconds +end + +function Time.toString(timestamp) + local hours, minutes, seconds = Time.getFields(timestamp) + local str = Math.numberToString(hours, 2) + str = str .. Time.SEPARATOR .. Math.numberToString(minutes, 2) + str = str .. Time.SEPARATOR .. Math.numberToString(seconds, 2) + return str +end + +return Time \ No newline at end of file diff --git a/sonic-radiance.love/game/init.lua b/sonic-radiance.love/game/init.lua index 4b85206..4ce5989 100644 --- a/sonic-radiance.love/game/init.lua +++ b/sonic-radiance.love/game/init.lua @@ -199,41 +199,11 @@ function Game:update(dt) end function Game:getTime() - local hours, minutes, seconds - seconds = math.floor(self.gametime) - minutes = math.floor(seconds / 60) - hours = math.floor(minutes / 60) - seconds = seconds % 60 - minutes = minutes % 60 - hours = hours - - return seconds, minutes, hours + return utils.time.getFields(self.gametime) end -function Game:getTimeString() - local string - local seconds, minutes, hours = self:getTime() - local stringSeconds, stringMinutes, stringHours - if (seconds <= 9) then - stringSeconds = 0 .. seconds - else - stringSeconds = seconds - end - - if (minutes <= 9) then - stringMinutes = 0 .. minutes - else - stringMinutes = minutes - end - - if (hours <= 9) then - stringHours = 0 .. hours - else - stringHours = hours - end - - string = stringHours .. ":" .. stringMinutes .. ":" .. stringSeconds - return string +function Game:getTimeString() + return utils.time.toString(self.gametime) end function Game:printTime()