Exemplo n.º 1
0
// parseJSONPos
// loadJSONRecursive runs down the hierarchy in the JSON file
func loadJSONRecursive(am *assetmanager.AssetManager, node map[string]interface{}, filename string, entityByID map[string]*Entity) *Entity {
	var id string
	var idOK, ok bool

	var childEntity *Entity

	entity := NewEntity(nil)

	// Do this first to help with error messaging
	id, idOK = node["Id"].(string)
	if idOK {
		entity.ID = id
		if entityByID != nil {
			entityByID[id] = entity
		}
	} else {
		id = "[No ID]"
	}

	// Do these first since X and Y might depend on them:
	_, ok = node["W"].(string)
	if ok {
		entity.W = am.ParseDimension(filename, id, "W", node)
	}
	_, ok = node["H"].(string)
	if ok {
		entity.H = am.ParseDimension(filename, id, "H", node)
	}
	assetName, ok := node["Asset"].(string)
	if ok {
		entity.Surface = am.Surfaces[assetName]
		entity.W = entity.Surface.W
		entity.H = entity.Surface.H
	}

	// Now do the rest of the properties
	for k, v := range node {
		switch k {
		case "Id", "W", "H", "Asset":
			// do nothing; handled above
		case "X":
			entity.X = am.ParsePosition(filename, id, entity.W, "X", node)
		case "Y":
			entity.Y = am.ParsePosition(filename, id, entity.H, "Y", node)
		case "Visible":
			entity.Visible = v.(bool)
		case "Children":
			for _, child := range v.([]interface{}) {
				childEntity = loadJSONRecursive(am, child.(map[string]interface{}), filename, entityByID)
				entity.AddChild(childEntity)
			}
		default:
			panic(fmt.Sprintf("scenegraph.LoadJSON: unrecognized key: %s", k))
		}
	}

	return entity
}