ConsoleInput.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Core/ProcessUtils.h>
  5. #include <Urho3D/Core/Timer.h>
  6. #include <Urho3D/Engine/Console.h>
  7. #include <Urho3D/Engine/Engine.h>
  8. #include <Urho3D/Engine/EngineEvents.h>
  9. #include <Urho3D/Input/Input.h>
  10. #include <Urho3D/IO/Log.h>
  11. #include <Urho3D/UI/Button.h>
  12. #include "ConsoleInput.h"
  13. #include <Urho3D/DebugNew.h>
  14. // Expands to this example's entry-point
  15. URHO3D_DEFINE_APPLICATION_MAIN(ConsoleInput)
  16. // Hunger level descriptions
  17. const char* hungerLevels[] = {
  18. "bursting",
  19. "well-fed",
  20. "fed",
  21. "hungry",
  22. "very hungry",
  23. "starving"
  24. };
  25. // Urho threat level descriptions
  26. const char* urhoThreatLevels[] = {
  27. "Suddenly Urho appears from a dark corner of the fish tank",
  28. "Urho seems to have his eyes set on you",
  29. "Urho is homing in on you mercilessly"
  30. };
  31. ConsoleInput::ConsoleInput(Context* context) :
  32. Sample(context)
  33. {
  34. }
  35. void ConsoleInput::Start()
  36. {
  37. // Execute base class startup
  38. Sample::Start();
  39. // Subscribe to console commands and the frame update
  40. SubscribeToEvent(E_CONSOLECOMMAND, URHO3D_HANDLER(ConsoleInput, HandleConsoleCommand));
  41. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(ConsoleInput, HandleUpdate));
  42. // Subscribe key down event
  43. SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(ConsoleInput, HandleEscKeyDown));
  44. UnsubscribeFromEvent(E_KEYUP);
  45. // Hide logo to make room for the console
  46. SetLogoVisible(false);
  47. // Show the console by default, make it large. Console will show the text edit field when there is at least one
  48. // subscriber for the console command event
  49. auto* console = GetSubsystem<Console>();
  50. console->SetNumRows(GetSubsystem<Graphics>()->GetHeight() / 16);
  51. console->SetNumBufferedRows(2 * console->GetNumRows());
  52. console->SetCommandInterpreter(GetTypeName());
  53. console->SetVisible(true);
  54. console->GetCloseButton()->SetVisible(false);
  55. console->AddAutoComplete("help");
  56. console->AddAutoComplete("eat");
  57. console->AddAutoComplete("hide");
  58. console->AddAutoComplete("wait");
  59. console->AddAutoComplete("score");
  60. console->AddAutoComplete("quit");
  61. // Show OS mouse cursor
  62. GetSubsystem<Input>()->SetMouseVisible(true);
  63. // Set the mouse mode to use in the sample
  64. Sample::InitMouseMode(MM_FREE);
  65. // Open the operating system console window (for stdin / stdout) if not open yet
  66. OpenConsoleWindow();
  67. // Initialize game and print the welcome message
  68. StartGame();
  69. // Randomize from system clock
  70. SetRandomSeed(Time::GetSystemTime());
  71. }
  72. void ConsoleInput::HandleConsoleCommand(StringHash eventType, VariantMap& eventData)
  73. {
  74. using namespace ConsoleCommand;
  75. if (eventData[P_ID].GetString() == GetTypeName())
  76. HandleInput(eventData[P_COMMAND].GetString());
  77. }
  78. void ConsoleInput::HandleUpdate(StringHash eventType, VariantMap& eventData)
  79. {
  80. // Check if there is input from stdin
  81. String input = GetConsoleInput();
  82. if (input.Length())
  83. HandleInput(input);
  84. }
  85. void ConsoleInput::HandleEscKeyDown(StringHash eventType, VariantMap& eventData)
  86. {
  87. // Unlike the other samples, exiting the engine when ESC is pressed instead of just closing the console
  88. if (eventData[KeyDown::P_KEY].GetI32() == KEY_ESCAPE && GetPlatform() != "Web")
  89. engine_->Exit();
  90. }
  91. void ConsoleInput::StartGame()
  92. {
  93. Print("Welcome to the Urho adventure game! You are the newest fish in the tank; your\n"
  94. "objective is to survive as long as possible. Beware of hunger and the merciless\n"
  95. "predator cichlid Urho, who appears from time to time. Evading Urho is easier\n"
  96. "with an empty stomach. Type 'help' for available commands.");
  97. gameOn_ = true;
  98. foodAvailable_ = false;
  99. eatenLastTurn_ = false;
  100. numTurns_ = 0;
  101. hunger_ = 2;
  102. urhoThreat_ = 0;
  103. }
  104. void ConsoleInput::EndGame(const String& message)
  105. {
  106. Print(message);
  107. Print("Game over! You survived " + String(numTurns_) + " turns.\n"
  108. "Do you want to play again (Y/N)?");
  109. gameOn_ = false;
  110. }
  111. void ConsoleInput::Advance()
  112. {
  113. if (urhoThreat_ > 0)
  114. {
  115. ++urhoThreat_;
  116. if (urhoThreat_ > 3)
  117. {
  118. EndGame("Urho has eaten you!");
  119. return;
  120. }
  121. }
  122. else if (urhoThreat_ < 0)
  123. ++urhoThreat_;
  124. if (urhoThreat_ == 0 && Random() < 0.2f)
  125. ++urhoThreat_;
  126. if (urhoThreat_ > 0)
  127. Print(String(urhoThreatLevels[urhoThreat_ - 1]) + ".");
  128. if ((numTurns_ & 3u) == 0 && !eatenLastTurn_)
  129. {
  130. ++hunger_;
  131. if (hunger_ > 5)
  132. {
  133. EndGame("You have died from starvation!");
  134. return;
  135. }
  136. else
  137. Print("You are " + String(hungerLevels[hunger_]) + ".");
  138. }
  139. eatenLastTurn_ = false;
  140. if (foodAvailable_)
  141. {
  142. Print("The floating pieces of fish food are quickly eaten by other fish in the tank.");
  143. foodAvailable_ = false;
  144. }
  145. else if (Random() < 0.15f)
  146. {
  147. Print("The overhead dispenser drops pieces of delicious fish food to the water!");
  148. foodAvailable_ = true;
  149. }
  150. ++numTurns_;
  151. }
  152. void ConsoleInput::HandleInput(const String& input)
  153. {
  154. String inputLower = input.ToLower().Trimmed();
  155. if (inputLower.Empty())
  156. {
  157. Print("Empty input given!");
  158. return;
  159. }
  160. if (inputLower == "quit" || inputLower == "exit")
  161. engine_->Exit();
  162. else if (gameOn_)
  163. {
  164. // Game is on
  165. if (inputLower == "help")
  166. Print("The following commands are available: 'eat', 'hide', 'wait', 'score', 'quit'.");
  167. else if (inputLower == "score")
  168. Print("You have survived " + String(numTurns_) + " turns.");
  169. else if (inputLower == "eat")
  170. {
  171. if (foodAvailable_)
  172. {
  173. Print("You eat several pieces of fish food.");
  174. foodAvailable_ = false;
  175. eatenLastTurn_ = true;
  176. hunger_ -= (hunger_ > 3) ? 2 : 1;
  177. if (hunger_ < 0)
  178. {
  179. EndGame("You have killed yourself by over-eating!");
  180. return;
  181. }
  182. else
  183. Print("You are now " + String(hungerLevels[hunger_]) + ".");
  184. }
  185. else
  186. Print("There is no food available.");
  187. Advance();
  188. }
  189. else if (inputLower == "wait")
  190. {
  191. Print("Time passes...");
  192. Advance();
  193. }
  194. else if (inputLower == "hide")
  195. {
  196. if (urhoThreat_ > 0)
  197. {
  198. bool evadeSuccess = hunger_ > 2 || Random() < 0.5f;
  199. if (evadeSuccess)
  200. {
  201. Print("You hide behind the thick bottom vegetation, until Urho grows bored.");
  202. urhoThreat_ = -2;
  203. }
  204. else
  205. Print("Your movements are too slow; you are unable to hide from Urho.");
  206. }
  207. else
  208. Print("There is nothing to hide from.");
  209. Advance();
  210. }
  211. else
  212. Print("Cannot understand the input '" + input + "'.");
  213. }
  214. else
  215. {
  216. // Game is over, wait for (y)es or (n)o reply
  217. if (inputLower[0] == 'y')
  218. StartGame();
  219. else if (inputLower[0] == 'n')
  220. engine_->Exit();
  221. else
  222. Print("Please answer 'y' or 'n'.");
  223. }
  224. }
  225. void ConsoleInput::Print(const String& output)
  226. {
  227. // Logging appears both in the engine console and stdout
  228. URHO3D_LOGRAW(output + "\n");
  229. }