[Snippet] Game Clock

Scripts

7 years
edited 7 years
Game Clock

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

Code (jass) Select
1
//! zinc
2
library GameClock { /* v1.0.0.7
3
*************************************************************************************
4
*
5
*    Game Clock
6
*        by nel
7
*
8
*       It allows you format the time and get the elapsed time.
9
*
10
************************************************************************************
11
*
12
*   SETTING
13
*
14
*/
15
    private constant string DELIMITER = " : "; /* It used to seperate the hour,
16
                                                  minute, and second */
17
/*
18
************************************************************************************
19
*
20
*    struct GameClock extends array
21
*
22
*        static method FormatTime takes real seconds return string
23
*            - It returns time as hh/mm/ss format.
24
*
25
*        static method operator ElapsedTime takes nothing returns real
26
*            - It returns elapsed time.
27
*
28
*        static method operator ElapsedTimeEx takes nothing returns string
29
*            - It returns elapsed time as hh/mm/ss format.
30
*
31
*        static method operator Delimiter takes nothing returns string
32
*            - It returns GameClock's delimiter.
33
*
34
*************************************************************************************
35
*
36
*    Issue:
37
*
38
*       This library will not give you an exact current elapsed time.
39
*
40
************************************************************************************/
41
42
    //! textmacro GAMECLOCK_TIMEFORMAT takes Time, delimiter
43
        if( $Time$ < 10 ) {
44
            format = format + "0" + I2S( $Time$ ) $delimiter$;
45
        } else {
46
            format = format + I2S( $Time$ ) $delimiter$;
47
        }
48
    //! endtextmacro
49
50
    private constant timer TIMER = CreateTimer();
51
52
    public { struct GameClock [] {
53
        public {
54
            static method FormatTime( real seconds ) -> string {
55
                integer s = R2I( ModuloReal( seconds, 60 ) );
56
                integer m = R2I( ModuloReal( seconds, 3600 ) / 60 );
57
                integer h = R2I( seconds / 3600 );
58
                string format = "";
59
60
                //! runtextmacro GAMECLOCK_TIMEFORMAT( "h", "+ DELIMITER" )
61
                //! runtextmacro GAMECLOCK_TIMEFORMAT( "m", "+ DELIMITER" )
62
                //! runtextmacro GAMECLOCK_TIMEFORMAT( "s", "" )
63
64
                return format;
65
            }
66
67
            static method operator ElapsedTime() -> real {
68
                return TimerGetElapsed( TIMER );
69
            }
70
71
            static method operator ElapsedTimeEx() -> string {
72
                return thistype.FormatTime( TimerGetElapsed(TIMER) );
73
            }
74
75
            static method operator Delimiter() -> string {
76
                return DELIMITER;
77
            }
78
        }
79
80
        private static method onInit() {
81
            TimerStart( TIMER, 100000000, true, null );
82
        }
83
    }}
84
}
85
//! endzinc
7 years
Nice code, clean and to the point :)

I think adding a demo map showing this code working could be a good thing. I did one for you
7 years
Quote from: moyack on December 22, 2018, 10:11:24 AM
Nice code, clean and to the point :)

I think adding a demo map showing this code working could be a good thing. I did one for you

I think you don't need a demo map for this, because the demo code is enough to understand how this snippet works. :v

*meh meh*