wall.c 4.0 KB

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