Beispiel #1
0
    public static CelestialVirtual Create(CelestialBody celestialBody)
    {
        string name   = celestialBody.name;
        double radius = celestialBody.Radius;

        GameObject       gameObject       = null;
        CelestialVirtual celestialVirtual = null;

        if (celestialBody.Type == CelestialType.Ship)
        {
            gameObject = new GameObject(name);

            celestialVirtual = gameObject.AddComponent <CelestialVirtual>();

            celestialVirtual.m_CelestialType = celestialBody.Type;
            celestialVirtual.m_OwnerID       = celestialBody.CelestialID;
            celestialVirtual.m_RadiusInKM    = celestialBody.Radius;

            celestialVirtual.m_InitialScale = 1;

            celestialVirtual.m_OrbitParentName = "";

            celestialVirtual.m_MaximumScaleMultiplier = 1U;
            celestialVirtual.Scale = 1;
        }
        else
        {
            string             prefabPath = CelestialBodyLoader.PrefabPath + celestialBody.name + "_Virtual";
            UnityEngine.Object prefab     = Resources.Load(prefabPath, typeof(GameObject));

            if (null != prefab)
            {
                gameObject = UnityEngine.Object.Instantiate(prefab) as GameObject;
            }

            if (null != gameObject)
            {
                gameObject.name = name;

                celestialVirtual = gameObject.AddComponent <CelestialVirtual>();

                celestialVirtual.m_CelestialType = celestialBody.Type;
                celestialVirtual.m_OwnerID       = celestialBody.CelestialID;
                celestialVirtual.m_RadiusInKM    = celestialBody.Radius;

                // TODO: This is a sphere shaped calculation so will need to support other types eventually?
                // Initial scale is in game diameter (2 * radius)
                celestialVirtual.m_InitialScale = celestialVirtual.CelestialRadius * 2;

                celestialVirtual.m_OrbitParentName = celestialBody.OrbitParentName;

                celestialVirtual.m_MaximumScaleMultiplier = (name != "Sol") ? 100U : 10U;
                celestialVirtual.Scale = 100;
            }
        }

        return(celestialVirtual);
    }
    // Use this for initialization
    public bool Init()
    {
        if (m_Initialized == false)
        {
            GameObject celestialBodyParent = new GameObject("Virtual Celestial Bodies");

            Dictionary <string, uint> celestialBodyIds = new Dictionary <string, uint>();

            // For each celestial body we will try to create a virtual version
            List <CelestialBody> celestialPhysicalBodies = CelestialManagerPhysical.Instance.GetCelestialBodies(CelestialBody.CelestialType.All);

            foreach (CelestialBody celestialPhysicalBody in celestialPhysicalBodies)
            {
                CelestialVirtual virtualBody = CelestialVirtual.Create(celestialPhysicalBody);

                m_CelestialBodies.Add(virtualBody.CelestialID, virtualBody);
                celestialBodyIds.Add(virtualBody.name, virtualBody.CelestialID);

                // For organization we parent all bodies to a dummy object
                virtualBody.transform.parent = celestialBodyParent.transform;

                UpdatePosition(virtualBody);
            }

            // Set up the celestial hierarchy now that all bodies have been instantiated
            List <CelestialBody> bodies = GetCelestialBodies(CelestialBody.CelestialType.All);

            foreach (CelestialBody body in bodies)
            {
                if (body.OrbitParentName.Length > 0)
                {
                    uint bodyID;
                    if (celestialBodyIds.TryGetValue(body.OrbitParentName, out bodyID))
                    {
                        CelestialBody parentBody;
                        if (m_CelestialBodies.TryGetValue(bodyID, out parentBody))
                        {
                            body.OrbitParentID = parentBody.CelestialID;
                        }
                        else
                        {
                            Debug.LogError("Failed to find orbit parent: " + bodyID.ToString());
                        }
                    }
                    else
                    {
                        Debug.LogError("Failed to find orbit parent: " + body.OrbitParentName);
                    }
                }
            }

            m_Initialized = true;
        }

        return(m_Initialized);
    }
    private void UpdatePosition(CelestialBody body)
    {
        CelestialVirtual virtualBody = body as CelestialVirtual;

        if (null != virtualBody)
        {
            CelestialBody celestialBody = CelestialManagerPhysical.Instance.GetCelestialBody(virtualBody.OwnerID);

            if (null != celestialBody)
            {
                CelestialPlanetoid planetoid = celestialBody as CelestialPlanetoid;

                if (null != planetoid)
                {
                    CelestialVector3 position = planetoid.CalculatePosition(CelestialTime.Instance.Current);

                    virtualBody.LocalPosition = position;

                    if (body.OrbitParentID != 0)
                    {
                        CelestialBody parentBody = GetCelestialBody(body.OrbitParentID);

                        if (parentBody != null)
                        {
                            position += parentBody.Position;
                        }
                    }

                    virtualBody.Position = position;
                }
                else
                {
                    // Set the virtual position to match the real planet's
                    virtualBody.LocalPosition = celestialBody.LocalPosition;
                    virtualBody.Position      = celestialBody.Position;
                }
            }
            else
            {
                Debug.LogWarning("Unable to update position of " + body.name);
            }
        }
    }
    public void Update(CelestialCamera celestialCamera)
    {
        foreach (KeyValuePair <uint, CelestialBody> celestialBodyRecord in m_CelestialBodies)
        {
            CelestialBody celestialBody = celestialBodyRecord.Value;

            UpdatePosition(celestialBody);
        }

        // Find the closest body to the camera and adjust the scale
        if (m_AutoScale && celestialCamera != null)
        {
            CelestialBody closestBody = GetClosestCelestialBody(CelestialBody.CelestialType.Planet, celestialCamera.transform.position);

            CelestialVirtual closestPlanet = closestBody as CelestialVirtual;

            if (null != closestPlanet)
            {
                float distance = (celestialCamera.transform.position - closestPlanet.transform.position).magnitude;

                if (distance < float.MaxValue)
                {
                    if (distance >= m_FarRange)
                    {
                        SetScale(1000.0f);
                    }
                    else if (distance <= m_CloseRange)
                    {
                        SetScale(1.0f);
                    }
                    else
                    {
                        float rangeScalar = (distance - m_CloseRange) / (m_FarRange - m_CloseRange);
                        SetScale(Math.Max(1.0f, rangeScalar * 1000));
                    }
                }
            }
        }
    }
