26_ConsoleInput.as 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Console input example.
  2. // This sample demonstrates:
  3. // - Implementing a crude text adventure game, which accepts input both through the engine console,
  4. // and standard input.
  5. // - Disabling default execution of console commands as immediate mode AngelScript.
  6. #include "Scripts/Utilities/Sample.as"
  7. bool gameOn;
  8. bool foodAvailable;
  9. bool eatenLastTurn;
  10. int numTurns;
  11. int hunger;
  12. int urhoThreat;
  13. // Hunger level descriptions
  14. String[] hungerLevels = {
  15. "bursting",
  16. "well-fed",
  17. "fed",
  18. "hungry",
  19. "very hungry",
  20. "starving"
  21. };
  22. // Urho threat level descriptions
  23. String[] urhoThreatLevels = {
  24. "Suddenly Urho appears from a dark corner of the fish tank",
  25. "Urho seems to have his eyes set on you",
  26. "Urho is homing in on you mercilessly"
  27. };
  28. void Start()
  29. {
  30. // Execute the common startup for samples
  31. SampleStart();
  32. // Subscribe to console commands and the frame update
  33. SubscribeToEvent("ConsoleCommand", "HandleConsoleCommand");
  34. SubscribeToEvent("Update", "HandleUpdate");
  35. // Subscribe key down event
  36. SubscribeToEvent("KeyDown", "HandleEscKeyDown");
  37. // Hide logo to make room for the console
  38. SetLogoVisible(false);
  39. // Show the console by default, make it large
  40. console.numRows = graphics.height / 16;
  41. console.visible = true;
  42. // Show OS mouse cursor
  43. input.mouseVisible = true;
  44. // Disable default execution of AngelScript from the console
  45. script.executeConsoleCommands = false;
  46. // Open the operating system console window (for stdin / stdout) if not open yet
  47. OpenConsoleWindow();
  48. // Initialize game and print the welcome message
  49. StartGame();
  50. // Randomize from system clock
  51. SetRandomSeed(time.systemTime);
  52. }
  53. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData)
  54. {
  55. HandleInput(eventData["Command"].GetString());
  56. }
  57. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  58. {
  59. // Check if there is input from stdin
  60. String input = GetConsoleInput();
  61. if (input.length > 0)
  62. HandleInput(input);
  63. }
  64. void HandleEscKeyDown(StringHash eventType, VariantMap& eventData)
  65. {
  66. // Unlike the other samples, exiting the engine when ESC is pressed instead of just closing the console
  67. if (eventData["Key"].GetInt() == KEY_ESC)
  68. engine.Exit();
  69. }
  70. void StartGame()
  71. {
  72. Print("Welcome to the Urho adventure game! You are the newest fish in the tank; your\n"
  73. "objective is to survive as long as possible. Beware of hunger and the merciless\n"
  74. "predator cichlid Urho, who appears from time to time. Evading Urho is easier\n"
  75. "with an empty stomach. Type 'help' for available commands.");
  76. gameOn = true;
  77. foodAvailable = false;
  78. eatenLastTurn = false;
  79. numTurns = 0;
  80. hunger = 2;
  81. urhoThreat = 0;
  82. }
  83. void EndGame(const String&in message)
  84. {
  85. Print(message);
  86. Print("Game over! You survived " + String(numTurns) + " turns.\n"
  87. "Do you want to play again (Y/N)?");
  88. gameOn = false;
  89. }
  90. void Advance()
  91. {
  92. if (urhoThreat > 0)
  93. {
  94. ++urhoThreat;
  95. if (urhoThreat > 3)
  96. {
  97. EndGame("Urho has eaten you!");
  98. return;
  99. }
  100. }
  101. else if (urhoThreat < 0)
  102. ++urhoThreat;
  103. if (urhoThreat == 0 && Random() < 0.2f)
  104. ++urhoThreat;
  105. if (urhoThreat > 0)
  106. Print(urhoThreatLevels[urhoThreat - 1] + ".");
  107. if ((numTurns & 3) == 0 && !eatenLastTurn)
  108. {
  109. ++hunger;
  110. if (hunger > 5)
  111. {
  112. EndGame("You have died from starvation!");
  113. return;
  114. }
  115. else
  116. Print("You are " + hungerLevels[hunger] + ".");
  117. }
  118. eatenLastTurn = false;
  119. if (foodAvailable)
  120. {
  121. Print("The floating pieces of fish food are quickly eaten by other fish in the tank.");
  122. foodAvailable = false;
  123. }
  124. else if (Random() < 0.15f)
  125. {
  126. Print("The overhead dispenser drops pieces of delicious fish food to the water!");
  127. foodAvailable = true;
  128. }
  129. ++numTurns;
  130. }
  131. void HandleInput(const String&in input)
  132. {
  133. String inputLower = input.ToLower().Trimmed();
  134. if (inputLower.empty)
  135. {
  136. Print("Empty input given!");
  137. return;
  138. }
  139. if (inputLower == "quit" || inputLower == "exit")
  140. engine.Exit();
  141. else if (gameOn)
  142. {
  143. // Game is on
  144. if (inputLower == "help")
  145. Print("The following commands are available: 'eat', 'hide', 'wait', 'score', 'quit'.");
  146. else if (inputLower == "score")
  147. Print("You have survived " + String(numTurns) + " turns.");
  148. else if (inputLower == "eat")
  149. {
  150. if (foodAvailable)
  151. {
  152. Print("You eat several pieces of fish food.");
  153. foodAvailable = false;
  154. eatenLastTurn = true;
  155. hunger -= (hunger > 3) ? 2 : 1;
  156. if (hunger < 0)
  157. {
  158. EndGame("You have killed yourself by over-eating!");
  159. return;
  160. }
  161. else
  162. Print("You are now " + hungerLevels[hunger] + ".");
  163. }
  164. else
  165. Print("There is no food available.");
  166. Advance();
  167. }
  168. else if (inputLower == "wait")
  169. {
  170. Print("Time passes...");
  171. Advance();
  172. }
  173. else if (inputLower == "hide")
  174. {
  175. if (urhoThreat > 0)
  176. {
  177. bool evadeSuccess = hunger > 2 || Random() < 0.5f;
  178. if (evadeSuccess)
  179. {
  180. Print("You hide behind the thick bottom vegetation, until Urho grows bored.");
  181. urhoThreat = -2;
  182. }
  183. else
  184. Print("Your movements are too slow; you are unable to hide from Urho.");
  185. }
  186. else
  187. Print("There is nothing to hide from.");
  188. Advance();
  189. }
  190. else
  191. Print("Cannot understand the input '" + input + "'.");
  192. }
  193. else
  194. {
  195. // Game is over, wait for (y)es or (n)o reply
  196. if (inputLower[0] == 'y')
  197. StartGame();
  198. else if (inputLower[0] == 'n')
  199. engine.Exit();
  200. else
  201. Print("Please answer 'y' or 'n'.");
  202. }
  203. }