26_ConsoleInput.as 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // Disable default execution of AngelScript from the console
  33. script.executeConsoleCommands = false;
  34. // Subscribe to console commands and the frame update
  35. SubscribeToEvent("ConsoleCommand", "HandleConsoleCommand");
  36. SubscribeToEvent("Update", "HandleUpdate");
  37. // Subscribe key down event
  38. SubscribeToEvent("KeyDown", "HandleEscKeyDown");
  39. // Hide logo to make room for the console
  40. SetLogoVisible(false);
  41. // Show the console by default, make it large
  42. console.numRows = graphics.height / 16;
  43. console.numBufferedRows = 2 * console.numRows;
  44. console.commandInterpreter = "ScriptEventInvoker";
  45. console.visible = true;
  46. console.closeButton.visible = false;
  47. // Show OS mouse cursor
  48. input.mouseVisible = true;
  49. // Open the operating system console window (for stdin / stdout) if not open yet
  50. // Do not open in fullscreen, as this would cause constant device loss
  51. if (!graphics.fullscreen)
  52. OpenConsoleWindow();
  53. // Initialize game and print the welcome message
  54. StartGame();
  55. // Randomize from system clock
  56. SetRandomSeed(time.systemTime);
  57. }
  58. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData)
  59. {
  60. if (eventData["Id"].GetString() == "ScriptEventInvoker")
  61. HandleInput(eventData["Command"].GetString());
  62. }
  63. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  64. {
  65. // Check if there is input from stdin
  66. String input = GetConsoleInput();
  67. if (input.length > 0)
  68. HandleInput(input);
  69. }
  70. void HandleEscKeyDown(StringHash eventType, VariantMap& eventData)
  71. {
  72. // Unlike the other samples, exiting the engine when ESC is pressed instead of just closing the console
  73. if (eventData["Key"].GetInt() == KEY_ESC)
  74. engine.Exit();
  75. }
  76. void StartGame()
  77. {
  78. Print("Welcome to the Urho adventure game! You are the newest fish in the tank; your\n"
  79. "objective is to survive as long as possible. Beware of hunger and the merciless\n"
  80. "predator cichlid Urho, who appears from time to time. Evading Urho is easier\n"
  81. "with an empty stomach. Type 'help' for available commands.");
  82. gameOn = true;
  83. foodAvailable = false;
  84. eatenLastTurn = false;
  85. numTurns = 0;
  86. hunger = 2;
  87. urhoThreat = 0;
  88. }
  89. void EndGame(const String&in message)
  90. {
  91. Print(message);
  92. Print("Game over! You survived " + String(numTurns) + " turns.\n"
  93. "Do you want to play again (Y/N)?");
  94. gameOn = false;
  95. }
  96. void Advance()
  97. {
  98. if (urhoThreat > 0)
  99. {
  100. ++urhoThreat;
  101. if (urhoThreat > 3)
  102. {
  103. EndGame("Urho has eaten you!");
  104. return;
  105. }
  106. }
  107. else if (urhoThreat < 0)
  108. ++urhoThreat;
  109. if (urhoThreat == 0 && Random() < 0.2f)
  110. ++urhoThreat;
  111. if (urhoThreat > 0)
  112. Print(urhoThreatLevels[urhoThreat - 1] + ".");
  113. if ((numTurns & 3) == 0 && !eatenLastTurn)
  114. {
  115. ++hunger;
  116. if (hunger > 5)
  117. {
  118. EndGame("You have died from starvation!");
  119. return;
  120. }
  121. else
  122. Print("You are " + hungerLevels[hunger] + ".");
  123. }
  124. eatenLastTurn = false;
  125. if (foodAvailable)
  126. {
  127. Print("The floating pieces of fish food are quickly eaten by other fish in the tank.");
  128. foodAvailable = false;
  129. }
  130. else if (Random() < 0.15f)
  131. {
  132. Print("The overhead dispenser drops pieces of delicious fish food to the water!");
  133. foodAvailable = true;
  134. }
  135. ++numTurns;
  136. }
  137. void HandleInput(const String&in input)
  138. {
  139. String inputLower = input.ToLower().Trimmed();
  140. if (inputLower.empty)
  141. {
  142. Print("Empty input given!");
  143. return;
  144. }
  145. if (inputLower == "quit" || inputLower == "exit")
  146. engine.Exit();
  147. else if (gameOn)
  148. {
  149. // Game is on
  150. if (inputLower == "help")
  151. Print("The following commands are available: 'eat', 'hide', 'wait', 'score', 'quit'.");
  152. else if (inputLower == "score")
  153. Print("You have survived " + String(numTurns) + " turns.");
  154. else if (inputLower == "eat")
  155. {
  156. if (foodAvailable)
  157. {
  158. Print("You eat several pieces of fish food.");
  159. foodAvailable = false;
  160. eatenLastTurn = true;
  161. hunger -= (hunger > 3) ? 2 : 1;
  162. if (hunger < 0)
  163. {
  164. EndGame("You have killed yourself by over-eating!");
  165. return;
  166. }
  167. else
  168. Print("You are now " + hungerLevels[hunger] + ".");
  169. }
  170. else
  171. Print("There is no food available.");
  172. Advance();
  173. }
  174. else if (inputLower == "wait")
  175. {
  176. Print("Time passes...");
  177. Advance();
  178. }
  179. else if (inputLower == "hide")
  180. {
  181. if (urhoThreat > 0)
  182. {
  183. bool evadeSuccess = hunger > 2 || Random() < 0.5f;
  184. if (evadeSuccess)
  185. {
  186. Print("You hide behind the thick bottom vegetation, until Urho grows bored.");
  187. urhoThreat = -2;
  188. }
  189. else
  190. Print("Your movements are too slow; you are unable to hide from Urho.");
  191. }
  192. else
  193. Print("There is nothing to hide from.");
  194. Advance();
  195. }
  196. else
  197. Print("Cannot understand the input '" + input + "'.");
  198. }
  199. else
  200. {
  201. // Game is over, wait for (y)es or (n)o reply
  202. if (inputLower[0] == 'y')
  203. StartGame();
  204. else if (inputLower[0] == 'n')
  205. engine.Exit();
  206. else
  207. Print("Please answer 'y' or 'n'.");
  208. }
  209. }
  210. // Create XML patch instructions for screen joystick layout specific to this sample app
  211. String patchInstructions =
  212. "<patch>" +
  213. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">" +
  214. " <attribute name=\"Is Visible\" value=\"false\" />" +
  215. " </add>" +
  216. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  217. " <attribute name=\"Is Visible\" value=\"false\" />" +
  218. " </add>" +
  219. "</patch>";