annotate src/zamikongine/thing.nim @ 489:54a1f8ee208e

big refactoring, part1
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 14:08:00 +0700
parents 14e5151f68d1
children 9edca5dc4e93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
1 {.experimental: "codeReordering".}
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
2 import std/times
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
3
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
4 type
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
5 Part* = object of RootObj
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
6 thing: ref Thing
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
7
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
8 Thing* = object of RootObj
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
9 parent*: ref Thing
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
10 children*: seq[ref Thing]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
11 parts*: seq[ref Part]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
12
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
13 method update*(thing: ref Thing, dt: Duration) {.base.} = discard
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
14
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
15 iterator partsOfType*[T: ref Part](root: ref Thing): T =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
16 var queue = @[root]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
17 while queue.len > 0:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
18 let thing = queue.pop
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
19 for part in thing.parts:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
20 if part of T:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
21 yield T(part)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
22 for child in thing.children:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
23 queue.insert(child, 0)
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
24
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
25 iterator allEntities*(root: ref Thing): ref Thing =
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
26 var queue = @[root]
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
27 while queue.len > 0:
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
28 let next = queue.pop
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
29 for child in next.children:
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
30 queue.add child
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 480
diff changeset
31 yield next