Timer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "Timer.h"
  2. #include "Spiel.h"
  3. Timer::Timer( int id, const char *name, int maxZeit, int x, int y, bool visible, bool autoWiederhohlung, bool runns, int farbe )
  4. : GameObject( TIMER, x, y, 0, 0 )
  5. {
  6. this->id = id;
  7. this->name = name;
  8. this->maxZeit = maxZeit;
  9. currentTime = (float)maxZeit;
  10. sichtbar = visible;
  11. this->autoWiederhohlung = autoWiederhohlung;
  12. this->runns = runns;
  13. this->pause = 0;
  14. this->farbe = farbe;
  15. }
  16. void Timer::start( Spiel *zSpiel, bool restart )
  17. {
  18. if( runns && !restart )
  19. return;
  20. currentTime = (float)maxZeit;
  21. runns = true;
  22. pause = 0;
  23. zSpiel->setTimerZuletztGestartet( dynamic_cast<Timer *>( getThis() ) );
  24. Ereignis *e = new Ereignis( TIMER_BEGINNT );
  25. e->addParameter( "Betroffener Timer", dynamic_cast<Timer *>( this->getThis() ) );
  26. zSpiel->throwEvent( e );
  27. }
  28. void Timer::setPause( Spiel *zSpiel, bool pause )
  29. {
  30. if( pause && !this->pause )
  31. zSpiel->setTimerZuletztPausiert( dynamic_cast<Timer *>( getThis() ) );
  32. if( !pause && this->pause )
  33. zSpiel->setTimerZuletztFortgesetzt( dynamic_cast<Timer *>( getThis() ) );
  34. this->pause = pause;
  35. }
  36. void Timer::setMaxZeit( int sekunden )
  37. {
  38. this->maxZeit = maxZeit;
  39. }
  40. void Timer::setAutoWiederhohlung( bool wiederhohlung )
  41. {
  42. this->autoWiederhohlung = autoWiederhohlung;
  43. }
  44. void Timer::setSichtbar( bool visible )
  45. {
  46. this->sichtbar = sichtbar;
  47. }
  48. void Timer::setFarbe( int farbe )
  49. {
  50. this->farbe = farbe;
  51. }
  52. void Timer::tick( double time, Spiel *zSpiel )
  53. {
  54. if( runns && !pause )
  55. {
  56. currentTime -= (float)time;
  57. if( currentTime <= 0 )
  58. {
  59. zSpiel->setTimerZuletztAbgelaufen( dynamic_cast<Timer *>( getThis() ) );
  60. runns = 0;
  61. Ereignis *e = new Ereignis( TIMER_RUNNS_OUT );
  62. e->addParameter( "Betroffener Timer", dynamic_cast<Timer *>( this->getThis() ) );
  63. zSpiel->throwEvent( e );
  64. if( autoWiederhohlung )
  65. start( zSpiel );
  66. }
  67. }
  68. }
  69. void Timer::setZeit( float zeit )
  70. {
  71. currentTime = zeit;
  72. }
  73. float Timer::getTimeLeft() const
  74. {
  75. return currentTime;
  76. }
  77. bool Timer::istSichtbar() const
  78. {
  79. return sichtbar;
  80. }
  81. int Timer::getFarbe() const
  82. {
  83. return farbe;
  84. }
  85. bool Timer::isRunning() const
  86. {
  87. return runns;
  88. }
  89. bool Timer::istPausiert() const
  90. {
  91. return pause;
  92. }
  93. int Timer::getMaxTime() const
  94. {
  95. return maxZeit;
  96. }
  97. bool Timer::istAutoWiederhohlung() const
  98. {
  99. return autoWiederhohlung;
  100. }
  101. int Timer::getId() const
  102. {
  103. return id;
  104. }