[Snippet] Game Clock

User avatar
nelmvn
Registered users
Registered users
Posts: 9
Joined: Mon Nov 19, 2018 5:44 pm
Reputation: 0
Location: Philippines
Contact:
Philippines

[Snippet] Game Clock

#1

Post by nelmvn » Thu Dec 20, 2018 2:57 pm

Game Clock

It allows you format the time and get the elapsed time.

Code: Select all

//! zinc
library GameClock { /* v1.0.0.7
*************************************************************************************
*
*    Game Clock
*        by nel
*
*       It allows you format the time and get the elapsed time.
*
************************************************************************************
*
*   SETTING
*
*/
    private constant string DELIMITER = " : "; /* It used to seperate the hour,
                                                  minute, and second */
/*
************************************************************************************
*
*    struct GameClock extends array
*
*        static method FormatTime takes real seconds return string
*            - It returns time as hh/mm/ss format.
*
*        static method operator ElapsedTime takes nothing returns real
*            - It returns elapsed time.
*
*        static method operator ElapsedTimeEx takes nothing returns string
*            - It returns elapsed time as hh/mm/ss format.
*
*        static method operator Delimiter takes nothing returns string
*            - It returns GameClock's delimiter.
*
*************************************************************************************
*
*    Issue:
*
*       This library will not give you an exact current elapsed time.
*
************************************************************************************/

    //! textmacro GAMECLOCK_TIMEFORMAT takes Time, delimiter
        if( $Time$ < 10 ) {
            format = format + "0" + I2S( $Time$ ) $delimiter$;
        } else {
            format = format + I2S( $Time$ ) $delimiter$;
        }
    //! endtextmacro

    private constant timer TIMER = CreateTimer();

    public { struct GameClock [] {
        public {
            static method FormatTime( real seconds ) -> string {
                integer s = R2I( ModuloReal( seconds, 60 ) );
                integer m = R2I( ModuloReal( seconds, 3600 ) / 60 );
                integer h = R2I( seconds / 3600 );
                string format = "";

                //! runtextmacro GAMECLOCK_TIMEFORMAT( "h", "+ DELIMITER" )
                //! runtextmacro GAMECLOCK_TIMEFORMAT( "m", "+ DELIMITER" )
                //! runtextmacro GAMECLOCK_TIMEFORMAT( "s", "" )

                return format;
            }

            static method operator ElapsedTime() -> real {
                return TimerGetElapsed( TIMER );
            }

            static method operator ElapsedTimeEx() -> string {
                return thistype.FormatTime( TimerGetElapsed(TIMER) );
            }

            static method operator Delimiter() -> string {
                return DELIMITER;
            }
        }

        private static method onInit() {
            TimerStart( TIMER, 100000000, true, null );
        }
    }}
}
//! endzinc
Demo
Show

Code: Select all

scope Test initializer Initialization

    private function Display takes nothing returns boolean
        local real r = GetRandomReal(0, 99999)
        local player p = GetLocalPlayer()

        call ClearTextMessages()
        call DisplayTimedTextToPlayer(p, 0, 0, 60, /*
            */ "|cffffcc00Format This time:|r " + R2S(r) + " seconds --> " + GameClock.FormatTime(r) + "\r\n" + /*
            */ "|cffffcc00Current Game Time in second:|r " + R2S( GameClock.ElapsedTime ) + "\r\n" + /*
            */ "|cffffcc00Current Game Time in hh/mm/ss format:|r " + GameClock.ElapsedTimeEx + "\r\n" + /*
            */ "|cffffcc00GameClock delimiter:|r " + GameClock.Delimiter /*
        */ )

        set p = null
        return false
    endfunction
   
    private function Initialization takes nothing returns nothing
        local trigger trg = CreateTrigger()

        call TriggerRegisterPlayerEvent(trg, Player(0), EVENT_PLAYER_END_CINEMATIC)
        call TriggerAddCondition(trg, Condition(function Display))

        set trg = null
    endfunction
   
endscope
Last edited by nelmvn on Sat Jun 22, 2019 5:58 pm, edited 11 times in total.

Return to “Systems”

×