コード例 #1
0
ファイル: EnvironmentMaster.cs プロジェクト: Hengle/VoxelGame
    private void LateUpdate()
    {
        if (gmap == null)
        {
            return;
        }
        float t         = Time.deltaTime * GameMaster.gameSpeed,
              ascension = gmap.ascension;

        ;

        //wind:
        {
            windTimer -= t;
            if (windTimer <= 0)
            {
                windTimer     = WIND_CHANGE_TIME * (0.1f + Random.value * 1.9f);
                newWindVector = Quaternion.AngleAxis((0.5f - Random.value) * 30, Vector3.up) * windVector;
                newWindVector = newWindVector * (1 + (0.5f - Random.value) * 0.5f);
            }
            if (windVector != newWindVector)
            {
                float st = WIND_CHANGE_STEP * t;
                windVector = Vector3.RotateTowards(windVector, newWindVector, st, st);
                float windpower = windVector.magnitude;
                Shader.SetGlobalFloat(vegetationShaderWindPropertyID, windpower);
                if (WindUpdateEvent != null)
                {
                    WindUpdateEvent(windVector);
                }
            }
        }

        if (GameMaster.realMaster.gameMode != GameMode.Editor & !GameMaster.loading)
        {
            //if (currentEnvironment.presetType != Environment.EnvironmentPresets.Default)
            //{
            if (environmentEventTimer > 0)
            {
                environmentEventTimer -= t;
                if (environmentEventTimer <= 0)
                {
                    //currentEnvironment.InnerEvent();
                    // environmentEventTimer = currentEnvironment.GetInnerEventTime();
                }
            }
            //}

            var dir = gmap.cityFlyDirection / 500f;
            dir.y = 0;
            if (decorations.Count > 0)
            {
                int     i = 0;
                Vector3 pos;
                float   sqrRadius = SKY_SPHERE_RADIUS * SKY_SPHERE_RADIUS;
                while (i < decorations.Count)
                {
                    pos  = decorations[i].position;
                    pos += dir;
                    decorations[i].position = pos;
                    if (Vector3.SqrMagnitude(pos) > sqrRadius * 4f)
                    {
                        Destroy(decorations[i].gameObject);
                        decorations.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
            if (lastSpawnDistance > 0)
            {
                lastSpawnDistance -= dir.magnitude;
            }

            if (showCelestialBodies)
            {
                if (celestialBodies != null && celestialBodies.Count > 0)
                {
                    Vector3 cpoint = Quaternion.AngleAxis(gmap.cityPoint.angle, Vector3.back) * (Vector3.up * gmap.cityPoint.height);
                    Vector3 mpoint;
                    foreach (var sb in celestialBodies)
                    {
                        mpoint            = Quaternion.AngleAxis(sb.Key.angle, Vector3.back) * (Vector3.up * sb.Key.height);
                        mpoint           -= cpoint;
                        mpoint.z          = mpoint.y * 10f; mpoint.x *= 10f;
                        mpoint.y          = 1f;
                        sb.Value.position = mpoint;
                    }
                }
            }

            //test lightnings
            if (false)
            {
                effectsTimer -= t;
                if (effectsTimer < 0)
                {
                    float f      = Chunk.chunkSize;
                    var   center = GameMaster.sceneCenter;
                    var   pos    = Random.onUnitSphere * f + center;
                    dir  = center - pos;
                    dir += Random.onUnitSphere;
                    RaycastHit rh;
                    if (Physics.Raycast(pos, dir, out rh, 2 * f))
                    {
                        Lightning.Strike(pos, rh.point);
                        var   hitobject = rh.collider;
                        float damage    = Lightning.CalculateDamage();
                        if (hitobject.tag == Structure.STRUCTURE_COLLIDER_TAG)
                        {
                            hitobject.transform.parent.GetComponent <Structure>().ApplyDamage(damage);
                        }
                        else
                        {
                            if (hitobject.tag == Chunk.BLOCK_COLLIDER_TAG)
                            {
                                var crh = GameMaster.realMaster.mainChunk.GetBlock(rh.point, rh.normal);
                                crh.block?.EnvironmentalStrike(crh.faceIndex, rh.point, 2, damage);
                            }
                        }
                    }
                    else
                    {
                        var end = pos + Random.onUnitSphere * 5f;
                        end.y = GameConstants.GetBottomBorder();
                        Lightning.Strike(pos, end);
                    }
                    effectsTimer = 1f;
                }
            }

            //sunlight changing
            var clv         = gmap.cityLookVector;
            var rightVector = Vector3.Cross(clv, Vector3.down);
            sunTransform.forward = Quaternion.AngleAxis(15f + ascension * 75f, rightVector) * clv;

            #region stability
            if (colonyController != null)
            {
                float hc = 1f, gc = 0f;
                hc = colonyController.happiness_coefficient;
                if (colonyController.storage != null)
                {
                    gc = colonyController.storage.standartResources[ResourceType.GRAPHONIUM_ID] / GameConstants.GRAPHONIUM_CRITICAL_MASS;

                    // + блоки?
                }
                float pc  = 0f;
                int   pop = colonyController.citizenCount;
                if (pop > POPULATION_CONDITION_1)
                {
                    if (pop >= POPULATION_CONDITION_3)
                    {
                        pc = POPULATION_STABILITY_EFFECT_3;
                    }
                    else
                    {
                        if (pop > POPULATION_CONDITION_2)
                        {
                            pc = POPULATION_STABILITY_EFFECT_2;
                        }
                        else
                        {
                            pc = POPULATION_STABILITY_EFFECT_1;
                        }
                    }
                }

                float structureStabilizersEffect = 1f;
                if (stabilityModifiers != null && stabilityModifiers.Count > 0)
                {
                    structureStabilizersEffect = 1f;
                    foreach (var sm in stabilityModifiers)
                    {
                        structureStabilizersEffect *= (1f + sm.Value);
                    }
                }
                targetStability =
                    (
                        0.5f * hc // happiness
                        - gc      // graphonium reserves
                        + pc      // population
                        + (1f - ascension) * structureStabilizersEffect
                    )
                    *
                    envStability;
                if (targetStability > 1f)
                {
                    targetStability = 1f;
                }
                else
                {
                    if (targetStability < 0f)
                    {
                        targetStability = 0f;
                    }
                }
                if (islandStability != targetStability)
                {
                    islandStability = Mathf.MoveTowards(islandStability, targetStability, GameConstants.STABILITY_CHANGE_SPEED * Time.deltaTime);
                }
                if (islandStability < 1f)
                {
                    float lcf  = colonyController.GetLevelCf();
                    float step = lcf > 1f ? 0f : CITY_CHANGE_HEIGHT_STEP * (1f - islandStability) * t * (1f - lcf);
                    if (step != 0f)
                    {
                        if (ascension < 0.5f)
                        {
                            if (gmap.cityPoint.height != 1f)
                            {
                                gmap.ChangeCityPointHeight(step);
                                positionChanged = true;
                            }
                        }
                        else
                        {
                            if (gmap.cityPoint.height != 0f)
                            {
                                gmap.ChangeCityPointHeight(-step);
                                positionChanged = true;
                            }
                        }
                    }
                }
            }
            // обновление освещения при движении города
            if (positionChanged)
            {
                RefreshEnvironment();
            }
            #endregion
        }
    }