wall.c 2.3 KB

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