wall.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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)
  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 i,j, x, y;
  102. float dist = tmp->d - tmp->h;
  103. for(i = 0; i < tmp->h; ++i) {
  104. if(dist <= 8) break;
  105. ML_line(64 + dist*cos1, 32 + dist*sin1, 64 + dist*cos2, 32 + dist*sin2, BLACK);
  106. --dist;
  107. }
  108. /*int x[4];
  109. int y[4];
  110. float slopes[4];
  111. int i = 0;
  112. int j = 0;
  113. int tmpInt = 0;
  114. int x1=0, x2 = 0;
  115. //finding the two active edges
  116. int leftDotIndex = 0, rightDotIndex = 0, leftSlope=0, rightSlope=0;
  117. x[0]=tmp->d * cos1 + 64;
  118. x[1]=tmp->d * cos2 + 64;
  119. x[2]= (tmp->h + tmp->d) * cos2 + 64;
  120. x[3]=(tmp->h + tmp->d) * cos1 + 64;
  121. y[0]=tmp->d * sin1 + 32;
  122. y[1]=tmp->d * sin2 + 32;
  123. y[2]= (tmp->h + tmp->d) * sin2 + 32;
  124. y[3]=(tmp->h + tmp->d) * sin1 + 32;
  125. */
  126. /*slopes[0] = (y[1] - y[0])/(x[1]-x[0]);
  127. slopes[1] = (y[2] - y[1])/(x[2]-x[1]);
  128. slopes[2] = (y[3] - y[2])/(x[3]-x[2]);
  129. slopes[3] = (y[0] - y[3])/(x[0]-x[3]);*/
  130. /*for(i = 0; i < tmp->h; i+=0.5)
  131. {
  132. ML_line((tmp->d + i) * cos2 + 64, (tmp->d + i) * sin2 + 32, (tmp->d + i) * cos1 + 64, (tmp->d + i) * sin1 + 32, BLACK);
  133. }*/
  134. /* i = y[0];
  135. j = y[0];
  136. for(i = 0; i < 4; i++)
  137. if(y[tmpInt] > y[i])
  138. tmpInt = i;
  139. i = tmpInt;
  140. tmpInt = 0;
  141. for(j = 0; j < 4; j++)
  142. if(y[tmpInt] < y[j])
  143. tmpInt = j;
  144. j = tmpInt;
  145. x1 = x[i];
  146. x2 = x[i];
  147. //i contains an index to the highest vertex and j the lowest
  148. tmpInt = 0;
  149. for(leftDotIndex = 0; leftDotIndex < 4; leftDotIndex ++)
  150. {
  151. if(leftDotIndex != i && leftDotIndex != j && (tmpInt == 0 || tmpInt > x[leftDotIndex]))
  152. tmpInt = x[leftDotIndex];
  153. }
  154. for(rightDotIndex = 0; rightDotIndex == i || rightDotIndex == j || rightDotIndex == leftDotIndex; rightDotIndex ++)
  155. {}
  156. tmpInt = i;
  157. while(i <= j)
  158. {
  159. //getting the active slopes' indexes
  160. if(y[i] < y[leftDotIndex])
  161. leftSlope = getSlopeIndex(i, leftDotIndex);
  162. else leftSlope = getSlopeIndex(leftDotIndex, j);
  163. if(y[i] < y[rightDotIndex])
  164. rightSlope = getSlopeIndex(i, rightDotIndex);
  165. else rightSlope = getSlopeIndex(rightDotIndex, j);
  166. ML_horizontal_line(y[tmpInt] + i, x1, x2, BLACK);
  167. ML_horizontal_line(y[tmpInt] + i, x2, x1, BLACK);
  168. x1 = x1 - (1/slopes[leftSlope]);
  169. x2 = x2 - (1/slopes[rightSlope]);
  170. i++;
  171. }*/
  172. //ML_filled_polygone(x, y, 4, BLACK);
  173. }
  174. }
  175. tmp = tmp->nxt;
  176. }while(tmp != NULL);
  177. }
  178. //tests every Wall in the list
  179. bool isColliding(Wall *list, int player_angle)
  180. {
  181. Wall *tmp;
  182. tmp = list;
  183. do{
  184. if(tmp != NULL)
  185. {
  186. if(tmp-> d <= 8)//if the wall is close enough from the center of the screen
  187. { //and is on the same line than the player
  188. if(tmp->line == (int)(player_angle/60)) //&& tmp->line * 60 + 60 > player_angle)
  189. { //BOOM
  190. return true;
  191. }
  192. }
  193. }
  194. tmp = tmp->nxt;
  195. }while(tmp != NULL);
  196. return false;
  197. }