block_part3.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "raylib.h"
  2. #include <math.h>
  3. int map[100][100] = {0};
  4. // Get our angle between two points.
  5. static float getangle(float x1,float y1,float x2,float y2);
  6. // Our rectsoverlap function. Returns true/false.
  7. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  8. int main(void)
  9. {
  10. // Initialization
  11. //--------------------------------------------------------------------------------------
  12. const int screenWidth = 800;
  13. const int screenHeight = 450;
  14. InitWindow(screenWidth, screenHeight, "raylib example.");
  15. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  16. //--------------------------------------------------------------------------------------
  17. for(int i=0;i<100;i++){
  18. map[i][0]=1;
  19. }
  20. map[5][1]=1;
  21. float posx = 320;
  22. float posy = 400;
  23. float animx;
  24. float animy;
  25. float animtargetx;
  26. float animtargety;
  27. bool startanim = false;
  28. // Main game loop
  29. while (!WindowShouldClose()) // Detect window close button or ESC key
  30. {
  31. // Update
  32. //----------------------------------------------------------------------------------
  33. if(startanim==false){
  34. if(IsMouseButtonDown(0)){
  35. float x = GetMouseX()/32;
  36. float y = GetMouseY()/32;
  37. if(map[(int)x][(int)y]==0){
  38. if( map[(int)x][(int)y-1]==1 || map[(int)x-1][(int)y]==1 || map[(int)x+1][(int)y]==1){
  39. //DrawRectangle(x*32,y*32,32,32,DARKGRAY);
  40. startanim=true;
  41. animtargetx = x*32;
  42. animtargety = y*32;
  43. animx = posx;
  44. animy = posy;
  45. }}
  46. }
  47. }
  48. if(startanim){
  49. float an = getangle(animx,animy,animtargetx,animtargety);
  50. animx += cos(an)*8;
  51. animy += sin(an)*8;
  52. if(rectsoverlap(animx-1,animy-1,32+2,32+2,animtargetx-1,animtargety-1,32+2,32+2)){
  53. startanim=false;
  54. map[(int)animtargetx/32][(int)animtargety/32]=1;
  55. }
  56. }
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. for(int y=0;y<10;y++){
  63. for(int x=0;x<100;x++){
  64. if(map[x][y]==1){
  65. DrawRectangle(x*32,y*32,32,32,RED);
  66. DrawRectangle(x*32+1,y*32+1,32-2,32-2,BLUE);
  67. }
  68. }}
  69. if(startanim){
  70. DrawRectangle(animx,animy,32,32,RED);
  71. }
  72. float x = GetMouseX()/32;
  73. float y = GetMouseY()/32;
  74. if(map[(int)x][(int)y]==0){
  75. if( map[(int)x][(int)y-1]==1 || map[(int)x-1][(int)y]==1 || map[(int)x+1][(int)y]==1){
  76. DrawRectangle(x*32,y*32,32,32,DARKGRAY);
  77. }}
  78. float an = getangle(posx,posy,x*32,y*32);
  79. DrawLine(posx,posy,posx+cos(an)*32,posy+sin(an)*32,DARKGRAY);
  80. EndDrawing();
  81. //----------------------------------------------------------------------------------
  82. }
  83. // De-Initialization
  84. //--------------------------------------------------------------------------------------
  85. CloseWindow(); // Close window and OpenGL context
  86. //--------------------------------------------------------------------------------------
  87. return 0;
  88. }
  89. // Return the angle from - to in float
  90. float getangle(float x1,float y1,float x2,float y2){
  91. return (float)atan2(y2-y1, x2-x1);
  92. }
  93. // Rectangles overlap
  94. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  95. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  96. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  97. return true;
  98. }