wall.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. void drawWalls(Wall *list, Camera *cam, int nb_lines)
  71. {//NEEDS A COMPLETE REWRITE TO SUPPORT THE LINE TRANSITIONS !
  72. Wall *tmp;
  73. tmp = list;
  74. do{
  75. if(tmp != NULL)
  76. {
  77. if(tmp->d + tmp->h< 64)
  78. {
  79. const float angle = ((360 / nb_lines) * tmp->line + cam->angle) * PI / 180;
  80. const float cos1 = cos(angle);
  81. const float cos2 = cos(angle + (360.0 / nb_lines) * (PI / 180));
  82. const float sin1 = sin(angle);
  83. const float sin2 = sin(angle + (360.0 / nb_lines) * (PI / 180));
  84. int i,j, x, y;
  85. float dist = tmp->d + cam->zoom;
  86. for(i = 0; i < tmp->h; ++i) {
  87. if(dist <= 8) break;
  88. ML_line(64 + dist*cos1, 32 + dist*sin1, 64 + dist*cos2, 32 + dist*sin2, BLACK);
  89. --dist;
  90. }
  91. }
  92. }
  93. tmp = tmp->nxt;
  94. }while(tmp != NULL);
  95. }
  96. //tests every Wall in the list
  97. bool isColliding(Wall *list, int player_angle, int nb_lines)
  98. {
  99. Wall *tmp;
  100. tmp = list;
  101. do{
  102. if(tmp != NULL)
  103. {
  104. if(tmp-> d <= 8)//if the wall is close enough from the center of the screen
  105. { //and is on the same line than the player
  106. if(tmp->line == (int)(player_angle/ 360 / nb_lines))
  107. { //BOOM
  108. return true;
  109. }
  110. }
  111. }
  112. tmp = tmp->nxt;
  113. }while(tmp != NULL);
  114. return false;
  115. }