A simple entity-component-system crate for rust with serialization support

Overview

Gallium

A simple entity-component-system crate for rust with serialization support

Usage

You can include the library using carge:

[dependencies]
gallium_ecs = "0.1.1"
gallium_ecs_derive = "0.1.0"

Components

Components are simple structs that can de defined using the component attribute. Note that all the data a component uses needs to be serializable by serde

#[gallium::component]
pub struct ExampleComponent {
  data_1: i32,
  data_2: String,
}

Entities

Entities can be build in two ways, the builder method or just by using the functions.

// The function method
let mut func_entity = Entity::new();
// Add a component to the entity
func_entity.add_component(ExampleComponent {});

// The builder method
let build_entity = EntityBuilder::new()
// Add a component to the entity
.with(ExampleComponent {})
.build();

You can fetch components in an entity like so:

// Returns a RefMut
let example_component = entity.get_component::().unwrap();

Systems

Systems are serializable structs that implement the system trait, implementation of system requires the usage of the system attribute for simplicity.

The tick function gets called everytime the systems with the specified tag get ticked.

The on_event function gets called everytime an event gets dispatched into the scene.

#[derive(Serialize, Deserialize)]
pub struct ExampleSystem

#[gallium::system]
impl System for ExampleSystem {
  fn tick(&mut self, _scene: &Scene) {
    // System tick code here
  }
  
  fn on_event(&self, _scene: &Scene, _tag: &str, _data: &mut dyn std::any::Any) {
    // Event handling here
  }
}

Scenes

Scenes are serializable objects that hold entities and systems to be run. Just like entites, these can be created using the builder method or using the functions.

// The function method
let mut func_scene = Scene::new();
// Add an entity to the scene
func_scene.add_entity(entity);
func_scene.add_system("init", ExampleSystem {});

// The builder method
let build_entity = SystemBuilder::new()
.with_entity(entity)
.with_system("init", ExampleSystem {})
.build();

Systems

You can tick the systems in the scenes like so:

let mut scene = SceneBuilder::new()
// Add a system to the scene
.with_system("init", ExampleSystem {})
.build();

// Tick all systems with the 'init' tag
scene.tick_systems("init");

you can also dispatch events over all the systems in the scene:

let mut scene = SceneBuilder::new()
// Add a system to the scene
.with_system("init", ExampleSystem {})
.build();

// Dispatch the event
let example_data = ... // Any type can be dispatched as data
scene.dispatch_event("example_event", &example_data); // The data has to be dispatched as a reference

Entities

To fetch a specific entity, you need to use its id:

let entity = scene.get_entity(0).unwrap(); // Returns a RefMut

To fetch a list of entity with specific components, you can get all the entities and filter through the list:

// Returns a Vec>
let entities = scene.get_entities()
.are_active() // Make sure to only fetch active components
.with_component::(); // Only fetch entities with specified components

Serialization

Both entities and scenes have methods to serialize into ron files.

// --- Serializing an entity ---
let entity = EntityBuilder::new()
// Add a serailizable component
.with(ExampleComponent{})
.build();
// Dump the entity to a ron-file
entity.export_ron("path_to_file").unwrap();

// --- Serializing a scene ---
let scene = SceneBuilder::new()
// Add the entity to the scene
.with(entity)
.build();
// Dump to scene to a file
scene.export_ron("path_to_file").unwrap();

Both can be deserialized aswell.

// Deserialize an entity
let entity = Entity::import_ron("path_to_file").unwrap();

// Deserialize a scene
let scene = Scene::import_ron("path_to_file").unwrap();
You might also like...
Fast, concurrent, arena-based allocator with drop support

blink-alloc Blink-alloc is extremely fast allocator based on the common idea of allocating linearly zipping a cursor through memory chunk and reset ev

Tiny Discord ticket support bot that utilizes the OpenAI GPT-3.5-turbo model.
Tiny Discord ticket support bot that utilizes the OpenAI GPT-3.5-turbo model.

BDFD AI Mod Our tiny Discord ticket support bot that utilizes the OpenAI GPT-3.5-turbo model. This project aims to help users by providing a very fast

Meet Rustacean GPT, an experimental project transforming OpenAi's GPT into a helpful, autonomous software engineer to support senior developers and simplify coding life! 🚀🤖🧠
Meet Rustacean GPT, an experimental project transforming OpenAi's GPT into a helpful, autonomous software engineer to support senior developers and simplify coding life! 🚀🤖🧠

