ecsgo

package module
v0.0.0-...-53dddcf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 8 Imported by: 0

README

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)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddComponent

func AddComponent[T any](r *Registry, entityId EntityId, val T)

func AddComponentToObserver

func AddComponentToObserver[T any](o *Observer)

func AddExcludeComponent

func AddExcludeComponent[T any](q *Query)

func AddOptionalReadWriteComponent

func AddOptionalReadWriteComponent[T any](q *Query)

func AddOptionalReadonlyComponent

func AddOptionalReadonlyComponent[T any](q *Query)

func AddReadWriteComponent

func AddReadWriteComponent[T any](q *Query)

func AddReadonlyComponent

func AddReadonlyComponent[T any](q *Query)

func GetComponent

func GetComponent[T any](c *ExecutionContext, entityId EntityId) *T

func GetComponentByAccessor

func GetComponentByAccessor[T any](acc *ArcheTypeAccessor) *T

func GetComponentObserver

func GetComponentObserver[T any](ctx *ObserverContext) *T

func HasArcheTypeComponent

func HasArcheTypeComponent[T any](a *ArcheType) bool

func HasComponent

func HasComponent[T any](c *ExecutionContext, entityId EntityId) bool

func RemoveComponent

func RemoveComponent[T any](r *Registry, entityId EntityId)

func RemoveComponentFromObserver

func RemoveComponentFromObserver[T any](o *Observer)

func SetComponentData

func SetComponentData[T any](acc *ArcheTypeAccessor, value T) bool

Types

type ArcheType

type ArcheType struct {
	// contains filtered or unexported fields
}

func (*ArcheType) Foreach

func (a *ArcheType) Foreach(fn func(accessor *ArcheTypeAccessor) error) error

func (*ArcheType) GetAccessor

func (a *ArcheType) GetAccessor(entityId EntityId) *ArcheTypeAccessor

type ArcheTypeAccessor

type ArcheTypeAccessor struct {
	// contains filtered or unexported fields
}

func (*ArcheTypeAccessor) GetArcheType

func (acc *ArcheTypeAccessor) GetArcheType() *ArcheType

func (*ArcheTypeAccessor) GetEntityId

func (acc *ArcheTypeAccessor) GetEntityId() EntityId

type EntityId

type EntityId struct {
	// contains filtered or unexported fields
}

func (EntityId) NotNil

func (e EntityId) NotNil() bool

type ExecutionContext

type ExecutionContext struct {
	// contains filtered or unexported fields
}

func (*ExecutionContext) CreateEntity

func (c *ExecutionContext) CreateEntity() EntityId

func (*ExecutionContext) GetDeltaTime

func (c *ExecutionContext) GetDeltaTime() time.Duration

func (*ExecutionContext) GetQueryResult

func (c *ExecutionContext) GetQueryResult(idx int) *QueryResult

func (*ExecutionContext) GetQueryResultCount

func (c *ExecutionContext) GetQueryResultCount() int

func (*ExecutionContext) GetResgiry

func (c *ExecutionContext) GetResgiry() *Registry

type Observer

type Observer struct {
	// contains filtered or unexported fields
}

func (*Observer) GetName

func (o *Observer) GetName() string

type ObserverContext

type ObserverContext struct {
	// contains filtered or unexported fields
}

func (*ObserverContext) CreateEntity

func (ctx *ObserverContext) CreateEntity() EntityId

func (*ObserverContext) GetArcheType

func (ctx *ObserverContext) GetArcheType() *ArcheType

func (*ObserverContext) GetEntityId

func (ctx *ObserverContext) GetEntityId() EntityId

type ObserverFunc

type ObserverFunc func(ctx *ObserverContext) error

type Query

type Query struct {
	// contains filtered or unexported fields
}

Component Query

func (*Query) AtLeastOneOfThem

func (q *Query) AtLeastOneOfThem(tps []reflect.Type)

func (*Query) AtLeastOneOfThemReadonly

func (q *Query) AtLeastOneOfThemReadonly(tps []reflect.Type)

type QueryResult

type QueryResult struct {
	// contains filtered or unexported fields
}

func (*QueryResult) ForeachEntities

func (qr *QueryResult) ForeachEntities(fn func(accessor *ArcheTypeAccessor) error) error

func (*QueryResult) GetArcheType

func (qr *QueryResult) GetArcheType(idx int) *ArcheType

func (*QueryResult) GetArcheTypeCount

func (qr *QueryResult) GetArcheTypeCount() int

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry() *Registry

func (*Registry) AddObserver

func (r *Registry) AddObserver(name string, fn ObserverFunc) *Observer

func (*Registry) AddSystem

func (r *Registry) AddSystem(name string, priority int, fn SystemFn) *System

func (*Registry) CreateEntity

func (r *Registry) CreateEntity() EntityId

func (*Registry) IsActiveEntity

func (r *Registry) IsActiveEntity(entityId EntityId) bool

func (*Registry) RemoveEntity

func (r *Registry) RemoveEntity(entityId EntityId)

func (*Registry) Tick

func (r *Registry) Tick(deltaTime time.Duration, ctx context.Context) error

type System

type System struct {
	// contains filtered or unexported fields
}

func (*System) GetName

func (s *System) GetName() string

func (*System) GetPriority

func (s *System) GetPriority() int

func (*System) NewQuery

func (s *System) NewQuery() *Query

type SystemFn

type SystemFn func(ctx *ExecutionContext) error

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL