block_part4.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "raylib.h"
  2. #include <math.h>
  3. int focus=0;
  4. int map[100][100] = {0};
  5. int tileHeight = 32;
  6. int tileWidth = 32;
  7. int mapWidth = 100;
  8. int mapHeight = 100;
  9. // Get our angle between two points.
  10. static float getangle(float x1,float y1,float x2,float y2);
  11. // Our rectsoverlap function. Returns true/false.
  12. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  13. //Unit collide with solid blocks true/false
  14. bool recttilecollide(int x, int y, int w, int h, int offsetx,int offsety);
  15. int main(void)
  16. {
  17. // Initialization
  18. //--------------------------------------------------------------------------------------
  19. const int screenWidth = 800;
  20. const int screenHeight = 450;
  21. InitWindow(screenWidth, screenHeight, "raylib example.");
  22. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  23. //--------------------------------------------------------------------------------------
  24. for(int i=0;i<100;i++){
  25. map[i][0]=1;
  26. }
  27. map[5][1]=1;
  28. float posx = 320;
  29. float posy = 400;
  30. float animx;
  31. float animy;
  32. float animtargetx;
  33. float animtargety;
  34. bool startanim = false;
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. if(startanim==false){
  41. if(IsMouseButtonDown(0)){
  42. float x = GetMouseX()/32;
  43. float y = GetMouseY()/32;
  44. if(map[(int)x][(int)y]==0){
  45. if( map[(int)x][(int)y-1]==1 || map[(int)x-1][(int)y]==1 || map[(int)x+1][(int)y]==1){
  46. //DrawRectangle(x*32,y*32,32,32,DARKGRAY);
  47. startanim=true;
  48. animtargetx = x*32;
  49. animtargety = y*32;
  50. animx = posx;
  51. animy = posy;
  52. bool banana=true;
  53. while(banana){
  54. float an = getangle(animx,animy,animtargetx,animtargety);
  55. animx += cos(an)*1;
  56. animy += sin(an)*1;
  57. if(recttilecollide(animx,animy,32,32,0,0)==true){
  58. banana=false;
  59. startanim=false;
  60. focus=1;
  61. }
  62. if(rectsoverlap(animx-1,animy-1,32+2,32+2,animtargetx-1,animtargety-1,1,1)){
  63. banana=false;
  64. animtargetx = x*32;
  65. animtargety = y*32;
  66. animx = posx;
  67. animy = posy;
  68. focus=0;
  69. }
  70. }
  71. }}
  72. }
  73. }
  74. if(startanim){
  75. float an = getangle(animx,animy,animtargetx,animtargety);
  76. animx += cos(an)*8;
  77. animy += sin(an)*8;
  78. if(rectsoverlap(animx-1,animy-1,32+2,32+2,animtargetx-1,animtargety-1,32+2,32+2)){
  79. startanim=false;
  80. map[(int)animtargetx/32][(int)animtargety/32]=1;
  81. }
  82. }
  83. //----------------------------------------------------------------------------------
  84. // Draw
  85. //----------------------------------------------------------------------------------
  86. BeginDrawing();
  87. ClearBackground(RAYWHITE);
  88. for(int y=0;y<10;y++){
  89. for(int x=0;x<100;x++){
  90. if(map[x][y]==1){
  91. DrawRectangle(x*32,y*32,32,32,RED);
  92. DrawRectangle(x*32+1,y*32+1,32-2,32-2,BLUE);
  93. }
  94. }}
  95. if(startanim){
  96. DrawRectangle(animx,animy,32,32,RED);
  97. }
  98. float x = GetMouseX()/32;
  99. float y = GetMouseY()/32;
  100. if(map[(int)x][(int)y]==0){
  101. if( map[(int)x][(int)y-1]==1 || map[(int)x-1][(int)y]==1 || map[(int)x+1][(int)y]==1){
  102. DrawRectangle(x*32,y*32,32,32,DARKGRAY);
  103. }}
  104. float an = getangle(posx,posy,x*32,y*32);
  105. DrawLine(posx,posy,posx+cos(an)*32,posy+sin(an)*32,DARKGRAY);
  106. EndDrawing();
  107. //----------------------------------------------------------------------------------
  108. }
  109. // De-Initialization
  110. //--------------------------------------------------------------------------------------
  111. CloseWindow(); // Close window and OpenGL context
  112. //--------------------------------------------------------------------------------------
  113. return 0;
  114. }
  115. // Return the angle from - to in float
  116. float getangle(float x1,float y1,float x2,float y2){
  117. return (float)atan2(y2-y1, x2-x1);
  118. }
  119. // Rectangles overlap
  120. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  121. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  122. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  123. return true;
  124. }
  125. //Unit collide with solid blocks true/false
  126. bool recttilecollide(int x, int y,int w, int h, int offsetx,int offsety){
  127. int cx = (x+offsetx)/tileWidth;
  128. int cy = (y+offsety)/tileHeight;
  129. for(int y2=cy-1; y2<cy+2;y2++){//Note that the - and + are to be set differently with differently sized players
  130. for(int x2=cx-1; x2<cx+2;x2++){
  131. if(x2>=0 && x2<mapWidth && y2>=0 && y2<mapHeight){
  132. if(map[x2][y2] == 1){
  133. int x3 = (x2)*tileWidth;
  134. int y3 = (y2)*tileHeight;
  135. if(rectsoverlap(x+offsetx,y+offsety,w,h,x3,y3,tileWidth,tileHeight)){
  136. return true;
  137. }
  138. }
  139. }
  140. }}
  141. return false;
  142. }