예제 #1
0
    public static VehicleSR Load(string path, bool removeOnError = false)
    {
        VehicleSR _v;

        using (FileStream file = File.Open(path, FileMode.Open))
        {
            try
            {
                _v = formatter.Deserialize(file) as VehicleSR;
            }
            catch (Exception ex)
            {
                _v = null;
                if (ex is XmlException)
                {
                    Debug.LogError("Invalid XML");
                }
                else if (ex is FormatException)
                {
                    Debug.LogError("Error while Deserializing");
                }
                else
                {
                    throw;
                }
            }
        }
        if (_v == null && removeOnError)
        {
            File.Delete(path);
            _v = new VehicleSR();
        }
        return(_v);
    }
예제 #2
0
    /// <summary>
    /// Load a SR vehicle
    /// </summary>
    /// <param name="file">File to load</param>
    /// <returns></returns>
    public GameObject LoadVehicleSR(string file)
    {
        VehicleSR v = VehicleSR.Load(file);

        if (v.version != 1)
        {
            return(null);
        }
        float mass = 0;

        Destroy(GameObject.Find("Vehicle"));
        GameObject vehicleGO = new GameObject("Vehicle");

        foreach (VehicleSR.Part part in v.parts)
        {
            GameObject go;
            GameObject prefab = null;
            if (!assets.TryGetValue(part.type, out prefab))
            {
                prefab = new GameObject();
                prefab.AddComponent <Rigidbody2D>();
                prefab.AddComponent <BoxCollider2D>();
                prefab.AddComponent <FixedJoint2D>();
                Debug.LogErrorFormat("The prefab '{0}' of part '{1}' is null.", part.type, part.id);
            }

            go = Instantiate(
                prefab,
                new Vector3(part.x, part.y),
                Quaternion.Euler(0, 0, Mathf.Rad2Deg * part.r)
                ) as GameObject;

            try
            {
                mass += go.GetComponent <Rigidbody2D>().mass;
            }
            catch { }
            go.GetComponent <Rigidbody2D>().simulated = false;
            go.name = part.id.ToString();
            go.transform.SetParent(vehicleGO.transform);
            go.transform.localScale = new Vector3(
                (part.flipX ? -1 : 1) / go.transform.parent.localScale.x,
                (part.flipY ? -1 : 1) / go.transform.parent.localScale.y, 1);
            if (part.type.ToLower().Contains("pod"))
            {
                go.tag = "Player";
                ActivationGroups ag     = go.AddComponent <ActivationGroups>();
                List <string[]>  stages = new List <string[]>();
                foreach (List <VehicleSR.Activate> stage in part.pod.stages)
                {
                    List <string> _stage = new List <string>();
                    foreach (VehicleSR.Activate act in stage)
                    {
                        _stage.Add(act.id.ToString());
                    }
                    stages.Add(_stage.ToArray());
                }
                ag.steps = stages.ToArray();
                ag.ready = true;
            }
        }
        massText.text = (mass * 500).ToString("N0") + " kg";



        foreach (VehicleSR.Connection con in v.connections)        //Set parents and connections
        {
            GameObject parent = GameObject.Find(con.parentPart.ToString());
            GameObject child  = GameObject.Find(con.childPart.ToString());
            child.transform.SetParent(parent.transform);
            if (child.CompareTag("Wheel"))
            {
                AnchoredJoint2D joint = child.GetComponent <AnchoredJoint2D>();
                joint.connectedBody   = parent.GetComponent <Rigidbody2D>();
                joint.connectedAnchor = child.transform.localPosition;
                joint.enabled         = true;
            }
            else
            {
                AnchoredJoint2D joint = child.GetComponent <AnchoredJoint2D>();
                joint.connectedBody   = parent.GetComponent <Rigidbody2D>();
                joint.enableCollision = true;
                joint.anchor          = child.transform.worldToLocalMatrix.MultiplyPoint3x4(
                    child.GetComponent <Collider2D>().bounds.ClosestPoint(parent.transform.position));
                joint.enabled = true;
            }
        }
        return(vehicleGO);
    }