ecs

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2021 License: MIT Imports: 4 Imported by: 14

README

Entity Component System

A fast, code generate ECS (no more interface{}). Game Engine agnostic.

GoDoc

Example:

package mycomponents

import "github.com/gabstv/ecs/v2"


type World struct {
	ecs.World
}

func NewWorld() *World {
	base := ecs.NewWorld()
	ecs.RegisterWorldDefaults(base)
	return &World{
		World: base,
	}
}

// Update all systems (sorted by priority)
func (w *World) Update(dt float64) {
	w.EachSystem(func(s ecs.System) bool {
		s.(System).Update(dt)
		return true
	})
}

type System interface {
	ecs.System
	Update(dt float64)
}

// ecsgen will create the component+system logic:

//go:generate go run ecsgen

// Position component data
//
// ecs:component
// uuid:9F414CAD-4C1B-49B2-980E-0A61302AD5DE
type Position struct {
	X float64
	Y float64
}

// Velocity component
//
// ecs:component
// the uuid for this component is generated automatically
type Velocity struct {
	X       float64
	Y       float64
}

// Update MovementSystem matches
//
// ecs:system
// uuid:43838027-AA12-4AD2-9F09-5DCBDA589779
// name:MovementSystem
// components: Position, Velocity
func (s *MovementSystem) Update(dt float64) {
	for _, v := range s.V().Matches() {
		v.Position.X += v.Velocity.X
		v.Position.Y += v.Velocity.Y
	}
}

Documentation

Index

Constants

View Source
const MaxFlagCapacity = 256

Variables

This section is empty.

Functions

func DispatchComponentEvent added in v2.2.0

func DispatchComponentEvent(c Component, t EventType, e Entity)

DispatchComponentEvent is a helper to dispatch component events

func RegisterComponent

func RegisterComponent(fn RegisterComponentFn)

func RegisterSystem

func RegisterSystem(fn RegisterSystemFn)

func RegisterWorldDefaults

func RegisterWorldDefaults(w World)

Types

type BaseSystem

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

BaseSystem implements Enable(), Disable() and Enabled() from ecs.System

func (*BaseSystem) Disable

func (s *BaseSystem) Disable()

Disable this system

func (*BaseSystem) Enable

func (s *BaseSystem) Enable()

Enable this system

func (*BaseSystem) Enabled

func (s *BaseSystem) Enabled() bool

Enabled returns if this system is enabled

type Component added in v2.2.0

type Component interface {
	UUID() string
	Name() string
	Flag() Flag
	Setup(w World, f Flag, key [4]byte)
	Upsert(e Entity, data interface{}) bool
	Remove(e Entity) bool
	World() World
}

type Entity

type Entity uint64

type EntityFlag

type EntityFlag struct {
	Entity Entity
	Flag   Flag
}

type EntityFlagSlice

type EntityFlagSlice []EntityFlag

func (EntityFlagSlice) Len

func (a EntityFlagSlice) Len() int

func (EntityFlagSlice) Less

func (a EntityFlagSlice) Less(i, j int) bool

func (EntityFlagSlice) Swap

func (a EntityFlagSlice) Swap(i, j int)

type Event added in v2.0.11

type Event struct {
	Type          EventType
	ComponentName string
	ComponentID   string
	Entity        Entity
}

type EventFn added in v2.0.11

type EventFn func(e Event)

type EventListener added in v2.0.11

type EventListener struct {
	ID   int64
	Mask EventType
	Fn   EventFn
}

type EventType added in v2.0.11

type EventType uint
const (
	EvtNone              EventType = 0
	EvtComponentAdded    EventType = 1 << 0
	EvtComponentRemoved  EventType = 1 << 1
	EvtComponentsResized EventType = 1 << 2
	EvtAny               EventType = 0b11111111111111111111111111111111
)

func (EventType) String added in v2.0.17

func (t EventType) String() string

type Flag

type Flag [4]uint64

Flag is a 256 bit binary flag

func MergeFlags added in v2.2.0

func MergeFlags(f ...Flag) Flag

MergeFlags performs an OR operation to return a single flag

func NewFlag

func NewFlag(bit uint8) Flag

NewFlag creates a new flag

func NewFlagRaw

func NewFlagRaw(a, b, c, d uint64) Flag

NewFlagRaw creates a new flag

func (Flag) And

func (f Flag) And(g Flag) Flag

And bitwise (f & g)

func (Flag) Clone

func (f Flag) Clone() Flag

Clone returns a new flag with identical data

func (Flag) Contains

func (f Flag) Contains(g Flag) bool

Contains tests if (f & g == g)

func (Flag) ContainsAny added in v2.1.0

func (f Flag) ContainsAny(g Flag) bool

ContainsAny tests if f contains at least one bit of g

func (Flag) Equals

func (f Flag) Equals(g Flag) bool

Equals checs if g contains the same bits

func (Flag) IsZero

func (f Flag) IsZero() bool

IsZero returns true if all bits are zero

func (Flag) Lowest added in v2.1.2

func (f Flag) Lowest() uint8

Lowest bit position (set to 1)

func (Flag) Or

func (f Flag) Or(g Flag) Flag

Or bitwise (f | g)

func (Flag) Xor

func (f Flag) Xor(g Flag) Flag

Xor bitwise (f ^ g)

type Locker added in v2.1.1

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

func (*Locker) Item added in v2.1.1

func (l *Locker) Item(name string) interface{}

func (*Locker) SetItem added in v2.1.1

func (l *Locker) SetItem(name string, value interface{})

type MatchFn

type MatchFn func(f Flag, w World) bool

type RegisterComponentFn

type RegisterComponentFn func() Component

type RegisterSystemFn

type RegisterSystemFn func() System

type SortedEntities

type SortedEntities []Entity

SortedEntities implements sort.Interface

func (SortedEntities) Len

func (a SortedEntities) Len() int

func (SortedEntities) Less

func (a SortedEntities) Less(i, j int) bool

func (SortedEntities) Swap

func (a SortedEntities) Swap(i, j int)

type System added in v2.2.0

type System interface {
	UUID() string
	Name() string
	ComponentAdded(e Entity, eflag Flag)
	ComponentRemoved(e Entity, eflag Flag)
	ComponentWillResize(cflag Flag)
	ComponentResized(cflag Flag)
	//V() View
	Priority() int64
	Setup(w World)
	Enable()
	Disable()
	Enabled() bool
}

type View

type View interface {
}

type World

type World interface {
	RegisterComponent(c Component)
	IsRegistered(id string) bool
	CFlag(e Entity) Flag
	NewEntity() Entity
	RemoveEntity(e Entity) bool
	C(id string) Component
	S(id string) System
	CAdded(e Entity, c Component, key [4]byte)
	CRemoved(e Entity, c Component, key [4]byte)
	CWillResize(c Component, key [4]byte)
	CResized(c Component, key [4]byte)
	AddSystem(s System) error
	RemoveSystem(s System)
	EachSystem(func(s System) bool)
	Dispatch(e Event)
	Listen(mask EventType, fn EventFn) int64
	RemoveListener(id int64)
	SetFlagGroup(name string, f Flag)
	FlagGroup(name string) Flag
	LGet(name string) interface{}
	LSet(name string, value interface{})
}

func NewWorld

func NewWorld() World

Directories

Path Synopsis
cmd
examples
simple
Code generated by ecs https://github.com/gabstv/ecs; DO NOT EDIT.
Code generated by ecs https://github.com/gabstv/ecs; DO NOT EDIT.

Jump to

Keyboard shortcuts

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