Skip to content

Instantly share code, notes, and snippets.

@Stuyk
Created March 16, 2019 22:25
Show Gist options
  • Save Stuyk/753d88065717715cfb32d6e9eb61c8fc to your computer and use it in GitHub Desktop.
Save Stuyk/753d88065717715cfb32d6e9eb61c8fc to your computer and use it in GitHub Desktop.
Dynamic Jobs - Video 19 - 21
namespace Main
{
public class Main : Events.Script
{
public static MarkerInstance NextObjective { get; set; } = null;
public static float Player_Money { get; set; } = 0;
public static int ResX = 0;
public static int ResY = 0;
public static int tick = 0;
public static int around_one_sec = 80;
public static bool check_for_key_press = true;
public Main()
{
Chat.Output("Loaded Clientside");
Events.Add("update_money", UpdateMoney);
Events.Add("load_objective", LoadObjective);
Events.Add("clear_all_markers", ClearJobMarkers);
Events.Tick += OnUpdate;
RAGE.Game.Graphics.GetScreenResolution(ref ResX, ref ResY);
}
private void OnUpdate(List<Events.TickNametagData> nametags)
{
tick++;
Draws();
if (tick >= around_one_sec)
{
HeartBeat();
}
}
public void HeartBeat()
{
tick = 0;
check_for_key_press = true;
if (NextObjective != null)
{
// Look for Marker Instance
MarkerInstance markerInstance = MarkerHelper.IsNearMarker(3f);
if (markerInstance != null)
{
NextObjective = null;
MarkerHelper.ClearMarker(markerInstance);
Events.CallRemote("marker_completed");
}
}
}
public static void ClearJobMarkers(object[] args)
{
MarkerHelper.ClearAllMarkers();
}
public static void LoadObjective(object[] args)
{
Vector3 markerPosition = (Vector3) args[0];
float markerSize = Convert.ToSingle(args[1]);
if (markerPosition == null)
return;
if (NextObjective != null)
{
MarkerHelper.ClearMarker(NextObjective);
}
NextObjective = new MarkerInstance(markerPosition, markerSize);
}
public static void UpdateMoney(object[] args)
{
Player_Money = Convert.ToSingle(args[0]);
}
public void Draws()
{
RAGE.Game.UIText.Draw($"${Player_Money}", new Point(ResX - 100, 50), 0.8f, Color.Blue, RAGE.Game.Font.ChaletComprimeCologne, false);
}
}
}
namespace Main
{
public static class MarkerHelper
{
public static List<MarkerInstance> Markers = new List<MarkerInstance>(); // Locally created markers.
public static void ClearAllMarkers()
{
while(Markers.Count != 0)
{
Markers[Markers.Count - 1].Delete();
Markers.Remove(Markers[Markers.Count - 1]);
}
}
public static void ClearMarker(MarkerInstance markerInstance)
{
markerInstance.Delete();
}
public static MarkerInstance IsNearMarker(float distance)
{
return Markers.Find(marker => marker.Marker.Position.DistanceTo2D(Player.LocalPlayer.Position) <= distance);
}
}
public class MarkerInstance
{
public Marker Marker { get; set; } = null;
public bool RecentlyPlaced = true;
public float Size = 1f;
public MarkerInstance(Vector3 position, float size)
{
Marker = new Marker(1, position.Subtract(new Vector3(0, 0, 1)), size, new Vector3(), new Vector3(), new RGBA(255, 255, 255, 100));
Size = size;
MarkerHelper.Markers.Add(this);
}
public void Delete()
{
Marker.Destroy();
MarkerHelper.Markers.Remove(this);
RAGE.Game.Audio.PlaySoundFrontend(1, "Beep_Red", "DLC_HEIST_HACKING_SNAKE_SOUNDS", true);
}
}
}
namespace testresource.DataModels
{
public class JobInfo
{
public Queue<ObjectiveInfo> Objectives = new Queue<ObjectiveInfo>();
public Client Owner { get; set; }
public void AddObjective(Vector3 position, float size)
{
ObjectiveInfo objectiveInfo = new ObjectiveInfo();
objectiveInfo.Position = position;
objectiveInfo.Size = size;
Objectives.Enqueue(objectiveInfo);
}
public void StartJob(Client client)
{
Owner = client;
NextObjective(client);
client.SetData("Job", this);
}
public void NextObjective(Client client)
{
ObjectiveInfo next_objective = Objectives.Peek();
client.TriggerEvent("load_objective", next_objective.Position, next_objective.Size);
}
public void CheckObjective(Client client)
{
ObjectiveInfo current_objective = Objectives.Peek();
if (!current_objective.IsInObjective(client))
return;
Objectives.Dequeue();
if (Objectives.Count == 0)
{
client.SendChatMessage("Finished All Objectives");
client.TriggerEvent("clear_all_markers");
return;
}
NextObjective(client);
}
}
}
namespace testresource.DataModels
{
public class ObjectiveInfo
{
public Vector3 Position { get; set; }
public float Size { get; set; }
public bool IsInObjective(Client client)
{
return (client.Position.DistanceTo2D(Position) <= Size) ? true : false;
}
}
}
namespace testresource.Handler
{
public class JobHandler : Script
{
public static List<Vector3> NewPositions = new List<Vector3>();
[RemoteEvent("marker_completed")]
public void RemoteEvent_MarkerCompleted(Client client)
{
if (!client.HasData("Job"))
return;
JobInfo jobInfo = client.GetData("Job");
jobInfo.CheckObjective(client);
}
[Command("add")]
public void CMD_AddPosition(Client client)
{
NewPositions.Add(client.Position);
}
[Command("startjob")]
public void CMD_StartJob(Client client, float size)
{
JobInfo jobInfo = new JobInfo();
foreach(Vector3 vector in NewPositions)
{
jobInfo.AddObjective(vector, size);
}
jobInfo.StartJob(client);
NewPositions = new List<Vector3>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment