Urho2DSpriterAnimation.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "Drawable2D.h"
  27. #include "Engine.h"
  28. #include "Font.h"
  29. #include "Graphics.h"
  30. #include "Input.h"
  31. #include "Octree.h"
  32. #include "Renderer.h"
  33. #include "ResourceCache.h"
  34. #include "Scene.h"
  35. #include "Text.h"
  36. #include "Urho2DSpriterAnimation.h"
  37. #include "Zone.h"
  38. #include "DebugNew.h"
  39. static const char* animationNames[] =
  40. {
  41. "idle",
  42. "run",
  43. "attack",
  44. "hit",
  45. "dead",
  46. "dead2",
  47. "dead3",
  48. };
  49. DEFINE_APPLICATION_MAIN(Urho2DSpriterAnimation)
  50. Urho2DSpriterAnimation::Urho2DSpriterAnimation(Context* context) :
  51. Sample(context),
  52. animationIndex_(0)
  53. {
  54. }
  55. void Urho2DSpriterAnimation::Start()
  56. {
  57. // Execute base class startup
  58. Sample::Start();
  59. // Create the scene content
  60. CreateScene();
  61. // Create the UI content
  62. CreateInstructions();
  63. // Setup the viewport for displaying the scene
  64. SetupViewport();
  65. // Hook up to the frame update events
  66. SubscribeToEvents();
  67. }
  68. void Urho2DSpriterAnimation::CreateScene()
  69. {
  70. scene_ = new Scene(context_);
  71. scene_->CreateComponent<Octree>();
  72. // Create camera node
  73. cameraNode_ = scene_->CreateChild("Camera");
  74. // Set camera's position
  75. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  76. Camera* camera = cameraNode_->CreateComponent<Camera>();
  77. camera->SetOrthographic(true);
  78. Graphics* graphics = GetSubsystem<Graphics>();
  79. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  80. ResourceCache* cache = GetSubsystem<ResourceCache>();
  81. AnimationSet2D* animationSet = cache->GetResource<AnimationSet2D>("Urho2D/imp/imp.scml");
  82. if (!animationSet)
  83. return;
  84. spriteNode_ = scene_->CreateChild("SpriterAnimation");
  85. spriteNode_->SetPosition(Vector3(-1.4f, 2.0f, 0.0f));
  86. AnimatedSprite2D* animatedSprite = spriteNode_->CreateComponent<AnimatedSprite2D>();
  87. animatedSprite->SetAnimation(animationSet, animationNames[animationIndex_]);
  88. }
  89. void Urho2DSpriterAnimation::CreateInstructions()
  90. {
  91. ResourceCache* cache = GetSubsystem<ResourceCache>();
  92. UI* ui = GetSubsystem<UI>();
  93. // Construct new Text object, set string to display and font to use
  94. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  95. instructionText->SetText("Mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.");
  96. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  97. // Position the text relative to the screen center
  98. instructionText->SetHorizontalAlignment(HA_CENTER);
  99. instructionText->SetVerticalAlignment(VA_CENTER);
  100. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  101. }
  102. void Urho2DSpriterAnimation::SetupViewport()
  103. {
  104. Renderer* renderer = GetSubsystem<Renderer>();
  105. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  106. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  107. renderer->SetViewport(0, viewport);
  108. }
  109. void Urho2DSpriterAnimation::MoveCamera(float timeStep)
  110. {
  111. // Do not move if the UI has a focused element (the console)
  112. if (GetSubsystem<UI>()->GetFocusElement())
  113. return;
  114. Input* input = GetSubsystem<Input>();
  115. // Movement speed as world units per second
  116. const float MOVE_SPEED = 4.0f;
  117. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  118. if (input->GetKeyDown('W'))
  119. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  120. if (input->GetKeyDown('S'))
  121. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  122. if (input->GetKeyDown('A'))
  123. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  124. if (input->GetKeyDown('D'))
  125. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  126. if (input->GetKeyDown(KEY_PAGEUP))
  127. {
  128. Camera* camera = cameraNode_->GetComponent<Camera>();
  129. camera->SetZoom(camera->GetZoom() * 1.01f);
  130. }
  131. if (input->GetKeyDown(KEY_PAGEDOWN))
  132. {
  133. Camera* camera = cameraNode_->GetComponent<Camera>();
  134. camera->SetZoom(camera->GetZoom() * 0.99f);
  135. }
  136. }
  137. void Urho2DSpriterAnimation::SubscribeToEvents()
  138. {
  139. // Subscribe HandleUpdate() function for processing update events
  140. SubscribeToEvent(E_UPDATE, HANDLER(Urho2DSpriterAnimation, HandleUpdate));
  141. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(Urho2DSpriterAnimation, HandleMouseButtonDown));
  142. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  143. UnsubscribeFromEvent(E_SCENEUPDATE);
  144. }
  145. void Urho2DSpriterAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  146. {
  147. using namespace Update;
  148. // Take the frame time step, which is stored as a float
  149. float timeStep = eventData[P_TIMESTEP].GetFloat();
  150. // Move the camera, scale movement with time step
  151. MoveCamera(timeStep);
  152. }
  153. void Urho2DSpriterAnimation::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  154. {
  155. AnimatedSprite2D* animatedSprite = spriteNode_->GetComponent<AnimatedSprite2D>();
  156. animationIndex_ = (animationIndex_ + 1) % 7;
  157. animatedSprite->SetAnimation(animationNames[animationIndex_], LM_FORCE_LOOPED);
  158. }