Clicker.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Core/ProcessUtils.h>
  5. #include <Urho3D/Input/Input.h>
  6. #include <Urho3D/UI/Font.h>
  7. #include <Urho3D/UI/Text.h>
  8. #include <Urho3D/UI/UI.h>
  9. #include "Clicker.h"
  10. #include <Urho3D/DebugNew.h>
  11. // Expands to this example's entry-point
  12. URHO3D_DEFINE_APPLICATION_MAIN(Clicker)
  13. Clicker::Clicker(Context* context)
  14. : Sample(context)
  15. {
  16. }
  17. void Clicker::Start()
  18. {
  19. Sample::Start();
  20. CreateUI();
  21. Sample::InitMouseMode(MM_FREE);
  22. SubscribeToEvents();
  23. }
  24. void Clicker::CreateUI()
  25. {
  26. ResourceCache* cache = GetSubsystem<ResourceCache>();
  27. XMLFile* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  28. UIElement* uiRoot = GetSubsystem<UI>()->GetRoot();
  29. uiRoot->SetDefaultStyle(style);
  30. // Text in the center of the screen will initially contain hint, and then score
  31. Text* scoreText = uiRoot->CreateChild<Text>("Score");
  32. scoreText->SetText("Hold LMB to play.\nClick RMB to upgrade power.");
  33. scoreText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  34. scoreText->SetColor(Color::GREEN);
  35. scoreText->SetHorizontalAlignment(HA_CENTER);
  36. scoreText->SetVerticalAlignment(VA_CENTER);
  37. Text* powerText = uiRoot->CreateChild<Text>("Power");
  38. powerText->SetText("Power: " + power_.ToString());
  39. powerText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  40. powerText->SetColor(Color::WHITE);
  41. powerText->SetPosition({10, 10});
  42. }
  43. void Clicker::SubscribeToEvents()
  44. {
  45. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Clicker, HandleUpdate));
  46. SubscribeToEvent(E_MOUSEBUTTONDOWN, URHO3D_HANDLER(Clicker, HandleMouseButtonDown));
  47. }
  48. static String ShortNumberRepresentation(const BigInt& value)
  49. {
  50. String str = value.ToString();
  51. u32 len = str.Length();
  52. if (len > 45)
  53. return str.Substring(0, len - 45) + " quattuordecillion";
  54. if (len > 42)
  55. return str.Substring(0, len - 42) + " tredecillion";
  56. if (len > 39)
  57. return str.Substring(0, len - 39) + " duodecillion";
  58. if (len > 36)
  59. return str.Substring(0, len - 36) + " undecillion";
  60. if (len > 33)
  61. return str.Substring(0, len - 33) + " decillion";
  62. if (len > 30)
  63. return str.Substring(0, len - 30) + " nonillion";
  64. if (len > 27)
  65. return str.Substring(0, len - 27) + " octillion";
  66. if (len > 24)
  67. return str.Substring(0, len - 24) + " septillion";
  68. if (len > 21)
  69. return str.Substring(0, len - 21) + " sextillion";
  70. if (len > 18)
  71. return str.Substring(0, len - 18) + " quintillion";
  72. if (len > 15)
  73. return str.Substring(0, len - 15) + " quadrillion";
  74. if (len > 12)
  75. return str.Substring(0, len - 12) + " trillion";
  76. if (len > 9)
  77. return str.Substring(0, len - 9) + " billion";
  78. if (len > 6)
  79. return str.Substring(0, len - 6) + " million";
  80. if (len > 3)
  81. return str.Substring(0, len - 3) + " thousand";
  82. return str;
  83. }
  84. void Clicker::HandleUpdate(StringHash eventType, VariantMap& eventData)
  85. {
  86. using namespace Update;
  87. float timeStep = eventData[P_TIMESTEP].GetFloat();
  88. if (clickDelay_ > 0.f)
  89. clickDelay_ -= timeStep;
  90. Input* input = GetSubsystem<Input>();
  91. if (input->GetMouseButtonDown(MOUSEB_LEFT) && clickDelay_ <= 0.f)
  92. {
  93. score_ += power_;
  94. UIElement* uiRoot = GetSubsystem<UI>()->GetRoot();
  95. Text* scoreText = static_cast<Text*>(uiRoot->GetChild("Score", false));
  96. scoreText->SetText(ShortNumberRepresentation(score_));
  97. clickDelay_ = 0.2f;
  98. }
  99. }
  100. void Clicker::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  101. {
  102. using namespace MouseButtonDown;
  103. MouseButton button = (MouseButton)eventData[P_BUTTON].GetU32();
  104. if (button == MOUSEB_RIGHT)
  105. {
  106. power_ *= 2;
  107. UIElement* uiRoot = GetSubsystem<UI>()->GetRoot();
  108. Text* powerText = static_cast<Text*>(uiRoot->GetChild("Power", false));
  109. powerText->SetText("Power: " + ShortNumberRepresentation(power_));
  110. }
  111. }