v2 / vlib / v / tests / generics / generic_receiver_embed_test.v
100 lines · 78 sloc · 1.79 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1pub type Entity = u64
2type ComponentType = u16
3
4pub struct Context {
5pub mut:
6 ecs &Ecs
7}
8
9///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10pub struct EntityManager {
11mut:
12 living_entity_count u64
13}
14
15fn (mut self EntityManager) create_entity() Entity {
16 entity := self.living_entity_count
17 self.living_entity_count++
18 return entity
19}
20
21///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
22
23pub interface ISystem {
24 ecs &Ecs
25mut:
26 init()
27}
28
29pub struct System {
30pub mut:
31 ecs &Ecs
32}
33
34pub fn (mut self System) init() {
35}
36
37pub struct SystemManager {
38mut:
39 system_array map[string]ISystem = {}
40}
41
42fn (mut self SystemManager) add_system[T]() &T {
43 mut t := T{}
44 self.system_array[typeof[T]().name] = t
45 return &t
46}
47
48////////////////////////////////////////////////////////////////////////////
49
50@[heap]
51pub struct Ecs {
52mut:
53 entity_manager EntityManager
54 system_manager SystemManager
55pub mut:
56 root_entity Entity
57}
58
59pub fn Ecs.new() &Ecs {
60 mut ecs := &Ecs{}
61 ecs.root_entity = ecs.create_entity()
62 return ecs
63}
64
65pub fn (mut self Ecs) create_entity() Entity {
66 return self.entity_manager.create_entity()
67}
68
69pub fn (mut self Ecs) add_system[T]() {
70 mut system := self.system_manager.add_system[T]()
71 system.ecs = &self
72 system.init()
73}
74
75////////////////////////////////////////////////////////////////////////////////////////////////////////
76
77struct NetworkUdpServerComponent {
78}
79
80struct NetworkServerSystemUdp {
81 System
82}
83
84pub struct ConfigSystem {
85 System
86}
87
88struct NetworkServerSystemUdpExt {
89 NetworkServerSystemUdp
90}
91
92fn test_main() {
93 mut ecs := Ecs.new()
94 mut root_entity := ecs.create_entity()
95
96 ecs.add_system[ConfigSystem]()
97 ecs.add_system[NetworkServerSystemUdpExt]()
98
99 assert true
100}
101