You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import (
"github.com/wfranczyk/ento"
)
typeComponent1struct{ valueint }
typeComponent2struct{ valueint }
typeComponent3struct{ valueint }
typeSystemstruct {
// Always use pointer to base component type (!)// Add `ento` tag to automatically bind componentsC1*Component1`ento:"required"`C2*Component2`ento:"required"`C3*Component2`ento:"optional"`// Systems can have non-component-bound fieldscallsint
}
// Update implements ento.System// Based on `ento` tag, entities will be selected for update by the system.// Entities that do not contain all the `required` components will be skipped.func (s*System) Update(entity*ento.Entity) {
// When Update is called, all the tagged components// will be already replaced with values from entitys.C1.value+=s.C2.value// Optional field will be set to null if entity does not contain themifs.C3!=nil {
s.C1.value+=s.C3.value
}
}
funcmain() {
// Create the world and register componentsworld:=ento.NewWorldBuilder().
// Use "zero-values" as values are ignored when registeringWithSparseComponents(Component1{}, Component2{}, Component3{}).
// Pre-allocate space for 256 entities (world can grow beyond that automatically)Build(256)
// Add systemssystem:=&System{}
world.AddSystems(system)
// Create entities (they are added to the world immediately)entity:=world.NewEntity(Component1{1}, Component2{1})
// Use Set to add or change their componentsentity.Set(Component3{1})
// Update the worldworld.Update()
// Use Get to receive component value (or nil if not present)varc1*Component1entity.Get(&c1)
println(c1.value==3) // true// Use Rem to remove component from entity// As Component2 is `required` in the System// it will no longer be updated by itentity.Rem(Component2{})
world.Update()
entity.Get(&c1)
println(c1.value==3) // true - the entity is no longer updated by the system
}
License
Ento is distributed under MIT license.
MIT License
Copyright (c) 2021 Wojciech Franczyk
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.