Rustacean GPT Welcome, fellow coding enthusiasts! 🚀 🤖 I am excited to introduce you to Rustacean GPT, my humble yet ambitious project that aims to t

Executable memory allocator with support for dual mapping and W^X protection

jit-allocator A simple memory allocator for executable code. Use JitAllocator type to allocate/release memory and virtual_memory module functions to e

This is a simple Telegram bot with interface to Firefly III to process and store simple transactions.
This is a simple Telegram bot with interface to Firefly III to process and store simple transactions.

Firefly Telegram Bot Fireflies are free, so beautiful. (Les lucioles sont libres, donc belles.) ― Charles de Leusse, Les Contes de la nuit This is a s

Rust crate for parsing stivale and stivale 2 structures.

stivale-rs Rust crate for parsing stivale and stivale 2 structures. Resources Stivale v2 Specification Stivale Specification License Licensed under ei

Rust crate to generate, manipulate and traverse trees.

SOCAREL Rust crate to generate, manipulate and traverse trees. It provides iterators for eight different traversal algorithms. Add and remove nodes in

A translation of Brendan Galea's Vulkan tutorial into Rust using the ash crate
A translation of Brendan Galea's Vulkan tutorial into Rust using the ash crate

Rust Light Vulkan Engine This is a translation of Brendan Galea's Vulkan tutorial into rust using the Ash crate. Original tutorial: Brendan Galea's Yo

Releases(0.2.0)
Owner
null
secmem-proc is a crate designed to harden a process against low-privileged attackers running on the same system trying to obtain secret memory contents of the current process.

secmem-proc is a crate designed to harden a process against low-privileged attackers running on the same system trying to obtain secret memory contents of the current process. More specifically, the crate disables core dumps and tries to disable tracing on unix-like OSes.

null 3 Dec 19, 2022
📦 Crate Protocol allows anyone to create, manage, and trade a tokenized basket of assets, which we refer to as a Crate.

?? Crate Protocol Crate Protocol allows anyone to create, manage, and trade a tokenized basket of assets, which we refer to as a Crate. A Crate is alw

Crate Protocol 63 Oct 31, 2022
Simple library to host lv2 plugins. Is not meant to support any kind of GUI.

lv2-host-minimal Simple library to host lv2 plugins. Is not meant to support any kind of GUI. Host fx plugins (audio in, audio out) Set parameters Hos

Cody Bloemhard 11 Aug 31, 2022
A dead-simple, extreme fast permission flag system for Rust with no dependencies

A dead-simple, extreme fast permission flag system for Rust with no dependencies

pan93412 2 Mar 17, 2022
3MF (3D Manufacturing Format) support for Rust

3MF (3D Manufacturing Format) support for Rust About This library provides support for 3MF files to programs written in the Rust programming language.

Hanno Braun 21 Dec 17, 2022
A (mostly) drop-in replacement for Rust's Result that provides backtrace support

Errant A (mostly) drop-in replacement for Rust's Result that provides backtrace support. Please note that Errant is still very early in development an

Joshua Barretto 17 Dec 26, 2022
Serde support for (rusty_)v8

serde_v8 Serde support for (rusty_)v8 WIP: see denoland/deno#9540 TODO Experiment with KeyCache to optimize struct keys Experiment with external v8 st

Aaron O'Mullan 13 Nov 28, 2022
Use enum to predicate something, support | and & operator.

Predicate Use enum to predicate something. Just need to implement Predicate Trait with predicate-macros crate, support | and & operator. Don't impleme

上铺小哥 9 Feb 8, 2022
RustSBI support on SiFive FU740 board; FU740 is a five-core heterogeneous processor with four SiFive U74 cores, and one SiFive S7 core

RustSBI 在 HiFive Unmatched 主板的支持软件 这个项目的目的是在SiFive HiFive Unmatched主板上支持RustSBI。 RustSBI是一个引导程序环境;主板上电时,RustSBI将会先行启动,而后,它将会找到一个可引导的操作系统,引导启动这个操作系统。 在

RustSBI 15 Dec 1, 2022
Add toast support in your dioxus project

Add toast support in your dioxus project

YuKun Liu 8 Dec 16, 2022