vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[Snippet] Game Clock

nel · 259

0 Members and 1 Guest are viewing this topic.

Rating

Average Score - 5 / 5

« Created: December 22, 2018, 10:12:05 AM by moyack »

[Snippet] Game Clock
on: December 21, 2018, 03:09:29 AM
Category: Variables
Language: ZINC

Game Clock

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

Code: jass
  1. //! zinc
  2. library GameClock { /* v1.0.0.5
  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.     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



Demo Code
Code: jass
  1. scope Test initializer Initialization
  2.  
  3.     private function Display takes nothing returns boolean
  4.         local real r = GetRandomReal(0, 99999)
  5.         local player p = GetLocalPlayer()
  6.  
  7.         call ClearTextMessages()
  8.         call DisplayTimedTextToPlayer(p, 0, 0, 60, /*
  9.             */ "|cffffcc00Format This time:|r " + R2S(r) + " seconds --> " + GameClock.FormatTime(r) + "\r\n" + /*
  10.             */ "|cffffcc00Current Game Time in second:|r " + R2S( GameClock.ElapsedTime ) + "\r\n" + /*
  11.             */ "|cffffcc00Current Game Time in hh/mm/ss format:|r " + GameClock.ElapsedTimeEx + "\r\n" + /*
  12.             */ "|cffffcc00GameClock delimiter:|r " + GameClock.Delimiter /*
  13.         */ )
  14.  
  15.         set p = null
  16.         return false
  17.     endfunction
  18.    
  19.     private function Initialization takes nothing returns nothing
  20.         local trigger trg = CreateTrigger()
  21.  
  22.         call TriggerRegisterPlayerEvent(trg, Player(0), EVENT_PLAYER_END_CINEMATIC)
  23.         call TriggerAddCondition(trg, Condition(function Display))
  24.  
  25.         set trg = null
  26.     endfunction
  27.    
  28. endscope
« Last Edit: February 20, 2019, 05:15:46 PM by nel »



Re: [Snippet] Game Clock
Reply #1 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


Re: [Snippet] Game Clock
Reply #2 on: December 22, 2018, 03:10:35 PM

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*



 

Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)