2
0

Example_-_MapSmoothplayer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "raylib.h"
  2. // This is our tile map.
  3. int map[10][20] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  4. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
  5. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
  6. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
  7. {1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1},
  8. {1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
  9. {1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
  10. {1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1},
  11. {1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,1},
  12. {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
  13. static float tileWidth;
  14. static float tileHeight;
  15. static int mapWidth;
  16. static int mapHeight;
  17. typedef struct player{
  18. Vector2 position;
  19. int width;
  20. int height;
  21. }player;
  22. static player myplayer = {0};
  23. // uses the myplayer.position coordinates.
  24. // If offsetx is 1 then it checks if the player x position PLus 1 is colliding with a tile.
  25. static bool playertilecollide(int offsetx,int offsety);
  26. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. mapWidth = 20;
  34. mapHeight = 10;
  35. // Our tile width and height. We use the screen dimension and the map dimension to
  36. // get tile dimensions that fill up the entire screen.
  37. tileWidth = (float)screenWidth/(float)mapWidth;
  38. tileHeight = (float)screenHeight/(float)mapHeight;
  39. InitWindow(screenWidth, screenHeight, "raylib example.");
  40. // Our player setup
  41. myplayer.position = (Vector2){4*tileWidth,6*tileHeight};
  42. myplayer.width = tileWidth/2;
  43. myplayer.height = tileHeight/2;
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. // Below here we read the key. If the l/r/u/d cursor is pressed then we check the new position is inside
  52. // the array bounds and then we check if the collision map on the new position is not set to flag true on that tile.
  53. if(IsKeyDown(KEY_RIGHT)&& myplayer.position.x+1<(mapWidth-1)*tileWidth){
  54. if(playertilecollide(1,0)==false){
  55. myplayer.position.x+=1;
  56. }
  57. }
  58. if(IsKeyDown(KEY_LEFT) && myplayer.position.x-1>-1){
  59. if(playertilecollide(-1,0)==false){
  60. myplayer.position.x-=1;
  61. }
  62. }
  63. if(IsKeyDown(KEY_UP)&& myplayer.position.y-1>-1){
  64. if(playertilecollide(0,-1)==false){
  65. myplayer.position.y-=1;
  66. }
  67. }
  68. if(IsKeyDown(KEY_DOWN) && myplayer.position.y+1<(mapHeight-1)*tileHeight){
  69. if(playertilecollide(0,1)==false){
  70. myplayer.position.y+=1;
  71. }
  72. }
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. // Draw our tilemap.
  79. for(int y=0;y<10;y++){
  80. for(int x=0;x<20;x++){
  81. if(map[y][x]==1){
  82. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLACK);
  83. }
  84. }
  85. }
  86. // Draw our player
  87. DrawRectangle(myplayer.position.x,myplayer.position.y,myplayer.width,myplayer.height,RED);
  88. DrawText("Use Cursor Keys to move around..",0,0,20,WHITE);
  89. EndDrawing();
  90. //----------------------------------------------------------------------------------
  91. }
  92. // De-Initialization
  93. //--------------------------------------------------------------------------------------
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }
  98. //Unit collide with solid blocks true/false
  99. bool playertilecollide(int offsetx,int offsety){
  100. int cx = (myplayer.position.x+offsetx)/tileWidth;
  101. int cy = (myplayer.position.y+offsety)/tileHeight;
  102. for(int y2=cy-1; y2<cy+2;y2++){//Note that the - and + are to be set differently with differently sized players
  103. for(int x2=cx-1; x2<cx+2;x2++){
  104. if(x2>=0 && x2<mapWidth && y2>=0 && y2<mapHeight){
  105. if(map[y2][x2] == 1){
  106. int x3 = (x2)*tileWidth;
  107. int y3 = (y2)*tileHeight;
  108. if(rectsoverlap(myplayer.position.x+offsetx,myplayer.position.y+offsety,myplayer.width,myplayer.height,x3,y3,tileWidth,tileHeight)){
  109. return true;
  110. }
  111. }
  112. }
  113. }}
  114. return false;
  115. }
  116. // Rectangles overlap
  117. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  118. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  119. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  120. return true;
  121. }