GithubHelp home page GithubHelp logo

entitas.cr's Introduction

Entitas.cr

Build Status Release Status Latest release

Entitas

A Entity Component System Framework for Crystal. Ported from: Entitas-CSharp

Entitas is a super fast Entity Component System Framework (ECS). Internal caching and blazing fast component access makes it second to none. Several design decisions have been made to work optimal in a garbage collected environment and to go easy on the garbage collector. Utilizing Crystal's macro system, all code generation is done compile time. No need for a code generator!

First glimpse

public def create_red_gem(context : GameContext, position)
  entity.is_game_board_element = true
  entity.is_movable = true
  entity = context.create_entity
  entity.add_position(position)
  entity.add_asset("RedGem")
  entity.is_interactive = true
  entity
end
entities = context.get_entities(Entitas::Matcher.all_of(Position, Velocity))
entities.each do |e|
  pos = e.position
  vel = e.velocity
end

Installation

Add this to your application's shard.yml:

dependencies:
  entitas:
    github: spoved/entitas.cr

Usage

require "entitas"

Documentation

Run crystal docs to generate API Docs and examples. You may need to include an example, as there are no default contexts and the macros need at least one for some things. To generate documentation run: crystal docs docs.cr

Since there is so much code that is auto generated, it is also recomended to include ./lib/entitas/src/entitas.cr when generating documentation for your application/lib. Like so:

crystal docs ./lib/entitas/src/entitas.cr ./src/

Error: enum Index must have at least one member

This error occurs when you have no components declared. The Entitas::Component::Index enum is auto generated by indexing all the sub-classes of Entitas::Component. Without any, the enum is empty, causing this error.

Examples

Examples can also be found in the spec/fixtures and examples folders. Ive replicated every test i could from the C# lib, so many examples and translations can be found there as well.

Logging

To enable verbose logging please provide the compile time flag -Dentitas_enable_logging. This will enable the Spoved::Logger. This is disabled by default to keep the code as performant as possible.

To enable some debug output for the code generator, include the flag -Dentitas_debug_generator.

Benchmarks

It is very important to make sure the lib is compariable to the original entitas. Here are some of the benchmarks to compare. Each benchmark was written as close as possible to the main source. As well as any aliases of the methods were tested. Most impressivly, the slowest test (EntityRemoveAddComponent) clocked in at 868.6 ms vs the crystal equivilant (#remove_component & #add_component) at 290.0 ms. Thats a 3x speed improvement.

Enitas C#

Running performance tests...
ContextCreateEntity:                    98.5355 ms
ContextDestroyEntity:                   33.0015 ms
ContextDestroyAllEntities:              32.4288 ms
ContextGetGroup:                        6.2042 ms
ContextGetEntities:                     2.322 ms
ContextHasEntity:                       1.9293 ms
ContextOnEntityReplaced:                21.4785 ms

EntityAddComponent:                     496.7223 ms
EntityGetComponent:                     39.1669 ms
EntityGetComponents:                    5.9182 ms
EntityHasComponent:                     5.4402 ms
EntityRemoveAddComponent:               868.6205 ms
EntityReplaceComponent:                 164.2688 ms

Entitas Cr

You can run the following command to run the benchmarks:

crystal run --release ./spec/performance/bench.cr

--## Entitas::Context(TEntity) ##--
-- Total execution time --

                       user     system      total        real
#get_entities      0.000000   0.000000   0.000000 (  0.001150)
#has_entity?       0.000000   0.000000   0.000000 (  0.000714)
OnEntityReplaced   0.000000   0.000000   0.000000 (  0.005321)

- Create Entity -
                             user     system      total        real
#create_entity w/o pre   0.080000   0.000000   0.080000 (  0.083808)
#create_entity w/ pre    0.020000   0.000000   0.020000 (  0.019606)

- Destroy all entities -
                                          user     system      total        real
control                               0.000000   0.000000   0.000000 (  0.000001)
#get_entities &Entity.destroy!        0.020000   0.010000   0.030000 (  0.021025)
#destroy_all_entities                 0.020000   0.000000   0.020000 (  0.020348)
#destroy_all_entities (pre-destroy)   0.020000   0.000000   0.020000 (  0.016953)

- #get_group -
                          user     system      total        real
#get_group by Int32   0.050000   0.000000   0.050000 (  0.053371)
#get_group by class   0.050000   0.000000   0.050000 (  0.047716)
#get_group by Enum    0.050000   0.000000   0.050000 (  0.047686)


--## Entitas::Entity ##--
-- Total execution time --

                         user     system      total        real
#add_component       0.370000   0.000000   0.370000 (  0.374146)
#get_components      0.000000   0.000000   0.000000 (  0.002171)
#replace_component   0.040000   0.000000   0.040000 (  0.040704)
#total_components    0.000000   0.000000   0.000000 (  0.000001)

- Get Component A -
                                                user     system      total        real
#get_component(Entitas::Component::Index)   0.040000   0.000000   0.040000 (  0.035134)
#get_component(Int32)                       0.030000   0.000000   0.030000 (  0.033637)
#get_component(A::INDEX)                    0.040000   0.000000   0.040000 (  0.036969)
#get_component(A::INDEX_VALUE)              0.030000   0.000000   0.030000 (  0.033226)
#a                                          0.040000   0.000000   0.040000 (  0.041205)

- Has Component? -
                      user     system      total        real
#has_component?   0.000000   0.000000   0.000000 (  0.000001)
#a?               0.000000   0.000000   0.000000 (  0.000001)

- Remove and Add Component -
                                         user     system      total        real
#remove_component & #add_component   0.250000   0.040000   0.290000 (  0.298851)
#del_a & #add_a                      0.250000   0.040000   0.290000 (  0.292872)

Development

Dont forget to add your pre-commit hooks!

ln ./pre-commit .git/hooks/pre-commit

Contributing

  1. Fork it (https://github.com/spoved/entitas.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

entitas.cr's People

Contributors

kalinon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

entitas.cr's Issues

Update examples/inter_context_communication.cr

Hello everyone,

First I love your work thanks a lot. I'm using it for some personal project. However I'm not a veteran of ECS, I'm still learning alot.

I saw examples/inter_context_communication.cr is a little bit dated, I think they have been update on the framework since then. Do you mind updating it if you can spare some time?

๐ŸคŸ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.