26_ConsoleInput.as 7.4 KB

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