ConsoleInput.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "Sample.h"
  5. /// Console input example.
  6. /// This sample demonstrates:
  7. /// - Implementing a crude text adventure game, which accepts input both through the engine console,
  8. /// and standard input.
  9. /// - Adding autocomplete options to the engine console.
  10. class ConsoleInput : public Sample
  11. {
  12. URHO3D_OBJECT(ConsoleInput, Sample);
  13. public:
  14. /// Construct.
  15. explicit ConsoleInput(Context* context);
  16. /// Setup after engine initialization and before running the main loop.
  17. void Start() override;
  18. protected:
  19. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  20. String GetScreenJoystickPatchString() const override { return
  21. "<patch>"
  22. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">"
  23. " <attribute name=\"Is Visible\" value=\"false\" />"
  24. " </add>"
  25. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  26. " <attribute name=\"Is Visible\" value=\"false\" />"
  27. " </add>"
  28. "</patch>";
  29. }
  30. private:
  31. /// Handle console command event.
  32. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  33. /// Handle frame update event.
  34. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  35. /// Handle ESC key down event to quit the engine.
  36. void HandleEscKeyDown(StringHash eventType, VariantMap& eventData);
  37. /// Print intro message and initialize the game state.
  38. void StartGame();
  39. /// Print game over message.
  40. void EndGame(const String& message);
  41. /// Advance the game state.
  42. void Advance();
  43. /// Handle user input either from the engine console or standard input.
  44. void HandleInput(const String& input);
  45. /// Print text to the engine console and standard output.
  46. void Print(const String& output);
  47. /// Game on flag.
  48. bool gameOn_{};
  49. /// Food dispensed flag.
  50. bool foodAvailable_{};
  51. /// Whether ate on the previous turn.
  52. bool eatenLastTurn_{};
  53. /// Number of turns survived.
  54. unsigned numTurns_{};
  55. /// Player's hunger level.
  56. int hunger_{};
  57. /// Threat of Urho level.
  58. int urhoThreat_{};
  59. };