示例#1
0
    public void StartTech()
    {
        DirectoryInfo techPath = new DirectoryInfo(@"db2\");
        FileInfo      tech     = techPath.GetFiles("vehicle.tech")[0];
        BinaryReader  br       = new BinaryReader(File.Open(tech.FullName, FileMode.Open));

        byte[] resource = new byte[tech.Length];
        br.Read(resource, 0, (int)tech.Length);
        br.Close();
        br       = null;
        tech     = null;
        techPath = null;
        int pos = 0;

        pos         += 4;       //13 ver??
        TechName     = byteRead.GetLine(resource, ref pos);
        creationTime = System.BitConverter.ToInt32(resource, pos);
        pos         += 4;
        System.DateTime dt = System.DateTime.FromBinary(creationTime);
        blockSize     = System.BitConverter.ToInt32(resource, pos);
        pos          += 4;
        AddParamCount = System.BitConverter.ToInt32(resource, pos);
        pos          += 4;

        unknVeh = System.BitConverter.ToInt32(resource, pos);
        pos    += 4;

        CarCount = System.BitConverter.ToInt32(resource, pos);
        pos     += 4;

        LorrieCout = System.BitConverter.ToInt32(resource, pos);
        pos       += 4;

        TractorCount = System.BitConverter.ToInt32(resource, pos);
        pos         += 4;

        SemiTrCount = System.BitConverter.ToInt32(resource, pos);
        pos        += 4;

        HeliCount = System.BitConverter.ToInt32(resource, pos);
        pos      += 4;

        VehCount = System.BitConverter.ToInt32(resource, pos);
        pos     += 4;

        TrucksBehaviour tb = TRUCKS.GetComponent <TrucksBehaviour>();

        for (int i = 0; i < VehCount; i++)
        {
            //GameObject newOb = new GameObject();
            //var veh = newOb.AddComponent<vehicle>();
            var veh = new vehicle();
            veh.Read(resource, ref pos);
            Transform truckOb = TRUCKS.transform.Find(veh.CarNode);
            if (truckOb)
            {
                Mesh me = new Mesh();
                me.Clear();
                List <Vector3> CM     = new List <Vector3>();
                List <int>     faces  = new List <int>();
                int            number = 0;
                for (int j = 0; j < veh.CornerMarkCount; j++)
                {
                    CM.Add(veh.CornerMark[j]);

                    /*if (j == 0)
                     * {
                     *      faces.AddRange(new int[] {0,2,1});
                     *      number+=2;
                     * }
                     * else
                     * {
                     *      if (number+1 == veh.CornerMarkCount)
                     *              faces.AddRange(new int[3]{(number),(number+1),(number-1)});
                     *      else
                     *              faces.AddRange(new int[6]{(number),(number+1),(number-1),(number+1),(number),(number+2)});
                     *      number+=2;
                     * }*/
                }
                me.vertices  = CM.ToArray();
                me.triangles = faces.ToArray();
                me.RecalculateBounds();
                var a = truckOb.gameObject.AddComponent <MeshCollider>();

                a.sharedMesh = me;
                a.convex     = true;
                me           = null;
                Component copy = truckOb.gameObject.AddComponent <vehicle>();

                System.Reflection.FieldInfo[] fields = veh.GetType().GetFields();
                foreach (System.Reflection.FieldInfo field in fields)
                {
                    field.SetValue(copy, field.GetValue(veh));
                }
                tb.Trucks.Add(truckOb.gameObject);

                Transform gob = null;

                GameObject Wheels = new GameObject("Wheel Colliders");
                Wheels.transform.SetParent(truckOb);
                foreach (Transform go in truckOb.transform)
                //for (int j = 0; j<truckOb.transform.childCount;j++)
                {
                    if (go.name == "hit_" + veh.CarNode)
                    {
                        truckOb.gameObject.GetComponent <vehicle>().hitObj = go.gameObject;
                    }

                    for (int k = 0; k < 8; k++)
                    {
                        //Debug.LogWarning(truckOb.transform.GetChild(j).name+"	:	"+veh.CarNode+"wheel"+j);
                        if (go.name == veh.CarNode + "wheel" + k)

                        /*gob = truckOb.Find(veh.CarNode+"wheel"+j);
                         *
                         * if (gob)*/
                        {
                            gob = go;
                            string WSpace = gob.GetChild(0).GetComponent <InvokeMe>().space;

                            truckOb.gameObject.GetComponent <vehicle>().WheelMe.Add(gob.transform.GetChild(0).gameObject);

                            string Space = gob.transform.GetChild(0).GetComponent <InvokeMe>().space;
                            int    num   = (int)System.Char.GetNumericValue(Space.Split(new string[] { "Wheel" }, System.StringSplitOptions.None)[1][0]);

                            truckOb.gameObject.GetComponent <vehicle>().WheelNum.Add(num);

                            GameObject SpcOb = TRUCKS.transform.Find(WSpace).gameObject;

                            /*WheelCollider wc = WheelCol.AddComponent<WheelCollider>(); //TODO: Удалить если используем JCar
                             * wc.mass = 20;
                             * wc.radius = veh.rwheel;
                             * //wc.wheelDampingRate = 0.25f;
                             * //wc.suspensionDistance = veh.SuspensionMax[1] - veh.SuspensionMin[1];
                             * //wc.suspensionDistance = 0.3f;
                             * wc.wheelDampingRate = veh.SuspensionMin[1]; //хз, откуда брать параметр
                             * wc.suspensionDistance = veh.SuspensionMax[1] - veh.SuspensionMin[1];
                             * JointSpring js = new JointSpring();
                             *
                             * js.spring = veh.KS[1]; //привет из Car Races Demo: сила пружины: ks*(hmax-h)
                             * js.damper = 4500;
                             * js.targetPosition = veh.SuspensionMin[1];
                             * wc.suspensionSpring = js;
                             * WheelFrictionCurve wf = new WheelFrictionCurve();
                             * wf.extremumSlip = 0.4f;
                             * wf.extremumValue = 1;
                             * wf.asymptoteSlip = 0.8f;
                             * wf.asymptoteValue = 0.5f;
                             * wf.stiffness = veh.CS[1];
                             * wc.forwardFriction = wf;
                             * WheelFrictionCurve sf = new WheelFrictionCurve();
                             * sf.extremumSlip = 0.2f;
                             * sf.extremumValue = 1;
                             * sf.asymptoteSlip = 0.5f;
                             * sf.asymptoteValue = 0.75f;
                             * sf.stiffness = 1;
                             * wc.sidewaysFriction = sf;*/
                        }
                        gob = null;
                    }
                }
            }
            else
            {
                Debug.LogError(veh.CarNode + " node not found");
            }
            //newOb.transform.SetParent(transform);
        }
    }