R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::GameInstance Class Reference

The one game of the server, with up to tuning::max_players players. More...

#include </__w/project-rtype/project-rtype/server/include/rtype/game_instance.hpp>

Public Member Functions

 GameInstance (std::optional< std::uint64_t > seed, aegis2d::ecs::WaveSchedule waves, GameRules rules={})
 Creates an empty game.
 
aegis2d::ecs::Entity join (std::uint8_t player)
 Adds the ship of a player to the game.
 
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_rewind()).
 
std::optional< aegis2d::ecs::Entity > ship (std::uint8_t player) const
 Ship of a player.
 
std::optional< PlayerState > player_state (std::uint8_t player)
 State of a player.
 
std::size_t players () const noexcept
 Players in the game, out of lives or not.
 
bool take_game_over ()
 Tells once per game that it is over: every player who joined is out.
 
std::uint64_t games () const noexcept
 Number of games played: 1, then 1 more each time the last player leaves.
 
ServerGame & game () noexcept
 The game being played.
 
const ServerGame & game () const noexcept
 The game being played.
 

Detailed Description

The one game of the server, with up to tuning::max_players players.

A session joins when its UDP endpoint is bound (RFC section 3.4) and leaves when it ends. Each player gets the ship of its player number, the player of LoginAccepted, so the number picks its ship and spawn point. When the last player leaves, the instance starts a fresh game with the same seed, waves and rules, for the next players. Not thread-safe: use it from one thread.

Constructor & Destructor Documentation

◆ GameInstance()

rtype::GameInstance::GameInstance ( std::optional< std::uint64_t >  seed,
aegis2d::ecs::WaveSchedule  waves,
GameRules  rules = {} 
)

Creates an empty game.

Parameters
seedSeed of the random spawns, or std::nullopt for none (see ServerGame).
wavesWaves to play in every game, at their start.
rulesHow every game plays (the rules of the room).

Member Function Documentation

◆ game() [1/2]

const ServerGame & rtype::GameInstance::game ( ) const
noexcept

The game being played.

Returns
The current game; another one after the last player leaves.

◆ game() [2/2]

ServerGame & rtype::GameInstance::game ( )
noexcept

The game being played.

Returns
The current game; another one after the last player leaves.

◆ games()

std::uint64_t rtype::GameInstance::games ( ) const
noexcept

Number of games played: 1, then 1 more each time the last player leaves.

Returns
The count since the construction.

◆ join()

aegis2d::ecs::Entity rtype::GameInstance::join ( std::uint8_t  player)

Adds the ship of a player to the game.

Parameters
playerPlayer number of the session, below tuning::max_players.
Returns
The ship.
Exceptions
std::out_of_rangeIf player is not below tuning::max_players.
std::logic_errorIf the player is in the game already.

◆ leave()

bool rtype::GameInstance::leave ( std::uint8_t  player)

Removes a player from the game; its ship disappears.

Parameters
playerPlayer number of the session.
Returns
false if the player was not in the game.

◆ player_state()

std::optional< PlayerState > rtype::GameInstance::player_state ( std::uint8_t  player)

State of a player.

Remembers the score of each living ship, which the ship takes with it when it is destroyed, so a player out of lives keeps its score.

Parameters
playerPlayer number.
Returns
Its ship, lives and score, or std::nullopt if it is not in the game.

◆ players()

std::size_t rtype::GameInstance::players ( ) const
noexcept

Players in the game, out of lives or not.

Returns
0 to tuning::max_players.

◆ set_input()

bool rtype::GameInstance::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.

The sequence of the input is kept for the snapshots (PlayerState::input), so the client knows which of its inputs the server applied (RFC section 4.3).

Parameters
playerPlayer number of the session.
inputHeld actions, from the player's most recent Input.
sequenceSequence of that Input (InputRecord::sequence).
Returns
false if the player is not in the game or is out of lives; the input and its sequence are then ignored.

◆ set_rewind()

bool rtype::GameInstance::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_rewind()).

Parameters
playerPlayer number of the session.
ticksrewind_ticks() of its round trip time.
Returns
false if the player is not in the game or is out of lives.

◆ ship()

std::optional< aegis2d::ecs::Entity > rtype::GameInstance::ship ( std::uint8_t  player) const

Ship of a player.

Parameters
playerPlayer number.
Returns
The ship, or std::nullopt if the player is not in the game or is out.

◆ take_game_over()

bool rtype::GameInstance::take_game_over ( )

Tells once per game that it is over: every player who joined is out.

Returns
true the first time it is called once the current game is over, false before and after.

The documentation for this class was generated from the following file: