much simpler component handling in NAT

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

View File

@@ -47,20 +47,8 @@
(joinPath archiveDir "entries" (e.id.withExtension "json"))
(Json.stringify e)))
(method :Dynamic componentData [:Entry e :String componentType]
(haxe.Json.parse (File.getContent (joinPath archiveDir "components" "$(dictGet e.components componentType).json"))))
(method fullData [:Entry e]
(object
id e.id
components
(for =>type id e.components
=>type (componentData e type))
files
e.files))
(method fullString [:Entry e]
(haxe.Json.stringify (fullData e) null "\t"))
(haxe.Json.stringify e null "\t"))
(function :Entry _newEntry []
(object

View File

@@ -184,7 +184,7 @@
(defCommand PrintComponent [entries (SelectedEntries null null)
componentType (Text null)]
(doFor e entries (ui.displayMessage (Std.string (archive.componentData e componentType)))) [])
(doFor e entries (ui.displayMessage (dictGet e.components componentType))) [])
(defCommand CreateEntry [name (Text null)]
[(archive.createEntry ->e

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