Splitting the State

<
>
September 15, 2021

In further preparation of multiplayer I split the game’s state from a single Game type,

pub struct Game {
    event_queue: Vec<ClientEvent>,
    cursor_pos: [f64; 2],
    last_drag: [f64; 2],
    mouse_down: Option<[f64; 2]>,
    planet: Planet,
    bp: Blueprint,
    tech_tree: TechTree,
    ..
}

that contained both UI-specifc state (such as the cursor’s location) and the ‘core’ state of the game such as the Planet.
Into a core

pub struct CoreGame {
    event_queue: Vec<GameEvent>,
    planet: Planet,
    bp: Blueprint,
    tech_tree: TechTree,
    ...

and a ‘client’ wrapper:

pub struct ClientGame {
    core: CoreGame,
    event_queue: Vec<ClientEvent>,
    cursor_pos: [f64; 2],
    last_drag: [f64; 2],
    mouse_down: Option<[f64; 2]>,
    ...
}

As you might have noticed there’s now also a GameEvent instead of just a ClientEvent.
I applied a similar separation to the events:

pub enum ClientEvent {
    Game(GameEvent),
    SetCursor(Option<Structure>),
    RotateCursor,
    ...
}

This way there’s a proper separation between state that’s only relevant for the UI / single user and state that’s relevant to the actual game and might be shared between client and server.
Same for events that might only be handled by the client and events that should be sent to the server.