Skip to content

File Countdown.cs

File List > LemonUI > LemonUI > Scaleform > Countdown.cs

Go to the documentation of this file


#if ALTV
using AltV.Net.Client;
#elif FIVEM
using CitizenFX.Core;
#elif RAGEMP
using RAGE.Game;
#elif RPH
using Rage;
#elif SHVDN3 || SHVDNC
using GTA;
#endif
using System;

namespace LemonUI.Scaleform
{
    public class Countdown : BaseScaleform
    {
        #region Fields

        private int duration = 3;
        private long lastStepTime = 0;

        #endregion

        #region Defaults

        // TODO: Add the Stunt Race sounds, Countdown_1 and Countdown_GO in DLC_AW_Frontend_Sounds
        // TODO: See why Countdown_GO doesn't plays correctly

        public static Sound DefaultCountSound = new Sound("HUD_MINI_GAME_SOUNDSET", "CHECKPOINT_NORMAL");
        public static Sound DefaultGoSound = new Sound("HUD_MINI_GAME_SOUNDSET", "GO");

        #endregion

        #region Properties

        public Sound CountSound { get; set; } = DefaultCountSound;
        public Sound GoSound { get; set; } = DefaultGoSound;
        public int Duration
        {
            get => duration;
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), "The duration can't be equal or under zero.");
                }

                duration = value;
            }
        }
        public int Current { get; private set; }

        #endregion

        #region Events

        public event EventHandler Started;
        public event EventHandler Finished;

        #endregion

        #region Constructors

        public Countdown() : base("COUNTDOWN")
        {
        }

        #endregion

        #region Tools

        private void ShowStep(int step)
        {
            string asString = step == 0 ? "GO" : step.ToString();

            CallFunction("SET_MESSAGE", asString, 255, 255, 255, true);
            CallFunction("FADE_MP", asString, 255, 255, 255);

            if (step == 0)
            {
                GoSound?.PlayFrontend();
            }
            else
            {
                CountSound?.PlayFrontend();
            }
        }

        #endregion

        #region Functions

        public void Start()
        {
            Visible = true;

#if ALTV
            lastStepTime = Alt.Natives.GetGameTimer();
#elif RAGEMP
            lastStepTime = Misc.GetGameTimer();
#elif FIVEM || RPH || SHVDN3 || SHVDNC
            lastStepTime = Game.GameTime;
#endif

            Current = duration;
            ShowStep(Current);
            Started?.Invoke(this, EventArgs.Empty);
        }
        public override void Process()
        {
            if (!Visible)
            {
                return;
            }

            if (lastStepTime > 0)
            {
#if ALTV
                long currentTime = Alt.Natives.GetGameTimer();
#elif RAGEMP
                long currentTime = Misc.GetGameTimer();
#elif FIVEM || RPH || SHVDN3 || SHVDNC
                long currentTime = Game.GameTime;
#endif

                if (currentTime - lastStepTime >= 1000)
                {
                    if (Current == 0)
                    {
                        lastStepTime = 0;
                        Visible = false;
                        return;
                    }

                    lastStepTime = currentTime;
                    Current--;
                    ShowStep(Current);

                    if (Current == 0)
                    {
                        Finished?.Invoke(this, EventArgs.Empty);
                    }
                }
            }

            DrawFullScreen();
        }
        public override void Update()
        {
        }

        #endregion
    }
}