player.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* This file is part of Invaders.
  2. *
  3. * Copyright (C) 2020 LCM.
  4. * You may use, distribute and modify Invaders under the terms of the
  5. * GPLv3 license, available at <https://www.gnu.org/licenses/\>.
  6. */
  7. #include "player.hpp"
  8. player::player() : game_object(C/2, R-1, 1)
  9. {
  10. length=8;
  11. weaponclass=0;
  12. weaponclassrkt=0;
  13. rocketlauncher=FALSE;
  14. commands[0]='a'; //left
  15. commands[1]='d'; //right
  16. commands[2]='w'; //up
  17. commands[3]='s'; //down
  18. godmode=false;
  19. }
  20. player::player(int Length) : game_object(C/2, R, 1)
  21. {
  22. length=(Length<2?2:(Length>20?20:Length));
  23. weaponclass=0;
  24. weaponclassrkt=0;
  25. rocketlauncher=FALSE;
  26. commands[0]='a'; //left
  27. commands[1]='d'; //right
  28. commands[2]='w'; //up
  29. commands[3]='s'; //down
  30. godmode=false;
  31. }
  32. void player::set_commands(int* _commands){
  33. for(int i=0;i<4;i++)
  34. commands[i]=_commands[i];
  35. }
  36. void player::next_pos(int command)
  37. {
  38. if((command==commands[0] || command==KEY_LEFT) && x>0) x-=2;
  39. if((command==commands[1] || command==KEY_RIGHT) && x<C-length) x+=2;
  40. if((command==commands[2] || command==KEY_UP) && y>0) y--;
  41. if((command==commands[3] || command==KEY_DOWN) && y<R-1) y++;
  42. }
  43. void player::shoot(std::list<bullet> & bullets)
  44. {
  45. static int thiscannon=0;
  46. if(weaponclass==0){
  47. if(thiscannon)
  48. {
  49. bullet new_bullet(x+(int)(length/2.),y,2);
  50. bullets.push_back(new_bullet);
  51. }
  52. else
  53. {
  54. bullet new_bullet(x+(int)(length/2.)-1,y,2);
  55. bullets.push_back(new_bullet);
  56. }
  57. thiscannon= ~thiscannon;
  58. }
  59. else if(weaponclass==1){
  60. bullet new_bullet1(x+(int)(length/2.),y,2);
  61. bullets.push_back(new_bullet1);
  62. bullet new_bullet2(x+(int)(length/2.)-1,y,2);
  63. bullets.push_back(new_bullet2);
  64. }
  65. else if(weaponclass==2){
  66. if(thiscannon)
  67. {
  68. bullet new_bullet(x+(int)(length/2.),y,2);
  69. bullets.push_back(new_bullet);
  70. }
  71. else
  72. {
  73. bullet new_bullet(x+(int)(length/2.)-1,y,2);
  74. bullets.push_back(new_bullet);
  75. }
  76. thiscannon= ~thiscannon;
  77. if(length>2){
  78. bullet new_bullet1(x+1,y,2);
  79. bullets.push_back(new_bullet1);
  80. bullet new_bullet3(x+(length-2>(int)(length/2.)?length-2:((int)(length/2.)+1>length?length-1:(int)(length/2.)+1)),y,2);
  81. bullets.push_back(new_bullet3);
  82. }
  83. }
  84. else if(weaponclass==3){
  85. bullet new_bullet(x+(int)(length/2.),y,2);
  86. bullets.push_back(new_bullet);
  87. bullet new_bullet2(x+(int)(length/2.)-1,y,2);
  88. bullets.push_back(new_bullet2);
  89. if(length>2){
  90. bullet new_bullet1(x+1,y,2);
  91. bullets.push_back(new_bullet1);
  92. bullet new_bullet3(x+(length-2>(int)(length/2.)?length-2:((int)(length/2.)+1>length?length-1:(int)(length/2.)+1)),y,2);
  93. bullets.push_back(new_bullet3);
  94. }
  95. }
  96. else if(weaponclass==4){
  97. bullet new_bullet4(x+(int)(length/2.)-1,y,2);
  98. bullets.push_back(new_bullet4);
  99. bullet new_bullet5(x+(int)(length/2.),y,2);
  100. bullets.push_back(new_bullet5);
  101. if(length>2){
  102. bullet new_bullet6(x+(int)(length/2.)+1,y,2);
  103. bullets.push_back(new_bullet6);
  104. bullet new_bullet2(x+(length-2>(int)(length/2.)+1?length-2:length-1),y,2);
  105. bullets.push_back(new_bullet2);
  106. bullet new_bullet3(x+(1<(int)(length/2.)-2?1:0),y,2);
  107. bullets.push_back(new_bullet3);
  108. }
  109. if(length>4){
  110. bullet new_bullet7(x+(int)(length/2.)-2,y,2);
  111. bullets.push_back(new_bullet7);
  112. }
  113. }
  114. else if(weaponclass==5){
  115. for(int i=0;i<length;i++){
  116. bullet new_bullet(x+i,y,2);
  117. bullets.push_back(new_bullet);
  118. }
  119. }
  120. }
  121. void player::shootrkt(std::list<rocket>& rockets)
  122. {
  123. static int thiscannon=0;
  124. if(weaponclassrkt==0){
  125. if(thiscannon)
  126. {
  127. rocket new_rocket(x+(int)(length/2.),y,1);
  128. rockets.push_back(new_rocket);
  129. }
  130. else
  131. {
  132. rocket new_rocket(x+(int)(length/2.)-1,y,-1);
  133. rockets.push_back(new_rocket);
  134. }
  135. thiscannon= ~thiscannon;
  136. }
  137. else if(weaponclassrkt==1){
  138. rocket new_rocket1(x+(int)(length/2.),y,1);
  139. rockets.push_back(new_rocket1);
  140. rocket new_rocket2(x+(int)(length/2.)-1,y,-1);
  141. rockets.push_back(new_rocket2);
  142. }
  143. else if(weaponclassrkt==2){
  144. if(thiscannon)
  145. {
  146. rocket new_rocket(x+(int)(length/2.),y,1);
  147. rockets.push_back(new_rocket);
  148. }
  149. else
  150. {
  151. rocket new_rocket(x+(int)(length/2.)-1,y,-1);
  152. rockets.push_back(new_rocket);
  153. }
  154. thiscannon= ~thiscannon;
  155. if(length>2){
  156. rocket new_rocket1(x+1,y,1);
  157. rockets.push_back(new_rocket1);
  158. rocket new_rocket3(x+(length-2>(int)(length/2.)?length-2:((int)(length/2.)+1>length?length-1:(int)(length/2.)+1)),y,-1);
  159. rockets.push_back(new_rocket3);
  160. }
  161. }
  162. else if(weaponclassrkt==3){
  163. rocket new_rocket(x+(int)(length/2.),y,1);
  164. rockets.push_back(new_rocket);
  165. rocket new_rocket2(x+(int)(length/2.)-1,y,-1);
  166. rockets.push_back(new_rocket2);
  167. if(length>2){
  168. rocket new_rocket1(x+1,y,1);
  169. rockets.push_back(new_rocket1);
  170. rocket new_rocket3(x+(length-2>(int)(length/2.)?length-2:((int)(length/2.)+1>length?length-1:(int)(length/2.)+1)),y,-1);
  171. rockets.push_back(new_rocket3);
  172. }
  173. }
  174. else if(weaponclassrkt==4){
  175. rocket new_rocket4(x+(int)(length/2.)-1,y,1);
  176. rockets.push_back(new_rocket4);
  177. rocket new_rocket5(x+(int)(length/2.),y,-1);
  178. rockets.push_back(new_rocket5);
  179. if(length>2){
  180. rocket new_rocket6(x+(int)(length/2.)+1,y,1);
  181. rockets.push_back(new_rocket6);
  182. rocket new_rocket2(x+(length-2>(int)(length/2.)+1?length-2:length-1),y,-1);
  183. rockets.push_back(new_rocket2);
  184. rocket new_rocket3(x+(1<(int)(length/2.)-2?1:0),y,1);
  185. rockets.push_back(new_rocket3);
  186. }
  187. if(length>4){
  188. rocket new_rocket7(x+(int)(length/2.)-2,y,-1);
  189. rockets.push_back(new_rocket7);
  190. }
  191. }
  192. else if(weaponclassrkt==5){
  193. for(int i=0;i<length;i++){
  194. rocket new_rocket(x+i,y,pow(-1.,(double)i));
  195. rockets.push_back(new_rocket);
  196. }
  197. }
  198. }