Last active
July 19, 2020 19:45
-
-
Save planetis-m/7040f305d21b3953b41aff5d7e9201cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const componentRegistry = CacheSeq"anymap.componentRegistry" | |
proc makeField(n: NimNode): NimNode = | |
let s = n.strVal | |
result = ident(toLowerAscii(s[0]) & substr(s, 1)) | |
macro component*(s: untyped): untyped = | |
expectKind s, nnkTypeDef | |
result = copyNimTree(s) | |
componentRegistry.add result[0].basename | |
macro world*(s: untyped): untyped = | |
expectKind s, nnkTypeDef | |
result = copyNimTree(s) | |
if result[2][2].kind == nnkEmpty: | |
result[2][2] = newNimNode(nnkRecList) | |
template makeStorage(component): untyped = | |
seq[component] | |
for c in items(componentRegistry): | |
result[2][2].add newIdentDefs(c.makeField, getAst(makeStorage(c))) | |
when isMainModule: | |
type | |
Position {.component.} = object | |
x, y: float | |
Velocity {.component.} = object | |
x, y: float | |
Acceleration {.component.} = object | |
x, y: float | |
World {.world.} = object | |
registry: seq[uint16] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
worth using this pattern instead: