wall.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. if(new == NULL) {
  9. // Uh oh
  10. return list;
  11. }
  12. new->d = d;
  13. new->h = h;
  14. new->id = id;
  15. new->line = line;
  16. new->nxt = NULL;
  17. if(list == NULL)
  18. return new;
  19. else if(list != NULL)
  20. {
  21. while (tmp->nxt != NULL){
  22. tmp = tmp->nxt;
  23. }
  24. tmp->nxt = new;
  25. return list;
  26. }
  27. }
  28. Wall *removeWall(Wall *list, int d)
  29. {
  30. Wall *tmp1;
  31. Wall *tmp2;
  32. if(list == NULL) return list;
  33. if(list->d <= d)
  34. {
  35. if(list->nxt != NULL)
  36. tmp1 = list->nxt;
  37. else
  38. tmp1 = NULL;
  39. free(list);
  40. return tmp1;
  41. }
  42. tmp1 = list;
  43. do{
  44. if(tmp1->nxt != NULL)
  45. {
  46. if(tmp1->nxt->d <= d)
  47. {
  48. tmp2 = tmp1->nxt;
  49. if(tmp1->nxt->nxt != NULL)
  50. tmp1->nxt = tmp1->nxt->nxt;
  51. else
  52. tmp1->nxt = NULL;
  53. free(tmp2);
  54. }
  55. }
  56. tmp1 = tmp1->nxt;
  57. }while(tmp1 != NULL);
  58. return list;
  59. }
  60. void updateWalls(Wall *list, unsigned int delta_time)
  61. {
  62. //we want to move the obstacle by 1 every two ticks (1/64 seconds ~= 1/60)
  63. //
  64. Wall *tmp;
  65. tmp = list;
  66. do{
  67. if(tmp != NULL)
  68. {
  69. //just reducing the distance from the center
  70. tmp->d -= 0.5 * delta_time;
  71. }
  72. tmp = tmp->nxt;
  73. }while(tmp != NULL);
  74. }
  75. void drawWalls(Wall *list, Camera *cam, int nb_lines, Line_Transition line_transition)
  76. {//NEEDS A COMPLETE REWRITE TO SUPPORT THE LINE TRANSITIONS !
  77. Wall *tmp;
  78. float coeff = 0.0;
  79. float transition_angle = 0.0;
  80. float delta_angle = 360.0 / nb_lines;
  81. float meh = 0;
  82. float offset = 0;
  83. if(line_transition.delta_nb_lines == 1)
  84. nb_lines ++;
  85. if(line_transition.counter_start != 0)
  86. coeff = (float)line_transition.counter / (float)line_transition.counter_start;
  87. transition_angle = delta_angle * coeff;
  88. offset = (delta_angle - transition_angle) * line_transition.delta_nb_lines/(float)nb_lines;
  89. tmp = list;
  90. do{
  91. if(tmp != NULL)
  92. {
  93. if(tmp->d + tmp->h< 64)
  94. {
  95. const float angle = ((delta_angle - offset) * tmp->line + cam->angle) * PI / 180.;
  96. const float cos1 = cos(angle);
  97. const float cos2 = cos(angle + (delta_angle - offset) * (PI / 180.));
  98. const float sin1 = sin(angle);
  99. const float sin2 = sin(angle + (delta_angle - offset) * (PI / 180.));
  100. int i,j, x, y;
  101. float dist = tmp->d + cam->zoom;
  102. for(i = 0; i < tmp->h && dist > 8; ++i) {
  103. ML_line(64 + dist*cos1, 32 + dist*sin1, 64 + dist*cos2, 32 + dist*sin2, BLACK);
  104. --dist;
  105. }
  106. }
  107. }
  108. tmp = tmp->nxt;
  109. }while(tmp != NULL);
  110. }
  111. //tests every Wall in the list
  112. bool isColliding(Wall *list, int player_angle, int nb_lines)
  113. {
  114. Wall *tmp;
  115. tmp = list;
  116. do{
  117. if(tmp != NULL)
  118. {
  119. if(tmp-> d <= 8+tmp->h + 2)//if the wall is close enough from the center of the screen
  120. { //and is on the same line than the player
  121. if(tmp->line == (int)(player_angle/ (360 / nb_lines)) && tmp->line < nb_lines)
  122. { //BOOM
  123. return true;
  124. }
  125. }
  126. }
  127. tmp = tmp->nxt;
  128. }while(tmp != NULL);
  129. return false;
  130. }