Laser.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "Laser.h"
  2. #include "../Karte/Karte.h"
  3. #include "../Spieler/Spieler.h"
  4. #include "../Define.h"
  5. // Inhalt der Laser Klasse aus Laser.h
  6. // Konstruktor
  7. Laser::Laser( int id, Vec2< double > pos, Vec2< double > speed, int sNum, double intensität, int tf )
  8. {
  9. this->id = id;
  10. this->pos = pos;
  11. this->speed = speed;
  12. this->sNum = sNum;
  13. this->intensität = intensität;
  14. this->startIntensität = intensität;
  15. this->tf = tf;
  16. ref = 1;
  17. }
  18. // privat
  19. char Laser::getOutCode( Punkt& p, Bild &zrObj ) const
  20. {
  21. char ret = 0;
  22. if( p.x < 0 )
  23. ret |= 1;
  24. else if( p.x >= zrObj.getBreite() )
  25. ret |= 2;
  26. if( p.y < 0 )
  27. ret |= 4;
  28. else if( p.y >= zrObj.getHeight() )
  29. ret |= 8;
  30. return ret;
  31. }
  32. // nicht constant
  33. void Laser::tick( int tick, Karte *zMap )
  34. {
  35. double tickVal = tick * TICK;
  36. pos += speed * tickVal;
  37. Vec2< int > gr = zMap->getSize();
  38. if( pos.x < 0 )
  39. pos.x += gr.x;
  40. if( pos.y < 0 )
  41. pos.y += gr.y;
  42. if( pos.x >= gr.x )
  43. pos.x -= gr.x;
  44. if( pos.y >= gr.y )
  45. pos.y -= gr.y;
  46. intensität -= tickVal * 2;
  47. }
  48. void Laser::render( Bild &zRObj )
  49. {
  50. int fa = (int)( ( intensität / startIntensität ) * 255 );
  51. int f = ( ( fa << 24 ) & 0xFF000000 ) | ( tf & 0xFFFFFF );
  52. Punkt a( pos );
  53. Punkt b( pos - ( speed / speed.getLength() * 10 ) );
  54. a += zRObj.getDrawOff();
  55. b += zRObj.getDrawOff();
  56. char outCode1 = getOutCode( a, zRObj );
  57. char outCode2 = getOutCode( b, zRObj );
  58. bool ok = 0;
  59. while( 1 )
  60. {
  61. int xMax = zRObj.getDrawGr().x - 1;
  62. int yMax = zRObj.getDrawGr().y - 1;
  63. if( !( outCode1 | outCode2 ) )
  64. {
  65. ok = 1;
  66. break;
  67. }
  68. else if( outCode1 & outCode2 )
  69. break;
  70. else
  71. {
  72. int x, y;
  73. char outCodeOut = outCode1 ? outCode1 : outCode2;
  74. if( outCodeOut & 8 )
  75. {
  76. x = (int)( a.x + ( b.x - a.x ) * ( yMax - a.y ) / ( b.y - a.y ) + 0.5 );
  77. y = yMax;
  78. }
  79. else if( outCodeOut & 4 )
  80. {
  81. x = (int)( a.x + ( b.x - a.x ) * ( zRObj.getDrawPos().y - a.y ) / ( b.y - a.y ) + 0.5 );
  82. y = zRObj.getDrawPos().y;
  83. }
  84. else if( outCodeOut & 2 )
  85. {
  86. y = (int)( a.y + ( b.y - a.y ) * ( xMax - a.x ) / ( b.x - a.x ) + 0.5 );
  87. x = xMax;
  88. }
  89. else if( outCodeOut & 1 )
  90. {
  91. y = (int)( a.y + ( b.y - a.y ) * ( zRObj.getDrawPos().x - a.x ) / ( b.x - a.x ) + 0.5 );
  92. x = zRObj.getDrawPos().x;
  93. }
  94. if( outCodeOut == outCode1 )
  95. {
  96. a.x = x;
  97. a.y = y;
  98. outCode1 = getOutCode( a, zRObj );
  99. }
  100. else
  101. {
  102. b.x = x;
  103. b.y = y;
  104. outCode2 = getOutCode( b, zRObj );
  105. }
  106. }
  107. }
  108. if( ok )
  109. {
  110. int xlän = b.x - a.x, axlän = abs( xlän );
  111. int ylän = b.y - a.y, aylän = abs( ylän );
  112. double xf = (double)xlän / ( aylän ? aylän : 1 );
  113. double yf = (double)ylän / ( axlän ? axlän : 1 );
  114. if( axlän > aylän )
  115. xf = xf < 0 ? -1 : 1;
  116. else
  117. yf = yf < 0 ? -1 : 1;
  118. double x = (double)a.x, y = (double)a.y;
  119. int maxP = (int)( sqrt( xlän * xlän + ylän * ylän ) + 0.5 );
  120. int count = 0;
  121. int *buffer = zRObj.getBuffer();
  122. int maxPixel = zRObj.getBreite() * zRObj.getHeight();
  123. while( !( (int)( x + 0.5 ) == b.x && (int)( y + 0.5 ) == b.y ) && count < maxP )
  124. {
  125. ++count;
  126. if( (int)( (int)( x + 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) < maxPixel )
  127. buffer[ (int)( (int)( x + 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) ] = 0xFFFFFFFF;
  128. if( (int)( (int)( x - 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) < maxPixel &&
  129. buffer[ (int)( (int)( x - 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  130. zRObj.alphaPixel( (int)( x - 0.5 ), (int)( y + 0.5 ), f );
  131. if( (int)( (int)( x + 1.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) < maxPixel &&
  132. buffer[ (int)( (int)( x + 1.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  133. zRObj.alphaPixel( (int)( x + 1.5 ), (int)( y + 0.5 ), f );
  134. if( (int)( (int)( x + 0.5 ) + (int)( y - 0.5 ) * zRObj.getBreite() ) < maxPixel &&
  135. buffer[ (int)( (int)( x + 0.5 ) + (int)( y - 0.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  136. zRObj.alphaPixel( (int)( x + 0.5 ), (int)( y - 0.5 ), f );
  137. if( (int)( (int)( x + 0.5 ) + (int)( y + 1.5 ) * zRObj.getBreite() ) < maxPixel &&
  138. buffer[ (int)( (int)( x + 0.5 ) + (int)( y + 1.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  139. zRObj.alphaPixel( (int)( x + 0.5 ), (int)( y + 1.5 ), f );
  140. x += xf, y += yf;
  141. }
  142. }
  143. }
  144. void Laser::renderMinimap( Bild &zRObj, Karte *zMap )
  145. {
  146. int x = (int)( ( pos.x / zMap->getSize().x ) * 250 + 0.5 );
  147. int y = (int)( ( pos.y / zMap->getSize().y ) * 250 + 0.5 );
  148. zRObj.setPixelDP( x + zRObj.getDrawOff().x, y + zRObj.getDrawOff().y, tf );
  149. }
  150. // constant
  151. int Laser::getId() const
  152. {
  153. return id;
  154. }
  155. int Laser::getSpieler() const
  156. {
  157. return sNum;
  158. }
  159. double Laser::getIntensität() const
  160. {
  161. return intensität;
  162. }
  163. // Reference Counting
  164. Laser *Laser::getThis()
  165. {
  166. ref++;
  167. return this;
  168. }
  169. Laser *Laser::release()
  170. {
  171. ref--;
  172. if( !ref )
  173. delete this;
  174. return 0;
  175. }