Beispiel #5
0
    // Use this for initialization
    private void Start()
    {
        if (m_VirtualManager.Init())
        {
            // Find all of the planets
            List <CelestialBody> planets = m_VirtualManager.GetCelestialBodies(CelestialBody.CelestialType.All);
            foreach (CelestialBody celestialBody in planets)
            {
                // Create HUD element
                CelestialBodyHUD celestialBodyHUD = CreateHUDElement(celestialBody);

                m_HUDList.Add(celestialBodyHUD);

                // Create orbit

                // Only virtual bodies have visual orbits
                CelestialVirtual celestialVirtual = celestialBody as CelestialVirtual;

                if (celestialBody.HasOrbit && null != celestialVirtual)
                {
                    CelestialBody virtualParent = m_VirtualManager.GetCelestialBody(celestialBody.OrbitParentID);

                    // The orbit needs the id of the physical owner and a reference to the virtual parent
                    CelestialOrbit orbit = CelestialOrbit.Create(celestialVirtual.OwnerID, virtualParent);

                    // Use the same grouping parent as the virtual owner
                    orbit.transform.SetParent(celestialBody.transform.parent);

                    m_CelestialOrbits.Add(orbit);
                }
            }

            // Setup the HUD objects to notify us when clicked
            CelestialClickable[] clickableGUIObjects = GetComponentsInChildren <CelestialClickable>();

            foreach (CelestialClickable clickableGUIObject in clickableGUIObjects)
            {
                if (clickableGUIObject.m_EnableClick)
                {
                    clickableGUIObject.SetSelected = ClickSelected;
                    clickableGUIObject.SetTargeted = ClickTargeted;
                }
                clickableGUIObject.DisableClickMiss = ClickDisableMiss;

                if (clickableGUIObject.m_EnableDrag)
                {
                    clickableGUIObject.MouseDrag = ClickDrag;
                }
            }

            // Set default filter
            if (m_ProximityPanel != null)
            {
                m_ProximityPanel.ToggleFilter_Planets();
            }

            // Set default scale
            SetAutoScale(false);

            // Setup the view camera to a default position
            CelestialBody body = m_VirtualManager.GetCelestialBody("Earth");
            if (body)
            {
                m_ViewCamera.SetTargetedObject(body);
            }
        }
        else
        {
            Debug.LogError("VirtualManager failed to initialize!");
        }
    }