2DSprite.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Graphics/Camera.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include <Atomic/Graphics/Octree.h>
  28. #include <Atomic/Graphics/Renderer.h>
  29. #include <Atomic/Graphics/Zone.h>
  30. #include <Atomic/Input/Input.h>
  31. #include <Atomic/Resource/ResourceCache.h>
  32. #include <Atomic/Scene/Scene.h>
  33. #include <Atomic/Atomic2D/AnimatedSprite2D.h>
  34. #include <Atomic/Atomic2D/AnimationSet2D.h>
  35. #include <Atomic/Atomic2D/Sprite2D.h>
  36. #include <Atomic/Scene/SceneEvents.h>
  37. #include <Atomic/UI/UI.h>
  38. #include "2DSprite.h"
  39. #include <Atomic/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. Atomic2DSprite::Atomic2DSprite(Context* context) :
  45. Sample(context)
  46. {
  47. }
  48. void Atomic2DSprite::Start()
  49. {
  50. // Execute base class startup
  51. Sample::Start();
  52. // Create the scene content
  53. CreateScene();
  54. // Create the UI content
  55. CreateInstructions();
  56. // Setup the viewport for displaying the scene
  57. SetupViewport();
  58. // Hook up to the frame update events
  59. SubscribeToEvents();
  60. // Set the mouse mode to use in the sample
  61. Sample::InitMouseMode(MM_FREE);
  62. }
  63. void Atomic2DSprite::CreateScene()
  64. {
  65. scene_ = new Scene(context_);
  66. scene_->CreateComponent<Octree>();
  67. // Create camera node
  68. cameraNode_ = scene_->CreateChild("Camera");
  69. // Set camera's position
  70. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  71. Camera* camera = cameraNode_->CreateComponent<Camera>();
  72. camera->SetOrthographic(true);
  73. Graphics* graphics = GetSubsystem<Graphics>();
  74. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  75. ResourceCache* cache = GetSubsystem<ResourceCache>();
  76. // Get sprite
  77. Sprite2D* sprite = cache->GetResource<Sprite2D>("Urho2D/Aster.png");
  78. if (!sprite)
  79. return;
  80. float halfWidth = graphics->GetWidth() * 0.5f * PIXEL_SIZE;
  81. float halfHeight = graphics->GetHeight() * 0.5f * PIXEL_SIZE;
  82. for (unsigned i = 0; i < NUM_SPRITES; ++i)
  83. {
  84. SharedPtr<Node> spriteNode(scene_->CreateChild("StaticSprite2D"));
  85. spriteNode->SetPosition(Vector3(Random(-halfWidth, halfWidth), Random(-halfHeight, halfHeight), 0.0f));
  86. StaticSprite2D* staticSprite = spriteNode->CreateComponent<StaticSprite2D>();
  87. // Set random color
  88. staticSprite->SetColor(Color(Random(1.0f), Random(1.0f), Random(1.0f), 1.0f));
  89. // Set blend mode
  90. staticSprite->SetBlendMode(BLEND_ALPHA);
  91. // Set sprite
  92. staticSprite->SetSprite(sprite);
  93. // Set move speed
  94. spriteNode->SetVar(VAR_MOVESPEED, Vector3(Random(-2.0f, 2.0f), Random(-2.0f, 2.0f), 0.0f));
  95. // Set rotate speed
  96. spriteNode->SetVar(VAR_ROTATESPEED, Random(-90.0f, 90.0f));
  97. // Add to sprite node vector
  98. spriteNodes_.Push(spriteNode);
  99. }
  100. // Get animation set
  101. AnimationSet2D* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/GoldIcon.scml");
  102. if (!animationSet)
  103. return;
  104. SharedPtr<Node> spriteNode(scene_->CreateChild("AnimatedSprite2D"));
  105. spriteNode->SetPosition(Vector3(0.0f, 0.0f, -1.0f));
  106. AnimatedSprite2D* animatedSprite = spriteNode->CreateComponent<AnimatedSprite2D>();
  107. // Set animation
  108. animatedSprite->SetAnimationSet(animationSet);
  109. animatedSprite->SetAnimation("idle");
  110. }
  111. void Atomic2DSprite::CreateInstructions()
  112. {
  113. SimpleCreateInstructions( "Use WASD keys to move, use PageUp PageDown keys to zoom." );
  114. }
  115. void Atomic2DSprite::SetupViewport()
  116. {
  117. Renderer* renderer = GetSubsystem<Renderer>();
  118. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  119. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  120. renderer->SetViewport(0, viewport);
  121. }
  122. void Atomic2DSprite::MoveCamera(float timeStep)
  123. {
  124. // Do not move if the UI has a focused element (the console)
  125. // if (GetSubsystem<UI>()->GetFocusElement())
  126. // return;
  127. Input* input = GetSubsystem<Input>();
  128. // Movement speed as world units per second
  129. const float MOVE_SPEED = 4.0f;
  130. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  131. if (input->GetKeyDown(KEY_W))
  132. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  133. if (input->GetKeyDown(KEY_S))
  134. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  135. if (input->GetKeyDown(KEY_A))
  136. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  137. if (input->GetKeyDown(KEY_D))
  138. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  139. if (input->GetKeyDown(KEY_PAGEUP))
  140. {
  141. Camera* camera = cameraNode_->GetComponent<Camera>();
  142. camera->SetZoom(camera->GetZoom() * 1.01f);
  143. }
  144. if (input->GetKeyDown(KEY_PAGEDOWN))
  145. {
  146. Camera* camera = cameraNode_->GetComponent<Camera>();
  147. camera->SetZoom(camera->GetZoom() * 0.99f);
  148. }
  149. }
  150. void Atomic2DSprite::SubscribeToEvents()
  151. {
  152. // Subscribe HandleUpdate() function for processing update events
  153. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(Atomic2DSprite, HandleUpdate));
  154. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  155. UnsubscribeFromEvent(E_SCENEUPDATE);
  156. }
  157. void Atomic2DSprite::HandleUpdate(StringHash eventType, VariantMap& eventData)
  158. {
  159. using namespace Update;
  160. // Take the frame time step, which is stored as a float
  161. float timeStep = eventData[P_TIMESTEP].GetFloat();
  162. // Move the camera, scale movement with time step
  163. MoveCamera(timeStep);
  164. Graphics* graphics = GetSubsystem<Graphics>();
  165. float halfWidth = (float)graphics->GetWidth() * 0.5f * PIXEL_SIZE;
  166. float halfHeight = (float)graphics->GetHeight() * 0.5f * PIXEL_SIZE;
  167. for (unsigned i = 0; i < spriteNodes_.Size(); ++i)
  168. {
  169. SharedPtr<Node> node = spriteNodes_[i];
  170. Vector3 position = node->GetPosition();
  171. Vector3 moveSpeed = node->GetVar(VAR_MOVESPEED).GetVector3();
  172. Vector3 newPosition = position + moveSpeed * timeStep;
  173. if (newPosition.x_ < -halfWidth || newPosition.x_ > halfWidth)
  174. {
  175. newPosition.x_ = position.x_;
  176. moveSpeed.x_ = -moveSpeed.x_;
  177. node->SetVar(VAR_MOVESPEED, moveSpeed);
  178. }
  179. if (newPosition.y_ < -halfHeight || newPosition.y_ > halfHeight)
  180. {
  181. newPosition.y_ = position.y_;
  182. moveSpeed.y_ = -moveSpeed.y_;
  183. node->SetVar(VAR_MOVESPEED, moveSpeed);
  184. }
  185. node->SetPosition(newPosition);
  186. float rotateSpeed = node->GetVar(VAR_ROTATESPEED).GetFloat();
  187. node->Roll(rotateSpeed * timeStep);
  188. }
  189. }