Urho2DSprite.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "AnimatedSprite2D.h"
  23. #include "AnimationSet2D.h"
  24. #include "Camera.h"
  25. #include "CoreEvents.h"
  26. #include "Engine.h"
  27. #include "Font.h"
  28. #include "Graphics.h"
  29. #include "Input.h"
  30. #include "Octree.h"
  31. #include "Renderer.h"
  32. #include "ResourceCache.h"
  33. #include "Scene.h"
  34. #include "Sprite2D.h"
  35. #include "StaticSprite2D.h"
  36. #include "Text.h"
  37. #include "Urho2DSprite.h"
  38. #include "Zone.h"
  39. #include "DebugNew.h"
  40. // Number of static sprites to draw
  41. static const unsigned NUM_SPRITES = 200;
  42. static const StringHash VAR_MOVESPEED("MoveSpeed");
  43. static const StringHash VAR_ROTATESPEED("RotateSpeed");
  44. DEFINE_APPLICATION_MAIN(Urho2DSprite)
  45. Urho2DSprite::Urho2DSprite(Context* context) :
  46. Sample(context)
  47. {
  48. }
  49. void Urho2DSprite::Start()
  50. {
  51. // Execute base class startup
  52. Sample::Start();
  53. // Create the scene content
  54. CreateScene();
  55. // Create the UI content
  56. CreateInstructions();
  57. // Setup the viewport for displaying the scene
  58. SetupViewport();
  59. // Hook up to the frame update events
  60. SubscribeToEvents();
  61. }
  62. void Urho2DSprite::CreateScene()
  63. {
  64. scene_ = new Scene(context_);
  65. scene_->CreateComponent<Octree>();
  66. // Create camera node
  67. cameraNode_ = scene_->CreateChild("Camera");
  68. // Set camera's position
  69. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  70. Camera* camera = cameraNode_->CreateComponent<Camera>();
  71. camera->SetOrthographic(true);
  72. Graphics* graphics = GetSubsystem<Graphics>();
  73. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  74. ResourceCache* cache = GetSubsystem<ResourceCache>();
  75. // Get sprite
  76. Sprite2D* sprite = cache->GetResource<Sprite2D>("Urho2D/Aster.png");
  77. if (!sprite)
  78. return;
  79. float halfWidth = graphics->GetWidth() * 0.5f * PIXEL_SIZE;
  80. float halfHeight = graphics->GetHeight() * 0.5f * PIXEL_SIZE;
  81. for (unsigned i = 0; i < NUM_SPRITES; ++i)
  82. {
  83. SharedPtr<Node> spriteNode(scene_->CreateChild("StaticSprite2D"));
  84. spriteNode->SetPosition(Vector3(Random(-halfWidth, halfWidth), Random(-halfHeight, halfHeight), 0.0f));
  85. StaticSprite2D* staticSprite = spriteNode->CreateComponent<StaticSprite2D>();
  86. // Set random color
  87. staticSprite->SetColor(Color(Random(1.0f), Random(1.0f), Random(1.0f), 1.0f));
  88. // Set blend mode
  89. staticSprite->SetBlendMode(BLEND_ALPHA);
  90. // Set sprite
  91. staticSprite->SetSprite(sprite);
  92. // Set move speed
  93. spriteNode->SetVar(VAR_MOVESPEED, Vector3(Random(-2.0f, 2.0f), Random(-2.0f, 2.0f), 0.0f));
  94. // Set rotate speed
  95. spriteNode->SetVar(VAR_ROTATESPEED, Random(-90.0f, 90.0f));
  96. // Add to sprite node vector
  97. spriteNodes_.Push(spriteNode);
  98. }
  99. // Get animation set
  100. AnimationSet2D* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/GoldIcon.scml");
  101. if (!animationSet)
  102. return;
  103. SharedPtr<Node> spriteNode(scene_->CreateChild("AnimatedSprite2D"));
  104. spriteNode->SetPosition(Vector3(0.0f, 0.0f, -1.0f));
  105. AnimatedSprite2D* animatedSprite = spriteNode->CreateComponent<AnimatedSprite2D>();
  106. // Set animation
  107. animatedSprite->SetAnimation(animationSet, "idle");
  108. }
  109. void Urho2DSprite::CreateInstructions()
  110. {
  111. ResourceCache* cache = GetSubsystem<ResourceCache>();
  112. UI* ui = GetSubsystem<UI>();
  113. // Construct new Text object, set string to display and font to use
  114. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  115. instructionText->SetText("Use WASD keys to move, use PageUp PageDown keys to zoom.");
  116. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  117. // Position the text relative to the screen center
  118. instructionText->SetHorizontalAlignment(HA_CENTER);
  119. instructionText->SetVerticalAlignment(VA_CENTER);
  120. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  121. }
  122. void Urho2DSprite::SetupViewport()
  123. {
  124. Renderer* renderer = GetSubsystem<Renderer>();
  125. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  126. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  127. renderer->SetViewport(0, viewport);
  128. }
  129. void Urho2DSprite::MoveCamera(float timeStep)
  130. {
  131. // Do not move if the UI has a focused element (the console)
  132. if (GetSubsystem<UI>()->GetFocusElement())
  133. return;
  134. Input* input = GetSubsystem<Input>();
  135. // Movement speed as world units per second
  136. const float MOVE_SPEED = 4.0f;
  137. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  138. if (input->GetKeyDown('W'))
  139. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  140. if (input->GetKeyDown('S'))
  141. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  142. if (input->GetKeyDown('A'))
  143. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  144. if (input->GetKeyDown('D'))
  145. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  146. if (input->GetKeyDown(KEY_PAGEUP))
  147. {
  148. Camera* camera = cameraNode_->GetComponent<Camera>();
  149. camera->SetZoom(camera->GetZoom() * 1.01f);
  150. }
  151. if (input->GetKeyDown(KEY_PAGEDOWN))
  152. {
  153. Camera* camera = cameraNode_->GetComponent<Camera>();
  154. camera->SetZoom(camera->GetZoom() * 0.99f);
  155. }
  156. }
  157. void Urho2DSprite::SubscribeToEvents()
  158. {
  159. // Subscribe HandleUpdate() function for processing update events
  160. SubscribeToEvent(E_UPDATE, HANDLER(Urho2DSprite, HandleUpdate));
  161. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  162. UnsubscribeFromEvent(E_SCENEUPDATE);
  163. }
  164. void Urho2DSprite::HandleUpdate(StringHash eventType, VariantMap& eventData)
  165. {
  166. using namespace Update;
  167. // Take the frame time step, which is stored as a float
  168. float timeStep = eventData[P_TIMESTEP].GetFloat();
  169. // Move the camera, scale movement with time step
  170. MoveCamera(timeStep);
  171. Graphics* graphics = GetSubsystem<Graphics>();
  172. float halfWidth = (float)graphics->GetWidth() * 0.5f * PIXEL_SIZE;
  173. float halfHeight = (float)graphics->GetHeight() * 0.5f * PIXEL_SIZE;
  174. for (unsigned i = 0; i < spriteNodes_.Size(); ++i)
  175. {
  176. SharedPtr<Node> node = spriteNodes_[i];
  177. Vector3 position = node->GetPosition();
  178. Vector3 moveSpeed = node->GetVar(VAR_MOVESPEED).GetVector3();
  179. Vector3 newPosition = position + moveSpeed * timeStep;
  180. if (newPosition.x_ < -halfWidth || newPosition.x_ > halfWidth)
  181. {
  182. newPosition.x_ = position.x_;
  183. moveSpeed.x_ = -moveSpeed.x_;
  184. node->SetVar(VAR_MOVESPEED, moveSpeed);
  185. }
  186. if (newPosition.y_ < -halfHeight || newPosition.y_ > halfHeight)
  187. {
  188. newPosition.y_ = position.y_;
  189. moveSpeed.y_ = -moveSpeed.y_;
  190. node->SetVar(VAR_MOVESPEED, moveSpeed);
  191. }
  192. node->SetPosition(newPosition);
  193. float rotateSpeed = node->GetVar(VAR_ROTATESPEED).GetFloat();
  194. node->Roll(rotateSpeed * timeStep);
  195. }
  196. }