comparison src/semicongine/scene.nim @ 305:44ecc0a01a9f

fix: some issues with new scene/api/component api
author Sam <sam@basx.dev>
date Tue, 27 Jun 2023 00:51:36 +0700
parents da0bd61abe91
children 046f7e2b1e13
comparison
equal deleted inserted replaced
304:0c6e73fcc61f 305:44ecc0a01a9f
25 entity*: Entity 25 entity*: Entity
26 26
27 Entity* = ref object of RootObj 27 Entity* = ref object of RootObj
28 name*: string 28 name*: string
29 internal_transform: Mat4 # todo: cache transform + only update VBO when transform changed 29 internal_transform: Mat4 # todo: cache transform + only update VBO when transform changed
30 parent*: Entity 30 parent: Entity
31 children*: seq[Entity] 31 children: seq[Entity]
32 components*: Table[string, Component] 32 components: Table[string, Component]
33 33
34 EntityAnimation* = ref object of Component 34 EntityAnimation* = ref object of Component
35 player: AnimationPlayer[Mat4] 35 player: AnimationPlayer[Mat4]
36 36
37 func newEntityAnimation*(animation: Animation[Mat4]): EntityAnimation = 37 func newEntityAnimation*(animation: Animation[Mat4]): EntityAnimation =
48 func stop*(animation: var EntityAnimation) = 48 func stop*(animation: var EntityAnimation) =
49 animation.player.stop() 49 animation.player.stop()
50 50
51 func update*(animation: var EntityAnimation, dt: float32) = 51 func update*(animation: var EntityAnimation, dt: float32) =
52 animation.player.advance(dt) 52 animation.player.advance(dt)
53
54 func parent(entity: Entity): Entity =
55 entity.parent
53 56
54 func transform*(entity: Entity): Mat4 = 57 func transform*(entity: Entity): Mat4 =
55 result = entity.internal_transform 58 result = entity.internal_transform
56 for component in entity.components.mvalues: 59 for component in entity.components.mvalues:
57 if component of EntityAnimation and EntityAnimation(component).player.playing: 60 if component of EntityAnimation and EntityAnimation(component).player.playing:
127 &"Entity animation: {animation.player.animation}" 130 &"Entity animation: {animation.player.animation}"
128 131
129 proc add*(entity: Entity, child: Entity) = 132 proc add*(entity: Entity, child: Entity) =
130 child.parent = entity 133 child.parent = entity
131 entity.children.add child 134 entity.children.add child
132 proc `[]=`*(entity: Entity, index: int, child: Entity) = 135 proc `[]=`*(entity: var Entity, index: int, child: var Entity) =
133 child.parent = entity 136 child.parent = entity
134 entity.children[index] = child 137 entity.children[index] = child
135 proc `[]`*(entity: Entity, index: int): Entity = 138 proc `[]`*(entity: Entity, index: int): Entity =
136 entity.children[index] 139 entity.children[index]
137 140