-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello.go
99 lines (81 loc) · 3.15 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// +build examples
// 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.
package main
import (
"github.com/wfranczyk/ento"
)
type Component1 struct{ value int }
type Component2 struct{ value int }
type Component3 struct{ value int }
type System struct {
// Always use pointer to base component type (!)
// Add `ento` tag to automatically bind components
C1 *Component1 `ento:"required"`
C2 *Component2 `ento:"required"`
C3 *Component2 `ento:"optional"`
// Systems can have non-component-bound fields
calls int
}
// 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) {
// Before Update is called, all tagged components
// will be replaced with values from entity
s.C1.value += s.C2.value
// Optional field will be set to null if entity does not contain them
if s.C3 != nil {
s.C1.value += s.C3.value
}
s.calls++
}
func main() {
// Create the world and register components
world := ento.NewWorldBuilder().
// Use "zero-values" as values are ignored when registering
WithSparseComponents(Component1{}, Component2{}, Component3{}).
// Pre-allocate space for 256 entities (world can grow beyond that automatically)
Build(256)
// Add systems
system := &System{}
world.AddSystems(system)
// Create an entity (it is added to the world immediately)
entity := world.AddEntity(Component1{1}, Component2{1})
// Use Set to add or change entity components
entity.Set(Component3{1})
// Update the world
world.Update()
println(system.calls) // prints: 1
// Use Get to receive component value (or nil if not present)
var c1 *Component1
entity.Get(&c1)
println(c1.value == 3) // prints: true
// Use Rem to remove component from entity
// As Component2 is `required` in the System
// it will no longer be updated by it
entity.Rem(Component2{})
world.Update()
println(system.calls) // prints: 1 - the system was not called
entity.Get(&c1)
println(c1.value == 3) // true - the entity is no longer updated by the system
}