GithubHelp home page GithubHelp logo

hyuntaeng / ecsgo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kongbong/ecsgo

0.0 0.0 0.0 4.44 MB

Cache friendly, Multi threading Entity Component System in Go (with Generic)

License: MIT License

Go 100.00%

ecsgo's Introduction

ECSGo

ECSGo is an Entity Component System(ECS) in Go. This is made with Generic Go, so it needs Go 1.18 version

  • Cache friendly data storage
  • Run systems in concurrently with analyzing dependency tree.

Example

package main

import (
    "log"

    "github.com/kongbong/ecsgo"
)

type Position struct {
    X float32
    Y float32
}

type Velocity struct {
    X float32
    Y float32
}

type HP struct {
    Hp    float32
    MaxHp float32
}

type EnemyTag struct{}

func main() {
    registry := ecsgo.New()
    defer registry.Free() // need to call before remove registry to free C malloc memory

    sys := ecsgo.AddSystem1[Velocity](registry, ecsgo.OnTick, func(r *ecsgo.Registry, iter *ecsgo.Iterator) {
        log.Println("This should not called as Entity has Velocity component")
    })
    ecsgo.Exclude[EnemyTag](sys)

    ecsgo.PostTask1[Velocity](registry, ecsgo.OnTick, func(r *ecsgo.Registry, iter *ecsgo.Iterator) {
        for ; !iter.IsNil(); iter.Next() {
            vel := ecsgo.Get[Velocity](iter)
            log.Println("This is one time called system", iter.Entity(), vel)
        }
    })

    sys = ecsgo.AddSystem1[Velocity](registry, ecsgo.OnTick, func(r *ecsgo.Registry, iter *ecsgo.Iterator) {
        for ; !iter.IsNil(); iter.Next() {
            vel := ecsgo.Get[Velocity](iter)
            log.Println("Velocity system", iter.Entity(), vel)
            // Velocity value of Entity is not changed as it is Readonly
            vel.X++
            vel.Y++
        }
    })
    ecsgo.Exclude[HP](sys)
    ecsgo.Readonly[Velocity](sys)

    sys = ecsgo.AddSystem2[Position, Velocity](registry, ecsgo.OnTick, func(r *ecsgo.Registry, iter *ecsgo.Iterator) {
        for ; !iter.IsNil(); iter.Next() {
            pos := ecsgo.Get[Position](iter)
            vel := ecsgo.Get[Velocity](iter)
            log.Println("Position, Velocity system", iter.Entity(), pos, vel, r.DeltaSeconds())
            // Position value is not changed only Velocity value is changed
            pos.X++
            pos.Y++
            vel.X++
            vel.Y++
        }
    })
    sys.SetTickInterval(1)
    ecsgo.Tag[EnemyTag](sys)
    ecsgo.Readonly[Position](sys)

    entity := registry.Create()
    ecsgo.AddComponent(registry, entity, &Position{10, 10})
    ecsgo.AddComponent(registry, entity, &Velocity{20, 20})
    ecsgo.AddTag[EnemyTag](registry, entity)

    registry.Run(ecsgo.FPS(10), ecsgo.FixedTick(true))
}

ecsgo's People

Contributors

kongbong avatar bfreis avatar

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.