tresinformal / drakkar

The tresinformal video game called 'Drakkar'
GNU General Public License v3.0
11 stars 4 forks source link

A player's state is changed by specific functions, rather than a setter #627

Open TheoPannetier opened 1 year ago

TheoPannetier commented 1 year ago

Context

So far, we use player::set_state() to change a player's states, e.g. to kill, revive or stun it. This is ok, but a more robust approach would be to change state only with a member function that takes care of the specific action.

This is particularly important if the logic of the game requires other things to happen when a state is changed, e.g. changing colour when a player dies or trigger a timer when the player is stun.

Test

{
      // (627) A player's state is changed by specific functions
    player p;
    p.stun();
    assert(p.get_state() == player_state::stunned);
    p.die();
    assert(p.get_state() == player_state::out);
    p.revive();
    assert(p.get_state() == player_state::active);
}
richelbilderbeek commented 1 year ago

@TheoPannetier I enjoy these tests! Consider adding this one, as (1) it assures const-correctness of player::get_state, (2) forces us to write down a default player_state.


{
      // (627) A player's state is changed by specific functions, by default a player is active
    const player p;
    assert(p.get_state() == player_state::active); //Or a better player_state
}
TheoPannetier commented 1 year ago

I took the liberty to add this change directly to develop 😇