Advertisement
Guest User

player.h

a guest
Jun 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. #ifndef PLAYER_HEADER
  2. #define PLAYER_HEADER
  3.  
  4. typedef struct _TIROS{//shot struct
  5. WINDOW *curWin;
  6. int vivo,locx,locy;
  7. int direcaotiro;
  8. } tiro;
  9.  
  10. tiro * inicializa_tiro(WINDOW *win){
  11. tiro *t = (tiro*) malloc(sizeof(tiro));
  12. t->curWin = win;
  13. t->locx = 0;
  14. t->locy = 0;
  15. t->vivo = 0;
  16. t->direcaotiro = '>';
  17. return t;
  18. }
  19.  
  20. typedef struct _JOGADOR{ //estrutura para jogador
  21. WINDOW *curWin;
  22. int locX,locY,maxX,maxY;//player coord //maximum coordinate
  23. unsigned short int direcao;//player arrow direction;
  24. int vida; //alive or not
  25. int qtiros;
  26. } jogador;
  27.  
  28.  
  29. jogador *inicializajogador(WINDOW *win,int y,int x)//initialize with specificated coordinates
  30. {
  31. jogador *p = (jogador*) malloc(sizeof(jogador));
  32. p->curWin = win;
  33. p->locX=x;p->locY=y;
  34. p->direcao='>';
  35. p->vida=5;
  36. p->qtiros=0;
  37. // (*p).vivo = 1;
  38. //this function write the curWin limits on maxX and maxY
  39. getmaxyx(p->curWin,p->maxY,p->maxX);
  40. return p;
  41. }
  42.  
  43.  
  44. void adciona_tiro_ao_jogador(jogador *p,tiro *t){
  45. p->qtiros++;
  46. t->curWin=p->curWin;
  47. switch (p->direcao)
  48. {
  49. case '^':
  50. t->direcaotiro = '^';
  51. t->locx=p->locX+1;//those 2 make the shots appear on the player coord
  52. t->locy=p->locY;//the small variations on locY and locX are to make the shots appear in the right position
  53. break;
  54. case '<':
  55. t->direcaotiro = '<';
  56. t->locx=p->locX-3;
  57. t->locy=p->locY;
  58. break;
  59. case 'v':
  60. t->direcaotiro = 'v';
  61. t->locx=p->locX+1;
  62. t->locy=p->locY+2;
  63. break;
  64. case '>':
  65. t->direcaotiro = '>';
  66. t->locx=p->locX+5;
  67. t->locy=p->locY;
  68. break;
  69. default:
  70. break;
  71. }
  72. t->vivo=1;
  73. }
  74. void clearPlayerTrack(jogador *p1){
  75. if(p1->direcao == '^'){
  76. mvwprintw(p1->curWin,p1->locY-1,p1->locX+1," ");
  77. mvwprintw(p1->curWin,p1->locY,p1->locX," ");
  78. }else if(p1->direcao == 'v'){
  79. mvwprintw(p1->curWin,p1->locY+1,p1->locX+1," ");
  80. mvwprintw(p1->curWin,p1->locY,p1->locX," ");
  81. }else if(p1->direcao == '>'){
  82. mvwprintw(p1->curWin,p1->locY,p1->locX," ");
  83. }else if(p1->direcao == '<'){
  84. mvwprintw(p1->curWin,p1->locY,p1->locX-2," ");
  85. if(p1->locX == 0){
  86. mvwprintw(p1->curWin,p1->locY,p1->locX," ");
  87. }
  88. }
  89. }
  90. /*each one of those functions controls p.direcao, p.locY and p.locX, the moviment limit
  91. that is defined on game(), is also used here*/
  92. void movUp(jogador *p1){
  93. if(p1->locY>0){
  94. clearPlayerTrack(p1);
  95. p1->locY--;
  96. p1->direcao = '^';
  97. }
  98. }
  99. void movLeft(jogador *p1){
  100. if(p1->locX>0){
  101. clearPlayerTrack(p1);
  102. p1->locX--;
  103. p1->direcao = '<';
  104. }
  105. }
  106. void movRight(jogador *p1){
  107. if(p1->locX<p1->maxX-4){
  108. clearPlayerTrack(p1);
  109. p1->locX++;
  110. p1->direcao = '>';
  111. }
  112. }
  113. void movDown(jogador *p1){
  114. if(p1->locY<p1->maxY-1){
  115. clearPlayerTrack(p1);
  116. p1->locY++;
  117. p1->direcao = 'v';
  118. }
  119.  
  120. }
  121.  
  122.  
  123. void desenhaplayer(jogador *p,int num){ //draw the player on terminal
  124. mvwprintw(p->curWin,p->locY,p->locX,"\\%d/",num); //print the keys that follow the player
  125. switch (p->direcao)
  126. {
  127. case '^':
  128. mvwprintw(p->curWin,p->locY-1,p->locX+1,"^",num);
  129. break;
  130. case '<':
  131. mvwprintw(p->curWin,p->locY,p->locX-2,"<",num);
  132. break;
  133. case 'v':
  134. mvwprintw(p->curWin,p->locY+1,p->locX+1,"v",num);
  135. break;
  136. case '>':
  137. mvwprintw(p->curWin,p->locY,p->locX+4,">",num);
  138. break;
  139. default:
  140. break;
  141. }
  142. return;
  143. }
  144.  
  145. int controle(jogador *p,float *tatirar,float *tandar,tiro *t[]){ //here is the controler for player1
  146. int choice = wgetch(p->curWin);
  147. switch (choice)
  148. {
  149. case ERR:
  150. break;
  151. case '8':
  152. case 'w':
  153. if(time_elapsed(tandar,0.08))
  154. movUp(p);
  155. break;
  156. case '4':
  157. case 'a':
  158. if(time_elapsed(tandar,0.06))
  159. movLeft(p);
  160. break;
  161. case '5':
  162. case 's':
  163. if(time_elapsed(tandar,0.08))
  164. movDown(p);
  165. break;
  166. case '6':
  167. case 'd':
  168. if(time_elapsed(tandar,0.06))
  169. movRight(p);
  170. break;
  171. case 32 ://space key
  172. case '0':
  173. if(time_elapsed(tatirar,0.2)){
  174. for(int i=0;i<MAX_SHOTS;i++){
  175. if(t[i]->vivo==0){
  176. adciona_tiro_ao_jogador(p,t[i]);
  177. break;
  178. }
  179. }
  180. }
  181. default:
  182. break;
  183. }
  184. return choice;
  185. }
  186.  
  187. void desenhatiroY(tiro * t[],jogador *p){//draw shots on terminal
  188. int cont;
  189. for(cont=0;cont<MAX_SHOTS;cont++){//MAX_SHOTS is number of shots defined in main
  190. if(t[cont]->vivo==1){
  191. if(t[cont]->direcaotiro=='^'){
  192. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,' ');
  193. t[cont]->locy--;
  194. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,'^');
  195. }else if(t[cont]->direcaotiro=='v'){
  196. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,' ');
  197. t[cont]->locy++;
  198. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,'v');
  199. }
  200. }
  201. }
  202. }
  203. void desenhatiroX(tiro * t[],jogador *p){//draw shots on terminal
  204. int cont;
  205. for(cont=0;cont<MAX_SHOTS;cont++){//MAX_SHOTS is number of shots defined in main
  206. if(t[cont]->vivo==1){
  207. if(t[cont]->direcaotiro=='<'){
  208. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,' ');
  209. t[cont]->locx--;
  210. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,'<');
  211. }else if(t[cont]->direcaotiro=='>'){
  212. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,' ');
  213. t[cont]->locx++;
  214. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,'>');
  215. }
  216. }
  217. }
  218. }
  219. void colisao_tiro(tiro *t[],int maxY,int maxX){
  220. int cont;
  221. for(cont=0;cont<MAX_SHOTS;cont++){
  222. if(t[cont]->locx<0||t[cont]->locx>maxX||t[cont]->locy<0||t[cont]->locy>maxY){
  223. t[cont]->vivo=0;
  224. mvwaddch(t[cont]->curWin,t[cont]->locy,t[cont]->locx,' ');
  225. }
  226. }
  227. }
  228.  
  229.  
  230. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement