Exemple #1
0
    // Set a area selection color
    private void SetAreaColor(Vector2Int start, Vector2Int end, Color color)
    {
        int xMin = 0;
        int xMax = 0;
        int yMin = 0;
        int yMax = 0;

        // Calculate the min/max area of the selection
        if (start.x > end.x)
        {
            xMin = end.x;
            xMax = start.x;
        }
        else
        {
            xMin = start.x;
            xMax = end.x;
        }

        if (start.y > end.y)
        {
            yMin = end.y;
            yMax = start.y;
        }
        else
        {
            yMin = start.y;
            yMax = end.y;
        }

        // Loop through all the area and color it
        for (int x = xMin; x <= xMax; x++)
        {
            for (int y = yMin; y <= yMax; y++)
            {
                EnvironmentTile tile = Environment.instance.GetTile(x, y);
                if (tile != null)
                {
                    tile.SetTint(color);
                }
            }
        }
    }
Exemple #2
0
    private void Update()
    {
        UpdateMinimap();

        // DayNight Cycle
        mDayTime += Time.deltaTime;
        if (mDayTime > DayLength)
        {
            mDayTime = 0.0f;
        }

        {
            // Change Time

            // Calculate the time of day in a range of 0-1
            float timeFraction = mDayTime / DayLength;
            // Offset the hours by 8 as the sun starts at a slightly rising angle
            int hour = (8 + (int)(24 * timeFraction)) % 24;
            // Get the hour in 12 hour format
            int hourTwelve = (hour % 12);
            // If we get a 0, we need to set it to 12
            if (hourTwelve == 0)
            {
                hourTwelve = 12;
            }
            // Get the total amount of 10 minutes in a day as we only need to change the tens part of the minute text
            int min = (int)((24 * 6) * timeFraction) % 6;
            TimeText.text = hourTwelve + ":" + min + "0 " + (hour > 11 ? "pm" : "am");


            float lightDelta = Mathf.Lerp(0, 360.0f, timeFraction);
            // Rotate the light from 0-360
            DirectionalLight.transform.eulerAngles = new Vector3(lightDelta, 90.0f, 0.0f);


            DayColor start;
            DayColor end;

            float dayColorFrackRange = 1.0f / DaylightScheduler.Length;

            int startIndex = Mathf.FloorToInt(mDayTime / (DayLength / DaylightScheduler.Length));


            start = DaylightScheduler[startIndex];
            end   = DaylightScheduler[(startIndex + 1) % DaylightScheduler.Length];


            float temp = (timeFraction - (dayColorFrackRange * startIndex)) / dayColorFrackRange;

            Color lightColor = Color.Lerp(start.color, end.color, temp);
            DirectionalLight.color      = lightColor;
            Camera.main.backgroundColor = lightColor;
            DirectionalLight.intensity  = Mathf.Lerp(start.brightness, end.brightness, temp);

            int dayTimeMusic   = 6;
            int nightTimeMusic = 19;


            // Update audio based on time
            {
                float maxMusic = 0.0f;
                float minMusic = -80.0f;


                // If we are in day time hours
                if (hour > dayTimeMusic && hour < nightTimeMusic)
                {
                    MusicMixer.SetFloat("MusicVol", maxMusic);
                    MusicMixer.SetFloat("NightTimeMusicVol", minMusic);

                    foreach (TextMeshProUGUI text in TextToDynamiclyColor)
                    {
                        text.color = dayTextColor;
                    }
                }
                // Are we in night time hours
                else if (hour > nightTimeMusic || hour < dayTimeMusic)
                {
                    MusicMixer.SetFloat("MusicVol", minMusic);
                    MusicMixer.SetFloat("NightTimeMusicVol", maxMusic);

                    foreach (TextMeshProUGUI text in TextToDynamiclyColor)
                    {
                        text.color = nightTextColor;
                    }
                }
                else
                {
                    float musicLerpFactor = (1.0f / 60) * (((24 * 60) * timeFraction) % 60);
                    if (hour == dayTimeMusic) // Transitioning to day time
                    {
                        MusicMixer.SetFloat("MusicVol", Mathf.Lerp(minMusic, maxMusic, musicLerpFactor));
                        MusicMixer.SetFloat("NightTimeMusicVol", Mathf.Lerp(maxMusic, minMusic, musicLerpFactor));

                        foreach (TextMeshProUGUI text in TextToDynamiclyColor)
                        {
                            text.color = Color.Lerp(nightTextColor, dayTextColor, musicLerpFactor);
                        }
                    }
                    else if (hour == nightTimeMusic) // Transitioning to night time
                    {
                        MusicMixer.SetFloat("MusicVol", Mathf.Lerp(maxMusic, minMusic, musicLerpFactor));
                        MusicMixer.SetFloat("NightTimeMusicVol", Mathf.Lerp(minMusic, maxMusic, musicLerpFactor));

                        foreach (TextMeshProUGUI text in TextToDynamiclyColor)
                        {
                            text.color = Color.Lerp(dayTextColor, nightTextColor, musicLerpFactor);
                        }
                    }
                }
            }
        }


        Ray screenClick = MainCamera.ScreenPointToRay(Input.mousePosition);
        // See what tiles are in the way of the cursor
        int hits = Physics.RaycastNonAlloc(screenClick, mRaycastHits);

        if (hits > 0 && !EventSystem.current.IsPointerOverGameObject())
        {
            // Calculate the closest tile from the cursor
            RaycastHit closestHit = mRaycastHits[0];
            float      distance   = (Camera.main.transform.position - closestHit.transform.position).magnitude;
            for (int i = 1; i < hits; i++)
            {
                float currentDistance = (Camera.main.transform.position - mRaycastHits[i].transform.position).magnitude;
                if (currentDistance < distance)
                {
                    distance   = currentDistance;
                    closestHit = mRaycastHits[i];
                }
            }


            EnvironmentTile tile = closestHit.transform.GetComponent <EnvironmentTile>();
            // If we have not already selected the tile, change its tint
            if (tile != null && tile != mCurrentHoveredTile)
            {
                tile.SetTint(new Color(1.0f, 0.75f, 0.75f));

                // If we have had a previous selected tile, then reset its tint
                if (mCurrentHoveredTile != null)
                {
                    mCurrentHoveredTile.SetTint(Color.white);
                }

                if (mCurrentAreaStart != null && mCurrentHoveredTile != null)
                {
                    SetAreaColor(mCurrentAreaStart.PositionTile, mCurrentHoveredTile.PositionTile, Color.white);

                    SetAreaColor(mCurrentAreaStart.PositionTile, tile.PositionTile, Color.cyan);
                }

                mCurrentHoveredTile = tile;
            }
        }
        else
        {
            // If we are not hitting any tiles, reset the current tiles tint
            if (mCurrentHoveredTile != null)
            {
                mCurrentHoveredTile.SetTint(Color.white);
                mCurrentHoveredTile = null;
            }
        }


        if (Input.GetMouseButtonDown(0))
        {
            // Process Area Click
            if (mCurrentAreaStart != null && mCurrentAreaEnd != null)
            {
                ActionSelector.Select(mCurrentAreaStart.PositionTile, mCurrentAreaEnd.PositionTile);
            }
            else
            {
                if (Input.GetKey(KeyCode.LeftControl))
                {
                    mCurrentAreaStart = mCurrentHoveredTile;
                }
                // Find out what actions are available from the current location and store them
                GatherActionList();
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (mCurrentAreaEnd != null)
            {
                // We are closing the selector from the area select
                ToggleState(InterfaceState.ActionSelector);
                mCurrentAreaStart = null;
                mCurrentAreaEnd   = null;
            }
            else if (mCurrentAreaStart != null)
            {
                SetAreaColor(mCurrentAreaStart.PositionTile, mCurrentHoveredTile.PositionTile, Color.white);
                mCurrentAreaEnd     = mCurrentHoveredTile;
                mCurrentHoveredTile = null;
                ToggleState(InterfaceState.ActionSelector);
            }
            else
            {
                ActionSelector.Select();
                if (HasBit(mInterfaceState, (int)InterfaceState.ActionSelector))
                {
                    ToggleState(InterfaceState.ActionSelector);
                }
            }
            mMouseHoldTime = 0;
        }

        if (Input.GetMouseButton(0))
        {
            // Are we not in area select mode
            if (mCurrentAreaStart == null)
            {
                // If the button is being held, add to the mouse hold time
                mMouseHoldTime += Time.deltaTime;
                // If we have passed the time required for the popup menu and the flag for the menu being open is not set, open it
                if (!HasBit(mInterfaceState, (int)InterfaceState.ActionSelector) && mMouseHoldTime > MinMenuOpenTime)
                {
                    ToggleState(InterfaceState.ActionSelector);
                }
            }
        }
    }