goecs

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: Apache-2.0 Imports: 4 Imported by: 36

README

goecs

A simple Go ECS

version godoc Build Status codecov conduct

Info

Entity–component–system (ECS) is an architectural patter that follows the composition over inheritance principle that allows greater flexibility in defining entities where every object in a world.

Every entity consists of one or more components which contains data or state. Therefore, the behavior of an entity can be changed at runtime by systems that add, remove or mutate components.

This eliminates the ambiguity problems of deep and wide inheritance hierarchies that are difficult to understand, maintain and extend.

Common ECS approaches are highly compatible and often combined with data-oriented design techniques.

For a more in deep read on this topic I could recommend this article.

Example

Run it on the Go Playground

package main

import (
	"fmt"
	"github.com/juan-medina/goecs"
	"reflect"
)

// Simple Usage
func main() {
	// creates the world
	world := goecs.New()

	// add our movement system
	world.AddSystem(MovementSystem)

	// add a listener
	world.AddListener(ChangePostListener)

	// add a first entity
	world.AddEntity(
		Pos{X: 0, Y: 0},
		Vel{X: 1, Y: 1},
	)

	// this entity shouldn't be updated
	world.AddEntity(
		Pos{X: 2, Y: 2},
	)

	// add a third entity
	world.AddEntity(
		Pos{X: 2, Y: 2},
		Vel{X: 3, Y: 3},
	)

	// ask the world to update
	if err := world.Update(0.5); err != nil {
		fmt.Printf("error on update %v\n", err)
	}

	// print the world
	fmt.Println()
	fmt.Println("Pos:")
	for it := world.Iterator(PosType); it != nil; it = it.Next() {
		fmt.Println(it.Value().Get(PosType))
	}
}

// MovementSystem is a simple movement system
func MovementSystem(world *goecs.World, delta float32) error {
	// get all the entities that we need to update, only if they have Pos & Vel
	for it := world.Iterator(PosType, VelType); it != nil; it = it.Next() {
		// get the values
		ent := it.Value()
		pos := ent.Get(PosType).(Pos)
		vel := ent.Get(VelType).(Vel)

		// calculate new pos
		npos := Pos{
			X: pos.X + (vel.X * delta),
			Y: pos.Y + vel.Y*delta,
		}

		// signal the change
		if err := world.Signal(PosChangeSignal{From: pos, To: npos}); err != nil {
			return err
		}

		// set the new pos
		ent.Set(npos)
	}

	return nil
}

// ChangePostListener listen to PosChangeSignal
func ChangePostListener(world *goecs.World, signal interface{}, delta float32) error {
	switch s := signal.(type) {
	case PosChangeSignal:
		// print the change
		fmt.Printf("pos change from %v to %v\n", s.From, s.To)
	}
	return nil
}

// Pos represent a 2D position
type Pos struct {
	X float32
	Y float32
}

// PosType is the reflect.Type of Pos
var PosType = reflect.TypeOf(Pos{})

// Vel represent a 2D velocity
type Vel struct {
	X float32
	Y float32
}

// VelType is the reflect.Type of Vel
var VelType = reflect.TypeOf(Vel{})

// PosChangeSignal is a signal that a Pos has change
type PosChangeSignal struct {
	From Pos
	To   Pos
}

This will output:

pos change from {0 0} to {0.5 0.5}
pos change from {2 2} to {3.5 3.5}

Pos:
{0.5 0.5}
{2 2}
{3.5 3.5}

Installation

go get -v -u github.com/juan-medina/goecs

License

    Copyright (C) 2020 Juan Medina

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

Documentation

Overview

Package goecs provides a Go implementation of an Entity–component–system (ECS)

Entity–component–system (ECS) is an architectural patter that follows the composition over inheritance principle that allows greater flexibility in defining entities where every object in a world.

Every entity consists of one or more components which contains data or state. Therefore, the behavior of an entity can be changed at runtime by systems that add, remove or mutate components.

This eliminates the ambiguity problems of deep and wide inheritance hierarchies that are difficult to understand, maintain and extend.

Common ECS approaches are highly compatible and often combined with data-oriented design techniques.

See: World, Entity

Example

Simple Usage

