| 1 | module main |
| 2 | |
| 3 | interface IGameObject { |
| 4 | mut: |
| 5 | name string |
| 6 | parent ?&IGameObject |
| 7 | children []&IGameObject |
| 8 | add_child(mut o IGameObject) |
| 9 | advance() |
| 10 | } |
| 11 | |
| 12 | @[heap] |
| 13 | struct GameObject implements IGameObject { |
| 14 | mut: |
| 15 | name string |
| 16 | parent ?&IGameObject |
| 17 | children []&IGameObject |
| 18 | } |
| 19 | |
| 20 | fn (mut gameobject GameObject) add_child(mut o IGameObject) { |
| 21 | o.parent = &gameobject |
| 22 | gameobject.children << o |
| 23 | } |
| 24 | |
| 25 | fn (mut gameobject GameObject) advance() { |
| 26 | if gameobject.parent != none { |
| 27 | eprintln('parent: ${gameobject.parent.name} ') |
| 28 | } |
| 29 | for mut child in gameobject.children { |
| 30 | child.advance() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn test_main() { |
| 35 | mut v1 := &GameObject{ |
| 36 | name: 'v1' |
| 37 | } |
| 38 | mut v2 := &GameObject{ |
| 39 | name: 'v2' |
| 40 | } |
| 41 | v1.add_child(mut v2) |
| 42 | v1.advance() |
| 43 | assert true |
| 44 | } |
| 45 | |