func TestSerialize_Goc(t *testing.T) {
	// test setup
	CoreFactoryFunc = testFactoryFunc

	data := []byte(
		`{
        "Type": "Goc",
	    "Name": "camera",
        "Components": [
          {
            "Type": "CameraComponent",
            "X": 100,
            "Y": 50
          },
          {
            "Type": "ColliderComponent",
            "Shape": "aabb"
          }
        ]
      }`)

	holder, _ := support.ReadData(data)

	goc := Goc{}
	SerializeInPlace(&goc, holder)
	test.ExpectEQ(t, "camera", goc.Name, "goc.Name was set correctly")

	cmp1 := goc.GetComp("serTestComp1").(*serTestComp1)
	test.ExpectEQ(t, 100, cmp1.X, "cmp1.X was set correctly")
	test.ExpectEQ(t, 50, cmp1.Y, "cmp1.Y was set correctly")
	test.ExpectEQ(t, 0, cmp1.Z, "cmp1.Z was set correctly")

	cmp2 := goc.GetComp("serTestComp2").(*serTestComp2)
	test.ExpectEQ(t, "aabb", cmp2.Shape, "cmp2.Shape was set correctly")
}
예제 #2
0
// LoadConfig serialize in a GameObject from a text file.
func LoadConfig(file string) GameObject {
	defer debug.Trace().UnTrace()

	data, err := support.OpenFile(file)
	if err != nil {
		support.LogFatal("Failed to open Config file: " + file)
	}

	holder, err := support.ReadData(data)
	if err != nil {
		support.LogFatal("Failed to read in Config file: " + file)
		return nil
	}

	goc := Goc{}
	SerializeInPlace(&goc, holder)
	return &goc
}
예제 #3
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 {
		support.LogFatal("Failed to open Config file: " + file)
	}

	holder, err := support.ReadData(data)
	if err != nil {
		support.LogFatal("Failed to read in Config file: " + file)
		return nil
	}

	m := v.(map[string]interface{})
	typename, err := m["Type"]
	if err != nil {
		log.Panic(err)
	}
	obj := factoryFunc(typename.(string))
	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
}
func Example_serialize() {
	// Must set up factory functions for any types that the factory will know
	// about.  These functions will typically go into a game's driver or some
	// other game specific library (since the factory needs to know about
	// every type it can possibly create).
	//
	CoreFactoryFunc = func(name string) interface{} {
		switch name {
		case "CameraComponent":
			return &serTestComp1{}
		case "ColliderComponent":
			return &serTestComp2{}
		case "serTestSpace":
			return &serTestSpace{}
		}
		return nil
	}

	exampleObjectFileData := []byte(
		`{
    "Type":"serTestSpace",
    "Name": "LevelSpace",
    "Gocs": [
        {
            "Type": "Goc",
            "Name": "Camera1",
            "Components": [
                {"Type": "CameraComponent", "X": 10, "Y": 10 },
                {"Type": "ColliderComponent", "Shape": "aabb"}
            ]
        },
        {
            "Type": "Goc",
            "Name": "Camera2",
            "Components": [
                {"Type": "CameraComponent", "X": 20, "Z": 30 },
                {"Type": "ColliderComponent", "Shape": "aabb"}
            ]
        },
        {
            "Type": "Goc",
            "Name": "Player",
            "Components": [
                {"Type": "ColliderComponent", "Shape": "circle"}
            ]
        }
    ]
}`)

	holder, _ := support.ReadData(exampleObjectFileData)

	space := serTestSpace{}
	SerializeInPlace(&space, holder)

	b, _ := json.MarshalIndent(space, "", "    ")
	fmt.Println(string(b))
	// Output:
	// {
	//     "Name": "LevelSpace",
	//     "Gocs": [
	//         {
	//             "Name": "Camera1",
	//             "Components": [
	//                 {
	//                     "X": 10,
	//                     "Y": 10,
	//                     "Z": 0
	//                 },
	//                 {
	//                     "Shape": "aabb"
	//                 }
	//             ]
	//         },
	//         {
	//             "Name": "Camera2",
	//             "Components": [
	//                 {
	//                     "X": 20,
	//                     "Y": 0,
	//                     "Z": 30
	//                 },
	//                 {
	//                     "Shape": "aabb"
	//                 }
	//             ]
	//         },
	//         {
	//             "Name": "Player",
	//             "Components": [
	//                 {
	//                     "Shape": "circle"
	//                 }
	//             ]
	//         }
	//     ]
	// }
}