36_Urho2DTileMap.as 6.2 KB

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