much simpler component handling in NAT

This commit is contained in:
2021-09-27 22:44:52 -06:00
parent 8cf5fe8a5a
commit 8e7b77ca8a
3 changed files with 11 additions and 36 deletions

View File

@@ -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