#component #group #specs #ecs #gamedev

component_group

Defines the ComponentGroup trait for managing a group of specs::Component instances and moving them between specs::Worlds

3 stable releases

3.0.0 Dec 24, 2020
2.0.0 Dec 24, 2019
1.0.0 Dec 23, 2018

#654 in Game dev

22 downloads per month

MPL-2.0 license

37KB

component_group

Crates.io Docs.rs Build Status Say Thanks!

This crate defines the ComponentGroup trait. This trait is used to make managing a group of specs::Component instances easier. This is useful for when you have several components that are often created, read, and updated together. You can use this trait to easily move an entire group of components between instances of specs::World.

Instead of having you keep duplicate code in sync across your application, this trait groups all of that logic in one place so you can focus and minimize the changes you need to make every time you add a component to the group.

The ComponentGroup trait can be automatically derived if each component in the group implements the Clone trait. This removes any of the boilerplate you may have needed to write in order to implement the trait yourself.

See the documentation for more details about the motivations for creating this trait and how to use it.

// Don't forget to add the component_group crate to your Cargo.toml file!
use component_group::ComponentGroup;

use specs::{World, Component, VecStorage, HashMapStorage};
use specs_derive::Component;

// These components are just for demonstration purposes. You should swap them
// out for your own. Components need to be Clone to use the automatic derive.

#[derive(Debug, Clone, Component)]
#[storage(VecStorage)]
pub struct Position {x: i32, y: i32}

#[derive(Debug, Clone, Component)]
#[storage(VecStorage)]
pub struct Velocity {x: i32, y: i32}

#[derive(Debug, Clone, Component)]
#[storage(VecStorage)]
pub struct Health(u32);

#[derive(Debug, Clone, Component)]
#[storage(HashMapStorage)]
pub struct Animation {frame: usize}

// This is all of the code you need to write to define the group and its operations!
#[derive(ComponentGroup)]
struct PlayerComponents {
    position: Position,
    velocity: Velocity,
    health: Health,
    // This optional component is allowed to not be present
    animation: Option<Animation>,
}

// Now you can easily add all of these components to an entity, load them all
// from the world, or even update them all at once!

Dependencies

~4.5MB
~84K SLoC