lowenware / dotrix

A 3D engine with ECS and Vulkan renderer for Rust developers
https://dotrix.rs
MIT License
292 stars 11 forks source link

Add states stack #110

Closed voxelias closed 3 years ago

voxelias commented 3 years ago

This PR implements application states stack.

Developer can define rules on what states what systems should run. By default any system runs on any state.

States are identified by data types and can be structure with or without data:

struct MainState {
    name: String,
}

struct PauseState {
    name: String,
    handled: bool,
}

This will add rule to enable system only when MainState is active

.with(
    System::from(camera::control)
        .with(State::on::<MainState>())
)

There could be more rules in fact:

.with(
    System::from(player::control)
        .with(State::on::<MainState>())
        .with(State::on::<InventoryState>())
)

It is also possible to enable on all but some states:

.with(
    System::from(ui_main)
        .with(State::off::<PauseState>())
)

There is a new service State that implements API to control the state of application:

fn startup(
    mut state: Mut<State>,
) {
    // Push main state
    state.push(MainState {
        name: String::from("Main state")
    });
}

When you need, you can return to previous state:


fn system_where_you_pop_the_state (
    mut state: Mut<State>,
) {
   ....
    if exit_state {
        state.pop_any();
    }
}