wall.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, Camera *cam, int nb_lines, Line_Transition line_transition)
  77. {//NEEDS A COMPLETE REWRITE TO SUPPORT THE LINE TRANSITIONS !
  78. Wall *tmp;
  79. fix coeff = 0;
  80. fix transition_angle = 0;
  81. fix delta_angle = fdiv(FIX(360), FIX(nb_lines));
  82. fix offset = 0;
  83. if(line_transition.delta_nb_lines == 1)
  84. nb_lines ++;
  85. if(line_transition.counter_start != 0)
  86. coeff = fdiv(FIX(line_transition.counter), FIX(line_transition.counter_start));
  87. transition_angle = fmul(delta_angle, coeff);
  88. offset = fdiv(fmul((delta_angle - transition_angle), FIX(line_transition.delta_nb_lines)), FIX(nb_lines));
  89. tmp = list;
  90. do{
  91. if(tmp != NULL)
  92. {
  93. if(tmp->d - tmp->h < 128)
  94. {
  95. const fix delta_angle_minus_offset = delta_angle - offset;
  96. const fix angle = fmul(delta_angle_minus_offset, FIX(tmp->line)) + FIX(cam->angle);
  97. const fix cos1 = fcos(angle);
  98. const fix cos2 = fcos(angle + delta_angle_minus_offset);
  99. const fix sin1 = fsin(angle);
  100. const fix sin2 = fsin(angle + delta_angle_minus_offset);
  101. int i;
  102. fix dist = ftofix(tmp->d + cam->zoom);
  103. for(i = 0; i < tmp->h && dist > FIX(8); ++i) {
  104. if(dist < FIX(96))
  105. ML_line(64 + fixtof(fmul(dist, cos1)), 32 + fixtof(fmul(dist,sin1)), 64 + fixtof(fmul(dist, cos2)), 32 + fixtof(fmul(dist, sin2)), BLACK);
  106. dist -= FIX(1);
  107. }
  108. }
  109. }
  110. tmp = tmp->nxt;
  111. }while(tmp != NULL);
  112. }
  113. //tests every Wall in the list
  114. bool isColliding(Wall *list, int player_angle, int nb_lines)
  115. {
  116. Wall *tmp;
  117. tmp = list;
  118. do{
  119. if(tmp != NULL)
  120. {
  121. if(tmp-> d <= 8+tmp->h + 2)//if the wall is close enough from the center of the screen
  122. { //and is on the same line than the player
  123. if(tmp->line == (int)(player_angle/ (360 / nb_lines)) && tmp->line < nb_lines)
  124. { //BOOM
  125. return true;
  126. }
  127. }
  128. }
  129. tmp = tmp->nxt;
  130. }while(tmp != NULL);
  131. return false;
  132. }