func (goc *JGoc) Unmarshal(data interface{}) { m := data.(map[string]interface{}) // Unmarshall all "normal" fields for k, v := range m { if k == "Type" || k == "Components" { continue } if k == "Name" { goc.SetName(v.(string)) continue } f := reflect.ValueOf(goc).Elem().FieldByName(k) serialization.SerializeInPlace(f, v) } // Special case handling for `Components` for _, v := range m["Components"].([]interface{}) { compData := v.(map[string]interface{}) typename, _ := compData["Type"] comp := serialization.FactoryFunc(typename.(string)).(JComponent) serialization.SerializeInPlace(comp, compData) // comp.SetOwner(goc) goc.AddComps(comp) } }
func TestVector_Unmarshal(t *testing.T) { b := []byte(`{"Type": "vec2.Vector", "X": 25, "Y": 12}`) holder, err := support.ReadData(b) if err != nil { panic(err) } v := Vector{} serialization.SerializeInPlace(&v, holder) c := test.Checker(t) c.Expect(test.EQ, Vector{25, 12}, v) }
// LoadConfig is needed as a backdoor to factory not being inititalized func LoadConfig(file string) types.JGameObject { defer debug.Trace().UnTrace() data, err := support.OpenFile(file) if err != nil { debug.Fatal("Failed to open Config file: " + file) } holder, err := support.ReadData(data) if err != nil { debug.Fatal("Failed to read in Config file: " + file) return nil } goc := types.JGoc{} serialization.SerializeInPlace(&goc, holder) return &goc }
// s := f.Create(f1, Name("space")) // goc1 := f.Create(f2, Owner(s), Name("goc1")) // goc2 := f.Create(f2, Owner(s), Dispatcher(d)) func (f *Factory) Create(file string, opts ...FactoryOpt) types.JGameObject { defer debug.Trace().UnTrace() // read in game object data data, err := support.OpenFile(file) if err != nil { debug.Fatal("OpenFile (", file, "):", err) } holder, err := support.ReadData(data) if err != nil { debug.Fatal("ReadData (", file, "):", err) } m := holder.(map[string]interface{}) typename, found := m["Type"] if !found { debug.Fatal("'Type' member not found in holder") } obj := serialization.FactoryFunc(typename.(string)).(types.JGameObject) serialization.SerializeInPlace(obj, holder) // apply options to game object for _, opt := range opts { opt(obj) } // check for transform, check for dispatcher, name etc if obj.Name() == "" { obj.SetName(fmt.Sprint("obj", len(f.ObjList))) } // add game object to factory f.ObjList = append(f.ObjList, obj) return obj // h := types.JGameObject{id: len(f.ObjList)} // f.NameMap[obj.Name()] = h // return h }