core_2d_camera.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - 2d camera
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. MAX_BUILDINGS = 100
  12. -- Initialization
  13. -------------------------------------------------------------------------------------------
  14. local screenWidth = 800
  15. local screenHeight = 450
  16. InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera")
  17. local player = Rectangle(400, 280, 40, 40)
  18. local buildings = {}
  19. local buildColors = {}
  20. local spacing = 0;
  21. for i = 1, MAX_BUILDINGS do
  22. buildings[i] = Rectangle(0, 0, 0, 0)
  23. buildings[i].width = GetRandomValue(50, 200)
  24. buildings[i].height = GetRandomValue(100, 800)
  25. buildings[i].y = screenHeight - 130 - buildings[i].height
  26. buildings[i].x = -6000 + spacing
  27. spacing = spacing + buildings[i].width
  28. buildColors[i] = Color(GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255)
  29. end
  30. local camera = Camera2D(Vector2(0, 0), Vector2(0, 0), 0.0, 1.0)
  31. camera.target = Vector2(player.x + 20, player.y + 20)
  32. camera.offset = Vector2(0, 0)
  33. camera.rotation = 0.0
  34. camera.zoom = 1.0
  35. SetTargetFPS(60)
  36. -------------------------------------------------------------------------------------------
  37. -- Main game loop
  38. while not WindowShouldClose() do -- Detect window close button or ESC key
  39. -- Update
  40. ---------------------------------------------------------------------------------------
  41. if (IsKeyDown(KEY.RIGHT)) then
  42. player.x = player.x + 2 -- Player movement
  43. camera.offset.x = camera.offset.x - 2 -- Camera displacement with player movement
  44. elseif (IsKeyDown(KEY.LEFT)) then
  45. player.x = player.x - 2 -- Player movement
  46. camera.offset.x = camera.offset.x + 2 -- Camera displacement with player movement
  47. end
  48. -- Camera target follows player
  49. camera.target = Vector2(player.x + 20, player.y + 20)
  50. -- Camera rotation controls
  51. if (IsKeyDown(KEY.A)) then camera.rotation = camera.rotation - 1
  52. elseif (IsKeyDown(KEY.S)) then camera.rotation = camera.rotation + 1
  53. end
  54. -- Limit camera rotation to 80 degrees (-40 to 40)
  55. if (camera.rotation > 40) then camera.rotation = 40
  56. elseif (camera.rotation < -40) then camera.rotation = -40
  57. end
  58. -- Camera zoom controls
  59. camera.zoom = camera.zoom + (GetMouseWheelMove()*0.05)
  60. if (camera.zoom > 3.0) then camera.zoom = 3.0
  61. elseif (camera.zoom < 0.1) then camera.zoom = 0.1
  62. end
  63. -- Camera reset (zoom and rotation)
  64. if (IsKeyPressed(KEY.R)) then
  65. camera.zoom = 1.0
  66. camera.rotation = 0.0
  67. end
  68. ---------------------------------------------------------------------------------------
  69. -- Draw
  70. ---------------------------------------------------------------------------------------
  71. BeginDrawing()
  72. ClearBackground(RAYWHITE)
  73. Begin2dMode(camera)
  74. DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY)
  75. for i = 1, MAX_BUILDINGS, 1 do DrawRectangleRec(buildings[i], buildColors[i]) end
  76. DrawRectangleRec(player, RED)
  77. DrawRectangle(camera.target.x, -500, 1, screenHeight*4, GREEN)
  78. DrawRectangle(-500, camera.target.y, screenWidth*4, 1, GREEN)
  79. End2dMode()
  80. DrawText("SCREEN AREA", 640, 10, 20, RED)
  81. DrawRectangle(0, 0, screenWidth, 5, RED)
  82. DrawRectangle(0, 5, 5, screenHeight - 10, RED)
  83. DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED)
  84. DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED)
  85. DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5))
  86. DrawRectangleLines( 10, 10, 250, 113, BLUE)
  87. DrawText("Free 2d camera controls:", 20, 20, 10, BLACK)
  88. DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY)
  89. DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY)
  90. DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY)
  91. DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY)
  92. EndDrawing();
  93. ---------------------------------------------------------------------------------------
  94. end
  95. -- De-Initialization
  96. -------------------------------------------------------------------------------------------
  97. CloseWindow() -- Close window and OpenGL context
  98. -------------------------------------------------------------------------------------------