36_Urho2DTileMap.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. -- - Interacting with the tile map
  7. require "LuaScripts/Utilities/Sample"
  8. function Start()
  9. -- Execute the common startup for samples
  10. SampleStart()
  11. -- Enable OS cursor
  12. input.mouseVisible = true
  13. -- Create the scene content
  14. CreateScene()
  15. -- Create the UI content
  16. CreateInstructions()
  17. -- Setup the viewport for displaying the scene
  18. SetupViewport()
  19. -- Set the mouse mode to use in the sample
  20. SampleInitMouseMode(MM_RELATIVE)
  21. -- Hook up to the frame update events
  22. SubscribeToEvents()
  23. end
  24. function CreateScene()
  25. scene_ = Scene()
  26. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  27. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  28. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  29. -- optimizing manner
  30. scene_:CreateComponent("Octree")
  31. -- Create a scene node for the camera, which we will move around
  32. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  33. cameraNode = scene_:CreateChild("Camera")
  34. -- Set an initial position for the camera scene node above the plane
  35. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  36. local camera = cameraNode:CreateComponent("Camera")
  37. camera.orthographic = true
  38. camera.orthoSize = graphics.height * PIXEL_SIZE
  39. 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)
  40. -- Get tmx file
  41. local tmxFile = cache:GetResource("TmxFile2D", "Urho2D/isometric_grass_and_water.tmx")
  42. if tmxFile == nil then
  43. return
  44. end
  45. local tileMapNode = scene_:CreateChild("TileMap")
  46. tileMapNode.position = Vector3(0.0, 0.0, -1.0)
  47. local tileMap = tileMapNode:CreateComponent("TileMap2D")
  48. tileMap.tmxFile = tmxFile
  49. -- Set camera's position
  50. local info = tileMap.info
  51. local x = info.mapWidth * 0.5
  52. local y = info.mapHeight * 0.5
  53. cameraNode.position = Vector3(x, y, -10.0)
  54. end
  55. function CreateInstructions()
  56. -- Construct new Text object, set string to display and font to use
  57. local instructionText = ui.root:CreateChild("Text")
  58. instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.\n LMB to remove a tile, RMB to swap grass and water.")
  59. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  60. -- Position the text relative to the screen center
  61. instructionText.horizontalAlignment = HA_CENTER
  62. instructionText.verticalAlignment = VA_CENTER
  63. instructionText:SetPosition(0, ui.root.height / 4)
  64. end
  65. function SetupViewport()
  66. -- 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
  67. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  68. -- use, but now we just use full screen and default render path configured in the engine command line options
  69. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  70. renderer:SetViewport(0, viewport)
  71. end
  72. function MoveCamera(timeStep)
  73. -- Do not move if the UI has a focused element (the console)
  74. if ui.focusElement ~= nil then
  75. return
  76. end
  77. -- Movement speed as world units per second
  78. local MOVE_SPEED = 4.0
  79. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  80. if input:GetKeyDown(KEY_W) then
  81. cameraNode:Translate(Vector3(0.0, 1.0, 0.0) * MOVE_SPEED * timeStep)
  82. end
  83. if input:GetKeyDown(KEY_S) then
  84. cameraNode:Translate(Vector3(0.0, -1.0, 0.0) * MOVE_SPEED * timeStep)
  85. end
  86. if input:GetKeyDown(KEY_A) then
  87. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  88. end
  89. if input:GetKeyDown(KEY_D) then
  90. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  91. end
  92. if input:GetKeyDown(KEY_PAGEUP) then
  93. local camera = cameraNode:GetComponent("Camera")
  94. camera.zoom = camera.zoom * 1.01
  95. end
  96. if input:GetKeyDown(KEY_PAGEDOWN) then
  97. local camera = cameraNode:GetComponent("Camera")
  98. camera.zoom = camera.zoom * 0.99
  99. end
  100. end
  101. function SubscribeToEvents()
  102. -- Subscribe HandleUpdate() function for processing update events
  103. SubscribeToEvent("Update", "HandleUpdate")
  104. -- Listen to mouse clicks
  105. SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown")
  106. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  107. UnsubscribeFromEvent("SceneUpdate")
  108. end
  109. function HandleUpdate(eventType, eventData)
  110. -- Take the frame time step, which is stored as a float
  111. local timeStep = eventData["TimeStep"]:GetFloat()
  112. -- Move the camera, scale movement with time step
  113. MoveCamera(timeStep)
  114. end
  115. function HandleMouseButtonDown(eventType, eventData)
  116. local tileMapNode = scene_:GetChild("TileMap", true)
  117. local map = tileMapNode:GetComponent("TileMap2D")
  118. local layer = map:GetLayer(0)
  119. success, x, y = map:PositionToTileIndex(GetMousePositionXY())
  120. if success then
  121. -- Get tile's sprite. Note that layer.GetTile(x, y).sprite is read-only, so we get the sprite through tile's node
  122. local n = layer:GetTileNode(x, y)
  123. if n == nil then
  124. return
  125. end
  126. local sprite = n:GetComponent("StaticSprite2D")
  127. if input:GetMouseButtonDown(MOUSEB_RIGHT) then
  128. -- Swap grass and water
  129. if layer:GetTile(x, y).gid < 9 then -- First 8 sprites in the "isometric_grass_and_water.png" tileset are mostly grass and from 9 to 24 they are mostly water
  130. sprite.sprite = layer:GetTile(0, 0).sprite -- Replace grass by water sprite used in top tile
  131. else sprite.sprite = layer:GetTile(24, 24).sprite end -- Replace water by grass sprite used in bottom tile
  132. else sprite.sprite = nil end -- 'Remove' sprite
  133. end
  134. end
  135. function GetMousePositionXY()
  136. local camera = cameraNode:GetComponent("Camera")
  137. local screenPoint = Vector3(input.mousePosition.x / graphics.width, input.mousePosition.y / graphics.height, 10)
  138. local worldPoint = camera:ScreenToWorldPoint(screenPoint)
  139. return Vector2(worldPoint.x, worldPoint.y)
  140. end
  141. -- Create XML patch instructions for screen joystick layout specific to this sample app
  142. function GetScreenJoystickPatchString()
  143. return
  144. "<patch>" ..
  145. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  146. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  147. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  148. " <element type=\"Text\">" ..
  149. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  150. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  151. " </element>" ..
  152. " </add>" ..
  153. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  154. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  155. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  156. " <element type=\"Text\">" ..
  157. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  158. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  159. " </element>" ..
  160. " </add>" ..
  161. "</patch>"
  162. end