Example #1
0
        private void handleVesselUpdate(KLFVesselUpdate vessel_update)
        {
            //Debug.Log("*** Handling update!");

            //Build the key for the vessel
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(vessel_update.ownerName);
            sb.Append(vessel_update.id.ToString());

            String vessel_key = sb.ToString();

            KLFVessel vessel = null;

            //Try to find the key in the vessel dictionary
            VesselEntry entry;
            if (vessels.TryGetValue(vessel_key, out entry))
            {
                vessel = entry.vessel;

                if (vessel == null || vessel.gameObj == null || vessel.vesselName != vessel_update.vesselName)
                {
                    //Delete the vessel if it's null or needs to be renamed
                    vessels.Remove(vessel_key);

                    if (vessel != null && vessel.gameObj != null)
                        GameObject.Destroy(vessel.gameObj);

                    vessel = null;
                }
                else
                {
                    //Update the entry's timestamp
                    VesselEntry new_entry = new VesselEntry();
                    new_entry.vessel = entry.vessel;
                    new_entry.lastUpdateTime = UnityEngine.Time.fixedTime;

                    vessels[vessel_key] = new_entry;
                }
            }

            if (vessel == null) {
                //Add the vessel to the dictionary
                vessel = new KLFVessel(vessel_update.vesselName, vessel_update.ownerName, vessel_update.id);
                entry = new VesselEntry();
                entry.vessel = vessel;
                entry.lastUpdateTime = UnityEngine.Time.fixedTime;

                if (vessels.ContainsKey(vessel_key))
                    vessels[vessel_key] = entry;
                else
                    vessels.Add(vessel_key, entry);

                /*Queue this update for the next update call because updating a vessel on the same step as
                 * creating it usually causes problems for some reason */
                vesselUpdateQueue.Enqueue(vessel_update);
            }
            else
                applyVesselUpdate(vessel_update, vessel); //Apply the vessel update to the existing vessel

            //Debug.Log("*** Updated handled");
        }
Example #2
0
        private void applyVesselUpdate(KLFVesselUpdate vessel_update, KLFVessel vessel)
        {
            //Debug.Log("*** Handling vessel update!");

            //Find the CelestialBody that matches the one in the update
            CelestialBody update_body = null;

            if (vessel.mainBody != null && vessel.mainBody.bodyName == vessel_update.bodyName)
                update_body = vessel.mainBody; //Vessel already has the correct body
            else
            {

                //Find the celestial body in the list of bodies
                foreach (CelestialBody body in FlightGlobals.Bodies)
                {
                    if (body.bodyName == vessel_update.bodyName)
                    {
                        update_body = body;
                        break;
                    }
                }

            }

            if (update_body != null)
            {

                //Convert float arrays to Vector3s
                Vector3 pos = new Vector3(vessel_update.localPosition[0], vessel_update.localPosition[1], vessel_update.localPosition[2]);
                Vector3 dir = new Vector3(vessel_update.localDirection[0], vessel_update.localDirection[1], vessel_update.localDirection[2]);
                Vector3 vel = new Vector3(vessel_update.localVelocity[0], vessel_update.localVelocity[1], vessel_update.localVelocity[2]);

                vessel.vesselName = vessel_update.vesselName;

                vessel.setOrbitalData(update_body, pos, vel, dir);

                vessel.situation = vessel_update.situation;
                vessel.state = vessel_update.state;
                vessel.timeScale = vessel_update.timeScale;

            }
        }