Entity Component System framework for Ebitengine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

package gohan
import (
var componentMutex sync.Mutex
// ComponentID is a component identifier. Each Component is assigned a unique ID
// via NewComponentID, and implements a ComponentID method returning its ID.
type ComponentID int
// Component represents data for an entity, and how it interacts with the world.
type Component interface {
ComponentID() ComponentID
var maxComponentID ComponentID
// NewComponentID returns the next available ComponentID.
func NewComponentID() ComponentID {
defer mutex.Unlock()
defer entityMutex.Unlock()
defer componentMutex.Unlock()
for i := Entity(1); i < maxEntityID; i++ {
gameComponents[i] = append(gameComponents[i], nil)
return maxComponentID
// AddComponent adds a Component to an Entity.
func (entity Entity) AddComponent(component Component) {
componentID := component.ComponentID()
gameComponents[entity][componentID] = component
defer entityMutex.Unlock()
modifiedEntities = append(modifiedEntities, entity)
// Component gets a Component of an Entity.
func (entity Entity) Component(componentID ComponentID) interface{} {
components := gameComponents[entity]
if components == nil {
return nil
return components[componentID]