• Stars
    star
    1,616
  • Rank 28,963 (Top 0.6 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High performance Rust ECS library

Legion ECS

Build Status Crates.io Docs.rs

Legion aims to be a feature rich high performance Entity component system (ECS) library for Rust game projects with minimal boilerplate.

Getting Started

Worlds

Worlds are collections of entities, where each entity can have an arbitrary collection of components attached.

use legion::*;
let world = World::default();

Entities can be inserted via either push (for a single entity) or extend (for a collection of entities with the same component types). The world will create a unique ID for each entity upon insertion that you can use to refer to that entity later.

// a component is any type that is 'static, sized, send and sync
#[derive(Clone, Copy, Debug, PartialEq)]
struct Position {
    x: f32,
    y: f32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Velocity {
    dx: f32,
    dy: f32,
}

// push a component tuple into the world to create an entity
let entity: Entity = world.push((Position { x: 0.0, y: 0.0 }, Velocity { dx: 0.0, dy: 0.0 }));

// or extend via an IntoIterator of tuples to add many at once (this is faster)
let entities: &[Entity] = world.extend(vec![
    (Position { x: 0.0, y: 0.0 }, Velocity { dx: 0.0, dy: 0.0 }),
    (Position { x: 1.0, y: 1.0 }, Velocity { dx: 0.0, dy: 0.0 }),
    (Position { x: 2.0, y: 2.0 }, Velocity { dx: 0.0, dy: 0.0 }),
]);

You can access entities via entries. Entries allow you to query an entity to find out what types of components are attached to it, to get component references, or to add and remove components.

// entries return `None` if the entity does not exist
if let Some(mut entry) = world.entry(entity) {
    // access information about the entity's archetype
    println!("{:?} has {:?}", entity, entry.archetype().layout().component_types());

    // add an extra component
    entry.add_component(12f32);

    // access the entity's components, returns `None` if the entity does not have the component
    assert_eq!(entry.get_component::<f32>().unwrap(), &12f32);
}

Queries

Entries are not the most convenient or performant way to search or bulk-access a world. Queries allow for high performance and expressive iteration through the entities in a world.

// you define a query be declaring what components you want to find, and how you will access them
let mut query = <&Position>::query();

// you can then iterate through the components found in the world
for position in query.iter(&world) {
    println!("{:?}", position);
}

You can search for entities which have all of a set of components.

// construct a query from a "view tuple"
let mut query = <(&Velocity, &mut Position)>::query();

// this time we have &Velocity and &mut Position
for (velocity, position) in query.iter_mut(&mut world) {
    position.x += velocity.x;
    position.y += velocity.y;
}

You can augment a basic query with additional filters. For example, you can choose to exclude entities which also have a certain component, or only include entities for which a certain component has changed since the last time the query ran (this filtering is conservative and coarse-grained)

// you can use boolean expressions when adding filters
let mut query = <(&Velocity, &mut Position)>::query()
    .filter(!component::<Ignore>() & maybe_changed::<Position>());

for (velocity, position) in query.iter_mut(&mut world) {
    position.x += velocity.dx;
    position.y += velocity.dy;
}

There is much more than can be done with queries. See the module documentation for more information.

Systems

You may have noticed that when we wanted to write to a component, we needed to use iter_mut to iterate through our query. But perhaps your application wants to be able to process different components on different entities, perhaps even at the same time in parallel? While it is possible to do this manually (see World::split), this is very difficult to do when the different pieces of the application don't know what components each other need, or might or might not even have conflicting access requirements.

Systems and the Schedule automates this process, and can even schedule work at a more granular level than you can otherwise do manually. A system is a unit of work. Each system is defined as a function which is provided access to queries and shared resources. These systems can then be appended to a schedule, which is a linear sequence of systems, ordered by when side effects (such as writes to components) should be observed. The schedule will automatically parallelize the execution of all systems whilst maintaining the apparent order of execution from the perspective of each system.

// a system fn which loops through Position and Velocity components, and reads the Time shared resource
// the #[system] macro generates a fn called update_positions_system() which will construct our system
#[system(for_each)]
fn update_positions(pos: &mut Position, vel: &Velocity, #[resource] time: &Time) {
    pos.x += vel.dx * time.elapsed_seconds;
    pos.y += vel.dy * time.elapsed_seconds;
}

// construct a schedule (you should do this on init)
let mut schedule = Schedule::builder()
    .add_system(update_positions_system())
    .build();

// run our schedule (you should do this each update)
schedule.execute(&mut world, &mut resources);

See the systems module and the system proc macro for more information.

Feature Flags

Legion provides a few feature flags:

  • parallel - Enables parallel iterators and parallel schedule execution via the rayon library. Enabled by default.
  • extended-tuple-impls - Extends the maximum size of view and component tuples from 8 to 24, at the cost of increased compile times. Off by default.
  • serialize - Enables the serde serialization module and associated functionality. Enabled by default.
  • crossbeam-events - Implements the EventSender trait for crossbeam Sender channels, allowing them to be used for event subscriptions. Enabled by default.

WASM

Legion runs with parallelism on by default, which is not currently supported by Web Assembly as it runs single-threaded. Therefore, to build for WASM, ensure you set default-features = false in Cargo.toml. Additionally, if you want to use the serialize feature, you must enable either the stdweb or wasm-bindgen features, which will be proxied through to the uuid crate. See the uuid crate for more information.

legion = { version = "*", default-features = false, features = ["wasm-bindgen"] }

More Repositories

1

amethyst

Data-oriented and data-driven game engine written in Rust
Rust
7,982
star
2

specs

Specs - Parallel ECS
Rust
2,483
star
3

rlua

High level Lua bindings to Rust
C
1,628
star
4

bracket-lib

The Roguelike Toolkit (RLTK), implemented for Rust.
Rust
1,500
star
5

rustrogueliketutorial

Roguelike Tutorial in Rust - using RLTK
Rust
894
star
6

rendy

State of the art "build your own engine" kit powered by gfx-hal
Rust
815
star
7

distill

Asset pipeline system for game engines & editor suites.
Rust
367
star
8

shred

Shared resource dispatcher
Rust
231
star
9

evoli

An ecosystem-simulation game made with Amethyst
Rust
217
star
10

amethyst-starter-2d

Seed project for 2D games
Rust
201
star
11

space-menace

An action 2D platformer made with Amethyst game engine
Rust
180
star
12

shotcaller

A moddable RTS/MOBA game made with bracket-lib and minigene.
Rust
143
star
13

serde-diff

Utility for comparing two structs and re-applying the differences to other structs
Rust
120
star
14

hibitset

Hierarchical bit set container
Rust
114
star
15

voxel-mapper

Make beautiful voxel maps.
Rust
113
star
16

specs-physics

nphysics integration for the Specs entity component system
Rust
94
star
17

sheep

Modular and lightweight spritesheet packer ๐Ÿ‘
Rust
89
star
18

tools

Game development tools for the Amethyst engine
Rust
80
star
19

grumpy_visitors

๐Ÿง™โ€โ™‚๏ธ๐Ÿง™โ€โ™€๏ธ A prototype of a top-down EvilInvasion-like 2D arcade/action (with co-op!)
Rust
77
star
20

amethyst-imgui

imgui integration for Amethyst
Rust
66
star
21

legion_transform

A Unity-inspired hierarchical transform implementation using Legion ECS
Rust
51
star
22

editor-core

Crate that allows an Amethyst game to communicate with an editor.
Rust
44
star
23

rfcs

RFCs are documents that contain major plans and decisions for the engine
32
star
24

dwarf_seeks_fortune

A 2D puzzle platformer made with the Amethyst game engine.
Rust
25
star
25

amethyst_iced

Iced rendering plugin for the Amethyst game engine
Rust
24
star
26

pong_wasm

WASM end-to-end proof of concept -- work in progress
Rust
22
star
27

web_worker

Rust
19
star
28

ludumdare42

A game made by the Amethyst team for Ludum Dare 42
Rust
16
star
29

website-legacy

Project website and blog (DEPRECATED)
HTML
11
star
30

website

Official Amethyst website
JavaScript
9
star
31

amethyst-rhusics

A bridge between Amethyst and rhusics (unmaintained)
Rust
9
star
32

awesome-specs

A curated list of projects that use or help with using Specs.
6
star
33

crystal-editor

Svelte
6
star
34

ludumdare43

Rust
2
star
35

resources

Files that are important to keep around but not tied to any specific code base
1
star
36

builder

The docker container used in amethyst's CI/CD infrastructure.
Dockerfile
1
star
37

wasm_rush_report

Report about adding WASM support to the Amethyst game engine.
1
star
38

laminar-ffi

Crate that exposes Laminar functionality to C
Rust
1
star
39

amethyst-docs-builder

Webhook server that builds amethyst's book and documentation
Go
1
star
40

amethystup

Setup script for Amethyst dependencies
Shell
1
star
41

documentation

Non-rustdoc documentation and policies
1
star