ecs

package
v0.0.0-...-544bd7e Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2016 License: BSD-2-Clause Imports: 5 Imported by: 1

Documentation

Overview

Package ecs is the implementation of the Entity Component System design used by github.com/paked/engi. It is predominately used by games, however will find use in other applications.

Copyright 2014-2015 Harrison Shoebridge and other contributors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component interface {
	Type() string
}

Component is a piece of data which belongs to an Entity

type Entity

type Entity struct {
	Pattern string
	// contains filtered or unexported fields
}

Entity is the E in Entity Component System. It belongs to any amount of Systems, and has a number of Components

func NewEntity

func NewEntity(requires []string) *Entity

NewEntity creates a new Entity given an array of Systems which should be required

func (*Entity) AddComponent

func (e *Entity) AddComponent(component Component)

AddComponent adds a new Component to the Entity

func (*Entity) Component

func (e *Entity) Component(x interface{}) bool

Component takes a double pointer to a Component, and populates it with the value of the right type.

func (*Entity) ComponentFast

func (e *Entity) ComponentFast(c Component) interface{}

ComponentFast returns the same object as Component but without using reflect (and thus faster). Be sure to define the .Type() such that it takes a pointer receiver

func (*Entity) DoesRequire

func (e *Entity) DoesRequire(name string) bool

DoesRequire checks if the Entity requires a system

func (*Entity) ID

func (e *Entity) ID() string

ID returns the string ID of the Entity

func (*Entity) RemoveComponent

func (e *Entity) RemoveComponent(component Component)

RemoveComponent removes a Component from the Entity

type System

type System struct {
	EntityMap            map[string]*Entity
	ShouldSkipOnHeadless bool
}

System is the default implementation of the Systemer interface.

func NewSystem

func NewSystem() *System

NewSystem returns a new default System

func (*System) AddEntity

func (s *System) AddEntity(entity *Entity)

func (System) Entities

func (s System) Entities() []*Entity

func (System) New

func (s System) New()

func (System) Post

func (s System) Post()

func (System) Pre

func (s System) Pre()

func (System) Priority

func (s System) Priority() int

func (*System) RemoveEntity

func (s *System) RemoveEntity(entity *Entity)

func (System) RunInParallel

func (s System) RunInParallel() bool

type Systemer

type Systemer interface {
	// Type returns an individual string identifier, usually the struct name
	// eg. "RenderSystem", "CollisionSystem"...
	Type() string
	// Priority is used to create the order in which Systemers are processed
	Priority() int
	// RunInParallel checks whether the System can run in parallel. This is ran every update,
	// so it is possible to run serial when the system has < 10 entities, and then run in parallel
	// when not
	RunInParallel() bool

	// New is the initialisation of the System
	New(*World)
	// Pre is ran just before the System updates, every frame
	Pre()
	// Post is ran just after the System updates, every frame
	Post()
	// Update is ran every frame, and for each Entity which belongs to the
	// System
	Update(entity *Entity, dt float32)

	// Entities returns a slice of all Entities
	Entities() []*Entity
	// AddEntity adds a new Entity to the System
	AddEntity(entity *Entity)
	// AddEntity removes an Entity from the System
	RemoveEntity(entity *Entity)
}

Systemer is an interface which implements a System. A System iterates over the Entitys it is required by, and can process their Components

type Systemers

type Systemers []Systemer

Systemers implements a sortable list of System. It is indexed on System.Priority().

func (Systemers) Len

func (s Systemers) Len() int

func (Systemers) Less

func (s Systemers) Less(i, j int) bool

func (Systemers) Swap

func (s Systemers) Swap(i, j int)

type World

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

World contains a bunch of Entitys, and a bunch of Systems. It is the recommended way to run ecs

func (*World) AddEntity

func (w *World) AddEntity(entity *Entity)

AddEntity adds a new Entity to the World, and its required Systems

func (*World) AddSystem

func (w *World) AddSystem(system Systemer)

AddSystem adds a new System to the World, and then sorts them based on Priority

func (*World) Entities

func (w *World) Entities() []*Entity

Entities returns the list of Entities

func (*World) HasSystem

func (w *World) HasSystem(systemType string) bool

func (*World) New

func (w *World) New()

New initialises the World

func (*World) RemoveEntity

func (w *World) RemoveEntity(entity *Entity)

RemoveEntity removes an Entity from the World and its required Systems

func (*World) Systems

func (w *World) Systems() []Systemer

Systems returns a list of Systems

func (*World) Update

func (w *World) Update(dt float32)

Update is called on each frame, with dt being the time difference in seconds since the last Update call

Jump to

Keyboard shortcuts

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