Example #1
0
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)
	}
}
Example #2
0
// 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
}