explosions_01.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Here some random eplosion/fireworks effects.
  3. // I actually discovered here that the GetRandomValue does not return floats!
  4. //
  5. //
  6. //
  7. #include "raylib.h"
  8. #include <math.h>
  9. #define MAX_EFFECT 1000
  10. int myMap[10][11] = { {1,1,1,1,1,1,1,1,1,1,1},
  11. {1,1,1,1,1,1,1,1,1,1,1},
  12. {1,1,1,1,1,1,1,1,1,1,1},
  13. {1,1,1,1,1,1,1,1,1,1,1},
  14. {1,1,1,1,1,1,1,1,1,1,1},
  15. {1,1,1,1,1,1,1,1,1,1,1},
  16. {1,1,1,1,1,1,1,1,1,1,1},
  17. {1,1,1,1,1,1,1,1,1,1,1},
  18. {1,1,1,1,1,1,1,1,1,1,1},
  19. {1,1,1,1,1,1,1,1,1,1,1}
  20. };
  21. int mapWidth = 11;
  22. int mapHeight = 10;
  23. float tileWidth;
  24. float tileHeight;
  25. static struct effect{
  26. bool active;
  27. Vector2 position;
  28. Vector2 inc;
  29. Vector2 incmod;
  30. int w;
  31. int h;
  32. int countdown;
  33. }effect;
  34. static struct effect arr_effect[MAX_EFFECT];
  35. int screenWidth;
  36. int screenHeight;
  37. void createeffect();
  38. //Unit collide with solid blocks true/false
  39. bool recttilecollide(int x, int y, int w, int h, int offsetx,int offsety);
  40. // Our rectsoverlap function. Returns true/false.
  41. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  42. int main(void)
  43. {
  44. // Initialization
  45. //--------------------------------------------------------------------------------------
  46. screenWidth = 800;
  47. screenHeight = 450;
  48. InitWindow(screenWidth, screenHeight, "raylib example.");
  49. tileWidth = ceil((float)(float)screenWidth/(float)mapWidth);
  50. tileHeight = ceil((float)screenHeight/(float)mapHeight);
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. createeffect();
  54. int effectdelay=0;
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. effectdelay--;
  61. if(effectdelay<0){
  62. effectdelay=GetRandomValue(0,100);
  63. createeffect();
  64. }
  65. // update the effect
  66. for(int i=0;i<MAX_EFFECT;i++){
  67. if(arr_effect[i].active==true){
  68. // When exit the effect and disable
  69. arr_effect[i].countdown--;
  70. if(arr_effect[i].countdown<0)arr_effect[i].active=false;
  71. arr_effect[i].inc.y += arr_effect[i].incmod.y;
  72. if(arr_effect[i].inc.x<0){
  73. arr_effect[i].inc.x-=arr_effect[i].incmod.x;
  74. }else{
  75. arr_effect[i].inc.x+=arr_effect[i].incmod.x;;
  76. }
  77. arr_effect[i].position.x += arr_effect[i].inc.x;
  78. arr_effect[i].position.y += arr_effect[i].inc.y;
  79. }
  80. }
  81. //----------------------------------------------------------------------------------
  82. // Draw
  83. //----------------------------------------------------------------------------------
  84. BeginDrawing();
  85. ClearBackground(RAYWHITE);
  86. // draw the effect
  87. for(int i=0;i<MAX_EFFECT;i++){
  88. if(arr_effect[i].active==false)continue;
  89. DrawRectangle(arr_effect[i].position.x,arr_effect[i].position.y,arr_effect[i].w,arr_effect[i].h,RED);
  90. }
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. CloseWindow(); // Close window and OpenGL context
  97. //--------------------------------------------------------------------------------------
  98. return 0;
  99. }
  100. void createeffect(){
  101. int posx = GetRandomValue(0,screenWidth);
  102. int posy = GetRandomValue(0,screenHeight);
  103. int i=0;
  104. int cnt=0;
  105. while(cnt<50){
  106. i++;
  107. if(i>MAX_EFFECT){
  108. continue;
  109. cnt=51;
  110. }
  111. if(arr_effect[i].active==true){
  112. continue;
  113. }
  114. arr_effect[i].active = true;
  115. arr_effect[i].position.x = posx;
  116. arr_effect[i].position.y = posy;
  117. arr_effect[i].w = 16;
  118. arr_effect[i].h = 16;
  119. arr_effect[i].inc.x = GetRandomValue(-1,1);
  120. arr_effect[i].inc.y = GetRandomValue(-10,-5);
  121. arr_effect[i].incmod.x = (float)(GetRandomValue(0,100)/2500.0f);
  122. arr_effect[i].incmod.y = (float)(GetRandomValue(0,100)/1000.0f)+0.2f;
  123. if(GetRandomValue(0,8)==1){
  124. arr_effect[i].incmod.x*=5;
  125. arr_effect[i].inc.y*=1.5;
  126. }
  127. arr_effect[i].countdown = GetRandomValue(50,120);
  128. cnt++;
  129. }
  130. }
  131. //Unit collide with solid blocks true/false
  132. bool recttilecollide(int x, int y,int w, int h, int offsetx,int offsety){
  133. int cx = (x+offsetx)/tileWidth;
  134. int cy = (y+offsety)/tileHeight;
  135. for(int y2=cy-1; y2<cy+2;y2++){//Note that the - and + are to be set differently with differently sized players
  136. for(int x2=cx-1; x2<cx+2;x2++){
  137. if(x2>=0 && x2<mapWidth && y2>=0 && y2<mapHeight){
  138. if(myMap[y2][x2] == 1){
  139. int x3 = (x2)*tileWidth;
  140. int y3 = (y2)*tileHeight;
  141. if(rectsoverlap(x+offsetx,y+offsety,w,h,x3,y3,tileWidth,tileHeight)){
  142. return true;
  143. }
  144. }
  145. }
  146. }}
  147. return false;
  148. }
  149. // Rectangles overlap
  150. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  151. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  152. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  153. return true;
  154. }