Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BallisticNG.RaceUI;
- using BallisticNG.UserStats;
- using GameData;
- using GameData.Constants;
- using Steamworks;
- using UnityEngine;
- namespace BallisticNG.Gamemodes
- {
- public class GmSpeedLap : Gamemode
- {
- /*---Interfaces---*/
- private ScriptableMenu _pauseInterface;
- private ScriptableMenu _eventCompleteInterface;
- private ScriptableHud[] _speedAndShieldHud;
- private ScriptableHud[] _notificationHud;
- private TimeSpeedLap _timeHud;
- private ScriptableHud[] _weaponHud;
- private ScriptableHud[] _nowPlayingHud;
- /*---Lap Tracking---*/
- private float _perfectLaps;
- private float _laps;
- private float _bestLap;
- private float _totalTime;
- private bool _disableHudOnNextFrame;
- private float _lapInvalidateTimer;
- private bool _lapInvalidated;
- private bool _doneInvalidation;
- private GhostManager _ghostManager;
- public GmSpeedLap()
- {
- }
- public GmSpeedLap(Config config) : base(config)
- {
- }
- public override void OnAwake()
- {
- base.OnAwake();
- RaceManager.Instance.AutoLapSetup = true;
- Race.RacerCount = 1;
- }
- public override void OnStart()
- {
- base.OnStart();
- if (Ships.LoadedShips.Count > 0) LoadTime(Ships.LoadedShips[0]);
- GameObject go = new GameObject("< Ghost Manager >");
- _ghostManager = go.AddComponent<GhostManager>();
- _ghostManager.Init(Ships.LoadedShips[0], true);
- }
- public override void OnUpdate()
- {
- base.OnUpdate();
- // set time hud total time
- if (Ships.LoadedShips[0].CurrentLap > 0)
- {
- _timeHud.CurrentTime = Ships.LoadedShips[0].CurrentLapTime;
- _timeHud.LapInvalidated = _lapInvalidated;
- /*---Lap Invalidation---*/
- if (Inputs.GetButton(Inputs.ButtonAfterburner, 0))
- {
- _lapInvalidateTimer += Time.unscaledDeltaTime;
- if (_lapInvalidateTimer > 2.0f && !_doneInvalidation)
- {
- _lapInvalidated = true;
- Ships.LoadedShips[0].CurrentPickup = PickupHelper.GivePickup(Ships.LoadedShips[0], E_WEAPONS.TURBO);
- BallisticEvents.Ui.CallOnTriggerMessage("LAP INVALIDATED", Ships.LoadedShips[0], Color.red);
- AudioHelpers.PlayOneShot(AudioHelpers.UI_KnockoutWarning, AudioHelpers.E_AUDIOCHANNEL.INTERFACE, 1.0f, 1.0f);
- _lapInvalidateTimer = 0.0f;
- _doneInvalidation = true;
- }
- }
- else
- {
- _lapInvalidateTimer = 0.0f;
- _doneInvalidation = false;
- }
- }
- Ships.LoadedShips[0].ShieldIntegrity = 100;
- }
- public override void OnFixedUpdate()
- {
- base.OnFixedUpdate();
- _totalTime += Time.deltaTime;
- if (_disableHudOnNextFrame)
- {
- CloseHuds(_speedAndShieldHud);
- CloseHuds(_notificationHud);
- _timeHud.Close();
- CloseHuds(_weaponHud);
- CloseHuds(_nowPlayingHud);
- _disableHudOnNextFrame = false;
- }
- }
- public override void OnShipTriggerStartLine(ShipRefs r)
- {
- if (r.LapValidated || r.CurrentLap == 0)
- {
- // update best time
- bool beatBestLap = r.CurrentLapTime < r.BestLapTime || !r.HasBestLapTime;
- if (_lapInvalidated)
- {
- beatBestLap = false;
- r.IsPerfectLap = false;
- }
- if (!Cheats.Enabled)
- {
- if (r.CurrentLap > 0) _ghostManager.StopGhostRecord(beatBestLap, beatBestLap);
- _ghostManager.RestartGhostPlayback();
- _ghostManager.StartGhostRecord();
- }
- // give turbo
- r.CurrentPickup = PickupHelper.GivePickup(r, E_WEAPONS.TURBO);
- r.LapValidated = false;
- r.PassedValidationGate = false;
- // lap stats
- if (Steam.Enabled)
- {
- SteamAchieves.IncrementStat(E_STATS.stat_laps);
- if (r.IsPerfectLap) SteamAchieves.IncrementStat(E_STATS.st_perfectlaps);
- }
- if (beatBestLap && r.CurrentLap > 0 && !Cheats.Enabled)
- {
- BallisticEvents.Ui.CallOnTriggerMessage("NEW LAP RECORD", r, ScriptableHud.BnGAccent);
- SaveTime(r);
- r.HasBestLapTime = true;
- r.BestLapTime = r.CurrentLapTime;
- SetHudBestTime(r.CurrentLapTime);
- }
- // perfect lap
- if (r.IsPerfectLap)
- {
- IncrementExperience("Perfect Lap", Experience.PerfectLap);
- BallisticEvents.Ui.CallOnTriggerMessage("PERFECT LAP", r, ScriptableHud.BnGAccent);
- AudioHelpers.PlayVoice(AudioHelpers.Voice_PerfectLap);
- }
- if (r.CurrentLap > 0) BallisticEvents.Ui.CallOnTriggerMessage(FloatToTime.Convert(r.CurrentLapTime, "0:00.00"), r, ScriptableHud.BnGAccent);
- if (r.CurrentLap == 0) r.CurrentLap = 1;
- // speed lap tracking
- if (!_lapInvalidated)
- {
- if (r.IsPerfectLap) ++_perfectLaps;
- if (r.CurrentLapTime < _bestLap || _bestLap == 0) _bestLap = r.CurrentLapTime;
- }
- ++_laps;
- r.TotalRaceTime = 0.0f;
- r.CurrentLapTime = 0.0f;
- r.IsPerfectLap = true;
- _lapInvalidated = false;
- BallisticEvents.Race.CallOnShipLapUpdate(r);
- }
- base.OnShipTriggerStartLine(r);
- }
- public override void OnShipTriggerMidLine(ShipRefs r)
- {
- base.OnShipTriggerMidLine(r);
- // checkpoint
- if (!r.PassedValidationGate)
- {
- AudioHelpers.PlayOneShot(AudioHelpers.UI_Checkpoint, AudioHelpers.E_AUDIOCHANNEL.INTERFACE, 1.0f, 1.0f);
- r.PassedValidationGate = true;
- }
- }
- public override void OnEventComplete()
- {
- base.OnEventComplete();
- // load event complete interface
- ShipRefs r = Ships.LoadedShips[0];
- Stats.IncrementStats(false, true);
- ((EventCompleteSpeedlap)_eventCompleteInterface).Open(r.BestLapTime, _bestLap, _totalTime, _laps, _perfectLaps);
- // replace ship camera with finished camera
- Object.Destroy(r.ShipCamera.GetComponent<ShipCamera>());
- ShipFCam fCam = r.ShipCamera.gameObject.AddComponent<ShipFCam>();
- fCam.r = r;
- // force close all of the interfaces (unpausing enables the interfaces again)
- _disableHudOnNextFrame = true;
- }
- public override void LoadInterfaces()
- {
- /*---Menus---*/
- _pauseInterface = InterfaceLoader.LoadMenu(InterfaceLoader.Menus.SessionPause, false);
- _eventCompleteInterface = InterfaceLoader.LoadMenu(InterfaceLoader.Menus.EventCompleteSpeedlap, false);
- /*---Huds---*/
- _speedAndShieldHud = CreateNewHuds(InterfaceLoader.Huds.SpeedAndShield);
- _notificationHud = CreateNewHuds(InterfaceLoader.Huds.NotificationBuffer);
- _timeHud = (TimeSpeedLap)InterfaceLoader.LoadHud(InterfaceLoader.Huds.TimeSpeedLap);
- _timeHud.TargetShip = Ships.LoadedShips[0];
- _weaponHud = CreateNewHuds(InterfaceLoader.Huds.Weapon);
- _nowPlayingHud = CreateNewHuds(InterfaceLoader.Huds.NowPlaying);
- }
- public override void DestroyInterfaces()
- {
- /*---Menus---*/
- if (_pauseInterface) Object.Destroy(_pauseInterface.gameObject);
- if (_eventCompleteInterface) Object.Destroy(_eventCompleteInterface.gameObject);
- /*---Huds---*/
- if (_speedAndShieldHud != null) DestroyHuds(_speedAndShieldHud);
- if (_notificationHud != null) DestroyHuds(_notificationHud);
- if (_timeHud) Object.Destroy(_timeHud.gameObject);
- if (_weaponHud != null) DestroyHuds(_weaponHud);
- if (_nowPlayingHud != null) DestroyHuds(_nowPlayingHud);
- }
- /// <summary>
- /// Sets the best lap time on the time HUD.
- /// </summary>
- public void SetHudBestTime(float time)
- {
- if (_timeHud) _timeHud.SetBestLap(time);
- }
- public override void SaveTime(ShipRefs r)
- {
- // to disk
- SaveData.WriteTime(SystemHelpers.GetLoadedScene(), Race.Speedclass, Name, r.CurrentLapTime);
- // to leaderboards
- if (Steam.Enabled && !RaceManager.Instance.LeaderboardsDisabled)
- {
- /*
- string leaderboardName = SteamLeaderboardManager.GetLeaderboardName(SystemHelpers.GetLoadedScene(), Race.Speedclass, Name, Cheats.ModernPhysics);
- SteamLeaderboardManager.teamUpload = Player.PlayerOneShip;
- SteamLeaderboardManager.LeaderboardTask(leaderboardName, SteamLeaderboardManager.E_LEADERBOARDTASK.Upload, r.CurrentLapTime);
- */
- Leaderboards.UploadScore(Leaderboards.GetLeaderboardName(this, SystemHelpers.GetLoadedScene(), Race.Speedclass, Cheats.ModernPhysics), Leaderboards.TimeToMiliseconds(r.CurrentLapTime),
- Player.PlayerOneShip, ELeaderboardSortMethod.k_ELeaderboardSortMethodAscending, ELeaderboardDisplayType.k_ELeaderboardDisplayTypeTimeMilliSeconds);
- }
- }
- public override void LoadTime(ShipRefs r)
- {
- base.LoadTime(r);
- if (!r.LoadedBestLapTime) return;
- r.BestLapTime = r.TargetTime;
- r.HasBestLapTime = true;
- if (r.HasBestLapTime) SetHudBestTime(r.TargetTime);
- else _timeHud.BestLapText.text = _timeHud.EmptyTime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement