- Joined
- Jun 17, 2007
- Messages
- 1,433
A library that's used to get the amount of day time that has passed since the beginning of the game. It requires more an arithmetic operation because the time can be slowed down, sped up, or stopped completely. I've also added IsDay() and is IsNight() for completeness.
I can see it being useful for stuff like evolution systems where the time might be slowed down or sped up for whatever reason. This system doesn't support SetTimeOfDay() because I would need a wrapper function to tell whether the time is being increased or decreased.
I can see it being useful for stuff like evolution systems where the time might be slowed down or sped up for whatever reason. This system doesn't support SetTimeOfDay() because I would need a wrapper function to tell whether the time is being increased or decreased.
JASS:
//To import, create a "trigger" called TimeOfDayLib and copy this code into it.
//Change the PRECISION constant to match your needs. If you changed the time gameplay constants, then also
//configure TIME_DAWN and TIME_DUSK.
//You are given access to the following functions:
//constant function GetTimeElapsed takes nothing returns real
//Gets the time that has been experienced by players since the start of the game (in seconds).
//It will adjust to SuspendTimeOfDay and SetTimeOfDayScale.
//function IsDay takes nothing returns boolean
//Checks if the current time is day.
//function IsNight takes nothing returns boolean
//Checks if the current time is night.
library TimeOfDayLib initializer Init
globals
private constant real PRECISION = .1
//This lower the variable is, the more precise answers from GetTimeElapsed will be. However, more code will be run.
//It's negligible, but the user might want to change this.
private constant real TIME_DUSK = 18.
//The time when day turns into night. Configure if you changed the gameplay constant.
private constant real TIME_DAWN = 6.
//The time when night turns into day. Configure if you changed the gameplay constant.
private real Increase = PRECISION*3
private real Time = 0.
private timer Tim = CreateTimer()
endglobals
constant function GetTimeElapsed takes nothing returns real
return Time
endfunction
function IsDay takes nothing returns boolean
local real r = GetFloatGameState(GAME_STATE_TIME_OF_DAY)
return r >= TIME_DAWN and r < TIME_DUSK
endfunction
function IsNight takes nothing returns boolean
local real r = GetFloatGameState(GAME_STATE_TIME_OF_DAY)
return r < TIME_DAWN and r >= TIME_DUSK
endfunction
private function onTimer takes nothing returns nothing
set Time = Time + Increase
debug call BJDebugMsg(R2S(GetTimeElapsed()))
endfunction
private function SuspendTimeOfDayEx takes boolean b returns nothing
if b then
call PauseTimer(Tim)
else
call TimerStart(Tim, PRECISION, true, function onTimer)
endif
endfunction
private function SetTimeOfDayScaleEx takes real r returns nothing
set Increase = PRECISION * 3 * r
endfunction
hook SuspendTimeOfDay SuspendTimeOfDayEx
hook SetTimeOfDayScale SetTimeOfDayScaleEx
private function Init takes nothing returns nothing
call TimerStart(Tim, PRECISION, true, function onTimer)
endfunction
endlibrary
Last edited: