wall.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "wall.h"
  2. Wall *addWall(Wall *list, int d, int h, int id, int line)
  3. {
  4. Wall *tmp;
  5. Wall *new;
  6. tmp = list;
  7. new = malloc(sizeof(Wall));
  8. new->d = d;
  9. new->h = h;
  10. new->id = id;
  11. new->line = line;
  12. new->nxt = NULL;
  13. if(list == NULL)
  14. return new;
  15. else if(list != NULL)
  16. {
  17. while (tmp->nxt != NULL){
  18. tmp = tmp->nxt;
  19. }
  20. tmp->nxt = new;
  21. return list;
  22. }
  23. }
  24. Wall *removeWall(Wall *list, int d)
  25. {
  26. Wall *tmp1;
  27. Wall *tmp2;
  28. if(list->d <= 0)
  29. {
  30. if(list->nxt != NULL)
  31. tmp1 = list->nxt;
  32. else
  33. tmp1 = NULL;
  34. free(list);
  35. return tmp1;
  36. }
  37. tmp1 = list;
  38. do{
  39. if(tmp1->nxt != NULL)
  40. {
  41. if(tmp1->nxt->d <= 0)
  42. {
  43. tmp2 = tmp1->nxt;
  44. if(tmp1->nxt->nxt != NULL)
  45. tmp1->nxt = tmp1->nxt->nxt;
  46. else
  47. tmp1->nxt = NULL;
  48. free(tmp2);
  49. }
  50. }
  51. tmp1 = tmp1->nxt;
  52. }while(tmp1 != NULL);
  53. return list;
  54. }
  55. void update(Wall *list, unsigned int delta_time)
  56. {
  57. Wall *tmp;
  58. tmp = list;
  59. do{
  60. if(tmp != NULL)
  61. {
  62. //just reducing the distance from the center
  63. tmp->d-=1;
  64. }
  65. tmp = tmp->nxt;
  66. }while(tmp != NULL);
  67. }
  68. //these are the actual drawing functions
  69. //they should the rewritten from scratch
  70. int getSlopeIndex(int dot1, int dot2)
  71. {
  72. if(dot2 - dot1 == 1)
  73. {
  74. return dot1;
  75. }else if(dot2 - dot1 == -1){
  76. return dot2;
  77. }else return 3;
  78. }
  79. void show(Wall *list, Camera *cam)
  80. {
  81. /*
  82. 0________________1
  83. / 0 \
  84. 3/ \1
  85. / \
  86. 3/______________________\2
  87. 2
  88. */
  89. Wall *tmp;
  90. tmp = list;
  91. do{
  92. if(tmp != NULL)
  93. {
  94. if(tmp->d + tmp->h< 64)
  95. {
  96. const float angle = PI * ((tmp->line)*60 +cam->angle) / 180;
  97. const float cos1 = cos(angle);
  98. const float cos2 = cos(angle + PI/3);
  99. const float sin1 = sin(angle);
  100. const float sin2 = sin(angle + PI/3);
  101. int x[4];
  102. int y[4];
  103. float slopes[4];
  104. int i = 0;
  105. int j = 0;
  106. int tmpInt = 0;
  107. int x1=0, x2 = 0;
  108. //finding the two active edges
  109. int leftDotIndex = 0, rightDotIndex = 0, leftSlope=0, rightSlope=0;
  110. x[0]=tmp->d * cos1 + 64;
  111. x[1]=tmp->d * cos2 + 64;
  112. x[2]= (tmp->h + tmp->d) * cos2 + 64;
  113. x[3]=(tmp->h + tmp->d) * cos1 + 64;
  114. y[0]=tmp->d * sin1 + 32;
  115. y[1]=tmp->d * sin2 + 32;
  116. y[2]= (tmp->h + tmp->d) * sin2 + 32;
  117. y[3]=(tmp->h + tmp->d) * sin1 + 32;
  118. /*slopes[0] = (y[1] - y[0])/(x[1]-x[0]);
  119. slopes[1] = (y[2] - y[1])/(x[2]-x[1]);
  120. slopes[2] = (y[3] - y[2])/(x[3]-x[2]);
  121. slopes[3] = (y[0] - y[3])/(x[0]-x[3]);*/
  122. /*for(i = 0; i < tmp->h; i+=0.5)
  123. {
  124. ML_line((tmp->d + i) * cos2 + 64, (tmp->d + i) * sin2 + 32, (tmp->d + i) * cos1 + 64, (tmp->d + i) * sin1 + 32, BLACK);
  125. }*/
  126. /* i = y[0];
  127. j = y[0];
  128. for(i = 0; i < 4; i++)
  129. if(y[tmpInt] > y[i])
  130. tmpInt = i;
  131. i = tmpInt;
  132. tmpInt = 0;
  133. for(j = 0; j < 4; j++)
  134. if(y[tmpInt] < y[j])
  135. tmpInt = j;
  136. j = tmpInt;
  137. x1 = x[i];
  138. x2 = x[i];
  139. //i contains an index to the highest vertex and j the lowest
  140. tmpInt = 0;
  141. for(leftDotIndex = 0; leftDotIndex < 4; leftDotIndex ++)
  142. {
  143. if(leftDotIndex != i && leftDotIndex != j && (tmpInt == 0 || tmpInt > x[leftDotIndex]))
  144. tmpInt = x[leftDotIndex];
  145. }
  146. for(rightDotIndex = 0; rightDotIndex == i || rightDotIndex == j || rightDotIndex == leftDotIndex; rightDotIndex ++)
  147. {}
  148. tmpInt = i;
  149. while(i <= j)
  150. {
  151. //getting the active slopes' indexes
  152. if(y[i] < y[leftDotIndex])
  153. leftSlope = getSlopeIndex(i, leftDotIndex);
  154. else leftSlope = getSlopeIndex(leftDotIndex, j);
  155. if(y[i] < y[rightDotIndex])
  156. rightSlope = getSlopeIndex(i, rightDotIndex);
  157. else rightSlope = getSlopeIndex(rightDotIndex, j);
  158. ML_horizontal_line(y[tmpInt] + i, x1, x2, BLACK);
  159. ML_horizontal_line(y[tmpInt] + i, x2, x1, BLACK);
  160. x1 = x1 - (1/slopes[leftSlope]);
  161. x2 = x2 - (1/slopes[rightSlope]);
  162. i++;
  163. }*/
  164. ML_filled_polygone(x, y, 4, BLACK);
  165. }
  166. }
  167. tmp = tmp->nxt;
  168. }while(tmp != NULL);
  169. }
  170. //tests every Wall in the list
  171. bool isColliding(Wall *list, int player_angle)
  172. {
  173. Wall *tmp;
  174. tmp = list;
  175. do{
  176. if(tmp != NULL)
  177. {
  178. if(tmp-> d <= 8)//if the wall is close enough from the center of the screen
  179. { //and is on the same line than the player
  180. if(tmp->line == (int)(player_angle/60)) //&& tmp->line * 60 + 60 > player_angle)
  181. { //BOOM
  182. return true;
  183. }
  184. }
  185. }
  186. tmp = tmp->nxt;
  187. }while(tmp != NULL);
  188. return false;
  189. }