Giter VIP home page Giter VIP logo

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 (
	"context"
	"log"
	"time"

	"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.NewRegistry()

	sys1 := registry.AddSystem("VelocitySystem", 0, func(ctx *ecsgo.ExecutionContext) error {
		qr := ctx.GetQueryResult(0)
		log.Println("This system should have not any archtype", qr.GetArcheTypeCount())
		return nil
	})
	q1 := sys1.NewQuery()
	ecsgo.AddReadWriteComponent[Velocity](q1)
	ecsgo.AddExcludeComponent[EnemyTag](q1)

	o := registry.AddObserver("AddVelocityObserver", func(ctx *ecsgo.ObserverContext) error {
		vel := ecsgo.GetComponentObserver[Velocity](ctx)
		log.Println("This is one time called system", ctx.GetEntityId(), vel)
		return nil
	})
	ecsgo.AddComponentToObserver[Velocity](o)

	sys2 := registry.AddSystem("VelocitySystem2", 0, func(ctx *ecsgo.ExecutionContext) error {
		qr := ctx.GetQueryResult(0)
		qr.ForeachEntities(func(accessor *ecsgo.ArcheTypeAccessor) error {
			vel := ecsgo.GetComponentByAccessor[Velocity](accessor)
			log.Println("VelocitySystem2", accessor.GetEntityId(), vel)
			return nil
		})
		return nil
	})
	q2 := sys2.NewQuery()
	ecsgo.AddExcludeComponent[HP](q2)
	ecsgo.AddReadonlyComponent[Velocity](q2)

	sys3 := registry.AddSystem("PositionAndVelocity", 0, func(ctx *ecsgo.ExecutionContext) error {
		qr := ctx.GetQueryResult(0)
		qr.ForeachEntities(func(accessor *ecsgo.ArcheTypeAccessor) error {
			pos := ecsgo.GetComponentByAccessor[Position](accessor)
			vel := ecsgo.GetComponentByAccessor[Velocity](accessor)
			log.Println("Position, Velocity system", accessor.GetEntityId(), pos, vel, ctx.GetDeltaTime())
			pos.X++
			pos.Y++
			vel.X++
			vel.Y++
			return nil
		})
		return nil
	})
	q3 := sys3.NewQuery()
	ecsgo.AddReadWriteComponent[Position](q3)
	ecsgo.AddReadWriteComponent[Velocity](q3)
	ecsgo.AddReadonlyComponent[EnemyTag](q3)

	entity := registry.CreateEntity()
	ecsgo.AddComponent(registry, entity, Position{10, 10})
	ecsgo.AddComponent(registry, entity, Velocity{20, 20})
	ecsgo.AddComponent(registry, entity, EnemyTag{})

	ctx := context.Background()
	for i := 0; i < 10; i++ {
		registry.Tick(time.Second, ctx)
		time.Sleep(time.Second)
	}
}

ecsgo's People

Contributors

kongbong avatar bfreis avatar

Stargazers

ColdBell avatar  avatar Michael Nelson avatar killroad avatar sunguangdong avatar Kunho Lee avatar João Wiciuk avatar Stoney Kang avatar Homin Lee avatar Chanshik Lim avatar tsingson avatar Yota Hamada avatar Artem Sedykh avatar  avatar Patryk Kalinowski avatar Kijae Choi(Craig) avatar Eundoo Song avatar JunSeong Park (Johan) avatar Andrei Pastramagiu avatar JungSu Kim avatar Mars Park avatar  avatar  avatar

Watchers

JungSu Kim avatar  avatar

Forkers

bfreis hyuntaeng

ecsgo's Issues

Race condition on `Registry.setSystemDeltaSeconds`

Hi!

It seems that there's a race condition in Registry.setSystemDeltaSeconds, or I might be misunderstanding something.

This is the minimal example that reproduces the issue:

func TestRaceCondition(t *testing.T) {
	reg := ecsgo.New()
	ecsgo.AddSystem(reg, ecsgo.OnTick, func(r *ecsgo.Registry) {})
	ecsgo.AddSystem(reg, ecsgo.OnTick, func(r *ecsgo.Registry) {})
	reg.Tick(1.0)
}

Running the snippet above with the race detector reveals the race. The goroutines launched for each of the systems end up calling setSystemDeltaSeconds(...) which write to r.sysDeltaSeconds, but there's no synchronization.

Any suggestions?
Thanks!

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.