All Projects → takahirox → ecs-rust

takahirox / ecs-rust

Licence: MIT license
Tiny ECS library in Rust

Programming Languages

javascript
184084 projects - #8 most used programming language
rust
11053 projects
CSS
56736 projects
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to ecs-rust

domain-lookup-tree
A tree structure in Rust optimized for looking up domain names, with wildcard support
Stars: ✭ 17 (-46.87%)
Mutual labels:  rust-crate
ecsdemo-crystal
Part 4 of a 4 part ECS Workshop
Stars: ✭ 54 (+68.75%)
Mutual labels:  ecs
cargo-clone
A cargo subcommand to fetch the source code of a Rust crate
Stars: ✭ 72 (+125%)
Mutual labels:  rust-crate
Curio
A Blazing Fast HTTP Client
Stars: ✭ 35 (+9.38%)
Mutual labels:  rust-crate
tiny-secp256k1
A tiny secp256k1 native/JS wrapper
Stars: ✭ 41 (+28.13%)
Mutual labels:  rust-crate
terraform-ecs-fargate-nlb
A Terraform template used for provisioning network application stacks on AWS ECS Fargate
Stars: ✭ 50 (+56.25%)
Mutual labels:  ecs
ecs-benchmark
ECS benchmark comparison
Stars: ✭ 68 (+112.5%)
Mutual labels:  ecs
polymorph
A fast and frugal entity-component-system library with a focus on code generation and compile time optimisation.
Stars: ✭ 74 (+131.25%)
Mutual labels:  ecs
cog
Macro powered ECS Framework written in Haxe
Stars: ✭ 29 (-9.37%)
Mutual labels:  ecs
ecs-logging-java
Centralized logging for Java applications with the Elastic stack made easy
Stars: ✭ 128 (+300%)
Mutual labels:  ecs
ECS-CommunityEdition
ECS Community Edition "Free & Frictionless"
Stars: ✭ 125 (+290.63%)
Mutual labels:  ecs
ECSEntityBuilder
Unity ECS Entity Builder/Wrapper
Stars: ✭ 39 (+21.88%)
Mutual labels:  ecs
ActionFlow
基于Unity ECS的节点执行图。可支持状态机执行流程和行为树的执行流程
Stars: ✭ 61 (+90.63%)
Mutual labels:  ecs
ecs
A dependency free, lightweight, fast Entity-Component System (ECS) implementation in Swift
Stars: ✭ 79 (+146.88%)
Mutual labels:  ecs
URSA
[DEPRECATED] integrated ECS framework for Unity
Stars: ✭ 30 (-6.25%)
Mutual labels:  ecs
crates-io-cn
Source code of crates-io.cn, also tools sets for sync crates.io
Stars: ✭ 20 (-37.5%)
Mutual labels:  rust-crate
ecs-drain-lambda
Automation of Draining ECS instances with Lambda, based on Autoscaling Group Lifecycle hooks or Spot Instance Interruption Notices
Stars: ✭ 56 (+75%)
Mutual labels:  ecs
terraform-aws-ecs-fargate-scheduled-task
AWS ECS Fargate Schedule Task Terraform Module
Stars: ✭ 25 (-21.87%)
Mutual labels:  ecs
ECS-Game
A roguelike written in Python adhering to ECS
Stars: ✭ 23 (-28.12%)
Mutual labels:  ecs
ecsgo
Provides an interactive prompt to connect to ECS Containers using the ECS ExecuteCommand API.
Stars: ✭ 30 (-6.25%)
Mutual labels:  ecs

ecs-rust

Build Status Crate

ecu-rust is a tiny toy ECS (Entity Component System) library written in Rust.

Online demos

Online WebAssembly demos

Rust code is compiled to WebAssembly with wasm-bindgen and it runs even in web browsers.

Web Collision demo

Features

  • Tiny ECS library
  • Easy to use
  • Memory safe with Rust
  • Offers a clean design with ECS
  • Web application compatible by compiling to WebAssembly

Documents

T.B.D.

Sample Code

// Import ecs-rust
use ecs_rust::world::World;
use ecs_rust::entity_manager::{EntityIdAccessor, EntityManager};
use ecs_rust::component::Component;
use ecs_rust::system::System;

// Define Components and Systems

struct Namable {
  name: &'static str
}

struct Position {
  x: f32,
  y: f32
}

struct Velocity {
  x: f32,
  y: f32
}

struct Step {
  num: u32
}

