zinc

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2020 License: MIT Imports: 0 Imported by: 0

README

Zinc

codecov Build Status

ZincECS is an entity-component-system package inspired by Simon Schmid's Entitas-CSharp and Atom proof-of-concept but for the go language. ZincECS uses code-generation to achieve a nice API similar to EntitasECS using the built-in ZincCLI. This package puts focus on modularity and ease of use with performance coming in as a close second.

Installation

This will install the ZincCLI along with the zinc package.

go get github.com/SirMetathyst/zinc/...

Quickstart

There isn't much we can do without any components so lets go ahead and generate some before we start.

zinc component add -p components -n position -d x:float32 -d y:float32 -o ./components
zinc component add -p components -n velocity -d x:float32 -d y:float32 -o ./components

We first call the ZincCLI and pass in some arguments. The p argument tells the ZincCLI that we want our generated component to have the package name of components. Then we tell it we want a component with the name of position and specify the data. The format must be in name:type but no checks are done to ensure valid go code. Lastly, we give it a folder where we want our component files to be generated in. Now we have some components generated to play with.

package main

import (
    "github.com/SirMetathyst/zinc"

    // importing your component package will
    // automatically register component types
    // with the default entity manager
    // see generated files for how to do it manually
    // if required. The convention I use is to call it a "kit"
    // where the root contains all your components and /systems
    // contains all logic for those components
    "xxx/xxx/to/yourkit"
)

func main() {

    // create an entity
    // uses the built-in default entity manager
    id := zinc.CreateEntity()

    // we can already use our component types 
    // setting a component will add or update it
    yourkit.SetPosition(id, yourkit.PositionData{10, 10})


    // get position with id
    pos := yourkit.Position(id)


    // there is also an API for passing in a different entity manager
    // these end in X
    pos := yourkit.PositionX(entityManager, id)

    // getting all entity ids in the entity manager
    entities := zinc.Entities()


    // getting groups of entities with specific components
    // will return a entity group that has position and velocity component
    group1 := zinc.Group(zinc.AllOf(yourkit.PositionKey, yourkit.VelocityKey))

    // ids of entities in group
    group1.Entities()

    // does the group have an entity?
    group1.HasEntity(id)

    // does the component exist for entity
    ok := yourkit.HasPosition(id)

    // delete the position component 
    yourkit.DeletePosition(id)

    // will return a entity group that has position but not velocity component
    group2 := zinc.Group(zinc.AllOf(yourkit.PositionKey).NoneOf(yourkit.VelocityKey))  
}

Systems

Zinc has a simple, built-in way to init/update/cleanup your systems

sys := zinc.NewSystems()
sys.Add(yourkit.NewPositionSystem())
sys.Add(yourkit.NewAllSystems()...)

// init systems, must have `Initialize()` method
sys.Initialize()

// update systems, must have `Update(dt float64)` method
sys.Update(deltaTime)

// cleanup systems, must have `Cleanup()` method
sys.Cleanup()

then you can implement a system for moving your position components around. add the system to Systems and loop through those and that's your game loop. You can even have systems for drawing things in a different Systems instance and execute them at different times. You can even return Systems of Systems and update them as a group of systems as long as that type has the supported method.


import (
    "github.com/SirMetathyst/zinc"
    "github.com/xxx/yourkit"
)

// PositionSystem ...
type PositionSystem struct {
	g         zinc.G
	em *zinc.EntityManager
}

// NewPositionSystem ...
func NewPositionSystem() *PositionSystem {
	return &PositionSystem{
		em: zinc.Default(),
		g:  zinc.Default().Group(zinc.AllOf(yourkit.PositionKey, yourkit.VelocityKey)),
	}
}

// NewPositionSystemWith ...
func NewPositionSystemWith(em *zinc.EntityManager) *PositionSystem {
	return &PositionSystem{
		em: em,
		g:  em.Group(zinc.AllOf(yourkit.PositionKey, yourkit.VelocityKey)),
	}
}

// Update ...
func (s PositionSystem) Update(dt float64) {
	for _, id := range s.g.Entities() {
		velocity := yourkit.VelocityX(s.em, id)
		position := yourkit.PositionX(s.em, id)
		position.X += velocity.X * dt
		position.Y += velocity.Y * dt
		yourkit.SetPositionX(s.em, id, position)
	}
}

Contributing

I dont really have a contribution guideline. Just post an issue or pull request if you'd like to add or change something in ZincECS. I generally welcome pull requests but don't be disappointed if it gets rejected. You can always fork it.

Projects/Examples I use ZincECS in

  • Zinckit - Collection of Systems and Components for ZincECS projects

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteEntities

func DeleteEntities()

DeleteEntities ...

func DeleteEntity

func DeleteEntity(id EntityID)

DeleteEntity ...

func GroupCount

func GroupCount() int

GroupCount ...

func HasEntity

