| 1 | pub type Entity = u64 |
| 2 | type ComponentType = u16 |
| 3 | |
| 4 | pub struct Context { |
| 5 | pub mut: |
| 6 | ecs &Ecs |
| 7 | } |
| 8 | |
| 9 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 10 | pub struct EntityManager { |
| 11 | mut: |
| 12 | living_entity_count u64 |
| 13 | } |
| 14 | |
| 15 | fn (mut self EntityManager) create_entity() Entity { |
| 16 | entity := self.living_entity_count |
| 17 | self.living_entity_count++ |
| 18 | return entity |
| 19 | } |
| 20 | |
| 21 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | pub interface ISystem { |
| 24 | ecs &Ecs |
| 25 | mut: |
| 26 | init() |
| 27 | } |
| 28 | |
| 29 | pub struct System { |
| 30 | pub mut: |
| 31 | ecs &Ecs |
| 32 | } |
| 33 | |
| 34 | pub fn (mut self System) init() { |
| 35 | } |
| 36 | |
| 37 | pub struct SystemManager { |
| 38 | mut: |
| 39 | system_array map[string]ISystem = {} |
| 40 | } |
| 41 | |
| 42 | fn (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] |
| 51 | pub struct Ecs { |
| 52 | mut: |
| 53 | entity_manager EntityManager |
| 54 | system_manager SystemManager |
| 55 | pub mut: |
| 56 | root_entity Entity |
| 57 | } |
| 58 | |
| 59 | pub fn Ecs.new() &Ecs { |
| 60 | mut ecs := &Ecs{} |
| 61 | ecs.root_entity = ecs.create_entity() |
| 62 | return ecs |
| 63 | } |
| 64 | |
| 65 | pub fn (mut self Ecs) create_entity() Entity { |
| 66 | return self.entity_manager.create_entity() |
| 67 | } |
| 68 | |
| 69 | pub 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 | |
| 77 | struct NetworkUdpServerComponent { |
| 78 | } |
| 79 | |
| 80 | struct NetworkServerSystemUdp { |
| 81 | System |
| 82 | } |
| 83 | |
| 84 | pub struct ConfigSystem { |
| 85 | System |
| 86 | } |
| 87 | |
| 88 | struct NetworkServerSystemUdpExt { |
| 89 | NetworkServerSystemUdp |
| 90 | } |
| 91 | |
| 92 | fn 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 | |