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 · 172

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
Download Demo Map

Game Clock

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

Code: jass
  1. //! zinc
  2. library GameClock { /* v1.0.0.3
  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.     public {
  16.         constant string delimiter = " : ";
  17.     }
  18. /*
  19. ************************************************************************************
  20. *
  21. *    Functions
  22. *
  23. *        function FormatTime takes real seconds returns string
  24. *            -   It returns time as hh/mm/ss format.
  25. *
  26. *        function GetElapsedTime takes nothing returns real
  27. *            -   It returns elapsed time.
  28. *
  29. *        function GetElapsedTimeFormatted takes nothing returns string
  30. *            -   It returns elapsed time as hh/mm/ss format.
  31. *
  32. ***********************************************************************************/
  33.  
  34.     private {
  35.         constant timer TIMER = CreateTimer();
  36.  
  37.         module inc { private static method onInit() {
  38.             TimerStart(TIMER, 100000000, true, null);
  39.         }}
  40.      
  41.         struct Z extends array { module inc; }
  42.     }
  43.  
  44.     public {
  45.         function FormatTime( real seconds ) -> string {
  46.             integer s = R2I(ModuloReal(seconds, 60));
  47.             integer m = R2I(ModuloReal(seconds, 3600) / 60);
  48.             integer h = R2I(seconds / 3600);
  49.             string format = "";
  50.          
  51.             //! textmacro GAMECLOCK_TimeFormat takes Time, delimiter
  52.             if( $Time$ < 10 ) {
  53.                 format = format + "0" + I2S($Time$) $delimiter$;
  54.             } else {
  55.                 format = format + I2S($Time$) $delimiter$;
  56.             }
  57.             //! endtextmacro
  58.             //! runtextmacro GAMECLOCK_TimeFormat("h", "+delimiter")
  59.             //! runtextmacro GAMECLOCK_TimeFormat("m", "+delimiter")
  60.             //! runtextmacro GAMECLOCK_TimeFormat("s", "")
  61.              
  62.             return format;
  63.         }
  64.    
  65.         function GetElapsedTime() -> real {
  66.             return TimerGetElapsed(TIMER);
  67.         }
  68.  
  69.         function GetElapsedTimeFormatted() -> string {
  70.             return FormatTime(TimerGetElapsed(TIMER));
  71.         }
  72.     }
  73. }
  74. //! endzinc



Demo Code
Code: jass
  1. //! zinc
  2. library Sample
  3.  
  4.     requires
  5.         GameClock
  6.  
  7. { private {
  8.     struct Initialization extends array { private {
  9.         static method onInit() {
  10.             trigger trg = CreateTrigger();
  11.  
  12.             BJDebugMsg( "Press ESC" );
  13.  
  14.             TriggerRegisterPlayerEvent( trg, Player(0), EVENT_PLAYER_END_CINEMATIC );
  15.             TriggerAddCondition( trg, Condition( function() -> boolean {
  16.                 ClearTextMessages();
  17.  
  18.                 BJDebugMsg( "Elapsed Time in hh/mm/ss format: " + GetElapsedTimeFormatted() );
  19.                 BJDebugMsg( "Elapsed Time in Seconds: " + R2S(GetElapsedTime()) );
  20.                 BJDebugMsg( "Get Random Number then format it: " + FormatTime(GetRandomReal(0, 9999)) );
  21.  
  22.                 return false;
  23.             }));
  24.  
  25.             trg = null;
  26.         }
  27.     }}
  28. }}
  29. //! endzinc
« Last Edit: January 09, 2019, 12:50:03 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