comparison src/semicongine/entity.nim @ 122:506090173619

did: implement uniforms, some refactoring
author Sam <sam@basx.dev>
date Sun, 09 Apr 2023 01:04:54 +0700
parents dfaddaf96f09
children 5871acc2977e
comparison
equal deleted inserted replaced
121:dfaddaf96f09 122:506090173619
14 children*: seq[Entity] 14 children*: seq[Entity]
15 components*: seq[Component] 15 components*: seq[Component]
16 16
17 17
18 method `$`*(entity: Entity): string {.base.} = entity.name 18 method `$`*(entity: Entity): string {.base.} = entity.name
19 method `$`*(part: Component): string {.base.} = 19 method `$`*(component: Component): string {.base.} =
20 "Unknown Component" 20 "Unknown Component"
21 21
22 proc add*(entity: Entity, child: Entity) = 22 proc add*(entity: Entity, child: Entity) =
23 child.parent = entity 23 child.parent = entity
24 entity.children.add child 24 entity.children.add child
25 proc add*(entity: Entity, part: Component) = 25 proc add*(entity: Entity, component: Component) =
26 part.entity = entity 26 component.entity = entity
27 entity.components.add part 27 entity.components.add component
28 proc add*(entity: Entity, children: seq[Entity]) = 28 proc add*(entity: Entity, children: seq[Entity]) =
29 for child in children: 29 for child in children:
30 child.parent = entity 30 child.parent = entity
31 entity.children.add child 31 entity.children.add child
32 proc add*(entity: Entity, components: seq[Component]) = 32 proc add*(entity: Entity, components: seq[Component]) =
33 for part in components: 33 for component in components:
34 part.entity = entity 34 component.entity = entity
35 entity.components.add part 35 entity.components.add component
36 36
37 func newEntity*(name: string = ""): Entity = 37 func newEntity*(name: string = ""): Entity =
38 result = new Entity 38 result = new Entity
39 result.name = name 39 result.name = name
40 result.transform = Unit4 40 result.transform = Unit4
49 result.name = name 49 result.name = name
50 result.transform = Unit4 50 result.transform = Unit4
51 if result.name == "": 51 if result.name == "":
52 result.name = &"Entity[{$(cast[ByteAddress](result))}]" 52 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
53 53
54 proc newEntity*(name: string, firstPart: Component, components: varargs[Component]): Entity = 54 proc newEntity*(name: string, firstComponent: Component, components: varargs[Component]): Entity =
55 result = new Entity 55 result = new Entity
56 result.name = name 56 result.name = name
57 result.add firstPart 57 result.add firstComponent
58 for part in components: 58 for component in components:
59 result.add part 59 result.add component
60 if result.name == "": 60 if result.name == "":
61 result.name = &"Entity[{$(cast[ByteAddress](result))}]" 61 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
62 result.transform = Unit4 62 result.transform = Unit4
63 63
64 func getModelTransform*(entity: Entity): Mat4 = 64 func getModelTransform*(entity: Entity): Mat4 =
66 var currentEntity = entity 66 var currentEntity = entity
67 while currentEntity != nil: 67 while currentEntity != nil:
68 result = currentEntity.transform * result 68 result = currentEntity.transform * result
69 currentEntity = currentEntity.parent 69 currentEntity = currentEntity.parent
70 70
71 iterator allPartsOfType*[T: Component](root: Entity): T = 71 iterator allComponentsOfType*[T: Component](root: Entity): T =
72 var queue = @[root] 72 var queue = @[root]
73 while queue.len > 0: 73 while queue.len > 0:
74 let entity = queue.pop 74 let entity = queue.pop
75 for part in entity.components: 75 for component in entity.components:
76 if part of T: 76 if component of T:
77 yield T(part) 77 yield T(component)
78 for i in countdown(entity.children.len - 1, 0): 78 for i in countdown(entity.children.len - 1, 0):
79 queue.add entity.children[i] 79 queue.add entity.children[i]
80 80
81 func firstWithName*(root: Entity, name: string): Entity = 81 func firstWithName*(root: Entity, name: string): Entity =
82 var queue = @[root] 82 var queue = @[root]
85 for child in next.children: 85 for child in next.children:
86 if child.name == name: 86 if child.name == name:
87 return child 87 return child
88 queue.add child 88 queue.add child
89 89
90 func firstPartWithName*[T: Component](root: Entity, name: string): T = 90 func firstComponentWithName*[T: Component](root: Entity, name: string): T =
91 var queue = @[root] 91 var queue = @[root]
92 while queue.len > 0: 92 while queue.len > 0:
93 let next = queue.pop 93 let next = queue.pop
94 for child in next.children: 94 for child in next.children:
95 if child.name == name: 95 if child.name == name:
96 for part in child.components: 96 for component in child.components:
97 if part of T: 97 if component of T:
98 return T(part) 98 return T(component)
99 queue.add child 99 queue.add child
100 100
101 func allWithName*(root: Entity, name: string): seq[Entity] = 101 func allWithName*(root: Entity, name: string): seq[Entity] =
102 var queue = @[root] 102 var queue = @[root]
103 while queue.len > 0: 103 while queue.len > 0:
105 for child in next.children: 105 for child in next.children:
106 if child.name == name: 106 if child.name == name:
107 result.add child 107 result.add child
108 queue.add child 108 queue.add child
109 109
110 func allPartsWithName*[T: Component](root: Entity, name: string): seq[T] = 110 func allComponentsWithName*[T: Component](root: Entity, name: string): seq[T] =
111 var queue = @[root] 111 var queue = @[root]
112 while queue.len > 0: 112 while queue.len > 0:
113 let next = queue.pop 113 let next = queue.pop
114 for child in next.children: 114 for child in next.children:
115 if child.name == name: 115 if child.name == name:
116 for part in child.components: 116 for component in child.components:
117 if part of T: 117 if component of T:
118 result.add T(part) 118 result.add T(component)
119 queue.add child 119 queue.add child
120 120
121 iterator allEntities*(root: Entity): Entity = 121 iterator allEntities*(root: Entity): Entity =
122 var queue = @[root] 122 var queue = @[root]
123 while queue.len > 0: 123 while queue.len > 0: