ConsoleInput.cpp 8.5 KB

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