spacesimulate.c 9.6 KB

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