wall.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "wall.h"
  2. #include "fixed.h"
  3. Wall *addWall(Wall *list, int d, int h, int id, int line)
  4. {
  5. Wall *tmp;
  6. Wall *new;
  7. tmp = list;
  8. new = malloc(sizeof(Wall));
  9. if(new == NULL) {
  10. // Uh oh
  11. return list;
  12. }
  13. new->d = d;
  14. new->h = h;
  15. new->id = id;
  16. new->line = line;
  17. new->nxt = NULL;
  18. if(list == NULL)
  19. return new;
  20. else if(list != NULL)
  21. {
  22. while (tmp->nxt != NULL){
  23. tmp = tmp->nxt;
  24. }
  25. tmp->nxt = new;
  26. return list;
  27. }
  28. }
  29. Wall *removeWall(Wall *list, int d)
  30. {
  31. Wall *tmp1;
  32. Wall *tmp2;
  33. if(list == NULL) return list;
  34. if(list->d <= d)
  35. {
  36. if(list->nxt != NULL)
  37. tmp1 = list->nxt;
  38. else
  39. tmp1 = NULL;
  40. free(list);
  41. return tmp1;
  42. }
  43. tmp1 = list;
  44. do{
  45. if(tmp1->nxt != NULL)
  46. {
  47. if(tmp1->nxt->d <= d)
  48. {
  49. tmp2 = tmp1->nxt;
  50. if(tmp1->nxt->nxt != NULL)
  51. tmp1->nxt = tmp1->nxt->nxt;
  52. else
  53. tmp1->nxt = NULL;
  54. free(tmp2);
  55. }
  56. }
  57. tmp1 = tmp1->nxt;
  58. }while(tmp1 != NULL);
  59. return list;
  60. }
  61. void updateWalls(Wall *list, unsigned int delta_time)
  62. {
  63. // we want to move the obstacle by 1 every two ticks (1/64 seconds ~= 1/60)
  64. //
  65. Wall *tmp;
  66. tmp = list;
  67. do{
  68. if(tmp != NULL)
  69. {
  70. // just reducing the distance from the center
  71. tmp->d -= 0.5 * delta_time;
  72. }
  73. tmp = tmp->nxt;
  74. }while(tmp != NULL);
  75. }
  76. void drawWalls(Wall *list, Game_Data *data, int nb_lines, Line_Transition line_transition)
  77. {// NEEDS A COMPLETE REWRITE TO SUPPORT THE LINE TRANSITIONS !
  78. Wall *tmp;
  79. ML_Color drawing_color = data->are_colors_reversed ? WHITE : BLACK;
  80. Camera *cam = &data->cam;
  81. fix coeff = 0;
  82. fix transition_angle = 0;
  83. fix delta_angle = fdiv(FIX(360), FIX(nb_lines));
  84. fix offset = 0;
  85. if(line_transition.delta_nb_lines == 1)
  86. nb_lines ++;
  87. if(line_transition.counter_start != 0)
  88. coeff = fdiv(FIX(line_transition.counter), FIX(line_transition.counter_start));
  89. transition_angle = fmul(delta_angle, coeff);
  90. offset = fdiv(fmul((delta_angle - transition_angle), FIX(line_transition.delta_nb_lines)), FIX(nb_lines));
  91. tmp = list;
  92. do{
  93. if(tmp != NULL)
  94. {
  95. if(tmp->d - tmp->h < 96)
  96. {
  97. const fix delta_angle_minus_offset = delta_angle - offset;
  98. const fix angle = fmul(delta_angle_minus_offset, FIX(tmp->line)) + FIX(cam->angle);
  99. const fix cos1 = fcos(angle);
  100. const fix cos2 = fcos(angle + delta_angle_minus_offset);
  101. const fix sin1 = fsin(angle);
  102. const fix sin2 = fsin(angle + delta_angle_minus_offset);
  103. int i;
  104. fix dist = ftofix(tmp->d + cam->zoom);
  105. for(i = 0; i < tmp->h && dist > FIX(8); ++i) {
  106. if(dist < FIX(96))
  107. ML_line(64 + fixtof(fmul(dist, cos1)), 32 + fixtof(fmul(dist,sin1)), 64 + fixtof(fmul(dist, cos2)), 32 + fixtof(fmul(dist, sin2)), drawing_color);
  108. dist -= FIX(1);
  109. }
  110. }
  111. }
  112. tmp = tmp->nxt;
  113. }while(tmp != NULL);
  114. }
  115. // Tests if the players hits a wall by its sides and not only by being crushed under it.
  116. bool isCollidingSide(Wall *list, int player_angle, int nb_lines) {
  117. Wall *tmp;
  118. tmp = list;
  119. do{
  120. if(tmp != NULL)
  121. {
  122. if(tmp-> d <= 8+tmp->h + 2)
  123. { // and is on the same line than the player
  124. if(tmp->line == (int)(player_angle/ (360 / nb_lines)) && tmp->line < nb_lines)
  125. {
  126. return true;
  127. }
  128. }
  129. }
  130. tmp = tmp->nxt;
  131. }while(tmp != NULL);
  132. return false;
  133. }
  134. // tests every Wall in the list
  135. bool isColliding(Wall *list, int player_angle, int nb_lines)
  136. {
  137. Wall *tmp;
  138. tmp = list;
  139. do{
  140. if(tmp != NULL)
  141. {
  142. if(tmp-> d <= 8+tmp->h + 2)// if the wall is close enough from the center of the screen
  143. //if(tmp-> d <= 8 + 2)// if the wall is close enough from the center of the screen
  144. { // and is on the same line than the player
  145. if(tmp->line == (int)(player_angle/ (360 / nb_lines)) && tmp->line < nb_lines)
  146. { // BOOM
  147. return true;
  148. }
  149. }
  150. }
  151. tmp = tmp->nxt;
  152. }while(tmp != NULL);
  153. return false;
  154. }