wall.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.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. //we want to move the obstacle by 1 every two ticks (1/64 seconds ~= 1/60)
  58. //
  59. Wall *tmp;
  60. tmp = list;
  61. do{
  62. if(tmp != NULL)
  63. {
  64. //just reducing the distance from the center
  65. tmp->d -= 0.5 * delta_time;
  66. }
  67. tmp = tmp->nxt;
  68. }while(tmp != NULL);
  69. }
  70. //these are the actual drawing functions
  71. //they should the rewritten from scratch
  72. int getSlopeIndex(int dot1, int dot2)
  73. {
  74. if(dot2 - dot1 == 1)
  75. {
  76. return dot1;
  77. }else if(dot2 - dot1 == -1){
  78. return dot2;
  79. }else return 3;
  80. }
  81. void show(Wall *list, Camera *cam)
  82. {
  83. /*
  84. 0________________1
  85. / 0 \
  86. 3/ \1
  87. / \
  88. 3/______________________\2
  89. 2
  90. */
  91. Wall *tmp;
  92. tmp = list;
  93. do{
  94. if(tmp != NULL)
  95. {
  96. if(tmp->d + tmp->h< 64)
  97. {
  98. const float angle = PI * ((tmp->line)*60 +cam->angle) / 180;
  99. const float cos1 = cos(angle);
  100. const float cos2 = cos(angle + PI/3);
  101. const float sin1 = sin(angle);
  102. const float sin2 = sin(angle + PI/3);
  103. int x[4];
  104. int y[4];
  105. float slopes[4];
  106. int i = 0;
  107. int j = 0;
  108. int tmpInt = 0;
  109. int x1=0, x2 = 0;
  110. //finding the two active edges
  111. int leftDotIndex = 0, rightDotIndex = 0, leftSlope=0, rightSlope=0;
  112. x[0]=tmp->d * cos1 + 64;
  113. x[1]=tmp->d * cos2 + 64;
  114. x[2]= (tmp->h + tmp->d) * cos2 + 64;
  115. x[3]=(tmp->h + tmp->d) * cos1 + 64;
  116. y[0]=tmp->d * sin1 + 32;
  117. y[1]=tmp->d * sin2 + 32;
  118. y[2]= (tmp->h + tmp->d) * sin2 + 32;
  119. y[3]=(tmp->h + tmp->d) * sin1 + 32;
  120. /*slopes[0] = (y[1] - y[0])/(x[1]-x[0]);
  121. slopes[1] = (y[2] - y[1])/(x[2]-x[1]);
  122. slopes[2] = (y[3] - y[2])/(x[3]-x[2]);
  123. slopes[3] = (y[0] - y[3])/(x[0]-x[3]);*/
  124. /*for(i = 0; i < tmp->h; i+=0.5)
  125. {
  126. ML_line((tmp->d + i) * cos2 + 64, (tmp->d + i) * sin2 + 32, (tmp->d + i) * cos1 + 64, (tmp->d + i) * sin1 + 32, BLACK);
  127. }*/
  128. /* i = y[0];
  129. j = y[0];
  130. for(i = 0; i < 4; i++)
  131. if(y[tmpInt] > y[i])
  132. tmpInt = i;
  133. i = tmpInt;
  134. tmpInt = 0;
  135. for(j = 0; j < 4; j++)
  136. if(y[tmpInt] < y[j])
  137. tmpInt = j;
  138. j = tmpInt;
  139. x1 = x[i];
  140. x2 = x[i];
  141. //i contains an index to the highest vertex and j the lowest
  142. tmpInt = 0;
  143. for(leftDotIndex = 0; leftDotIndex < 4; leftDotIndex ++)
  144. {
  145. if(leftDotIndex != i && leftDotIndex != j && (tmpInt == 0 || tmpInt > x[leftDotIndex]))
  146. tmpInt = x[leftDotIndex];
  147. }
  148. for(rightDotIndex = 0; rightDotIndex == i || rightDotIndex == j || rightDotIndex == leftDotIndex; rightDotIndex ++)
  149. {}
  150. tmpInt = i;
  151. while(i <= j)
  152. {
  153. //getting the active slopes' indexes
  154. if(y[i] < y[leftDotIndex])
  155. leftSlope = getSlopeIndex(i, leftDotIndex);
  156. else leftSlope = getSlopeIndex(leftDotIndex, j);
  157. if(y[i] < y[rightDotIndex])
  158. rightSlope = getSlopeIndex(i, rightDotIndex);
  159. else rightSlope = getSlopeIndex(rightDotIndex, j);
  160. ML_horizontal_line(y[tmpInt] + i, x1, x2, BLACK);
  161. ML_horizontal_line(y[tmpInt] + i, x2, x1, BLACK);
  162. x1 = x1 - (1/slopes[leftSlope]);
  163. x2 = x2 - (1/slopes[rightSlope]);
  164. i++;
  165. }*/
  166. ML_filled_polygone(x, y, 4, BLACK);
  167. }
  168. }
  169. tmp = tmp->nxt;
  170. }while(tmp != NULL);
  171. }
  172. //tests every Wall in the list
  173. bool isColliding(Wall *list, int player_angle)
  174. {
  175. Wall *tmp;
  176. tmp = list;
  177. do{
  178. if(tmp != NULL)
  179. {
  180. if(tmp-> d <= 8)//if the wall is close enough from the center of the screen
  181. { //and is on the same line than the player
  182. if(tmp->line == (int)(player_angle/60)) //&& tmp->line * 60 + 60 > player_angle)
  183. { //BOOM
  184. return true;
  185. }
  186. }
  187. }
  188. tmp = tmp->nxt;
  189. }while(tmp != NULL);
  190. return false;
  191. }