ConsoleInput.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "Sample.h"
  24. /// Console input example.
  25. /// This sample demonstrates:
  26. /// - Implementing a crude text adventure game, which accepts input both through the engine console,
  27. /// and standard input.
  28. /// - Adding autocomplete options to the engine console.
  29. class ConsoleInput : public Sample
  30. {
  31. URHO3D_OBJECT(ConsoleInput, Sample);
  32. public:
  33. /// Construct.
  34. ConsoleInput(Context* context);
  35. /// Setup after engine initialization and before running the main loop.
  36. virtual void Start() override;
  37. protected:
  38. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  39. virtual String GetScreenJoystickPatchString() const override { return
  40. "<patch>"
  41. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">"
  42. " <attribute name=\"Is Visible\" value=\"false\" />"
  43. " </add>"
  44. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  45. " <attribute name=\"Is Visible\" value=\"false\" />"
  46. " </add>"
  47. "</patch>";
  48. }
  49. private:
  50. /// Handle console command event.
  51. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  52. /// Handle frame update event.
  53. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  54. /// Handle ESC key down event to quit the engine.
  55. void HandleEscKeyDown(StringHash eventType, VariantMap& eventData);
  56. /// Print intro message and initialize the game state.
  57. void StartGame();
  58. /// Print game over message.
  59. void EndGame(const String& message);
  60. /// Advance the game state.
  61. void Advance();
  62. /// Handle user input either from the engine console or standard input.
  63. void HandleInput(const String& input);
  64. /// Print text to the engine console and standard output.
  65. void Print(const String& output);
  66. /// Game on flag.
  67. bool gameOn_;
  68. /// Food dispensed flag.
  69. bool foodAvailable_;
  70. /// Whether ate on the previous turn.
  71. bool eatenLastTurn_;
  72. /// Number of turns survived.
  73. int numTurns_;
  74. /// Player's hunger level.
  75. int hunger_;
  76. /// Threat of Urho level.
  77. int urhoThreat_;
  78. };