2UnitsMoveAside.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Here I tried to learn more about how to not let moving units go through each other when moving
  3. // on the map.
  4. //
  5. // Note : I think when just checking for collide between units they should just both stop and plan
  6. // a new path (for one?) towards the original destination.
  7. #include "raylib.h"
  8. #include <math.h>
  9. typedef struct unit{
  10. float x,y;
  11. int asidex,asidey;
  12. int wait;
  13. bool moveaside;
  14. int pathloc;
  15. int pathlen;
  16. }unit;
  17. int path1x[10]={10,11,12,13,14,15,16,17,18,19};
  18. int path1y[10]={5,5,5,5,5,5,5,5,5,5};
  19. int path2x[10]={19,18,17,16,15,14,13,12,11,10};
  20. int path2y[10]={5,5,5,5,5,5,5,5,5,5};
  21. unit unit1={0};
  22. unit unit2={0};
  23. float tileWidth,tileHeight;
  24. void iniunits();
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. InitWindow(screenWidth, screenHeight, "raylib example.");
  32. tileWidth = 24.0f;
  33. tileHeight = 24.0f;
  34. iniunits();
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. // Move unit 1
  43. if(unit1.wait>0)unit1.wait-=1;
  44. if(unit1.pathloc<unit1.pathlen && unit1.wait<=0){
  45. // get the next location in the path
  46. float x=path1x[unit1.pathloc]*tileWidth;
  47. float y=path1y[unit1.pathloc]*tileHeight;
  48. // If we are not moving aside currently
  49. if(unit1.moveaside==false){
  50. // see if our next position is colliding with unit2
  51. int ax = path2x[unit2.pathloc+1];
  52. int ay = path2y[unit2.pathloc+1];
  53. int bx = path1x[unit1.pathloc];
  54. int by = path1y[unit1.pathloc];
  55. if(ax==bx && ay==by){
  56. // We will collide
  57. unit1.moveaside=true;
  58. // Get a position on the map to move aside to
  59. bool found=false;
  60. while(found==false){
  61. unit1.asidex=bx+GetRandomValue(-1,1);
  62. unit1.asidey=by+GetRandomValue(-1,1);
  63. found=true;
  64. // if new position not on path of unit 1
  65. for(int i=unit1.pathloc-1;i<unit1.pathloc+1;i++){
  66. if(unit1.asidex==path1x[i] && unit1.asidey==path1y[i])found=false;
  67. }
  68. // if new position not on path of unit 2
  69. for(int i=unit2.pathloc-1;i<unit2.pathloc+1;i++){
  70. if(unit1.asidex==path2x[i] && unit1.asidey==path2y[i])found=false;
  71. }
  72. // Add checks for obstacles here...
  73. }
  74. }
  75. }
  76. // if we are moving aside currently then adjust were we are moving to.
  77. if(unit1.moveaside){
  78. x=unit1.asidex*tileWidth;
  79. y=unit1.asidey*tileHeight;
  80. }
  81. // Move our unit1
  82. if(unit1.x<x)unit1.x+=1;
  83. if(unit1.y<y)unit1.y+=1;
  84. if(unit1.x>x)unit1.x-=1;
  85. if(unit1.y>y)unit1.y-=1;
  86. // If we are at the destination..
  87. if( unit1.x==x &&
  88. unit1.y==y){
  89. // If we are moving aside then disable this mode and increase our location on the path.
  90. if(unit1.moveaside){
  91. unit1.moveaside=false;
  92. unit1.pathloc+=3;
  93. unit1.wait=60;
  94. // If we are on the destination and not moving aside then increase our path location.
  95. }else{
  96. unit1.pathloc++;
  97. }
  98. }
  99. }
  100. // Move unit 2
  101. if(unit2.pathloc<unit2.pathlen){
  102. float x=path2x[unit2.pathloc]*tileWidth;
  103. float y=path2y[unit2.pathloc]*tileHeight;
  104. if(unit2.x<x)unit2.x+=1;
  105. if(unit2.y<y)unit2.y+=1;
  106. if(unit2.x>x)unit2.x-=1;
  107. if(unit2.y>y)unit2.y-=1;
  108. if( unit2.x==x &&
  109. unit2.y==y){
  110. unit2.pathloc++;
  111. }
  112. }else{
  113. iniunits();
  114. }
  115. //----------------------------------------------------------------------------------
  116. // Draw
  117. //----------------------------------------------------------------------------------
  118. BeginDrawing();
  119. ClearBackground(RAYWHITE);
  120. DrawRectangle(unit1.x,unit1.y,tileWidth,tileHeight,RED);
  121. DrawRectangle(unit2.x,unit2.y,tileWidth,tileHeight,BLUE);
  122. EndDrawing();
  123. //----------------------------------------------------------------------------------
  124. }
  125. // De-Initialization
  126. //--------------------------------------------------------------------------------------
  127. CloseWindow(); // Close window and OpenGL context
  128. //--------------------------------------------------------------------------------------
  129. return 0;
  130. }
  131. void iniunits(){
  132. unit1.pathloc = 0;
  133. unit1.pathlen = 10;
  134. unit1.x = path1x[unit1.pathloc]*tileWidth;
  135. unit1.y = path1y[unit1.pathloc]*tileHeight;
  136. unit1.moveaside=false;
  137. unit2.pathloc = 0;
  138. unit2.pathlen = 10;
  139. unit2.x = path2x[unit2.pathloc]*tileWidth;
  140. unit2.y = path2y[unit2.pathloc]*tileHeight;
  141. unit2.moveaside=false;
  142. return;
  143. }