Urho2DTileMap.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Camera.h"
  23. #include "CoreEvents.h"
  24. #include "Engine.h"
  25. #include "Font.h"
  26. #include "Graphics.h"
  27. #include "Input.h"
  28. #include "Octree.h"
  29. #include "Renderer.h"
  30. #include "ResourceCache.h"
  31. #include "Scene.h"
  32. #include "Text.h"
  33. #include "Urho2DTileMap.h"
  34. #include "Zone.h"
  35. #include "TmxFile2D.h"
  36. #include "TileMap2D.h"
  37. #include "Drawable2D.h"
  38. #include "DebugNew.h"
  39. // Number of static sprites to draw
  40. static const StringHash VAR_MOVESPEED("MoveSpeed");
  41. static const StringHash VAR_ROTATESPEED("RotateSpeed");
  42. DEFINE_APPLICATION_MAIN(Urho2DTileMap)
  43. Urho2DTileMap::Urho2DTileMap(Context* context) :
  44. Sample(context)
  45. {
  46. }
  47. void Urho2DTileMap::Start()
  48. {
  49. // Execute base class startup
  50. Sample::Start();
  51. // Create the scene content
  52. CreateScene();
  53. // Create the UI content
  54. CreateInstructions();
  55. // Setup the viewport for displaying the scene
  56. SetupViewport();
  57. // Hook up to the frame update events
  58. SubscribeToEvents();
  59. }
  60. void Urho2DTileMap::CreateScene()
  61. {
  62. scene_ = new Scene(context_);
  63. scene_->CreateComponent<Octree>();
  64. // Create camera node
  65. cameraNode_ = scene_->CreateChild("Camera");
  66. // Set camera's position
  67. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  68. Camera* camera = cameraNode_->CreateComponent<Camera>();
  69. camera->SetOrthographic(true);
  70. Graphics* graphics = GetSubsystem<Graphics>();
  71. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  72. ResourceCache* cache = GetSubsystem<ResourceCache>();
  73. // Get tmx file
  74. TmxFile2D* tmxFile = cache->GetResource<TmxFile2D>("Urho2D/isometric_grass_and_water.tmx");
  75. if (!tmxFile)
  76. return;
  77. SharedPtr<Node> tileMapNode(scene_->CreateChild("TileMap"));
  78. tileMapNode->SetPosition(Vector3(0.0f, 0.0f, -1.0f));
  79. TileMap2D* tileMap = tileMapNode->CreateComponent<TileMap2D>();
  80. // Set animation
  81. tileMap->SetTmxFile(tmxFile);
  82. // Set camera's position
  83. const TileMapInfo2D& info = tileMap->GetInfo();
  84. float x = info.GetMapWidth() * 0.5f;
  85. float y = info.GetMapHeight() * 0.5f;
  86. cameraNode_->SetPosition(Vector3(x, y, -10.0f));
  87. }
  88. void Urho2DTileMap::CreateInstructions()
  89. {
  90. ResourceCache* cache = GetSubsystem<ResourceCache>();
  91. UI* ui = GetSubsystem<UI>();
  92. // Construct new Text object, set string to display and font to use
  93. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  94. instructionText->SetText("Use WASD keys to move, use PageUp PageDown keys to zoom.");
  95. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  96. // Position the text relative to the screen center
  97. instructionText->SetHorizontalAlignment(HA_CENTER);
  98. instructionText->SetVerticalAlignment(VA_CENTER);
  99. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  100. }
  101. void Urho2DTileMap::SetupViewport()
  102. {
  103. Renderer* renderer = GetSubsystem<Renderer>();
  104. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  105. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  106. renderer->SetViewport(0, viewport);
  107. }
  108. void Urho2DTileMap::MoveCamera(float timeStep)
  109. {
  110. // Do not move if the UI has a focused element (the console)
  111. if (GetSubsystem<UI>()->GetFocusElement())
  112. return;
  113. Input* input = GetSubsystem<Input>();
  114. // Movement speed as world units per second
  115. const float MOVE_SPEED = 4.0f;
  116. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  117. if (input->GetKeyDown('W'))
  118. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  119. if (input->GetKeyDown('S'))
  120. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  121. if (input->GetKeyDown('A'))
  122. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  123. if (input->GetKeyDown('D'))
  124. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  125. if (input->GetKeyDown(KEY_PAGEUP))
  126. {
  127. Camera* camera = cameraNode_->GetComponent<Camera>();
  128. camera->SetZoom(camera->GetZoom() * 1.01f);
  129. }
  130. if (input->GetKeyDown(KEY_PAGEDOWN))
  131. {
  132. Camera* camera = cameraNode_->GetComponent<Camera>();
  133. camera->SetZoom(camera->GetZoom() * 0.99f);
  134. }
  135. }
  136. void Urho2DTileMap::SubscribeToEvents()
  137. {
  138. // Subscribe HandleUpdate() function for processing update events
  139. SubscribeToEvent(E_UPDATE, HANDLER(Urho2DTileMap, HandleUpdate));
  140. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  141. UnsubscribeFromEvent(E_SCENEUPDATE);
  142. }
  143. void Urho2DTileMap::HandleUpdate(StringHash eventType, VariantMap& eventData)
  144. {
  145. using namespace Update;
  146. // Take the frame time step, which is stored as a float
  147. float timeStep = eventData[P_TIMESTEP].GetFloat();
  148. // Move the camera, scale movement with time step
  149. MoveCamera(timeStep);
  150. }