comparison 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
comparison
equal deleted inserted replaced
488:455f1a416fe7 489:54a1f8ee208e
1 {.experimental: "codeReordering".} 1 {.experimental: "codeReordering".}
2 import std/times
2 3
3 type 4 type
4 Part* = object of RootObj 5 Part* = object of RootObj
5 thing: ref Thing 6 thing: ref Thing
6 7
7 Thing* = object of RootObj 8 Thing* = object of RootObj
8 parent*: ref Thing 9 parent*: ref Thing
9 children*: seq[ref Thing] 10 children*: seq[ref Thing]
10 parts*: seq[ref Part] 11 parts*: seq[ref Part]
11 12
13 method update*(thing: ref Thing, dt: Duration) {.base.} = discard
14
12 iterator partsOfType*[T: ref Part](root: ref Thing): T = 15 iterator partsOfType*[T: ref Part](root: ref Thing): T =
13 var queue = @[root] 16 var queue = @[root]
14 while queue.len > 0: 17 while queue.len > 0:
15 let thing = queue.pop 18 let thing = queue.pop
16 for part in thing.parts: 19 for part in thing.parts:
17 if part of T: 20 if part of T:
18 yield T(part) 21 yield T(part)
19 for child in thing.children: 22 for child in thing.children:
20 queue.insert(child, 0) 23 queue.insert(child, 0)
24
25 iterator allEntities*(root: ref Thing): ref Thing =
26 var queue = @[root]
27 while queue.len > 0:
28 let next = queue.pop
29 for child in next.children:
30 queue.add child
31 yield next