Пример #1
0
        private void handleVesselUpdate(KLFVesselUpdate vessel_update)
        {
            if (!isInFlight)
            {
                //While not in-flight don't create KLF vessel, just store the active vessel status info
                if (vessel_update.state == State.ACTIVE) {

                    VesselStatusInfo status = new VesselStatusInfo();
                    status.info = vessel_update;
                    status.ownerName = vessel_update.player;
                    status.vesselName = vessel_update.name;
                    status.orbit = null;
                    status.lastUpdateTime = UnityEngine.Time.realtimeSinceStartup;
                    status.color = KLFVessel.generateActiveColor(status.ownerName);

                    if (playerStatus.ContainsKey(status.ownerName))
                        playerStatus[status.ownerName] = status;
                    else
                        playerStatus.Add(status.ownerName, status);
                }

                return; //Don't handle updates while not flying a ship
            }

            //Build the key for the vessel
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(vessel_update.player);
            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.name)
                {
                    //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.realtimeSinceStartup;

                    vessels[vessel_key] = new_entry;
                }
            }

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

                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
        }
Пример #2
0
        private void ApplyVesselUpdate(KLFVesselUpdate vesselUpdate, KLFVessel kVes)
        {
            //Find the CelestialBody that matches the one in the update
            CelestialBody updateBody = null;
            if(kVes.MainBody != null
            && kVes.MainBody.bodyName == vesselUpdate.BodyName)
                updateBody = kVes.MainBody; //already correct body
            else
                foreach (CelestialBody body in FlightGlobals.Bodies)
                    if (body.bodyName == vesselUpdate.BodyName)
                    {
                        updateBody = body;
                        break;
                    }

            if (updateBody != null)
            {//Convert float arrays to Vector3s
                Vector3 pos = new Vector3(vesselUpdate.Position[0], vesselUpdate.Position[1], vesselUpdate.Position[2]);
                Vector3 dir = new Vector3(vesselUpdate.Direction[0], vesselUpdate.Direction[1], vesselUpdate.Direction[2]);
                Vector3 vel = new Vector3(vesselUpdate.Velocity[0], vesselUpdate.Velocity[1], vesselUpdate.Velocity[2]);

                kVes.Info = vesselUpdate;
                kVes.SetOrbitalData(updateBody, pos, vel, dir);
            }

            if (vesselUpdate.State == State.Active)
            {//Update the player status info
                VesselStatusInfo status = new VesselStatusInfo();
                status.Info = vesselUpdate;
                status.User = vesselUpdate.Player;
                status.VesselName = vesselUpdate.Name;
                if (kVes.OrbitValid)
                    status.Orbit = kVes.OrbitRender.driver.orbit;
                status.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup;
                status.Color = KLFVessel.GenerateActiveColor(status.User);

                if (PlayerStatus.ContainsKey(status.User))
                    PlayerStatus[status.User] = status;
                else
                    PlayerStatus.Add(status.User, status);
            }
        }
Пример #3
0
        private void applyVesselUpdate(KLFVesselUpdate vessel_update, KLFVessel vessel)
        {
            //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.pos[0], vessel_update.pos[1], vessel_update.pos[2]);
                Vector3 dir = new Vector3(vessel_update.dir[0], vessel_update.dir[1], vessel_update.dir[2]);
                Vector3 vel = new Vector3(vessel_update.vel[0], vessel_update.vel[1], vessel_update.vel[2]);

                vessel.info = vessel_update;
                vessel.setOrbitalData(update_body, pos, vel, dir);

            }

            if (vessel_update.state == State.ACTIVE)
            {
                //Update the player status info
                VesselStatusInfo status = new VesselStatusInfo();
                status.info = vessel_update;
                status.ownerName = vessel_update.player;
                status.vesselName = vessel_update.name;

                if (vessel.orbitValid)
                    status.orbit = vessel.orbitRenderer.driver.orbit;

                status.lastUpdateTime = UnityEngine.Time.realtimeSinceStartup;
                status.color = KLFVessel.generateActiveColor(status.ownerName);

                if (playerStatus.ContainsKey(status.ownerName))
                    playerStatus[status.ownerName] = status;
                else
                    playerStatus.Add(status.ownerName, status);
            }
        }
Пример #4
0
        private void HandleVesselUpdate(KLFVesselUpdate vesselUpdate)
        {
            if (!IsInFlight)
            {
                //While not in-flight don't create KLF vessel, just store the active vessel status info
                if (vesselUpdate.State == State.Active)
                {
                    VesselStatusInfo status = new VesselStatusInfo();
                    status.Info = vesselUpdate;
                    status.User = vesselUpdate.Player;
                    status.VesselName = vesselUpdate.Name;
                    status.Orbit = null;
                    status.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup;
                    status.Color = KLFVessel.GenerateActiveColor(status.User);
                    if (PlayerStatus.ContainsKey(status.User))
                        PlayerStatus[status.User] = status;
                    else
                        PlayerStatus.Add(status.User, status);
                }
                return; //Don't handle updates while not flying a ship
            }

            //Build the key for the vessel
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(vesselUpdate.Player);
            sb.Append(vesselUpdate.Id.ToString());

            String vesselKey = sb.ToString();
            KLFVessel kVes = null;

            //Try to find the key in the vessel dictionary
            VesselEntry entry;
            if (Vessels.TryGetValue(vesselKey, out entry))
            {
                kVes = entry.Vessel;
                if(kVes == null
                || kVes.GameObj == null
                || kVes.VesselName != vesselUpdate.Name)
                {//Delete the vessel if it's null or needs to be renamed
                    Vessels.Remove(vesselKey);
                    if (kVes != null && kVes.GameObj != null)
                        GameObject.Destroy(kVes.GameObj);
                    kVes = null;
                }
                else
                {
                    //Update the entry's timestamp
                    VesselEntry newEntry = new VesselEntry();
                    newEntry.Vessel = entry.Vessel;
                    newEntry.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup;
                    Vessels[vesselKey] = newEntry;
                }
            }

            if (kVes == null)
            {//Add the vessel to the dictionary
                kVes = new KLFVessel(vesselUpdate.Name, vesselUpdate.Player, vesselUpdate.Id);
                entry = new VesselEntry();
                entry.Vessel = kVes;
                entry.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup;

                if (Vessels.ContainsKey(vesselKey))
                    Vessels[vesselKey] = entry;
                else
                    Vessels.Add(vesselKey, 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(vesselUpdate);
            }
            else
                ApplyVesselUpdate(vesselUpdate, kVes); //Apply the vessel update to the existing vessel
        }