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