Advertisement
Guest User

BallisticNG - Speedlap Gamemode

a guest
Aug 15th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.55 KB | None | 0 0
  1. using BallisticNG.RaceUI;
  2. using BallisticNG.UserStats;
  3. using GameData;
  4. using GameData.Constants;
  5. using Steamworks;
  6. using UnityEngine;
  7.  
  8. namespace BallisticNG.Gamemodes
  9. {
  10.     public class GmSpeedLap : Gamemode
  11.     {
  12.         /*---Interfaces---*/
  13.         private ScriptableMenu _pauseInterface;
  14.         private ScriptableMenu _eventCompleteInterface;
  15.  
  16.         private ScriptableHud[] _speedAndShieldHud;
  17.         private ScriptableHud[] _notificationHud;
  18.         private TimeSpeedLap _timeHud;
  19.         private ScriptableHud[] _weaponHud;
  20.         private ScriptableHud[] _nowPlayingHud;
  21.  
  22.         /*---Lap Tracking---*/
  23.         private float _perfectLaps;
  24.         private float _laps;
  25.         private float _bestLap;
  26.         private float _totalTime;
  27.  
  28.         private bool _disableHudOnNextFrame;
  29.  
  30.         private float _lapInvalidateTimer;
  31.         private bool _lapInvalidated;
  32.         private bool _doneInvalidation;
  33.         private GhostManager _ghostManager;
  34.  
  35.         public GmSpeedLap()
  36.         {
  37.            
  38.         }
  39.  
  40.         public GmSpeedLap(Config config) : base(config)
  41.         {
  42.  
  43.         }
  44.  
  45.         public override void OnAwake()
  46.         {
  47.             base.OnAwake();
  48.  
  49.             RaceManager.Instance.AutoLapSetup = true;
  50.             Race.RacerCount = 1;
  51.         }
  52.  
  53.         public override void OnStart()
  54.         {
  55.             base.OnStart();
  56.             if (Ships.LoadedShips.Count > 0) LoadTime(Ships.LoadedShips[0]);
  57.  
  58.             GameObject go = new GameObject("< Ghost Manager >");
  59.             _ghostManager = go.AddComponent<GhostManager>();
  60.             _ghostManager.Init(Ships.LoadedShips[0], true);
  61.         }
  62.  
  63.         public override void OnUpdate()
  64.         {
  65.             base.OnUpdate();
  66.  
  67.             // set time hud total time
  68.             if (Ships.LoadedShips[0].CurrentLap > 0)
  69.             {
  70.                 _timeHud.CurrentTime = Ships.LoadedShips[0].CurrentLapTime;
  71.                 _timeHud.LapInvalidated = _lapInvalidated;
  72.  
  73.                 /*---Lap Invalidation---*/
  74.                 if (Inputs.GetButton(Inputs.ButtonAfterburner, 0))
  75.                 {
  76.                     _lapInvalidateTimer += Time.unscaledDeltaTime;
  77.                     if (_lapInvalidateTimer > 2.0f && !_doneInvalidation)
  78.                     {
  79.                         _lapInvalidated = true;
  80.                         Ships.LoadedShips[0].CurrentPickup = PickupHelper.GivePickup(Ships.LoadedShips[0], E_WEAPONS.TURBO);
  81.                         BallisticEvents.Ui.CallOnTriggerMessage("LAP INVALIDATED", Ships.LoadedShips[0], Color.red);
  82.                         AudioHelpers.PlayOneShot(AudioHelpers.UI_KnockoutWarning, AudioHelpers.E_AUDIOCHANNEL.INTERFACE, 1.0f, 1.0f);
  83.  
  84.                         _lapInvalidateTimer = 0.0f;
  85.                         _doneInvalidation = true;
  86.                     }
  87.                 }
  88.                 else
  89.                 {
  90.                     _lapInvalidateTimer = 0.0f;
  91.                     _doneInvalidation = false;
  92.                 }
  93.             }
  94.             Ships.LoadedShips[0].ShieldIntegrity = 100;
  95.  
  96.         }
  97.  
  98.         public override void OnFixedUpdate()
  99.         {
  100.             base.OnFixedUpdate();
  101.  
  102.             _totalTime += Time.deltaTime;
  103.             if (_disableHudOnNextFrame)
  104.             {
  105.                 CloseHuds(_speedAndShieldHud);
  106.                 CloseHuds(_notificationHud);
  107.                 _timeHud.Close();
  108.                 CloseHuds(_weaponHud);
  109.                 CloseHuds(_nowPlayingHud);
  110.  
  111.                 _disableHudOnNextFrame = false;
  112.             }
  113.         }
  114.  
  115.         public override void OnShipTriggerStartLine(ShipRefs r)
  116.         {
  117.             if (r.LapValidated || r.CurrentLap == 0)
  118.             {
  119.                 // update best time
  120.                 bool beatBestLap = r.CurrentLapTime < r.BestLapTime || !r.HasBestLapTime;
  121.                 if (_lapInvalidated)
  122.                 {
  123.                     beatBestLap = false;
  124.                     r.IsPerfectLap = false;
  125.                 }
  126.  
  127.                 if (!Cheats.Enabled)
  128.                 {
  129.                     if (r.CurrentLap > 0) _ghostManager.StopGhostRecord(beatBestLap, beatBestLap);
  130.                     _ghostManager.RestartGhostPlayback();
  131.                     _ghostManager.StartGhostRecord();
  132.                 }
  133.  
  134.                 // give turbo
  135.                 r.CurrentPickup = PickupHelper.GivePickup(r, E_WEAPONS.TURBO);
  136.  
  137.                 r.LapValidated = false;
  138.                 r.PassedValidationGate = false;
  139.  
  140.                 // lap stats
  141.                 if (Steam.Enabled)
  142.                 {
  143.                     SteamAchieves.IncrementStat(E_STATS.stat_laps);
  144.                     if (r.IsPerfectLap) SteamAchieves.IncrementStat(E_STATS.st_perfectlaps);
  145.                 }
  146.  
  147.                 if (beatBestLap && r.CurrentLap > 0 && !Cheats.Enabled)
  148.                 {
  149.                     BallisticEvents.Ui.CallOnTriggerMessage("NEW LAP RECORD", r, ScriptableHud.BnGAccent);
  150.                     SaveTime(r);
  151.  
  152.                     r.HasBestLapTime = true;
  153.                     r.BestLapTime = r.CurrentLapTime;
  154.                     SetHudBestTime(r.CurrentLapTime);
  155.                 }
  156.  
  157.                 // perfect lap
  158.                 if (r.IsPerfectLap)
  159.                 {
  160.                     IncrementExperience("Perfect Lap", Experience.PerfectLap);
  161.                     BallisticEvents.Ui.CallOnTriggerMessage("PERFECT LAP", r, ScriptableHud.BnGAccent);
  162.                     AudioHelpers.PlayVoice(AudioHelpers.Voice_PerfectLap);
  163.                 }
  164.  
  165.                 if (r.CurrentLap > 0) BallisticEvents.Ui.CallOnTriggerMessage(FloatToTime.Convert(r.CurrentLapTime, "0:00.00"), r, ScriptableHud.BnGAccent);
  166.                 if (r.CurrentLap == 0) r.CurrentLap = 1;
  167.  
  168.                 // speed lap tracking
  169.                 if (!_lapInvalidated)
  170.                 {
  171.                     if (r.IsPerfectLap) ++_perfectLaps;
  172.                     if (r.CurrentLapTime < _bestLap || _bestLap == 0) _bestLap = r.CurrentLapTime;
  173.                 }
  174.                 ++_laps;
  175.  
  176.                 r.TotalRaceTime = 0.0f;
  177.                 r.CurrentLapTime = 0.0f;
  178.                 r.IsPerfectLap = true;
  179.                 _lapInvalidated = false;
  180.                 BallisticEvents.Race.CallOnShipLapUpdate(r);
  181.             }
  182.  
  183.             base.OnShipTriggerStartLine(r);
  184.         }
  185.  
  186.         public override void OnShipTriggerMidLine(ShipRefs r)
  187.         {
  188.             base.OnShipTriggerMidLine(r);
  189.  
  190.             // checkpoint
  191.             if (!r.PassedValidationGate)
  192.             {
  193.                 AudioHelpers.PlayOneShot(AudioHelpers.UI_Checkpoint, AudioHelpers.E_AUDIOCHANNEL.INTERFACE, 1.0f, 1.0f);
  194.                 r.PassedValidationGate = true;
  195.             }
  196.         }
  197.  
  198.         public override void OnEventComplete()
  199.         {
  200.             base.OnEventComplete();
  201.  
  202.             // load event complete interface
  203.             ShipRefs r = Ships.LoadedShips[0];
  204.             Stats.IncrementStats(false, true);
  205.             ((EventCompleteSpeedlap)_eventCompleteInterface).Open(r.BestLapTime, _bestLap, _totalTime, _laps, _perfectLaps);
  206.  
  207.             // replace ship camera with finished camera
  208.             Object.Destroy(r.ShipCamera.GetComponent<ShipCamera>());
  209.             ShipFCam fCam = r.ShipCamera.gameObject.AddComponent<ShipFCam>();
  210.             fCam.r = r;
  211.  
  212.             // force close all of the interfaces (unpausing enables the interfaces again)
  213.             _disableHudOnNextFrame = true;
  214.         }
  215.  
  216.         public override void LoadInterfaces()
  217.         {
  218.             /*---Menus---*/
  219.             _pauseInterface = InterfaceLoader.LoadMenu(InterfaceLoader.Menus.SessionPause, false);
  220.             _eventCompleteInterface = InterfaceLoader.LoadMenu(InterfaceLoader.Menus.EventCompleteSpeedlap, false);
  221.  
  222.             /*---Huds---*/
  223.             _speedAndShieldHud = CreateNewHuds(InterfaceLoader.Huds.SpeedAndShield);
  224.             _notificationHud = CreateNewHuds(InterfaceLoader.Huds.NotificationBuffer);
  225.             _timeHud = (TimeSpeedLap)InterfaceLoader.LoadHud(InterfaceLoader.Huds.TimeSpeedLap);
  226.             _timeHud.TargetShip = Ships.LoadedShips[0];
  227.             _weaponHud = CreateNewHuds(InterfaceLoader.Huds.Weapon);
  228.             _nowPlayingHud = CreateNewHuds(InterfaceLoader.Huds.NowPlaying);
  229.         }
  230.  
  231.         public override void DestroyInterfaces()
  232.         {
  233.             /*---Menus---*/
  234.             if (_pauseInterface) Object.Destroy(_pauseInterface.gameObject);
  235.             if (_eventCompleteInterface) Object.Destroy(_eventCompleteInterface.gameObject);
  236.  
  237.             /*---Huds---*/
  238.             if (_speedAndShieldHud != null) DestroyHuds(_speedAndShieldHud);
  239.             if (_notificationHud != null) DestroyHuds(_notificationHud);
  240.             if (_timeHud) Object.Destroy(_timeHud.gameObject);
  241.             if (_weaponHud != null) DestroyHuds(_weaponHud);
  242.             if (_nowPlayingHud != null) DestroyHuds(_nowPlayingHud);
  243.         }
  244.  
  245.         /// <summary>
  246.         /// Sets the best lap time on the time HUD.
  247.         /// </summary>
  248.         public void SetHudBestTime(float time)
  249.         {
  250.             if (_timeHud) _timeHud.SetBestLap(time);
  251.         }
  252.  
  253.         public override void SaveTime(ShipRefs r)
  254.         {
  255.             // to disk
  256.             SaveData.WriteTime(SystemHelpers.GetLoadedScene(), Race.Speedclass, Name, r.CurrentLapTime);
  257.  
  258.             // to leaderboards
  259.             if (Steam.Enabled && !RaceManager.Instance.LeaderboardsDisabled)
  260.             {
  261.                 /*
  262.                 string leaderboardName = SteamLeaderboardManager.GetLeaderboardName(SystemHelpers.GetLoadedScene(), Race.Speedclass, Name, Cheats.ModernPhysics);
  263.  
  264.                 SteamLeaderboardManager.teamUpload = Player.PlayerOneShip;
  265.                 SteamLeaderboardManager.LeaderboardTask(leaderboardName, SteamLeaderboardManager.E_LEADERBOARDTASK.Upload, r.CurrentLapTime);
  266.                 */
  267.  
  268.                 Leaderboards.UploadScore(Leaderboards.GetLeaderboardName(this, SystemHelpers.GetLoadedScene(), Race.Speedclass, Cheats.ModernPhysics), Leaderboards.TimeToMiliseconds(r.CurrentLapTime),
  269.                     Player.PlayerOneShip, ELeaderboardSortMethod.k_ELeaderboardSortMethodAscending, ELeaderboardDisplayType.k_ELeaderboardDisplayTypeTimeMilliSeconds);
  270.             }
  271.         }
  272.  
  273.         public override void LoadTime(ShipRefs r)
  274.         {
  275.             base.LoadTime(r);
  276.             if (!r.LoadedBestLapTime) return;
  277.  
  278.             r.BestLapTime = r.TargetTime;
  279.             r.HasBestLapTime = true;
  280.  
  281.             if (r.HasBestLapTime) SetHudBestTime(r.TargetTime);
  282.             else _timeHud.BestLapText.text = _timeHud.EmptyTime;
  283.         }
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement