R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
game_instance.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/ecs/components.hpp>
5#include <aegis2d/ecs/entity.hpp>
6#include <aegis2d/ecs/waves.hpp>
7
9#include <rtype/tuning.hpp>
10
11#include <array>
12#include <cstddef>
13#include <cstdint>
14#include <memory>
15#include <optional>
16
17namespace rtype {
18
22 std::optional<aegis2d::ecs::Entity> ship;
24 std::uint32_t lives = 0;
26 std::uint32_t score = 0;
28 std::optional<std::uint16_t> input;
29};
30
38public:
43 GameInstance(std::optional<std::uint64_t> seed, aegis2d::ecs::WaveSchedule waves,
44 GameRules rules = {});
45
51 aegis2d::ecs::Entity join(std::uint8_t player);
52
56 bool leave(std::uint8_t player);
57
66 bool set_input(std::uint8_t player, aegis2d::ecs::PlayerInput input, std::uint16_t sequence);
67
73 bool set_rewind(std::uint8_t player, std::uint32_t ticks);
74
78 [[nodiscard]] std::optional<aegis2d::ecs::Entity> ship(std::uint8_t player) const;
79
85 [[nodiscard]] std::optional<PlayerState> player_state(std::uint8_t player);
86
89 [[nodiscard]] std::size_t players() const noexcept;
90
94 [[nodiscard]] bool take_game_over();
95
98 [[nodiscard]] std::uint64_t games() const noexcept;
99
102 [[nodiscard]] ServerGame& game() noexcept;
103
105 [[nodiscard]] const ServerGame& game() const noexcept;
106
107private:
108 void start();
109
110 std::optional<std::uint64_t> seed_;
111 aegis2d::ecs::WaveSchedule waves_;
112 GameRules rules_;
113 // ServerGame cannot move: each game is a new object.
114 std::unique_ptr<ServerGame> game_;
115 std::array<std::optional<aegis2d::ecs::Entity>, tuning::max_players> ships_;
116 std::array<std::uint32_t, tuning::max_players> scores_{};
117 std::array<std::optional<std::uint16_t>, tuning::max_players> inputs_{};
118 std::uint64_t games_ = 0;
119 bool over_told_ = false;
120};
121
122} // namespace rtype
The one game of the server, with up to tuning::max_players players.
Definition game_instance.hpp:37
std::size_t players() const noexcept
Players in the game, out of lives or not.
std::uint64_t games() const noexcept
Number of games played: 1, then 1 more each time the last player leaves.
std::optional< aegis2d::ecs::Entity > ship(std::uint8_t player) const
Ship of a player.
GameInstance(std::optional< std::uint64_t > seed, aegis2d::ecs::WaveSchedule waves, GameRules rules={})
Creates an empty game.
ServerGame & game() noexcept
The game being played.
bool take_game_over()
Tells once per game that it is over: every player who joined is out.
bool leave(std::uint8_t player)
Removes a player from the game; its ship disappears.
bool set_input(std::uint8_t player, aegis2d::ecs::PlayerInput input, std::uint16_t sequence)
Sets the actions a player holds, applied at every tick until the next call.
bool set_rewind(std::uint8_t player, std::uint32_t ticks)
Sets how many ticks back a player's missiles meet the enemies (lag compensation, ServerGame::set_rewi...
aegis2d::ecs::Entity join(std::uint8_t player)
Adds the ship of a player to the game.
std::optional< PlayerState > player_state(std::uint8_t player)
State of a player.
One R-Type game on the server: the world, its systems in order, and the players.
Definition server_game.hpp:75
constexpr std::size_t max_players
Players in one game, as the subject requires.
Definition tuning.hpp:33
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
The R-Type simulation run by the server: world, systems and players.
How a game plays: the rules of its room, as the gameplay reads them.
Definition server_game.hpp:35
What the game says of a player, for the snapshots.
Definition game_instance.hpp:20
std::optional< std::uint16_t > input
Sequence of the last input applied to its ship, or nothing before the first.
Definition game_instance.hpp:28
std::uint32_t score
Score; once out, the last score of its ship.
Definition game_instance.hpp:26
std::uint32_t lives
Lives left, 0 once out.
Definition game_instance.hpp:24
std::optional< aegis2d::ecs::Entity > ship
Its ship, or std::nullopt once out of lives.
Definition game_instance.hpp:22
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.