struct PrintStepSystem;
struct MoveSystem;
struct PrintPositionSystem;

// Implement Components and Systems

impl Component for Namable {}
impl Component for Position {}
impl Component for Velocity {}
impl Component for Step {}

impl System for PrintStepSystem {
  fn update(&mut self, manager: &mut EntityManager, _accessor: &mut EntityIdAccessor) {
    let steps = manager.borrow_components_mut::<Step>().unwrap();
    for step in steps.iter_mut() {
      step.num += 1;
      println!("Step {}", step.num);
    }
  }
}

impl System for MoveSystem {
  fn update(&mut self, manager: &mut EntityManager, accessor: &mut EntityIdAccessor) {
    let entity_ids = accessor.borrow_ids_for_pair::<Velocity, Position>(manager).unwrap();
    for id in entity_ids.iter() {
      let (velocity, mut position) = manager.borrow_component_pair_mut::<Velocity, Position>(*id).unwrap();
      position.x += velocity.x;
      position.y += velocity.y;
    }
  }
}

impl System for PrintPositionSystem {
  fn update(&mut self, manager: &mut EntityManager, accessor: &mut EntityIdAccessor) {
    let entity_ids = accessor.borrow_ids_for_pair::<Namable, Position>(manager).unwrap();
    for id in entity_ids.iter() {
      let name = manager.borrow_component::<Namable>(*id).unwrap();
      let position = manager.borrow_component::<Position>(*id).unwrap();
      println!("{} is at ({}, {})", name.name, position.x, position.y);
    }
  }
}

// Build an application and Run

fn main() {
  // Create world
  let mut world = World::new();

  // Register Components to world
  world
    .register_component::<Step>()
    .register_component::<Namable>()
    .register_component::<Position>()
    .register_component::<Velocity>();

  // Create Entities and add Components to them
  {
    let entity_id = world.create_entity();
    world.add_component_to_entity(entity_id, Step {num: 0});
  }

  {
    let entity_id = world.create_entity();
    world
      .add_component_to_entity(entity_id, Namable {name: "Alice"})
      .add_component_to_entity(entity_id, Position {x: 0.0, y: 0.0})
      .add_component_to_entity(entity_id, Velocity {x: 1.0, y: 2.0});
  }

  {
    let entity_id = world.create_entity();
    world
      .add_component_to_entity(entity_id, Namable {name: "Bob"})
      .add_component_to_entity(entity_id, Position {x: 0.0, y: 0.0})
      .add_component_to_entity(entity_id, Velocity {x: -2.0, y: 1.0});
  }

  {
    // Unmovable
    let entity_id = world.create_entity();
    world
      .add_component_to_entity(entity_id, Namable {name: "Rock"})
      .add_component_to_entity(entity_id, Position {x: 0.0, y: 0.0});
  }

  // Add Systems to world
  world
    .add_system(PrintStepSystem {})
    .add_system(MoveSystem {})
    .add_system(PrintPositionSystem {});

  // Run
  for _i in 0..3 {
    world.update();
  }
}

/*
 * Result:
 * Step 1
 * Alice is at (1, 2)
 * Bob is at (-2, 1)
 * Rock is at (0, 0)
 * Step 2
 * Alice is at (2, 4)
 * Bob is at (-4, 2)
 * Rock is at (0, 0)
 * Step 3
 * Alice is at (3, 6)
 * Bob is at (-6, 3)
 * Rock is at (0, 0)
 */

How to import

The library is released at crates.io. Add the following line into Cargo.toml of your Rust project.

[dependencies]
ecs_rust = "0.0.4"

And add the following lines in your Rust code to import the library.

use ecs_rust::world::World;
use ecs_rust::entity_manager::{EntityIdAccessor, EntityManager};
use ecs_rust::component::Component;
use ecs_rust::system::System;

How to build the library locally

$ git clone https://github.com/takahirox/ecs-rust.git
$ cd ecs-rust
$ cargo build

How to run desktop examples locally

$ cd ecs-rust
$ cargo run --example example_name

How to run web examles locally

Prerequirements

  • Install wasm-bindgen client
  • Install Rust wasm32-unknown-unknown target with $ rustup target add wasm32-unknown-unknown
  • Install http-server with $ npm install -g http-server, or other local servers
$ cd ecs-rust/web
$ bash build_examples.sh
$ http-server . -p 8080 -c-1
# Access http://localhost:8080/examples/index.html on your web browser

How to run tests

T.B.D.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].