Skip to content

Commit

Permalink
Dev reward system (#6)
Browse files Browse the repository at this point in the history
* reward system implemented

* - small fixes
- Warehouse locations michael added

* trafficking cleanup

* Update Los.Santos.Dope.Wars.csproj

Co-authored-by: Bobo <Bobo@BOBO-GAME>
  • Loading branch information
BoBoBaSs84 and Bobo authored Jun 30, 2022
1 parent b05f98c commit ed97b79
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 105 deletions.
4 changes: 1 addition & 3 deletions Classes/DrugWarehouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ public void CreateBlip(BlipSprite blipSprite = BlipSprite.BusinessForSale, BlipC
}

/// <inheritdoc/>
public void ChangeBlip(string? blipName, BlipSprite blipSprite = BlipSprite.BusinessForSale, BlipColor blipColor = BlipColor.White)
public void ChangeBlip(BlipSprite blipSprite = BlipSprite.BusinessForSale, BlipColor blipColor = BlipColor.White)
{
if (BlipCreated)
{
if (blipName is not null)
Blip!.Name = blipName;
Blip!.Sprite = blipSprite;
Blip!.Color = blipColor;
}
Expand Down
12 changes: 9 additions & 3 deletions Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,17 @@ public static class Constants
public static readonly Vector3 MissionMarkerScale = new(8f, 8f, 1.5f);

public static readonly Color MissionMarkerColor = Color.FromArgb(255, 240, 200, 80);

public static readonly Vector3 WarehouseEntranceFranklin = new(-320.3f, -1389.8f, 36.5f);

public static readonly Vector3 WarehouseLocationFranklin = new(466.3f, -1384.1f, 37.1f);
public static readonly Vector3 WarehouseLocationFranklin = new(-307.3f, -1399.5f, 41.6f);

public static readonly Vector3 WarehouseEntranceFranklin = new(477.71f, -1397.76f, 31.04f);
public static readonly Vector3 WarehouseMissionStartFranklin = new(-323.7f, -1400.5f, 31.8f);

public static readonly Vector3 WarehouseMissionStartFranklin = new(488.73f, 1400.86f, 29.31f);
public static readonly Vector3 WarehouseEntranceMichael = new(794.2f, -102.8f, 82f);

public static readonly Vector3 WarehouseLocationMichael = new(799.5f, -94.6f, 80.6f);

public static readonly Vector3 WarehouseMissionStartMichael = new(794.8f, -79.2f, 80.6f);
}
}
3 changes: 1 addition & 2 deletions Contracts/IPlayerProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public interface IPlayerProperty
/// </summary>
/// <param name="blipSprite"></param>
/// <param name="blipColor"></param>
/// <param name="blipName"></param>
void ChangeBlip(string? blipName, BlipSprite blipSprite = BlipSprite.BusinessForSale, BlipColor blipColor = BlipColor.White);
void ChangeBlip(BlipSprite blipSprite = BlipSprite.BusinessForSale, BlipColor blipColor = BlipColor.White);
}
}
80 changes: 80 additions & 0 deletions Features/RewardSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using GTA.UI;
using Los.Santos.Dope.Wars.Extension;
using Los.Santos.Dope.Wars.Persistence;
using System;
using System.ComponentModel;

