Code Monkey home page Code Monkey logo

gecs's Introduction

Gecs

An Entity-Component-System storage implement with golang. It's a sparse sets based inspired by EnTT.

  • Fast iterator.
  • Reuse entity id.
  • Combine include/exclude Component query supported.

All the technique/features/idea are from EnTT. (use the amazing ecs implement Entt if you code cpp.)

As go does not support generic now. So we should add extra info to identify different component type.(It's called component id in the project)

Build Status codecov codebeat badge license

Getting Started

go get github.com/tutumagi/gecs

Example

package main

import (
	"fmt"
	"github.com/tutumagi/gecs"
)
// pre define your component data type
type Name string
type Age int
type Position struct {
	X int
	Y int
	Z int
}

func main() {
	// create a registry
	registry := gecs.NewRegistry()

	// register all components what you will use later
	// It's to identify component type
	NameID := registry.RegisterComponent("name")
	AgeID := registry.RegisterComponent("age")
	PositionID := registry.RegisterComponent("position")

	// create an entity
	entity := registry.Create()

	// assign component data
	registry.Assign(entity, NameID, Name("tufei"))
	registry.Assign(entity, AgeID, Age(12))

	// check whether an entity has given components data or not
	fmt.Printf("entity %v has <Name>: %v\n", entity, registry.Has(entity, NameID))              // true
	fmt.Printf("entity %v has <Name & Age>: %v\n", entity, registry.Has(entity, NameID, AgeID)) // true
	fmt.Printf("entity %v has <Position>: %v\n", entity, registry.Has(entity, PositionID))      // false

	// iterator entities by givin component
	registry.View(NameID, AgeID).Each(func(entity gecs.EntityID, datas map[gecs.ComponentID]interface{}) {
		name := datas[NameID].(Name) // the component type bind componentID must be consistent with you assign before
		age := datas[AgeID].(Age)

		fmt.Printf("name is %v\n", name)
		fmt.Printf("age is %v\n", age)
	})
}

Prerequisites

GO 1.14 and above

TODO

  • exclude test
  • avoid gc as much as possible
  • notify when update component data(create/update/remove)
  • avoid iterate empty component data
  • snapshot
  • group
  • database backend
  • benchmark

License

This project is licensed under the MIT License - see the LICENSE file for details

gecs's People

Contributors

tutumagi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sogues

gecs's Issues

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.