spacesimulateMult.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*******************************************************************************************
  2. *
  3. *
  4. *
  5. ********************************************************************************************/
  6. #include "raylib.h"
  7. #include <math.h>
  8. #define MAXASTEROIDS 3
  9. #define MAXSHIPS 3
  10. // Variables for storing and restoring the current game to and from memory
  11. bool savedactive[MAXASTEROIDS];
  12. Vector2 savedposition[MAXASTEROIDS];
  13. float savedangle[MAXASTEROIDS];
  14. float savedthrust[MAXASTEROIDS];
  15. float savedthrustdec[MAXASTEROIDS];
  16. int savedradius[MAXASTEROIDS];
  17. float savedrotation[MAXASTEROIDS];
  18. float savedrotspeed[MAXASTEROIDS];
  19. bool shipssavedactive[MAXSHIPS];
  20. Vector2 shipssavedposition[MAXSHIPS];
  21. float shipssavedangle[MAXSHIPS];
  22. float shipssavedthrust[MAXSHIPS];
  23. float shipssavedthrustdec[MAXSHIPS];
  24. int shipssavedradius[MAXSHIPS];
  25. // Regular variables.
  26. typedef struct asteroid{
  27. bool active;
  28. Vector2 position;
  29. float angle;
  30. float thrust;
  31. float thrustdec;
  32. int radius;
  33. float rotation;
  34. float rotspeed;
  35. }asteroid;
  36. static struct asteroid ast[MAXASTEROIDS];
  37. typedef struct ship{
  38. bool active;
  39. Vector2 position;
  40. float angle;
  41. float thrust;
  42. float thrustdec;
  43. int radius;
  44. }ship;
  45. static struct ship shp[MAXSHIPS];
  46. Texture2D sprites;
  47. Texture2D rocks;
  48. const int screenWidth = 800;
  49. const int screenHeight = 450;
  50. void drawship(int x, int y, int ship,int rotation);
  51. void drawrock(int x, int y, int rock, int rotation);
  52. void updateasteroids();
  53. void drawasteroids();
  54. void iniasteroids();
  55. void simulate();
  56. void savestate();
  57. void restorestate();
  58. void updateships();
  59. void drawships();
  60. void updateships();
  61. void iniships();
  62. // Our rectsoverlap function. Returns true/false.
  63. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  64. int main(void)
  65. {
  66. // Initialization
  67. //--------------------------------------------------------------------------------------
  68. InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
  69. sprites = LoadTexture("resources/spaceships.png");
  70. rocks = LoadTexture("resources/spacerocks.png");
  71. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  72. //--------------------------------------------------------------------------------------
  73. iniasteroids();
  74. iniships();
  75. // Main game loop
  76. while (!WindowShouldClose()) // Detect window close button or ESC key
  77. {
  78. // Update
  79. //----------------------------------------------------------------------------------
  80. // TODO: Update your variables here
  81. //----------------------------------------------------------------------------------
  82. if(IsKeyPressed(KEY_SPACE)){
  83. simulate();
  84. }
  85. updateasteroids();
  86. updateships();
  87. for(int i=0;i<MAXASTEROIDS*MAXSHIPS;i++){
  88. simulate();
  89. }
  90. // Draw
  91. //----------------------------------------------------------------------------------
  92. BeginDrawing();
  93. ClearBackground(BLACK);
  94. //drawship(100,100,146,0);
  95. drawships();
  96. drawasteroids();
  97. //drawrock(200,100,83,0);
  98. //DrawText(FormatText("%f",(float)GetRandomValue(0,10000)/10000*2-1), 190, 200, 20, LIGHTGRAY);
  99. EndDrawing();
  100. //----------------------------------------------------------------------------------
  101. }
  102. // De-Initialization
  103. UnloadTexture(sprites);
  104. UnloadTexture(rocks);
  105. //--------------------------------------------------------------------------------------
  106. CloseWindow(); // Close window and OpenGL context
  107. //--------------------------------------------------------------------------------------
  108. return 0;
  109. }
  110. void simulate(){
  111. for(int ship=0;ship<MAXSHIPS;ship++){
  112. if(shp[ship].thrust>0)return;
  113. }
  114. savestate();
  115. bool collided=false;
  116. float rthrust[MAXSHIPS];
  117. for(int j=0;j<MAXSHIPS;j++){
  118. rthrust[j]=(float)GetRandomValue(1000,3000)/1000.0f;
  119. }
  120. float angle[MAXSHIPS];
  121. for(int j=0;j<MAXSHIPS;j++){
  122. angle[j]=GetRandomValue(0,360)/DEG2RAD;
  123. }
  124. for(int j=0;j<MAXSHIPS;j++){
  125. shp[j].thrust=rthrust[j];
  126. shp[j].angle=angle[j];
  127. }
  128. for(int depth=0;depth<1500;depth++){
  129. updateasteroids();
  130. updateships();
  131. for(int ship=0;ship<MAXSHIPS;ship++){
  132. //
  133. for(int i=0;i<MAXASTEROIDS;i++){
  134. if(rectsoverlap(shp[ship].position.x,shp[ship].position.y,32,32,ast[i].position.x,ast[i].position.y,32,32)==true){
  135. collided=true;
  136. restorestate();
  137. return;
  138. }
  139. }
  140. //
  141. }
  142. }
  143. restorestate();
  144. if(collided==false){
  145. for(int j=0;j<MAXSHIPS;j++){
  146. shp[j].thrust = rthrust[j];
  147. shp[j].angle = angle[j];
  148. }
  149. }
  150. }
  151. void iniasteroids(){
  152. for(int i=0;i<MAXASTEROIDS;i++){
  153. ast[i].active = true;
  154. ast[i].position.x = GetRandomValue(64,screenWidth-64);
  155. ast[i].position.y = GetRandomValue(64,screenHeight-64);
  156. ast[i].radius = 16;
  157. ast[i].thrust = 2;
  158. ast[i].angle = (float)GetRandomValue(0,PI*1000)/PI*1000.0f;
  159. ast[i].thrustdec = 0;
  160. ast[i].rotation = 0;
  161. ast[i].rotspeed = (float)GetRandomValue(0,1000)/10000.0f;
  162. if(GetRandomValue(0,10)<5)ast[i].rotspeed=-ast[i].rotspeed;
  163. }
  164. }
  165. void drawasteroids(){
  166. for(int i=0;i<MAXASTEROIDS;i++){
  167. if(ast[i].active==false)return;
  168. drawrock(ast[i].position.x,ast[i].position.y,83,ast[i].rotation*RAD2DEG);
  169. }
  170. }
  171. void updateasteroids(){
  172. for(int i=0;i<MAXASTEROIDS;i++){
  173. if(ast[i].active==false)return;
  174. ast[i].position.x += cos(ast[i].angle)*ast[i].thrust;
  175. ast[i].position.y += sin(ast[i].angle)*ast[i].thrust;
  176. if(ast[i].thrust>0){
  177. ast[i].thrust -= ast[i].thrustdec;
  178. }else{
  179. ast[i].thrust = 0;
  180. }
  181. ast[i].rotation+=ast[i].rotspeed;//
  182. if(ast[i].rotation>PI*2.0f)ast[i].rotation=0;
  183. if(ast[i].rotation<0.0f)ast[i].rotation=PI*2;
  184. //
  185. if(ast[i].position.y>screenHeight+32)ast[i].position.y=-32;
  186. if(ast[i].position.y<-32)ast[i].position.y=screenHeight+32;
  187. if(ast[i].position.x<-32)ast[i].position.x=screenWidth+32;
  188. if(ast[i].position.x>screenWidth+32)ast[i].position.x=-32;
  189. }
  190. }
  191. void drawships(){
  192. for(int i=0;i<MAXSHIPS;i++){
  193. if(shp[i].active==false)return;
  194. drawship(shp[i].position.x,shp[i].position.y,146,shp[i].angle*RAD2DEG+90);
  195. }
  196. }
  197. void updateships(){
  198. for(int i=0;i<MAXSHIPS;i++){
  199. if(shp[i].active==false)return;
  200. shp[i].position.x += cos(shp[i].angle)*shp[i].thrust;
  201. shp[i].position.y += sin(shp[i].angle)*shp[i].thrust;
  202. if(shp[i].thrust>0){
  203. shp[i].thrust -= shp[i].thrustdec;
  204. }else{
  205. shp[i].thrust = 0;
  206. }
  207. //
  208. if(shp[i].position.y>screenHeight+32)shp[i].position.y=-32;
  209. if(shp[i].position.y<-32)shp[i].position.y=screenHeight+32;
  210. if(shp[i].position.x<-32)shp[i].position.x=screenWidth+32;
  211. if(shp[i].position.x>screenWidth+32)shp[i].position.x=-32;
  212. }
  213. }
  214. void drawship(int x, int y, int ship, int rotation){
  215. // Get the tile x , y position.
  216. int ty = ship / 20;
  217. int tx = ship-(ty*20);;
  218. DrawTexturePro(sprites, (Rectangle){tx*16,ty*16,16,16},// the -96 (-)means mirror on x axis
  219. (Rectangle){x,y,32,32},
  220. (Vector2){16,16},rotation,WHITE);
  221. }
  222. void drawrock(int x, int y, int rock, int rotation){
  223. // Get the tile x , y position.
  224. int ty = rock / 20;
  225. int tx = rock-(ty*20);;
  226. DrawTexturePro(rocks, (Rectangle){tx*16,ty*16,16,16},// the -96 (-)means mirror on x axis
  227. (Rectangle){x,y,32,32},
  228. (Vector2){16,16},rotation,WHITE);
  229. }
  230. // save and restore state are to be used to save the screen and activity before the simulation starts.
  231. // when the simulation ends the restorestate can be used to restore to the last saved state.
  232. void savestate(){
  233. for(int i=0;i<MAXASTEROIDS;i++){
  234. savedactive[i] = ast[i].active;
  235. savedposition[i]= ast[i].position;
  236. savedangle[i]= ast[i].angle;
  237. savedthrust[i]= ast[i].thrust;
  238. savedthrustdec[i]= ast[i].thrustdec;
  239. savedradius[i]= ast[i].radius;
  240. savedrotation[i]= ast[i].rotation;
  241. savedrotspeed[i]= ast[i].rotspeed;
  242. }
  243. for(int i=0;i<MAXSHIPS;i++){
  244. shipssavedactive[i]=shp[i].active;
  245. shipssavedposition[i]=shp[i].position;
  246. shipssavedangle[i]=shp[i].angle;
  247. shipssavedthrust[i]=shp[i].thrust;
  248. shipssavedthrustdec[i]=shp[i].thrustdec;
  249. shipssavedradius[i]=shp[i].radius;
  250. }
  251. }
  252. void restorestate(){
  253. for(int i=0;i<MAXASTEROIDS;i++){
  254. ast[i].active=savedactive[i];
  255. ast[i].position=savedposition[i];
  256. ast[i].angle=savedangle[i];
  257. ast[i].thrust=savedthrust[i];
  258. ast[i].thrustdec=savedthrustdec[i];
  259. ast[i].radius=savedradius[i];
  260. ast[i].rotation=savedrotation[i];
  261. ast[i].rotspeed=savedrotspeed[i];
  262. }
  263. for(int i=0;i<MAXSHIPS;i++){
  264. shp[i].active=shipssavedactive[i];
  265. shp[i].position=shipssavedposition[i];
  266. shp[i].angle=shipssavedangle[i];
  267. shp[i].thrust=shipssavedthrust[i];
  268. shp[i].thrustdec=shipssavedthrustdec[i];
  269. shp[i].radius=shipssavedradius[i];
  270. }
  271. }
  272. // Rectangles overlap
  273. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  274. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  275. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  276. return true;
  277. }
  278. void iniships(){
  279. for(int i=0;i<MAXSHIPS;i++){
  280. shp[i].position.x = 100;
  281. shp[i].position.y = 100;
  282. shp[i].active = true;
  283. shp[i].radius = 16;
  284. shp[i].thrust = 0;
  285. shp[i].thrustdec = 0.01;
  286. }
  287. }