namespace Los.Santos.Dope.Wars.Features
{
/// <summary>
/// The <see cref="RewardSystem"/> class servers as the handler for rewarding the currently played character throughout the game progress
/// </summary>
public static class RewardSystem
{
private static GameState? _gameState;
private static PlayerStats? _playerStats;

/// <summary>
/// The <see cref="Initialized"/> property indicates if the <see cref="Init(GameState)"/> method was called
/// </summary>
public static bool Initialized { get; private set; }

/// <summary>
/// Standard constructor for the <see cref="RewardSystem"/> class
/// </summary>
static RewardSystem() { }

/// <summary>
/// The <see cref="OnTick(object, EventArgs)"/> method, run for every tick
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void OnTick(object sender, EventArgs e)
{
if (!Initialized)
return;

if (_playerStats != Utils.GetPlayerStatsFromModel(_gameState!))
{
_playerStats!.PropertyChanged -= OnCurrentLevelPropertyChanged;
_playerStats = Utils.GetPlayerStatsFromModel(_gameState!);
_playerStats.PropertyChanged += OnCurrentLevelPropertyChanged;
}
}

/// <summary>
/// The <see cref="Init(GameState)"/> method must be called from outside with the needed parameters
/// </summary>
/// <param name="gameState"></param>
public static void Init(GameState gameState)
{
_gameState = gameState;
_playerStats = Utils.GetPlayerStatsFromModel(gameState);
_playerStats.PropertyChanged += OnCurrentLevelPropertyChanged;
Initialized = true;
}

/// <summary>
/// The <see cref="OnCurrentLevelPropertyChanged(object, PropertyChangedEventArgs)"/> method is where the magic happens
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void OnCurrentLevelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (sender is PlayerStats playerStats)
{
// this is what happens for every level up
Notification.Show($"Congratulations you have reached level ~y~{playerStats.CurrentLevel}~w~. Your bag size has been increased to ~y~{playerStats.MaxBagSize}~w~.");

// this is when the warehouse reward has been granted
if (playerStats.CurrentLevel.Equals(5))
{
playerStats.SpecialReward.Warehouse |= Enums.WarehouseStates.Unlocked;
Notification.Show($"Congratulations you have unlocked the ~y~warehouse~w~. Buy it to store drugs and drug money safely.");
// if we have come this far, save
Utils.SaveGameState(_gameState!);
}
}
}
}
}
35 changes: 22 additions & 13 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GTA;
using Los.Santos.Dope.Wars.Extension;
using Los.Santos.Dope.Wars.Features;
using Los.Santos.Dope.Wars.Missions;
using Los.Santos.Dope.Wars.Persistence;
using System;
Expand All @@ -15,8 +16,9 @@ public class Main : Script
private static bool _gameStateLoaded;
private static bool _traffickingLoaded;
private static bool _warehouseLoaded;
private static GameSettings GameSettings = null!;
private static GameState GameState = null!;
private static bool _rewardSystemLoaded;
private static GameSettings? GameSettings;
private static GameState? GameState;

/// <summary>
/// The <see cref="ScriptDirectory"/> property, this is the main directory for logging and saving the game config and the game state
Expand All @@ -31,16 +33,16 @@ public Main()
{
Interval = 10;
ScriptDirectory = BaseDirectory;
Init();

Tick += OnTick;
Tick += Trafficking.OnTick;
Tick += Warehouse.OnTick;
Tick += RewardSystem.OnTick;

Aborted += OnAborted;
Aborted += Trafficking.OnAborted;
Aborted += Warehouse.OnAborted;

Init();
Aborted += Warehouse.OnAborted;
}
#endregion

Expand All @@ -55,16 +57,23 @@ private void OnTick(object sender, EventArgs e)

if (!_traffickingLoaded)
{
Trafficking.Init(GameSettings, GameState);
_traffickingLoaded = true;
Logger.Status($"Trafficking loaded: {_traffickingLoaded}");
Trafficking.Init(GameSettings!, GameState!);
_traffickingLoaded = Trafficking.Initialized;
Logger.Status($"{nameof(Trafficking)} loaded: {_traffickingLoaded}");
}

if (!_warehouseLoaded)
{
Warehouse.Init(GameState);
_warehouseLoaded = true;
Logger.Status($"Warehouse loaded: {_traffickingLoaded}");
Warehouse.Init(GameState!);
_warehouseLoaded = Warehouse.Initialized;
Logger.Status($"{nameof(Warehouse)} loaded: {_traffickingLoaded}");
}

if (!_rewardSystemLoaded)
{
RewardSystem.Init(GameState!);
_rewardSystemLoaded = RewardSystem.Initialized;
Logger.Status($"{nameof(RewardSystem)} loaded: {_traffickingLoaded}");
}
}

