36_Urho2DTileMap.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. -- Urho2D tile map example.
  2. -- This sample demonstrates:
  3. -- - Creating a 2D scene with tile map
  4. -- - Displaying the scene using the Renderer subsystem
  5. -- - Handling keyboard to move and zoom 2D camera
  6. require "LuaScripts/Utilities/Sample"
  7. function Start()
  8. -- Execute the common startup for samples
  9. SampleStart()
  10. -- Create the scene content
  11. CreateScene()
  12. -- Create the UI content
  13. CreateInstructions()
  14. -- Setup the viewport for displaying the scene
  15. SetupViewport()
  16. -- Hook up to the frame update events
  17. SubscribeToEvents()
  18. end
  19. function CreateScene()
  20. scene_ = Scene()
  21. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  22. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  23. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  24. -- optimizing manner
  25. scene_:CreateComponent("Octree")
  26. -- Create a scene node for the camera, which we will move around
  27. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  28. cameraNode = scene_:CreateChild("Camera")
  29. -- Set an initial position for the camera scene node above the plane
  30. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  31. local camera = cameraNode:CreateComponent("Camera")
  32. camera.orthographic = true
  33. camera.orthoSize = graphics.height * PIXEL_SIZE
  34. camera.zoom = 1.0 * Min(graphics.width / 1280, graphics.height / 800) -- Set zoom according to user's resolution to ensure full visibility (initial zoom (1.0) is set for full visibility at 1280x800 resolution)
  35. -- Get tmx file
  36. local tmxFile = cache:GetResource("TmxFile2D", "Urho2D/isometric_grass_and_water.tmx")
  37. if tmxFile == nil then
  38. return
  39. end
  40. local tileMapNode = scene_:CreateChild("TileMap")
  41. tileMapNode.position = Vector3(0.0, 0.0, -1.0)
  42. local tileMap = tileMapNode:CreateComponent("TileMap2D")
  43. tileMap.tmxFile = tmxFile
  44. -- Set camera's position
  45. local info = tileMap.info
  46. local x = info.mapWidth * 0.5
  47. local y = info.mapHeight * 0.5
  48. cameraNode.position = Vector3(x, y, -10.0)
  49. end
  50. function CreateInstructions()
  51. -- Construct new Text object, set string to display and font to use
  52. local instructionText = ui.root:CreateChild("Text")
  53. instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.")
  54. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  55. -- Position the text relative to the screen center
  56. instructionText.horizontalAlignment = HA_CENTER
  57. instructionText.verticalAlignment = VA_CENTER
  58. instructionText:SetPosition(0, ui.root.height / 4)
  59. end
  60. function SetupViewport()
  61. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  62. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  63. -- use, but now we just use full screen and default render path configured in the engine command line options
  64. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  65. renderer:SetViewport(0, viewport)
  66. end
  67. function MoveCamera(timeStep)
  68. -- Do not move if the UI has a focused element (the console)
  69. if ui.focusElement ~= nil then
  70. return
  71. end
  72. -- Movement speed as world units per second
  73. local MOVE_SPEED = 4.0
  74. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  75. if input:GetKeyDown(KEY_W) then
  76. cameraNode:Translate(Vector3(0.0, 1.0, 0.0) * MOVE_SPEED * timeStep)
  77. end
  78. if input:GetKeyDown(KEY_S) then
  79. cameraNode:Translate(Vector3(0.0, -1.0, 0.0) * MOVE_SPEED * timeStep)
  80. end
  81. if input:GetKeyDown(KEY_A) then
  82. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  83. end
  84. if input:GetKeyDown(KEY_D) then
  85. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  86. end
  87. if input:GetKeyDown(KEY_PAGEUP) then
  88. local camera = cameraNode:GetComponent("Camera")
  89. camera.zoom = camera.zoom * 1.01
  90. end
  91. if input:GetKeyDown(KEY_PAGEDOWN) then
  92. local camera = cameraNode:GetComponent("Camera")
  93. camera.zoom = camera.zoom * 0.99
  94. end
  95. end
  96. function SubscribeToEvents()
  97. -- Subscribe HandleUpdate() function for processing update events
  98. SubscribeToEvent("Update", "HandleUpdate")
  99. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  100. UnsubscribeFromEvent("SceneUpdate")
  101. end
  102. function HandleUpdate(eventType, eventData)
  103. -- Take the frame time step, which is stored as a float
  104. local timeStep = eventData:GetFloat("TimeStep")
  105. -- Move the camera, scale movement with time step
  106. MoveCamera(timeStep)
  107. end
  108. -- Create XML patch instructions for screen joystick layout specific to this sample app
  109. function GetScreenJoystickPatchString()
  110. return
  111. "<patch>" ..
  112. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  113. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  114. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  115. " <element type=\"Text\">" ..
  116. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  117. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  118. " </element>" ..
  119. " </add>" ..
  120. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  121. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  122. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  123. " <element type=\"Text\">" ..
  124. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  125. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  126. " </element>" ..
  127. " </add>" ..
  128. "</patch>"
  129. end