コード例 #1
0
 public CSolarSystem(int id)
 {
     Id                = id;
     Stopwatch         = new Stopwatch();
     State             = SolarSystemState.FindSystemAnomaly;
     SystemAnomalyList = new List <CSystemAnomaly>();
 }
コード例 #2
0
        public SolarSystemState Clone()
        {
            var clone = new SolarSystemState();

            clone.CopyFrom(this);
            return(clone);
        }
コード例 #3
0
 public void CopyFrom(SolarSystemState other)
 {
     frames  = other.frames;
     sunMass = other.sunMass;
     gravivationalConstant = other.gravivationalConstant;
     simTime = other.simTime;
     planets = new List <PlanetState>(other.planets);
     comets  = new List <CometState>(other.comets);
 }
コード例 #4
0
    public void RestartPaths()
    {
        _cometPathState = _currState.Clone();

        _cometPaths = new Queue <Vector3> [_cometPathState.comets.Count];
        for (int i = 0; i < _cometPaths.Length; i++)
        {
            _cometPaths[i] = new Queue <Vector3>();
        }
    }
コード例 #5
0
    private void destroySimulation()
    {
        if (OnDestroySystem != null)
        {
            OnDestroySystem();
        }

        _currState = null;
        _prevState = null;

        foreach (var planet in _spawnedPlanets)
        {
            DestroyImmediate(planet.gameObject);
        }
        _spawnedPlanets.Clear();
    }
コード例 #6
0
        private void FindSystemAnomaly()
        {
            Log.WriteLine("CSolarSystem.FindSystemAnomaly()");
            var activeSystemAnomly = SystemAnomalyList.FirstOrDefault(x => x.State != SystemAnomalyState.Complete);

            if (activeSystemAnomly != null)
            {
                activeSystemAnomly.Pulse();
            }
            else
            {
                var newSystemAnomlyList = Global.Ship.Scanners.System.GetAnomalies().ToList().FirstOrDefault(NotInSystemAnomalyList);
                if (newSystemAnomlyList != null)
                {
                    SystemAnomalyList.Add(new CSystemAnomaly(newSystemAnomlyList));
                }
                else
                {
                    State = SolarSystemState.Complete;
                }
            }
        }
コード例 #7
0
    private void createSimulation()
    {
        _simTime = 0;
        if (_currState != null || _prevState != null)
        {
            destroySimulation();
        }

        _currState         = new SolarSystemState();
        _currState.sunMass = _sunMass;
        _currState.gravivationalConstant = _gravitationalConstant;

        //Generate the planets for the solar system
        for (int i = 0; i < _planetCount; i++)
        {
            var planet = Instantiate(_planetPrefab);
            planet.transform.parent        = _displayAnchor;
            planet.transform.localPosition = Vector3.zero;
            planet.transform.localRotation = Quaternion.identity;
            planet.transform.localScale    = Vector3.one;

            var planetState = new PlanetState();

            float orbitRadius     = Random.Range(_orbitRadiusRange.x, _orbitRadiusRange.y);
            float mass            = Random.Range(_massRange.x, _massRange.y);
            float revolutionSpeed = Random.Range(_revolutionRange.x, _revolutionRange.y);
            float axisTilt        = _axisTiltDistribution.Evaluate(Random.value);

            float orbitalVelocity = Mathf.Sqrt(_gravitationalConstant * _sunMass / orbitRadius);
            float orbitLength     = Mathf.PI * 2 * orbitRadius;
            float orbitPeriod     = orbitLength / orbitalVelocity;
            float angularSpeed    = Mathf.PI * 2 / orbitPeriod;

            if (Random.value > _chanceToRevolveBackwards)
            {
                revolutionSpeed = -revolutionSpeed;
            }

            float hue        = Random.value;
            float saturation = Random.Range(_saturationRange.x, _saturationRange.y);
            float value      = Random.Range(_valueRange.x, _valueRange.y);

            planetState.angle              = Random.Range(0, Mathf.PI * 2);
            planetState.angularSpeed       = angularSpeed;
            planetState.distanceFromCenter = orbitRadius;
            planetState.mass = mass;

            planet.gameObject.SetActive(true);
            planet.Init(revolutionSpeed, mass, axisTilt, Color.HSVToRGB(hue, saturation, value));

            _currState.planets.Add(planetState);
            _spawnedPlanets.Add(planet);
        }

        //Add starting commets
        for (int i = 0; i < _cometCount; i++)
        {
            var rot = Quaternion.Euler(0, Random.Range(0, 360), 0);
            _currState.comets.Add(new CometState()
            {
                position = rot * (Vector3.right * 0.3f + Vector3.up * 0.05f),
                velocity = rot * (Vector3.forward * 0.4f)
            });
        }

        _prevState = _currState.Clone();

        //Generate planet orbit mesh
        {
            foreach (var planet in _currState.planets)
            {
                for (int i = 0; i < _planetOrbitResolution; i++)
                {
                    _tempLines.Add(i + _tempVerts.Count);
                    _tempLines.Add((i + 1) % _planetOrbitResolution + _tempVerts.Count);
                }

                for (int i = 0; i < _planetOrbitResolution; i++)
                {
                    float angle = Mathf.PI * 2 * i / (float)_planetOrbitResolution;
                    float dx    = Mathf.Cos(angle) * planet.distanceFromCenter;
                    float dz    = Mathf.Sin(angle) * planet.distanceFromCenter;
                    _tempVerts.Add(new Vector3(dx, 0, dz));
                }
            }

            if (_planetOrbitMesh == null)
            {
                _planetOrbitMesh = new Mesh();
            }
            _planetOrbitMesh.Clear();
            _planetOrbitMesh.SetVertices(_tempVerts);
            _planetOrbitMesh.SetIndices(_tempLines.ToArray(), MeshTopology.Lines, 0);

            _tempVerts.Clear();
            _tempLines.Clear();
        }

        //Restart the comet paths
        RestartPaths();

        if (OnCreateSystem != null)
        {
            OnCreateSystem();
        }
    }