Expand All @@ -74,7 +83,7 @@ private static void Init()
{
if (!_settingsLoaded || !_gameStateLoaded)
{
Logger.Status($"Mod: {Constants.AssemblyName} - Vesion: {Constants.AssemblyVersion}");
Logger.Status($"Game: {Constants.AssemblyName} - Vesion: {Constants.AssemblyVersion}");

if (!_settingsLoaded)
{
Expand All @@ -93,7 +102,7 @@ private static void Init()
{
GameState = loadedGameState;
loadedGameState.LastRestock = ScriptHookUtils.GetGameDate().AddHours(-25);
Logger.Status($"Last game state loaded. Version: {GameSettings.Version}");
Logger.Status($"Last game state loaded. Version: {GameSettings!.Version}");
_gameStateLoaded = success;
}
}
Expand Down
77 changes: 42 additions & 35 deletions Missions/Trafficking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ namespace Los.Santos.Dope.Wars.Missions
/// </summary>
public class Trafficking
{
private static List<DrugDealer> DrugDealers = null!;
private static GameSettings GameSettings = null!;
private static GameState GameState = null!;
private static Script Script = null!;
private static PlayerStats PlayerStats = null!;
private static List<DrugDealer>? _drugDealers;
private static GameSettings? _gameSettings;
private static GameState? _gameState;
private static PlayerStats? _playerStats;
private static Ped? _player;

private static DrugDealer? CurrentDrugDealer { get; set; }

/// <summary>
/// The <see cref="Initialized"/> property indicates if the <see cref="Init(GameSettings, GameState)"/> method was called
/// </summary>
public static bool Initialized { get; private set; }

/// <summary>
/// The empty <see cref="Trafficking"/> class constructor
/// </summary>
Expand All @@ -34,9 +39,11 @@ static Trafficking() { }
/// <param name="gameState"></param>
public static void Init(GameSettings gameSettings, GameState gameState)
{
GameSettings = gameSettings;
GameState = gameState;
DrugDealers = GetDrugDealers();
_gameSettings = gameSettings;
_gameState = gameState;
_drugDealers = GetDrugDealers();
_player = Game.Player.Character;
Initialized = true;
}

/// <summary>
Expand All @@ -46,12 +53,12 @@ public static void Init(GameSettings gameSettings, GameState gameState)
/// <param name="e"></param>
public static void OnAborted(object sender, EventArgs e)
{
foreach (DrugDealer? dealer in DrugDealers)
foreach (DrugDealer? dealer in _drugDealers!)
{
dealer.DeleteBlip();
dealer.DeletePed();
}
DrugDealers.Clear();
_drugDealers.Clear();
}

/// <summary>
Expand All @@ -61,59 +68,59 @@ public static void OnAborted(object sender, EventArgs e)
/// <param name="e"></param>
public static void OnTick(object sender, EventArgs e)
{
if (sender is Script script)
Script = script;

while (GameState is null && GameState is null)
if (!Initialized)
return;

try
{
Ped player = Game.Player.Character;
PlayerStats = Utils.GetPlayerStatsFromModel(GameState);
if (_player != Game.Player.Character)
_player = Game.Player.Character;

if (_playerStats != Utils.GetPlayerStatsFromModel(_gameState!))
_playerStats = Utils.GetPlayerStatsFromModel(_gameState!);

// The dealer drug stash restock (quantity)
if (ScriptHookUtils.GetGameDate() > GameState.LastRestock.AddHours(GameSettings.DealerSettings.RestockIntervalHours))
if (ScriptHookUtils.GetGameDate() > _gameState!.LastRestock.AddHours(_gameSettings!.DealerSettings.RestockIntervalHours))
{
GameState.LastRestock = ScriptHookUtils.GetGameDate();
_gameState.LastRestock = ScriptHookUtils.GetGameDate();

foreach (DrugDealer dealer in DrugDealers)
foreach (DrugDealer dealer in _drugDealers!)
{
dealer.DrugStash.Init();
dealer.DrugStash.RestockQuantity(PlayerStats, GameSettings);
dealer.DrugStash.RefreshDrugMoney(PlayerStats, GameSettings);
dealer.DrugStash.RefreshCurrentPrice(PlayerStats, GameSettings);
dealer.DrugStash.RestockQuantity(_playerStats, _gameSettings);
dealer.DrugStash.RefreshDrugMoney(_playerStats, _gameSettings);
dealer.DrugStash.RefreshCurrentPrice(_playerStats, _gameSettings);
}
ScriptHookUtils.NotifyWithPicture("Anonymous", "Tip-off", "The drug dealers have been restocked.", 0);
Utils.SaveGameState(GameState);
Utils.SaveGameState(_gameState);
}
else
// The dealer drug stash refresh (money & prices)
if (ScriptHookUtils.GetGameDate() > GameState.LastRefresh.AddHours(GameSettings.DealerSettings.RefreshIntervalHours))
if (ScriptHookUtils.GetGameDate() > _gameState.LastRefresh.AddHours(_gameSettings.DealerSettings.RefreshIntervalHours))
{
GameState.LastRefresh = ScriptHookUtils.GetGameDate();
foreach (DrugDealer dealer in DrugDealers)
_gameState.LastRefresh = ScriptHookUtils.GetGameDate();
foreach (DrugDealer dealer in _drugDealers!)
{
dealer.DrugStash.RefreshDrugMoney(PlayerStats, GameSettings);
dealer.DrugStash.RefreshCurrentPrice(PlayerStats, GameSettings);
dealer.DrugStash.RefreshDrugMoney(_playerStats, _gameSettings);
dealer.DrugStash.RefreshCurrentPrice(_playerStats, _gameSettings);
}
Utils.SaveGameState(GameState);
Utils.SaveGameState(_gameState);
}

foreach (DrugDealer dealer in DrugDealers)
foreach (DrugDealer dealer in _drugDealers!)
{
// creating the blips if not already created
if (!dealer.BlipCreated)
{
dealer.CreateBlip();
}
// if the player is in range of the dealer
if (player.IsInRange(dealer.Position, 100f))
if (_player.IsInRange(dealer.Position, 100f))
{
// if the ped was not created
if (!dealer.PedCreated)
{
(float health, float armor) = Utils.GetDealerHealthArmor(GameSettings.DealerSettings, PlayerStats.CurrentLevel);
(float health, float armor) = Utils.GetDealerHealthArmor(_gameSettings.DealerSettings, _playerStats.CurrentLevel);
int money = dealer.DrugStash.Money;
dealer.CreatePed(health, armor, money);
}
Expand All @@ -126,10 +133,10 @@ public static void OnTick(object sender, EventArgs e)
}

// now we are real close to the dealer
if (player.IsInRange(dealer.Position, 3f) && CurrentDrugDealer is null && Game.Player.WantedLevel == 0)
if (_player.IsInRange(dealer.Position, 3f) && CurrentDrugDealer is null && Game.Player.WantedLevel == 0)
{
CurrentDrugDealer = dealer;
DealMenu.Init(PlayerStats.DrugStash, (DrugStash)dealer.DrugStash, GameState);
DealMenu.Init(_playerStats.DrugStash, (DrugStash)dealer.DrugStash, _gameState);

if (CheckIfDealerCanTrade(dealer))
DealMenu.ShowDealMenu = true;
Expand All @@ -138,7 +145,7 @@ public static void OnTick(object sender, EventArgs e)
DealMenu.ShowDealMenu = false;
}

else if ((!player.IsInRange(dealer.Position, 3f) && CurrentDrugDealer == dealer) || Game.Player.WantedLevel != 0)
else if ((!_player.IsInRange(dealer.Position, 3f) && CurrentDrugDealer == dealer) || Game.Player.WantedLevel != 0)
{
CurrentDrugDealer = null!;
DealMenu.ShowDealMenu = false;
Expand Down
Loading

0 comments on commit ed97b79

Please sign in to comment.