[][src]Trait coffee::Game

pub trait Game {
type View;
type Input;

const TICKS_PER_SECOND: u16;
const DEBUG_KEY: Option<KeyCode>;

    fn new(window: &mut Window) -> Result<(Self, Self::View, Self::Input)>
    where
        Self: Sized
;
fn update(&mut self, view: &Self::View, window: &Window);
fn draw(&self, view: &mut Self::View, window: &mut Window, timer: &Timer); fn on_input(&self, _input: &mut Self::Input, _event: Event) { ... }
fn on_close_request(&self, _input: &mut Self::Input) -> bool { ... }
fn interact(
        &mut self,
        _input: &mut Self::Input,
        _view: &mut Self::View,
        _gpu: &mut Gpu
    ) { ... }
fn debug(
        &self,
        _input: &Self::Input,
        _view: &Self::View,
        window: &mut Window,
        debug: &mut Debug
    ) { ... }
fn run(window_settings: WindowSettings) -> Result<()>
    where
        Self: Sized
, { ... } }

The entrypoint of the engine. It describes your game logic.

Implementors of this trait should hold the game state.

Coffee forces you to decouple your game state from your view and input state. While this might seem limiting at first, it helps you to keep mutability at bay and forces you to think about the architecture of your game.

Ideally, your game state should be an opaque type with a meaningful API with clear boundaries. External code (like draw code or input code) should rely on this API to do its job.

Associated Types

type View

The view data of your game.

This type should hold all the assets and state necessary to render your game and UI.

type Input

The input data of your game.

For instance, you could start by simply using a HashSet here to track which keys are pressed at any given time.

Loading content...

Associated Constants

const TICKS_PER_SECOND: u16

Defines how many times the update function should be called per second.

A common value is 60.

const DEBUG_KEY: Option<KeyCode>

Defines the key that will be used to toggle the debug view. Set it to None if you want to disable it.

By default, it is set to F12.

Loading content...

Required methods

fn new(window: &mut Window) -> Result<(Self, Self::View, Self::Input)> where
    Self: Sized

Create your game here.

You need to return your initial game state, view state, and input state.

It is recommended to load your game assets right here. You can use the load module to declaratively describe how to load your assets and get a consistent loading screen for free!

fn update(&mut self, view: &Self::View, window: &Window)

Update your game state here.

The TICKS_PER_SECOND constant defines how many times this function will be called per second. This function may be called multiple times per frame if it is necessary.

Notice that you are also allowed to access view and window data. This can be useful if your game state needs to know how much of the world is visible.

fn draw(&self, view: &mut Self::View, window: &mut Window, timer: &Timer)

Draw your game here.

Check out the graphics module to learn more about rendering in Coffee.

This function will be called once per frame.

Loading content...

Provided methods

fn on_input(&self, _input: &mut Self::Input, _event: Event)

Process an input event and keep track of it in your Input type.

This function may be called multiple times during event processing, before interact.

By default, it does nothing.

fn on_close_request(&self, _input: &mut Self::Input) -> bool

Handle a close request from the operating system to the game window.

This function should return true to allow the game loop to end, otherwise false.

By default, it does nothing and returns true.

fn interact(
    &mut self,
    _input: &mut Self::Input,
    _view: &mut Self::View,
    _gpu: &mut Gpu
)

Consume your Input to let users interact with your game.

Right before an update, input events will be processed and this function will be called. This reduces latency when multiple updates need to happen during a single frame.

If no update is needed during a frame, it will still be called once, right after processing input events and before drawing. This allows you to keep your view updated every frame in order to offer a smooth user experience independently of the TICKS_PER_SECOND setting.

You can access the GPU if, as a consequence of the interaction, you need to prepare some assets before rendering.

By default, it does nothing.

fn debug(
    &self,
    _input: &Self::Input,
    _view: &Self::View,
    window: &mut Window,
    debug: &mut Debug
)

Implement this function to display debug information.

It is called after draw once per frame when debug has been toggled using the DEBUG_KEY. Anything you draw here will be on top. Debug code is only called when compiling with debug_assertions or the debug feature enabled.

By default, it shows Debug, which displays a brief summary about game performance in the top left corner.

fn run(window_settings: WindowSettings) -> Result<()> where
    Self: Sized

Runs the Game with the given WindowSettings.

Loading content...

Implementors

Loading content...