Documentation ¶
Overview ¶
Package goecs is our public package
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 ( "github.com/juan-medina/goecs/entity" "github.com/juan-medina/goecs/world" "log" "reflect" ) // Simple Usage func main() { // creates the world wld := world.New() // add our movement system wld.AddSystem(MovementSystem) // add a listener wld.Listen(ChangePostListener) // add a first entity wld.Add(entity.New( Pos{X: 0, Y: 0}, Vel{X: 1, Y: 1}, )) // this entity shouldn't be updated wld.Add(entity.New( Pos{X: 2, Y: 2}, )) // add a third entity wld.Add(entity.New( Pos{X: 2, Y: 2}, Vel{X: 3, Y: 3}, )) // ask the world to update if err := wld.Update(0.5); err != nil { log.Fatalf("error on update %v", err) } // print the world log.Printf("world: %v", wld) } // MovementSystem is a simple movement system func MovementSystem(wld *world.World, delta float32) error { // get all the entities that we need to update, only if they have Pos & Vel for it := wld.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 := wld.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(wld *world.World, signal interface{}, delta float32) error { switch s := signal.(type) { case PosChangeSignal: // print the change log.Printf("pos change from %v to %v", 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:
Directories ¶
Path | Synopsis |
---|---|
Package entity contains the objects to manage the Entity objects in a ECS
|
Package entity contains the objects to manage the Entity objects in a ECS |
Package sparse provides the creation of sparse.Slice via sparse.NewSlice
|
Package sparse provides the creation of sparse.Slice via sparse.NewSlice |
Package view allows to get a View of entity.Entity objects
|
Package view allows to get a View of entity.Entity objects |
Package world allows to create an use our ECS World
|
Package world allows to create an use our ECS World |
Click to show internal directories.
Click to hide internal directories.