AppState_Benchmark02.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "AppState_Benchmark02.h"
  4. #include "AppStateManager.h"
  5. #include "Benchmark02_WomanMover.h"
  6. #include <Urho3D/Core/Context.h>
  7. #include <Urho3D/Graphics/AnimatedModel.h>
  8. #include <Urho3D/Graphics/Animation.h>
  9. #include <Urho3D/Graphics/AnimationController.h>
  10. #include <Urho3D/Graphics/AnimationState.h>
  11. #include <Urho3D/Input/Input.h>
  12. #include <Urho3D/Resource/ResourceCache.h>
  13. #include <Urho3D/Scene/SceneEvents.h>
  14. #include <Urho3D/Scene/SplinePath.h>
  15. #include <Urho3D/DebugNew.h>
  16. using namespace Urho3D;
  17. AppState_Benchmark02::AppState_Benchmark02(Context* context)
  18. : AppState_Base(context)
  19. {
  20. name_ = "Orcs & Humans";
  21. // This constructor is called once when the application runs, so we can register here
  22. context->RegisterFactory<Benchmark02_WomanMover>();
  23. }
  24. void AppState_Benchmark02::OnEnter()
  25. {
  26. assert(!scene_);
  27. LoadSceneXml("99_Benchmark/Scenes/Benchmark02.xml");
  28. Vector3 castlePos = scene_->GetChild("Castle")->GetPosition();
  29. BoundingBox castleTop(castlePos - Vector3(7.f, 0.f, 7.f), castlePos + Vector3(7.f, 0.f, 7.f));
  30. ResourceCache* cache = GetSubsystem<ResourceCache>();
  31. Vector<Node*> womans;
  32. scene_->GetChildrenWithTag(womans, "woman");
  33. for (Node* woman : womans)
  34. {
  35. Benchmark02_WomanMover* mover = woman->CreateComponent<Benchmark02_WomanMover>();
  36. mover->SetParameters(2.f, 100.f, castleTop);
  37. AnimatedModel* modelObject = woman->GetComponent<AnimatedModel>();
  38. Animation* walkAnimation = cache->GetResource<Animation>("Models/Kachujin/Kachujin_Walk.ani");
  39. AnimationState* state = modelObject->AddAnimationState(walkAnimation);
  40. if (state)
  41. {
  42. state->SetWeight(1.0f);
  43. state->SetLooped(true);
  44. state->SetTime(Random(walkAnimation->GetLength()));
  45. }
  46. }
  47. Vector<Node*> mutants;
  48. scene_->GetChildrenWithTag(mutants, "mutant");
  49. for (Node* mutant : mutants)
  50. {
  51. AnimationController* animCtrl = mutant->CreateComponent<AnimationController>();
  52. animCtrl->PlayExclusive("Models/Mutant/Mutant_Idle0.ani", 0, true, 0.f);
  53. animCtrl->SetTime("Models/Mutant/Mutant_Idle0.ani", Random(animCtrl->GetLength("Models/Mutant/Mutant_Idle0.ani")));
  54. }
  55. Node* mutantGeneral = scene_->GetChild("MutantGeneral");
  56. AnimationController* generalAnimCtrl = mutantGeneral->CreateComponent<AnimationController>();
  57. generalAnimCtrl->PlayExclusive("Models/Mutant/Mutant_Idle1.ani", 0, true, 0.f);
  58. generalAnimCtrl->SetTime("Models/Mutant/Mutant_Idle1.ani", Random(generalAnimCtrl->GetLength("Models/Mutant/Mutant_Idle1.ani")));
  59. Node* cameraNode = scene_->GetChild("Camera");
  60. Node* cameraPath = scene_->GetChild("CameraPath");
  61. SplinePath* cameraSplinePath = cameraPath->CreateComponent<SplinePath>();
  62. cameraSplinePath->SetControlledNode(cameraNode);
  63. for (Node* child : cameraPath->GetChildren())
  64. cameraSplinePath->AddControlPoint(child);
  65. cameraSplinePath->SetSpeed(2.f);
  66. cameraSplinePath->SetInterpolationMode(InterpolationMode::CATMULL_ROM_FULL_CURVE);
  67. Node* cameraTargetNode = scene_->CreateChild("CameraTarget");
  68. Node* cameraTargetPath = scene_->GetChild("CameraTargetPath");
  69. SplinePath* cameraTargetSplinePath = cameraPath->CreateComponent<SplinePath>();
  70. cameraTargetSplinePath->SetControlledNode(cameraTargetNode);
  71. for (Node* child : cameraPath->GetChildren())
  72. cameraTargetSplinePath->AddControlPoint(child);
  73. cameraTargetSplinePath->SetSpeed(2.f);
  74. cameraTargetSplinePath->SetInterpolationMode(InterpolationMode::CATMULL_ROM_FULL_CURVE);
  75. GetSubsystem<Input>()->SetMouseVisible(false);
  76. SetupViewport();
  77. SubscribeToEvent(scene_, E_SCENEUPDATE, URHO3D_HANDLER(AppState_Benchmark02, HandleSceneUpdate));
  78. fpsCounter_.Clear();
  79. }
  80. void AppState_Benchmark02::OnLeave()
  81. {
  82. UnsubscribeFromAllEvents();
  83. DestroyViewport();
  84. scene_ = nullptr;
  85. }
  86. void AppState_Benchmark02::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  87. {
  88. float timeStep = eventData[SceneUpdate::P_TIMESTEP].GetFloat();
  89. fpsCounter_.Update(timeStep);
  90. UpdateCurrentFpsElement();
  91. if (GetSubsystem<Input>()->GetKeyDown(KEY_ESCAPE))
  92. {
  93. GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_MAINSCREEN);
  94. return;
  95. }
  96. Node* cameraPath = scene_->GetChild("CameraPath");
  97. SplinePath* cameraSplinePath = cameraPath->GetComponent<SplinePath>();
  98. cameraSplinePath->Move(timeStep);
  99. Node* cameraTargetPath = scene_->GetChild("CameraTargetPath");
  100. SplinePath* cameraTargetSplinePath = cameraPath->GetComponent<SplinePath>();
  101. cameraTargetSplinePath->Move(timeStep);
  102. Node* cameraTargetNode = scene_->GetChild("CameraTarget");
  103. Node* cameraNode = scene_->GetChild("Camera");
  104. cameraNode->LookAt(cameraTargetNode->GetPosition());
  105. if (cameraSplinePath->IsFinished())
  106. GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_RESULTSCREEN);
  107. }