wall.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.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 updateWalls(Wall *list, unsigned int delta_time)
  56. {
  57. //we want to move the obstacle by 1 every two ticks (1/64 seconds ~= 1/60)
  58. //
  59. Wall *tmp;
  60. tmp = list;
  61. do{
  62. if(tmp != NULL)
  63. {
  64. //just reducing the distance from the center
  65. tmp->d -= 0.5 * delta_time;
  66. }
  67. tmp = tmp->nxt;
  68. }while(tmp != NULL);
  69. }
  70. //these are the actual drawing functions
  71. //they should the rewritten from scratch
  72. int getSlopeIndex(int dot1, int dot2)
  73. {
  74. if(dot2 - dot1 == 1)
  75. {
  76. return dot1;
  77. }else if(dot2 - dot1 == -1){
  78. return dot2;
  79. }else return 3;
  80. }
  81. void drawWalls(Wall *list, Camera *cam)
  82. {
  83. Wall *tmp;
  84. tmp = list;
  85. do{
  86. if(tmp != NULL)
  87. {
  88. if(tmp->d + tmp->h< 64)
  89. {
  90. const float angle = PI * ((tmp->line)*60 +cam->angle) / 180;
  91. const float cos1 = cos(angle);
  92. const float cos2 = cos(angle + PI/3);
  93. const float sin1 = sin(angle);
  94. const float sin2 = sin(angle + PI/3);
  95. int i,j, x, y;
  96. float dist = tmp->d - tmp->h;
  97. for(i = 0; i < tmp->h; ++i) {
  98. if(dist <= 8) break;
  99. ML_line(64 + dist*cos1, 32 + dist*sin1, 64 + dist*cos2, 32 + dist*sin2, BLACK);
  100. --dist;
  101. }
  102. }
  103. }
  104. tmp = tmp->nxt;
  105. }while(tmp != NULL);
  106. }
  107. //tests every Wall in the list
  108. bool isColliding(Wall *list, int player_angle)
  109. {
  110. Wall *tmp;
  111. tmp = list;
  112. do{
  113. if(tmp != NULL)
  114. {
  115. if(tmp-> d <= 8)//if the wall is close enough from the center of the screen
  116. { //and is on the same line than the player
  117. if(tmp->line == (int)(player_angle/60)) //&& tmp->line * 60 + 60 > player_angle)
  118. { //BOOM
  119. return true;
  120. }
  121. }
  122. }
  123. tmp = tmp->nxt;
  124. }while(tmp != NULL);
  125. return false;
  126. }