55_Clicker.as 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "Scripts/Utilities/Sample.as"
  2. // Current score
  3. BigInt score_;
  4. // Number of points received per click
  5. BigInt power_ = 1;
  6. // Delay after click
  7. float clickDelay_ = 0.f;
  8. void Start()
  9. {
  10. SampleStart();
  11. CreateUI();
  12. SampleInitMouseMode(MM_FREE);
  13. SubscribeToEvents();
  14. }
  15. // Construct UI elements
  16. void CreateUI()
  17. {
  18. XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  19. ui.root.defaultStyle = style;
  20. // Text in the center of the screen will initially contain hint, and then score
  21. Text@ scoreText = ui.root.CreateChild("Text", "Score");
  22. scoreText.SetText("Hold LMB to play.\nClick RMB to upgrade power.");
  23. scoreText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30);
  24. scoreText.SetColor(Color::GREEN);
  25. scoreText.SetHorizontalAlignment(HA_CENTER);
  26. scoreText.SetVerticalAlignment(VA_CENTER);
  27. Text@ powerText = ui.root.CreateChild("Text", "Power");
  28. powerText.SetText("Power: " + power_.ToString());
  29. powerText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30);
  30. powerText.SetColor(Color::WHITE);
  31. powerText.SetPosition(IntVector2(10, 10));
  32. }
  33. void SubscribeToEvents()
  34. {
  35. SubscribeToEvent("Update", "HandleUpdate");
  36. SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown");
  37. }
  38. String ShortNumberRepresentation(const BigInt&in value)
  39. {
  40. String str = value.ToString();
  41. uint len = str.Length();
  42. if (len > 45)
  43. return str.Substring(0, len - 45) + " quattuordecillion";
  44. if (len > 42)
  45. return str.Substring(0, len - 42) + " tredecillion";
  46. if (len > 39)
  47. return str.Substring(0, len - 39) + " duodecillion";
  48. if (len > 36)
  49. return str.Substring(0, len - 36) + " undecillion";
  50. if (len > 33)
  51. return str.Substring(0, len - 33) + " decillion";
  52. if (len > 30)
  53. return str.Substring(0, len - 30) + " nonillion";
  54. if (len > 27)
  55. return str.Substring(0, len - 27) + " octillion";
  56. if (len > 24)
  57. return str.Substring(0, len - 24) + " septillion";
  58. if (len > 21)
  59. return str.Substring(0, len - 21) + " sextillion";
  60. if (len > 18)
  61. return str.Substring(0, len - 18) + " quintillion";
  62. if (len > 15)
  63. return str.Substring(0, len - 15) + " quadrillion";
  64. if (len > 12)
  65. return str.Substring(0, len - 12) + " trillion";
  66. if (len > 9)
  67. return str.Substring(0, len - 9) + " billion";
  68. if (len > 6)
  69. return str.Substring(0, len - 6) + " million";
  70. if (len > 3)
  71. return str.Substring(0, len - 3) + " thousand";
  72. return str;
  73. }
  74. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  75. {
  76. float timeStep = eventData["TimeStep"].GetFloat();
  77. if (clickDelay_ > 0.f)
  78. clickDelay_ -= timeStep;
  79. if (input.GetMouseButtonDown(MOUSEB_LEFT) && clickDelay_ <= 0.f)
  80. {
  81. score_ += power_;
  82. Text@ scoreText = cast<Text>(ui.root.GetChild("Score", false));
  83. scoreText.SetText(ShortNumberRepresentation(score_));
  84. clickDelay_ = 0.2f;
  85. }
  86. }
  87. // Handle the mouse click event
  88. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  89. {
  90. MouseButton button = eventData["Button"].GetU32();
  91. if (button == MOUSEB_RIGHT)
  92. {
  93. power_ *= 2;
  94. Text@ powerText = cast<Text>(ui.root.GetChild("Power", false));
  95. powerText.SetText("Power: " + ShortNumberRepresentation(power_));
  96. }
  97. }
  98. // Create XML patch instructions for screen joystick layout specific to this sample app
  99. String patchInstructions =
  100. "<patch>" +
  101. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  102. " <attribute name=\"Is Visible\" value=\"false\" />" +
  103. " </add>" +
  104. "</patch>";