game_session.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_SESSION_HPP
  17. #define HEADER_SUPERTUX_SUPERTUX_GAME_SESSION_HPP
  18. #include "supertux/screen.hpp"
  19. #include "util/currenton.hpp"
  20. #include <memory>
  21. #include <unordered_map>
  22. #include <vector>
  23. #include <squirrel.h>
  24. #include "math/vector.hpp"
  25. #include "squirrel/squirrel_scheduler.hpp"
  26. #include "squirrel/squirrel_util.hpp"
  27. #include "supertux/game_object.hpp"
  28. #include "supertux/player_status.hpp"
  29. #include "supertux/screen_fade.hpp"
  30. #include "supertux/sequence.hpp"
  31. #include "supertux/timer.hpp"
  32. #include "video/surface_ptr.hpp"
  33. class CodeController;
  34. class DrawingContext;
  35. class EndSequence;
  36. class Level;
  37. class Player;
  38. class Sector;
  39. class Statistics;
  40. class Savegame;
  41. /** Screen that runs a Level, where Players run and jump through Sectors. */
  42. class GameSession final : public Screen,
  43. public Currenton<GameSession>
  44. {
  45. private:
  46. struct SpawnPoint
  47. {
  48. /* If a spawnpoint is set, the spawn position shall not, and vice versa. */
  49. SpawnPoint(const std::string& sector_,
  50. const Vector& position_,
  51. bool is_checkpoint_ = false) :
  52. sector(sector_),
  53. spawnpoint(),
  54. position(position_),
  55. is_checkpoint(is_checkpoint_)
  56. {}
  57. SpawnPoint(const std::string& sector_,
  58. const std::string& spawnpoint_,
  59. bool is_checkpoint_ = false) :
  60. sector(sector_),
  61. spawnpoint(spawnpoint_),
  62. position(),
  63. is_checkpoint(is_checkpoint_)
  64. {}
  65. std::string sector;
  66. std::string spawnpoint;
  67. Vector position;
  68. bool is_checkpoint;
  69. };
  70. public:
  71. GameSession(const std::string& levelfile, Savegame& savegame, Statistics* statistics = nullptr,
  72. bool preserve_music = false);
  73. virtual void draw(Compositor& compositor) override;
  74. virtual void update(float dt_sec, const Controller& controller) override;
  75. virtual void setup() override;
  76. virtual void leave() override;
  77. virtual IntegrationStatus get_status() const override;
  78. /** ends the current level */
  79. void finish(bool win = true);
  80. void respawn(const std::string& sectorname, const std::string& spawnpointname);
  81. void respawn_with_fade(const std::string& sectorname,
  82. const std::string& spawnpointname,
  83. const ScreenFade::FadeType fade_type,
  84. const Vector &fade_point,
  85. const bool make_invincible = false);
  86. void reset_level();
  87. void set_start_point(const std::string& sectorname,
  88. const std::string& spawnpointname);
  89. void set_start_pos(const std::string& sectorname, const Vector& pos);
  90. void set_respawn_point(const std::string& sectorname,
  91. const std::string& spawnpointname);
  92. void set_respawn_pos(const std::string& sectorname, const Vector& pos);
  93. void clear_respawn_points();
  94. const SpawnPoint& get_last_spawnpoint() const;
  95. void set_checkpoint_pos(const std::string& sectorname, const Vector& pos);
  96. const SpawnPoint* get_active_checkpoint_spawnpoint() const;
  97. Sector& get_current_sector() const { return *m_currentsector; }
  98. Level& get_current_level() const { return *m_level; }
  99. void start_sequence(Player* caller, Sequence seq, const SequenceData* data = nullptr);
  100. void set_target_timer_paused(bool paused);
  101. /**
  102. * returns the "working directory" usually this is the directory where the
  103. * currently played level resides. This is used when locating additional
  104. * resources for the current level/world
  105. */
  106. std::string get_working_directory() const;
  107. std::string get_level_file() const { return m_levelfile; }
  108. bool has_active_sequence() const;
  109. int restart_level(bool after_death = false, bool preserve_music = false);
  110. void toggle_pause();
  111. void abort_level();
  112. bool is_active() const;
  113. Savegame& get_savegame() const { return m_savegame; }
  114. void set_scheduler(SquirrelScheduler& new_scheduler);
  115. private:
  116. void check_end_conditions();
  117. void drawstatus(DrawingContext& context);
  118. void draw_pause(DrawingContext& context);
  119. void on_escape_press(bool force_quick_respawn);
  120. Vector get_fade_point() const;
  121. Vector get_fade_point(const Vector& position) const;
  122. public:
  123. bool reset_button;
  124. bool reset_checkpoint_button;
  125. bool m_prevent_death; /**< true if players should enter ghost mode instead of dying */
  126. private:
  127. std::unique_ptr<Level> m_level;
  128. SurfacePtr m_statistics_backdrop;
  129. // scripts
  130. SquirrelObjectList m_scripts;
  131. Sector* m_currentsector;
  132. EndSequence* m_end_sequence;
  133. bool m_game_pause;
  134. float m_speed_before_pause;
  135. std::string m_levelfile;
  136. // Spawnpoints
  137. std::vector<SpawnPoint> m_spawnpoints;
  138. const SpawnPoint* m_activated_checkpoint;
  139. // the sector and spawnpoint we should spawn after this frame
  140. std::string m_newsector;
  141. std::string m_newspawnpoint;
  142. ScreenFade::FadeType m_spawn_fade_type;
  143. Timer m_spawn_fade_timer;
  144. bool m_spawn_with_invincibility;
  145. Statistics* m_best_level_statistics;
  146. Savegame& m_savegame;
  147. // Note: m_play_time should reset when a level is restarted from the beginning
  148. // but NOT if Tux respawns at a checkpoint (for LevelTimes to work)
  149. float m_play_time; /**< total time in seconds that this session ran interactively */
  150. bool m_levelintro_shown; /**< true if the LevelIntro screen was already shown */
  151. int m_coins_at_start; /** How many coins does the player have at the start */
  152. std::vector<BonusType> m_boni_at_start; /** What boni does the player have at the start */
  153. std::vector<int> m_max_fire_bullets_at_start; /** How many fire bullets does the player have */
  154. std::vector<int> m_max_ice_bullets_at_start; /** How many ice bullets does the player have */
  155. bool m_active; /** Game active? **/
  156. bool m_end_seq_started;
  157. bool m_pause_target_timer;
  158. std::unique_ptr<GameObject> m_current_cutscene_text;
  159. Timer m_endsequence_timer;
  160. private:
  161. GameSession(const GameSession&) = delete;
  162. GameSession& operator=(const GameSession&) = delete;
  163. };
  164. #endif
  165. /* EOF */