much simpler component handling in NAT
This commit is contained in:
@@ -6,32 +6,19 @@
|
||||
(defMacro hasComponent [e componentType]
|
||||
`(.exists .components ,e ,(symbolName componentType)))
|
||||
|
||||
(defMacro _componentPath [archive e componentType]
|
||||
`(joinPath .archiveDir (the nat.Archive ,archive) "components" (+ (dictGet (the Map<String,String> .components ,e) ,(symbolName componentType)) ".json")))
|
||||
|
||||
// Changes to the object returned by (readComponent) will not be saved! Use (withWritableComponents) for making changes
|
||||
(defMacro readComponent [archive e componentType]
|
||||
// TODO add to the documentation a hint that macros should use fully qualified type paths so macro caller classes don't need to import everything
|
||||
`(the nat.components ,componentType
|
||||
(tink.Json.parse
|
||||
(sys.io.File.getContent (_componentPath ,archive ,e ,componentType)))))
|
||||
|
||||
// Components have to be saved individually after writing because the Entity only knows their ids,
|
||||
// not the data they contain. This is more ergonomically done by using (withWritableComponents...)
|
||||
(defMacro writeComponent [archive e componentType c]
|
||||
`{
|
||||
(print (+ "saving " (the nat.components ,componentType ,c) " as " ,(symbolName componentType) " for " .id ,e))
|
||||
(sys.io.File.saveContent
|
||||
(_componentPath ,archive ,e ,componentType)
|
||||
(tink.Json.stringify
|
||||
(the nat.components ,componentType ,c)))})
|
||||
`(let [componentData (dictGet (the Map<String,String> .components ,e) ,(symbolName componentType))]
|
||||
(print (+ "reading " componentData " as " ,(symbolName componentType) " for " .id ,e))
|
||||
(the nat.components ,componentType
|
||||
// TODO add to the documentation a hint that macros should use fully qualified type paths so macro caller classes don't need to import everything
|
||||
(tink.Json.parse componentData))))
|
||||
|
||||
// TODO check not overwriting a component
|
||||
(defMacro addComponent [archive e componentType c]
|
||||
`(withWritableEntry ,archive ,e (let [componentId (uuid.Uuid.v4)]
|
||||
(dictSet .components ,e ,(symbolName componentType) componentId)
|
||||
(writeComponent ,archive ,e ,componentType ,c)
|
||||
,e)))
|
||||
`(withWritableEntry ,archive ,e
|
||||
(print (+ "adding " (the nat.components ,componentType ,c) " as " ,(symbolName componentType) " for " .id ,e))
|
||||
(dictSet .components ,e ,(symbolName componentType) (tink.Json.stringify (the nat.components ,componentType ,c)))))
|
||||
|
||||
// Retrieve multiple components from an Entity with mutable access.
|
||||
// All components will be serialized after the block is done.
|
||||
@@ -47,7 +34,7 @@
|
||||
(doFor [name type] bindingPairs
|
||||
(bindingList.push `&mut ,name)
|
||||
(bindingList.push `(readComponent ,archive ,e ,type))
|
||||
(saveList.push `(writeComponent ,archive ,e ,type ,name)))
|
||||
(saveList.push `(dictSet .components ,e ,(symbolName type) (tink.Json.stringify (the nat.components ,type ,name)))))
|
||||
`(let [,@bindingList
|
||||
,retValSymbol {,@body}]
|
||||
,@saveList
|
||||
|
||||
Reference in New Issue
Block a user