ConsoleInput.cpp 8.2 KB

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