Laser.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. : Object2D()
  9. {
  10. this->id = id;
  11. setPosition( pos );
  12. setSpeed( speed );
  13. this->sNum = sNum;
  14. this->intensität = intensität;
  15. this->startIntensität = intensität;
  16. this->tf = tf;
  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. Rect2< float > Laser::getBoundingBox() const
  34. {
  35. return Rect2< float >();
  36. }
  37. bool Laser::tick( const WeltInfo &info, double tv )
  38. {
  39. __super::tick( info, tv );
  40. intensität -= tv * 2;
  41. return 1;
  42. }
  43. void Laser::render( Mat3< float > &kamMat, Bild &zRObj )
  44. {
  45. int fa = (int)( ( intensität / startIntensität ) * 255 );
  46. int f = ( ( fa << 24 ) & 0xFF000000 ) | ( tf & 0xFFFFFF );
  47. Punkt a = (Punkt)( kamMat * getPosition() );
  48. Punkt b = (Punkt)( kamMat * ( getPosition() - ( getSpeed() / getSpeed().getLength() * 10 ) ) );
  49. a += zRObj.getDrawOff();
  50. b += zRObj.getDrawOff();
  51. char outCode1 = getOutCode( a, zRObj );
  52. char outCode2 = getOutCode( b, zRObj );
  53. bool ok = 0;
  54. while( 1 )
  55. {
  56. int xMax = zRObj.getDrawGr().x - 1;
  57. int yMax = zRObj.getDrawGr().y - 1;
  58. if( !( outCode1 | outCode2 ) )
  59. {
  60. ok = 1;
  61. break;
  62. }
  63. else if( outCode1 & outCode2 )
  64. break;
  65. else
  66. {
  67. int x = 0, y = 0;
  68. char outCodeOut = outCode1 ? outCode1 : outCode2;
  69. if( outCodeOut & 8 )
  70. {
  71. x = (int)( a.x + ( b.x - a.x ) * ( yMax - a.y ) / ( b.y - a.y ) + 0.5 );
  72. y = yMax;
  73. }
  74. else if( outCodeOut & 4 )
  75. {
  76. x = (int)( a.x + ( b.x - a.x ) * ( zRObj.getDrawPos().y - a.y ) / ( b.y - a.y ) + 0.5 );
  77. y = zRObj.getDrawPos().y;
  78. }
  79. else if( outCodeOut & 2 )
  80. {
  81. y = (int)( a.y + ( b.y - a.y ) * ( xMax - a.x ) / ( b.x - a.x ) + 0.5 );
  82. x = xMax;
  83. }
  84. else if( outCodeOut & 1 )
  85. {
  86. y = (int)( a.y + ( b.y - a.y ) * ( zRObj.getDrawPos().x - a.x ) / ( b.x - a.x ) + 0.5 );
  87. x = zRObj.getDrawPos().x;
  88. }
  89. if( outCodeOut == outCode1 )
  90. {
  91. a.x = x;
  92. a.y = y;
  93. outCode1 = getOutCode( a, zRObj );
  94. }
  95. else
  96. {
  97. b.x = x;
  98. b.y = y;
  99. outCode2 = getOutCode( b, zRObj );
  100. }
  101. }
  102. }
  103. if( ok )
  104. {
  105. int xlän = b.x - a.x, axlän = abs( xlän );
  106. int ylän = b.y - a.y, aylän = abs( ylän );
  107. double xf = (double)xlän / ( aylän ? aylän : 1 );
  108. double yf = (double)ylän / ( axlän ? axlän : 1 );
  109. if( axlän > aylän )
  110. xf = xf < 0 ? -1 : 1;
  111. else
  112. yf = yf < 0 ? -1 : 1;
  113. double x = (double)a.x, y = (double)a.y;
  114. int maxP = (int)( sqrt( xlän * xlän + ylän * ylän ) + 0.5 );
  115. int count = 0;
  116. int *buffer = zRObj.getBuffer();
  117. int maxPixel = zRObj.getBreite() * zRObj.getHeight();
  118. while( !( (int)( x + 0.5 ) == b.x && (int)( y + 0.5 ) == b.y ) && count < maxP )
  119. {
  120. ++count;
  121. if( (int)( (int)( x + 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) < maxPixel )
  122. buffer[ (int)( (int)( x + 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) ] = 0xFFFFFFFF;
  123. if( (int)( (int)( x - 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) < maxPixel &&
  124. buffer[ (int)( (int)( x - 0.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  125. zRObj.alphaPixel( (int)( x - 0.5 ), (int)( y + 0.5 ), f );
  126. if( (int)( (int)( x + 1.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) < maxPixel &&
  127. buffer[ (int)( (int)( x + 1.5 ) + (int)( y + 0.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  128. zRObj.alphaPixel( (int)( x + 1.5 ), (int)( y + 0.5 ), f );
  129. if( (int)( (int)( x + 0.5 ) + (int)( y - 0.5 ) * zRObj.getBreite() ) < maxPixel &&
  130. buffer[ (int)( (int)( x + 0.5 ) + (int)( y - 0.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  131. zRObj.alphaPixel( (int)( x + 0.5 ), (int)( y - 0.5 ), f );
  132. if( (int)( (int)( x + 0.5 ) + (int)( y + 1.5 ) * zRObj.getBreite() ) < maxPixel &&
  133. buffer[ (int)( (int)( x + 0.5 ) + (int)( y + 1.5 ) * zRObj.getBreite() ) ] != 0xFFFFFFFF )
  134. zRObj.alphaPixel( (int)( x + 0.5 ), (int)( y + 1.5 ), f );
  135. x += xf, y += yf;
  136. }
  137. }
  138. }
  139. // constant
  140. int Laser::getId() const
  141. {
  142. return id;
  143. }
  144. int Laser::getSpieler() const
  145. {
  146. return sNum;
  147. }
  148. double Laser::getIntensität( Vec2< double > targetSpeed ) const
  149. {
  150. return intensität * ( ( speed - targetSpeed ).getLength() / 200 );
  151. }
  152. // Reference Counting
  153. Laser *Laser::getThis()
  154. {
  155. ref++;
  156. return this;
  157. }
  158. Laser *Laser::release()
  159. {
  160. ref--;
  161. if( !ref )
  162. delete this;
  163. return 0;
  164. }