Geschoss.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "Geschoss.h"
  2. #include "Spiel.h"
  3. Geschoss::Geschoss( ResourceRegistry *zResources, int id, float speed, GeschossTyp typ, Richtung r, int x, int y, Spieler *besitzer )
  4. : GameObject( GESCHOSS, x, y, 15, 15 )
  5. {
  6. this->speed = speed;
  7. this->richtung = r;
  8. this->besitzer = besitzer;
  9. resources = zResources->getThis();
  10. tunnelBenutzt = 0;
  11. umgelenkt = 0;
  12. geschosseGetroffen = 0;
  13. schalter = 0;
  14. texturScale = 1;
  15. intersectWithOwner = 1;
  16. this->id = id;
  17. setTyp( typ );
  18. }
  19. Geschoss::~Geschoss()
  20. {
  21. if( besitzer )
  22. besitzer->release();
  23. resources->release();
  24. }
  25. void Geschoss::invertDirection()
  26. {
  27. richtung = invert( richtung );
  28. }
  29. void Geschoss::addUmlenkung( Spiel *zSpiel )
  30. {
  31. zSpiel->setGeschossZuletztUmgelenkt( (Geschoss *)getThis() );
  32. umgelenkt++;
  33. }
  34. void Geschoss::addGeschossTreffer( Spiel *zSpiel )
  35. {
  36. zSpiel->setGeschossZuletztGeschossGetroffen( (Geschoss *)getThis() );
  37. geschosseGetroffen++;
  38. }
  39. void Geschoss::addTunnel( Spiel *zSpiel )
  40. {
  41. zSpiel->setGeschossZuletztTunnelBenutzt( (Geschoss *)getThis() );
  42. schalter++;
  43. }
  44. void Geschoss::addSchalter()
  45. {
  46. id++;
  47. }
  48. void Geschoss::setSpeed( float speed )
  49. {
  50. this->speed = speed;
  51. }
  52. void Geschoss::setBesitzer( Spieler *besitzer )
  53. {
  54. if( this->besitzer )
  55. this->besitzer->release();
  56. this->besitzer = besitzer;
  57. }
  58. void Geschoss::setTyp( GeschossTyp typ )
  59. {
  60. if( textur )
  61. textur->release();
  62. this->typ = typ;
  63. if( typ == GESCHOSS_PFEIL )
  64. {
  65. if( richtung == RECHTS )
  66. textur = resources->zResource( R_PFEIL_GESCHOSS, 0 )->getImages()->getThis();
  67. else if( richtung == LINKS )
  68. {
  69. Bild *r = resources->zResource( R_PFEIL_GESCHOSS, 0 )->getImages();
  70. this->textur = new Bild();
  71. this->textur->neuBild( r->getBreite(), r->getHeight(), 0 );
  72. this->textur->drawBild180( 0, 0, r->getBreite(), r->getHeight(), *r );
  73. }
  74. else
  75. {
  76. Bild *r = resources->zResource( R_PFEIL_GESCHOSS, 0 )->getImages();
  77. this->textur = new Bild();
  78. this->textur->neuBild( r->getHeight(), r->getBreite(), 0 );
  79. if( richtung == UNTEN )
  80. this->textur->drawBild90( 0, 0, r->getHeight(), r->getBreite(), *r );
  81. else
  82. this->textur->drawBild270( 0, 0, r->getHeight(), r->getBreite(), *r );
  83. }
  84. if( richtung == OBEN || richtung == UNTEN )
  85. {
  86. setWidth( 7 );
  87. setHeight( 30 );
  88. }
  89. else
  90. {
  91. setWidth( 30 );
  92. setHeight( 7 );
  93. }
  94. }
  95. else if( typ == GESCHOSS_FEUERBALL )
  96. {
  97. setWidth( 20 );
  98. setHeight( 20 );
  99. currentImage = 0;
  100. nextImage = 0.075;
  101. textur = resources->zResource( R_FEUERBALL_GESCHOSS, 0 )->getImages()->getThis();
  102. }
  103. else
  104. {
  105. setWidth( 15 );
  106. setHeight( 15 );
  107. if( typ == GESCHOSS_DRACHENAUGE )
  108. {
  109. currentImage = 0;
  110. nextImage = 0.075;
  111. textur = resources->zResource( R_DRACHENAUGE_GESCHOSS, 0 )->getImages()->getThis();
  112. }
  113. if( typ == GESCHOSS_KUGEL )
  114. textur = resources->zResource( R_KUGEL_GESCHOSS, 0 )->getImages()->getThis();
  115. if( typ == GESCHOSS_MINE )
  116. textur = resources->zResource( R_MINE_GESCHOSS, 0 )->getImages()->getThis();
  117. }
  118. }
  119. void Geschoss::setRichtung( Richtung r )
  120. {
  121. richtung = r;
  122. }
  123. void Geschoss::tick( double zeit )
  124. {
  125. switch( richtung )
  126. {
  127. case OBEN:
  128. y -= (float)zeit * speed;
  129. break;
  130. case UNTEN:
  131. y += (float)zeit * speed;
  132. break;
  133. case RECHTS:
  134. x += (float)zeit * speed;
  135. break;
  136. case LINKS:
  137. x -= (float)zeit * speed;
  138. break;
  139. default:
  140. break;
  141. }
  142. if( typ == GESCHOSS_FEUERBALL )
  143. {
  144. nextImage -= zeit;
  145. if( nextImage <= 0 )
  146. {
  147. nextImage += 0.075;
  148. currentImage++;
  149. if( resources->zResource( R_FEUERBALL_GESCHOSS, 0 )->getImageCount() >= currentImage )
  150. currentImage = 0;
  151. textur->release();
  152. textur = resources->zResource( R_FEUERBALL_GESCHOSS, 0 )->getImage( currentImage );
  153. }
  154. }
  155. if( typ == GESCHOSS_DRACHENAUGE )
  156. {
  157. nextImage -= zeit;
  158. if( nextImage <= 0 )
  159. {
  160. nextImage += 0.075;
  161. currentImage++;
  162. if( resources->zResource( R_DRACHENAUGE_GESCHOSS, 0 )->getImageCount() >= currentImage )
  163. currentImage = 0;
  164. textur->release();
  165. textur = resources->zResource( R_DRACHENAUGE_GESCHOSS, 0 )->getImage( currentImage );
  166. }
  167. }
  168. }
  169. bool Geschoss::intersectsWith( GameObject *zObj )
  170. {
  171. if( zObj == besitzer && intersectWithOwner )
  172. {
  173. intersectWithOwner &= GameObject::intersectsWith( zObj );
  174. return 0;
  175. }
  176. return GameObject::intersectsWith( zObj );
  177. }
  178. GeschossTyp Geschoss::getTyp() const
  179. {
  180. return typ;
  181. }
  182. Spieler *Geschoss::zBesitzer() const
  183. {
  184. return besitzer;
  185. }
  186. Spieler *Geschoss::getBesitzer() const
  187. {
  188. return besitzer ? (Spieler *)besitzer->getThis() : 0;
  189. }
  190. Richtung Geschoss::getRichtung() const
  191. {
  192. return richtung;
  193. }
  194. FeuerballTreffer::FeuerballTreffer( ResourceRegistry *zResources, int id, int x, int y, Spieler *besitzer, int maxZeit )
  195. : GameObject( FEUERBALL_TREFFER, x - 75, y - 75, 150, 150 )
  196. {
  197. this->id = id;
  198. this->besitzer = besitzer;
  199. this->timeLeft = (float)maxZeit;
  200. count = 0;
  201. animation = zResources->getResource( R_FEUERBALL_TREFFER, 0 );
  202. currentImage = 0;
  203. texturScale = 1;
  204. nextImage = 0.075;
  205. textur = animation->getImage( currentImage );
  206. }
  207. FeuerballTreffer::~FeuerballTreffer()
  208. {
  209. besitzer->release();
  210. animation->release();
  211. }
  212. void FeuerballTreffer::tick( double zeit )
  213. {
  214. timeLeft -= (float)zeit;
  215. count++;
  216. nextImage -= zeit;
  217. if( nextImage <= 0 )
  218. {
  219. nextImage += 0.075;
  220. currentImage++;
  221. if( currentImage >= animation->getImageCount() )
  222. currentImage = 0;
  223. textur->release();
  224. textur = animation->getImage( currentImage );
  225. }
  226. }
  227. bool FeuerballTreffer::isOver() const
  228. {
  229. return timeLeft <= 0;
  230. }
  231. bool FeuerballTreffer::makeDamage() const
  232. {
  233. return count % 2;
  234. }
  235. Spieler *FeuerballTreffer::zVerursacher() const
  236. {
  237. return besitzer;
  238. }