From 0dc3b19bc21ba5a2ddfe74b1836337e91fa2e7ab Mon Sep 17 00:00:00 2001 From: Rich Dunne Date: Thu, 20 Aug 2020 15:31:10 -0600 Subject: [PATCH] Refactored with more encapsulation, better protection for class fields --- SceneManager/Object Classes/Waypoint.cs | 132 ++++++++++++++++++------ 1 file changed, 101 insertions(+), 31 deletions(-) diff --git a/SceneManager/Object Classes/Waypoint.cs b/SceneManager/Object Classes/Waypoint.cs index 35fe941..bc0d4f1 100644 --- a/SceneManager/Object Classes/Waypoint.cs +++ b/SceneManager/Object Classes/Waypoint.cs @@ -1,52 +1,122 @@ using Rage; +using System.Runtime.InteropServices; namespace SceneManager { public class Waypoint { - public int Path; - public int WaypointNum; - public Vector3 WaypointPos; - public float Speed; - public VehicleDrivingFlags DrivingFlag; - public Blip WaypointBlip; - public uint YieldZone; - public bool Collector; - public float CollectorRadius; - public Blip CollectorRadiusBlip; + public int Path { get; private set; } + public int Number { get; private set; } + public Vector3 Position { get; private set; } + public float Speed { get; private set; } + public VehicleDrivingFlags DrivingFlag { get; private set; } + public Blip Blip { get; private set; } + public uint YieldZone { get; private set; } + public bool Collector { get; private set; } + public float CollectorRadius { get; private set; } + public Blip CollectorRadiusBlip { get; private set; } - // Can this constructor be deleted? - //public Waypoint(int path, Vector3 waypointPos, float speed, VehicleDrivingFlags drivingFlag, Blip waypointBlip, uint yieldZone) - //{ - // Path = path; - // WaypointPos = waypointPos; - // Speed = speed; - // DrivingFlag = drivingFlag; - // WaypointBlip = waypointBlip; - // YieldZone = yieldZone; - //} + public Waypoint(int path, int waypointNum, Vector3 waypointPos, float speed, VehicleDrivingFlags drivingFlag, Blip waypointBlip) + { + Path = path; + Number = waypointNum; + Position = waypointPos; + Speed = speed; + DrivingFlag = drivingFlag; + Blip = waypointBlip; + + } public Waypoint(int path, int waypointNum, Vector3 waypointPos, float speed, VehicleDrivingFlags drivingFlag, Blip waypointBlip, bool collector, float collectorRadius, uint yieldZone) { Path = path; - WaypointNum = waypointNum; - WaypointPos = waypointPos; + Number = waypointNum; + Position = waypointPos; Speed = speed; DrivingFlag = drivingFlag; - WaypointBlip = waypointBlip; + Blip = waypointBlip; Collector = collector; CollectorRadius = collectorRadius; - if (collector) + YieldZone = yieldZone; + CollectorRadiusBlip = new Blip(waypointBlip.Position, collectorRadius) { - YieldZone = yieldZone; - CollectorRadiusBlip = new Blip(waypointBlip.Position, collectorRadius) - { - Color = waypointBlip.Color, - Alpha = 0.5f - }; - + Color = waypointBlip.Color, + Alpha = 0.5f + }; + } + public void UpdateWaypoint(Waypoint currentWaypoint, VehicleDrivingFlags drivingFlag, float drivingSpeed, bool collectorWaypointChecked, float collectorRadius, bool updateWaypointPositionChecked) + { + UpdateDrivingFlag(drivingFlag); + UpdateWaypointSpeed(drivingSpeed); + UpdateCollectorOptions(currentWaypoint, drivingSpeed, collectorWaypointChecked, collectorRadius); + if (updateWaypointPositionChecked) + { + UpdateWaypointPosition(Game.LocalPlayer.Character.Position); } } + + private void UpdateCollectorOptions(Waypoint currentWaypoint, float drivingSpeed, bool collectorWaypointChecked, float collectorRadius) + { + if (collectorWaypointChecked) + { + Collector = true; + YieldZone = World.AddSpeedZone(Game.LocalPlayer.Character.Position, 50f, drivingSpeed); + if (CollectorRadiusBlip) + { + currentWaypoint.CollectorRadiusBlip.Alpha = 0.5f; + currentWaypoint.CollectorRadiusBlip.Scale = collectorRadius; + } + else + { + CollectorRadiusBlip = new Blip(Blip.Position, collectorRadius) + { + Color = Blip.Color, + Alpha = 0.5f + }; + } + CollectorRadius = collectorRadius; + } + else + { + Collector = false; + World.RemoveSpeedZone(YieldZone); + if (CollectorRadiusBlip) + { + CollectorRadiusBlip.Delete(); + } + } + } + + private void UpdateWaypointPosition(Vector3 newWaypointPosition) + { + Position = newWaypointPosition; + UpdateWaypointBlipPosition(); + } + + private void UpdateWaypointSpeed(float newWaypointSpeed) + { + Speed = newWaypointSpeed; + } + + private void UpdateDrivingFlag(VehicleDrivingFlags newDrivingFlag) + { + DrivingFlag = newDrivingFlag; + } + + private void UpdateWaypointBlipPosition() + { + Blip.Position = Game.LocalPlayer.Character.Position; + } + + public void UpdatePathNumber(int newPathNumber) + { + Path = newPathNumber; + } + + public void UpdateWaypointNumber(int newWaypointNumber) + { + Number = newWaypointNumber; + } } }