/*
 * Copyright (c) 2020 Juan Medina.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

package main

import (
	"fmt"
	"github.com/juan-medina/goecs"
	"reflect"
)

// Simple Usage
func main() {
	// creates the world
	world := goecs.New()

	// add our movement system
	world.AddSystem(MovementSystem)

	// add a listener
	world.AddListener(ChangePostListener)

	// add a first entity
	world.AddEntity(
		Pos{X: 0, Y: 0},
		Vel{X: 1, Y: 1},
	)

	// this entity shouldn't be updated
	world.AddEntity(
		Pos{X: 2, Y: 2},
	)

	// add a third entity
	world.AddEntity(
		Pos{X: 2, Y: 2},
		Vel{X: 3, Y: 3},
	)

	// ask the world to update
	if err := world.Update(0.5); err != nil {
		fmt.Printf("error on update %v\n", err)
	}

	// print the world
	fmt.Println()
	fmt.Println("Pos:")
	for it := world.Iterator(PosType); it != nil; it = it.Next() {
		fmt.Println(it.Value().Get(PosType))
	}
}

// MovementSystem is a simple movement system
func MovementSystem(world *goecs.World, delta float32) error {
	// get all the entities that we need to update, only if they have Pos & Vel
	for it := world.Iterator(PosType, VelType); it != nil; it = it.Next() {
		// get the values
		ent := it.Value()
		pos := ent.Get(PosType).(Pos)
		vel := ent.Get(VelType).(Vel)

		// calculate new pos
		npos := Pos{
			X: pos.X + (vel.X * delta),
			Y: pos.Y + vel.Y*delta,
		}

		// signal the change
		if err := world.Signal(PosChangeSignal{From: pos, To: npos}); err != nil {
			return err
		}

		// set the new pos
		ent.Set(npos)
	}

	return nil
}

// ChangePostListener listen to PosChangeSignal
func ChangePostListener(world *goecs.World, signal interface{}, delta float32) error {
	switch s := signal.(type) {
	case PosChangeSignal:
		// print the change
		fmt.Printf("pos change from %v to %v\n", s.From, s.To)
	}
	return nil
}

// Pos represent a 2D position
type Pos struct {
	X float32
	Y float32
}

// PosType is the reflect.Type of Pos
var PosType = reflect.TypeOf(Pos{})

// Vel represent a 2D velocity
type Vel struct {
	X float32
	Y float32
}

// VelType is the reflect.Type of Vel
var VelType = reflect.TypeOf(Vel{})

// PosChangeSignal is a signal that a Pos has change
type PosChangeSignal struct {
	From Pos
	To   Pos
}
Output:

pos change from {0 0} to {0.5 0.5}
pos change from {2 2} to {3.5 3.5}

Pos:
{0.5 0.5}
{2 2}
{3.5 3.5}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entity

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

Entity represents a instance of an object in a ECS

func NewEntity

func NewEntity(components ...interface{}) *Entity

NewEntity creates a new Entity giving a set of varg components

func (*Entity) Add

func (ent *Entity) Add(component interface{}) *Entity

Add a new component into an Entity

func (Entity) Contains

func (ent Entity) Contains(rtypes ...reflect.Type) bool

Contains check that the Entity has the given varg reflect.Type

func (Entity) Get

func (ent Entity) Get(rtype reflect.Type) interface{}

Get the component of the given reflect.Type

func (Entity) ID

func (ent Entity) ID() int64

ID : get the unique id for this Entity

func (Entity) NotContains

func (ent Entity) NotContains(rtypes ...reflect.Type) bool

NotContains check that the Entity has not the given varg reflect.Type

func (*Entity) Remove

func (ent *Entity) Remove(typ reflect.Type)

Remove the component of the given reflect.Type

func (*Entity) Set

func (ent *Entity) Set(component interface{}) *Entity

Set a new component into an Entity

func (Entity) String

func (ent Entity) String() string

String : get a string representation of an Entity

type Iterator

type Iterator interface {
	// Next returns next element
	Next() Iterator
	// Value returns the current item value
	Value() *Entity
}

Iterator for view

type Listener

type Listener func(world *World, signal interface{}, delta float32) error

Listener the get notified that a new signal has been received by World.Signal

type System

type System func(world *World, delta float32) error

System get invoke with Update() from a World

type View

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

View represent a set of Entity objects

func NewView

func NewView() *View

NewView creates a new empty View

func (*View) AddEntity

func (vw *View) AddEntity(components ...interface{}) *Entity

AddEntity a Entity instance to a View given it components

func (*View) Clear

func (vw *View) Clear()

Clear removes all Entity from the view.View

func (*View) Iterator

func (vw *View) Iterator(rtypes ...reflect.Type) Iterator

Iterator return an view.Iterator for the given varg reflect.Type

func (*View) Remove

func (vw *View) Remove(ent *Entity) error

Remove a Entity from a View

func (View) Size

func (vw View) Size() int

Size of Entity in the View

func (*View) Sort

func (vw *View) Sort(less func(a, b *Entity) bool)

Sort the entities in place with a less function

func (View) String

func (vw View) String() string

String get a string representation of a View

type World

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

World is a view.View that contains the Entity and System of our ECS

func New

func New() *World

New creates a new World

func (*World) AddListener

func (world *World) AddListener(lis Listener)

AddListener adds the given Listener to the world

func (*World) AddListenerWithPriority

func (world *World) AddListenerWithPriority(lis Listener, priority int32)

AddListenerWithPriority adds the given Listener to the world with a priority

func (*World) AddSystem

func (world *World) AddSystem(sys System)

AddSystem adds the given System to the world

func (*World) AddSystemWithPriority

func (world *World) AddSystemWithPriority(sys System, priority int32)

AddSystemWithPriority adds the given System to the world with a priority

func (*World) Clear

func (world *World) Clear()

Clear removes all System, Listener, Signals and Entity from the World

func (*World) Signal

func (world *World) Signal(signal interface{}) error

Signal to be sent

func (World) String

func (world World) String() string

String get a string representation of our World

func (*World) Update

func (world *World) Update(delta float32) error

Update ask to update the System send the pending signals

Directories

Path Synopsis
Package sparse provides the creation of sparse.Slice via sparse.NewSlice
Package sparse provides the creation of sparse.Slice via sparse.NewSlice

Jump to

Keyboard shortcuts

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