much simpler component handling in NAT
This commit is contained in:
@@ -47,20 +47,8 @@
|
|||||||
(joinPath archiveDir "entries" (e.id.withExtension "json"))
|
(joinPath archiveDir "entries" (e.id.withExtension "json"))
|
||||||
(Json.stringify e)))
|
(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]
|
(method fullString [:Entry e]
|
||||||
(haxe.Json.stringify (fullData e) null "\t"))
|
(haxe.Json.stringify e null "\t"))
|
||||||
|
|
||||||
(function :Entry _newEntry []
|
(function :Entry _newEntry []
|
||||||
(object
|
(object
|
||||||
|
|||||||
@@ -184,7 +184,7 @@
|
|||||||
|
|
||||||
(defCommand PrintComponent [entries (SelectedEntries null null)
|
(defCommand PrintComponent [entries (SelectedEntries null null)
|
||||||
componentType (Text 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)]
|
(defCommand CreateEntry [name (Text null)]
|
||||||
[(archive.createEntry ->e
|
[(archive.createEntry ->e
|
||||||
|
|||||||
@@ -6,32 +6,19 @@
|
|||||||
(defMacro hasComponent [e componentType]
|
(defMacro hasComponent [e componentType]
|
||||||
`(.exists .components ,e ,(symbolName 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
|
// Changes to the object returned by (readComponent) will not be saved! Use (withWritableComponents) for making changes
|
||||||
(defMacro readComponent [archive e componentType]
|
(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
|
`(let [componentData (dictGet (the Map<String,String> .components ,e) ,(symbolName componentType))]
|
||||||
`(the nat.components ,componentType
|
(print (+ "reading " componentData " as " ,(symbolName componentType) " for " .id ,e))
|
||||||
(tink.Json.parse
|
(the nat.components ,componentType
|
||||||
(sys.io.File.getContent (_componentPath ,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
|
||||||
|
(tink.Json.parse componentData))))
|
||||||
// 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)))})
|
|
||||||
|
|
||||||
// TODO check not overwriting a component
|
// TODO check not overwriting a component
|
||||||
(defMacro addComponent [archive e componentType c]
|
(defMacro addComponent [archive e componentType c]
|
||||||
`(withWritableEntry ,archive ,e (let [componentId (uuid.Uuid.v4)]
|
`(withWritableEntry ,archive ,e
|
||||||
(dictSet .components ,e ,(symbolName componentType) componentId)
|
(print (+ "adding " (the nat.components ,componentType ,c) " as " ,(symbolName componentType) " for " .id ,e))
|
||||||
(writeComponent ,archive ,e ,componentType ,c)
|
(dictSet .components ,e ,(symbolName componentType) (tink.Json.stringify (the nat.components ,componentType ,c)))))
|
||||||
,e)))
|
|
||||||
|
|
||||||
// Retrieve multiple components from an Entity with mutable access.
|
// Retrieve multiple components from an Entity with mutable access.
|
||||||
// All components will be serialized after the block is done.
|
// All components will be serialized after the block is done.
|
||||||
@@ -47,7 +34,7 @@
|
|||||||
(doFor [name type] bindingPairs
|
(doFor [name type] bindingPairs
|
||||||
(bindingList.push `&mut ,name)
|
(bindingList.push `&mut ,name)
|
||||||
(bindingList.push `(readComponent ,archive ,e ,type))
|
(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
|
`(let [,@bindingList
|
||||||
,retValSymbol {,@body}]
|
,retValSymbol {,@body}]
|
||||||
,@saveList
|
,@saveList
|
||||||
|
|||||||
Reference in New Issue
Block a user