func HasEntity(id EntityID) bool

HasEntity ...

func Reset

func Reset()

Reset ...

func ResetAll

func ResetAll()

ResetAll ...

Types

type C

type C interface {
	Entities() []EntityID
	ClearCollectedEntities()
}

C ...

func CreateCollector

func CreateCollector(et ...ET) C

CreateCollector ...

func NewCollector

func NewCollector(group []G, groupEvent []GroupEvent) C

NewCollector ... TODO: Write TEST

type CMP

type CMP interface {
	DeleteEntity(id EntityID)
	HasEntity(id EntityID) bool
}

CMP ...

type CTX

type CTX interface {
	ComponentAdded(key uint, id EntityID)
	ComponentDeleted(key uint, id EntityID)
	ComponentUpdated(key uint, id EntityID)
	HasEntity(id EntityID) bool
}

CTX ...

type CU

type CU interface {
	Cleanup()
}

CU ...

type ET

type ET interface {
	Matcher() M
	GroupEvent() GroupEvent
}

ET ...

func Added

func Added(keys ...uint) ET

Added ...

func Deleted

func Deleted(keys ...uint) ET

Deleted ...

func Updated

func Updated(keys ...uint) ET

Updated ...

type EntityEventFunc

type EntityEventFunc func(key uint, id EntityID)

EntityEventFunc ...

type EntityID

type EntityID uint

EntityID ...

func CreateEntity

func CreateEntity() EntityID

CreateEntity ...

func Entities

func Entities() []EntityID

Entities ...

type EntityManager

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

EntityManager ...

func Default

func Default() *EntityManager

Default ...

func NewEntityManager

func NewEntityManager() *EntityManager

NewEntityManager ...

func (*EntityManager) Component

func (e *EntityManager) Component(key uint) (c CMP, ok bool)

Component ...

func (*EntityManager) CreateCollector

func (e *EntityManager) CreateCollector(et ...ET) C

CreateCollector ...

func (*EntityManager) CreateEntity

func (e *EntityManager) CreateEntity() EntityID

CreateEntity ...

func (*EntityManager) DeleteEntities

func (e *EntityManager) DeleteEntities()

DeleteEntities ...

func (*EntityManager) DeleteEntity

func (e *EntityManager) DeleteEntity(id EntityID)

DeleteEntity ...

func (*EntityManager) Entities

func (e *EntityManager) Entities() []EntityID

Entities ...

func (*EntityManager) Group

func (e *EntityManager) Group(m M) G

Group ...

func (*EntityManager) GroupCount

func (e *EntityManager) GroupCount() int

GroupCount ...

func (*EntityManager) HasEntity

func (e *EntityManager) HasEntity(id EntityID) bool

HasEntity ...

func (*EntityManager) RegisterComponent

func (e *EntityManager) RegisterComponent(key uint, c CMP) CTX

RegisterComponent ...

func (*EntityManager) Reset

func (e *EntityManager) Reset()

Reset ...

func (*EntityManager) ResetAll

func (e *EntityManager) ResetAll()

ResetAll ...

type G

type G interface {
	HandleEntitySilently(id EntityID)
	HandleEntity(key uint, id EntityID)
	UpdateEntity(key uint, id EntityID)
	HasEntity(id EntityID) bool
	Entities() []EntityID
	HandleEntityAdded(f EntityEventFunc)
	HandleEntityDeleted(f EntityEventFunc)
	HandleEntityUpdated(f EntityEventFunc)
}

G ...

func Group

func Group(m M) G

Group ...

type GroupEvent

type GroupEvent uint

GroupEvent ...

const (
	// GroupEventAdded ...
	GroupEventAdded GroupEvent = iota
	// GroupEventDeleted ...
	GroupEventDeleted
	// GroupEventUpdated ...
	GroupEventUpdated
)

type I

type I interface {
	Initialize()
}

I ...

type M

type M interface {
	HasAllOf(keys ...uint) bool
	HasNoneOf(keys ...uint) bool

	AllOfSlice() []uint
	AllOf(keys ...uint) M
	NoneOfSlice() []uint
	NoneOf(keys ...uint) M

	Match(e *EntityManager, id EntityID) bool
	Hash() uint
}

M ...

func AllOf

func AllOf(keys ...uint) M

AllOf ...

func NewMatcher

func NewMatcher() M

NewMatcher ...

func NoneOf

func NoneOf(keys ...uint) M

NoneOf ...

type S

type S interface{}

S ...

type SS

type SS interface {
	// Initialize ...
	I
	// Update ...
	U
	// Cleanup ...
	CU
	Add(sys ...S)
}

SS ...

func NewSystems

func NewSystems() SS

NewSystems ... TODO: Write TEST

type U

type U interface {
	Update(dt float64)
}

U ...

Directories

Path Synopsis
cmd
Package kit ...
Package kit ...

Jump to

Keyboard shortcuts

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