• Stars
    star
    963
  • Rank 47,492 (Top 1.0 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

A handy ECS

hecs

Documentation Crates.io License: Apache 2.0

hecs provides a high-performance, minimalist entity-component-system (ECS) world. It is a library, not a framework. In place of an explicit "System" abstraction, a World's entities are easily queried from regular code. Organize your application however you like!

Example

let mut world = World::new();
// Nearly any type can be used as a component with zero boilerplate
let a = world.spawn((123, true, "abc"));
let b = world.spawn((42, false));
// Systems can be simple for loops
for (id, (number, &flag)) in world.query_mut::<(&mut i32, &bool)>() {
  if flag { *number *= 2; }
}
// Random access is simple and safe
assert_eq!(*world.get::<&i32>(a).unwrap(), 246);
assert_eq!(*world.get::<&i32>(b).unwrap(), 42);

Why ECS?

Entity-component-system architecture makes it easy to compose loosely-coupled state and behavior. An ECS world consists of:

  • any number of entities, which represent distinct objects
  • a collection of component data associated with each entity, where each entity has at most one component of any type, and two entities may have different components

That world is then manipulated by systems, each of which accesses all entities having a particular set of component types. Systems implement self-contained behavior like physics (e.g. by accessing "position", "velocity", and "collision" components) or rendering (e.g. by accessing "position" and "sprite" components).

New components and systems can be added to a complex application without interfering with existing logic, making the ECS paradigm well suited to applications where many layers of overlapping behavior will be defined on the same set of objects, particularly if new behaviors will be added in the future. This flexibility sets it apart from traditional approaches based on heterogeneous collections of explicitly defined object types, where implementing new combinations of behaviors (e.g. a vehicle which is also a questgiver) can require far-reaching changes.

Performance

In addition to having excellent composability, the ECS paradigm can also provide exceptional speed and cache locality. hecs internally tracks groups of entities which all have the same components. Each group has a dense, contiguous array for each type of component. When a system accesses all entities with a certain set of components, a fast linear traversal can be made through each group having a superset of those components. This is effectively a columnar database, and has the same benefits: the CPU can accurately predict memory accesses, bypassing unneeded data, maximizing cache use and minimizing latency.

Why Not ECS?

hecs strives to be lightweight and unobtrusive so it can be useful in a wide range of applications. Even so, it's not appropriate for every game. If your game will have few types of entities, consider a simpler architecture such as storing each type of entity in a separate plain Vec. Similarly, ECS may be overkill for games that don't call for batch processing of entities.

Even for games that benefit, an ECS world is not a be-all end-all data structure. Most games will store significant amounts of state in other structures. For example, many games maintain a spatial index structure (e.g. a tile map or bounding volume hierarchy) used to find entities and obstacles near a certain location for efficient collision detection without searching the entire world.

If you need to search for specific entities using criteria other than the types of their components, consider maintaining a specialized index beside your world, storing Entity handles and whatever other data is necessary. Insert into the index when spawning relevant entities, and include a component with that allows efficiently removing them from the index when despawning.

Other Libraries

hecs owes a great deal to the free exchange of ideas in Rust's ECS library ecosystem. Particularly influential examples include:

  • bevy, which continually pushes the envelope for performance and ergonomics in the context of a batteries-included framework
  • specs, which was key in popularizing ECS in Rust
  • legion, which demonstrated archetypal memory layout and trait-less components

If hecs doesn't suit you, one of those might do the trick!

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Disclaimer

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

More Repositories

1

openxrs

OpenXR bindings for Rust
Rust
271
star
2

hypermine

A hyperbolic voxel game
Rust
160
star
3

oddio

Lightweight game audio
Rust
146
star
4

quicr

Experimental Rust QUIC implementation - moved to https://github.com/djc/quinn
Rust
67
star
5

planetmap

Streaming planetary terrain mapping
Rust
56
star
6

nachat

Portable desktop chat client for Matrix
C++
49
star
7

vk-shader-macros

Procedural macros for working with Vulkan shaders
Rust
41
star
8

lahar

Async-oriented Vulkan helpers
Rust
29
star
9

timer-queue

Pure, minimal, and scalable timers
Rust
24
star
10

reprap-utils

NO LONGER SUPPORTED -- Various lightweight UNIXy command-line utilities for working with RepRap machines on POSIX-compatible systems
C
22
star
11

riot-electron-nix

Deprecated in favor of riot-desktop in nixpkgs
Nix
16
star
12

libreprap

Now living at https://github.com/timschmidt/repsnapper -- Cross-platform cross-machine RepRap communications library
C
16
star
13

clatter

Simplex noise
Rust
15
star
14

fuzzyblue

Real-time physically-based atmospheric scattering using Vulkan and Rust
Rust
13
star
15

lru-slab

Pre-allocated storage with constant-time LRU tracking
Rust
10
star
16

sieve-tree

Rust
9
star
17

metaserve

Toolkit for building meta servers for multiplayer games
Rust
8
star
18

cllvm

Hand-written LLVM bindings for Common Lisp
Common Lisp
7
star
19

blox2d

A lisp port of the Box2D physics engine
Common Lisp
7
star
20

reprap-firmware

A new cleaner, stabler, faster, more portable, and more maintainable firmware for RepRap machines (initially targeted at the Sanguino), written from scratch in conformant C99 to work without the Arduino environment. Please note that although it is nearing completion, the firmware is not yet usable.
C
7
star
21

sd-listen-fds

Minimal self-contained crate to accept file descriptors from systemd
Rust
6
star
22

yapb

Yet Another Progress Bar
Rust
6
star
23

matrix-appservice-irc-nixos

matrix-appservice-irc module for NixOS
Nix
5
star
24

presentash

Rust
4
star
25

opus-mux

Opus file demultiplexer
Rust
4
star
26

Thune

Experimental lisp IRC bot
Common Lisp
3
star
27

icfpc-2012

My team's submission to the 2012 ICFP Programming Contest
Haskell
3
star
28

psf2

Minimal, unopinionated PSF2 bitmap font parser
Rust
3
star
29

cl-chipmunk

Common Lisp CFFI wrapper to the chipmunk 2D physics library
Common Lisp
3
star
30

spangle

Generate plausible, aesthetically appealing spherical starfields
Rust
3
star
31

err-ctx

Contextual error reporting helpers
Rust
2
star
32

llvm-st

ST-based interface to the Haskell LLVM bindings
Haskell
2
star
33

quasigroup-completion

A backtracking quasigroup completion solver whose solutions are verified correct, written in Idris.
2
star
34

bitvis

Bitcoin market visualizer
C
2
star
35

cmpt885-hw2

C++
2
star
36

carchive

Archive encoding for content addressing
Rust
2
star
37

ircl

A minimalist IRC library for Common Lisp
Common Lisp
2
star
38

version-consts-git

Code generation support for git-versioned rust crates that want to know their own version
Rust
2
star
39

quest

Common Lisp
2
star
40

rackup

Rust
1
star
41

pew

Thermodynamic internal ballistics simulation
Rust
1
star
42

rsctp

Native Rust SCTP implementation
Rust
1
star
43

hhue

Command-line control for Philips Hue bridge
Haskell
1
star
44

buclet

CFFI bindings to Bullet
Common Lisp
1
star
45

renderdoc-api-rs

Rust bindings for the renderdoc API
Rust
1
star
46

wair

Rust
1
star
47

Sune

Common Lisp
1
star
48

rustlike

Rust
1
star
49

cmpt885-hw1

C
1
star
50

thune2

Cleaned up IRC bot
Common Lisp
1
star
51

whoosh

Continuous distribution based random gun generator toy
Haskell
1
star
52

capnpc-racket

Racket code generator plugin for Cap'n Proto
Racket
1
star
53

rustart

Rust random art implementation
Rust
1
star
54

fwoosh

OpenGL- and/or game-related fiddlery
Common Lisp
1
star