Macro impl_game_object

Source
macro_rules! impl_game_object {
    (/push_info $infos:ident/ $field_type:ty) => { ... };
    (/push_info $infos:ident/ $field_type:ty, $($field_types:ty),+) => { ... };
    (/push_component $components:ident, $self:ident/ $field_name:ident: $field_type:ty) => { ... };
    (/push_component $components:ident, $self:ident/ $field_name:ident: $field_type:ty, $($field_names:ident: $field_types:ty),+) => { ... };
    (impl GameObject for $struct_name:ident using components {
        $($field_names:ident: $field_types:ty),+$(,)?
    }) => { ... };
}
Expand description

Generates a GameObject impl block for a type.

This takes a list of the struct’s field names and types to be used as the components of this game object. Note that component types must be bytemuck::Pod.

The using components part is intended to signal that it’s not a regular impl block, taking a list of field names and types similar to a struct definition, instead of trait function implementations.

§Example

use engine::impl_game_object;

// NOTE: Zeroable and Pod are manually implemented here to avoid
// the engine depending on proc macros. They should generally be
// derived, if compile times allow, as Pod has a lot of
// requirements that are easy to forget.

#[derive(Debug, Clone, Copy)]
#[repr(C)]
struct Position { pub x: i32, pub y: i32 }
unsafe impl bytemuck::Zeroable for Position {}
unsafe impl bytemuck::Pod for Position {}

#[derive(Debug, Clone, Copy)]
#[repr(C)]
struct Velocity { pub x: i32, pub y: i32 }
unsafe impl bytemuck::Zeroable for Velocity {}
unsafe impl bytemuck::Pod for Velocity {}

#[derive(Debug)]
struct Foo {
    pub position: Position,
    pub velocity: Velocity,
}

impl_game_object! {
    impl GameObject for Foo using components {
        position: Position,
        velocity: Velocity,
    }
}

For a more fully featured example for using these game objects, see the documentation for Scene.