Skip to content
This repository has been archived by the owner on Oct 9, 2022. It is now read-only.

Commit

Permalink
Inventory Fix, Keep Held Item, Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildBoy12 committed Nov 10, 2020
1 parent 71f2809 commit 2fef5cf
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 89 deletions.
35 changes: 20 additions & 15 deletions Spies/Components/ChaosSpy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace Spies.Components
using System.Linq;
using UnityEngine;
using PlayerEvents = Exiled.Events.Handlers.Player;
using static Spies;

using static Spies;

public class ChaosSpy : MonoBehaviour
{
private Player _player;
public Team disguisedTeam = Team.RIP;
private RoleType _disguisedRoleType;

private void Start()
{
_player = Player.Get(gameObject);
Expand All @@ -26,26 +26,27 @@ private void Start()
private IEnumerator<float> InitSpy()
{
yield return Timing.WaitUntilFalse(() => disguisedTeam == Team.RIP);

_player.Broadcast(Instance.Config.ChaosSpies.SpawningBroadcast.Duration, Instance.Config.ChaosSpies.SpawningBroadcast.Content);

_player.Broadcast(Instance.Config.ChaosSpies.SpawningBroadcast.Duration,
Instance.Config.ChaosSpies.SpawningBroadcast.Content);
_player.SendConsoleMessage(Instance.Config.ChaosSpies.SpawningConsoleInfo, "yellow");

_disguisedRoleType = _player.Role;
_player.IsFriendlyFireEnabled = true;
if (!Instance.Config.MtfSpies.Inventory.Any())

if (!Instance.Config.ChaosSpies.Inventory.Any())
yield break;

if (Instance.Config.ChaosSpies.Inventory.TryGetValue(_disguisedRoleType, out List<ItemType> items))
_player.ResetInventory(items);
}

private void Update()
{
if (_player == null || _player.Role != _disguisedRoleType)
Destroy(false);
}

public void OnHurting(HurtingEventArgs ev)
{
if (ev.Attacker == ev.Target)
Expand All @@ -55,8 +56,9 @@ public void OnHurting(HurtingEventArgs ev)
{
if (Instance.Config.PreventSpyFf)
ev.Amount = 0;

ev.Attacker.Broadcast(Instance.Config.ChaosSpies.SameTeamBroadcast.Duration, Instance.Config.ChaosSpies.SameTeamBroadcast.Content);

ev.Attacker.Broadcast(Instance.Config.ChaosSpies.SameTeamBroadcast.Duration,
Instance.Config.ChaosSpies.SameTeamBroadcast.Content);
return;
}

Expand All @@ -65,13 +67,16 @@ public void OnHurting(HurtingEventArgs ev)

Destroy();
}

public void Destroy(bool reveal = true)
{
if (reveal)
{
var curItem = _player.CurrentItem;
_player.SetRole(Instance.Config.ChaosSpies.HiddenRole, true);
_player.Broadcast(Instance.Config.ChaosSpies.RevealBroadcast.Duration, Instance.Config.ChaosSpies.RevealBroadcast.Content);
_player.Broadcast(Instance.Config.ChaosSpies.RevealBroadcast.Duration,
Instance.Config.ChaosSpies.RevealBroadcast.Content);
Timing.CallDelayed(0.275f, () => { _player.CurrentItem = curItem; });
}

_player.IsFriendlyFireEnabled = false;
Expand Down
33 changes: 19 additions & 14 deletions Spies/Components/MtfSpy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace Spies.Components
using System.Linq;
using UnityEngine;
using PlayerEvents = Exiled.Events.Handlers.Player;
using static Spies;
using static Spies;

public class MtfSpy : MonoBehaviour
{
private Player _player;
public Team disguisedTeam = Team.RIP;
private RoleType _disguisedRoleType;

private void Start()
{
_player = Player.Get(gameObject);
Expand All @@ -26,23 +26,24 @@ private void Start()
private IEnumerator<float> InitSpy()
{
yield return Timing.WaitUntilFalse(() => disguisedTeam == Team.RIP);

_player.Broadcast(Instance.Config.MtfSpies.SpawningBroadcast.Duration, Instance.Config.MtfSpies.SpawningBroadcast.Content);

_player.Broadcast(Instance.Config.MtfSpies.SpawningBroadcast.Duration,
Instance.Config.MtfSpies.SpawningBroadcast.Content);
_player.SendConsoleMessage(Instance.Config.MtfSpies.SpawningConsoleInfo, "yellow");

_disguisedRoleType = _player.Role;
_player.IsFriendlyFireEnabled = true;
if (Instance.Config.ChaosSpies.Inventory.Any())

if (Instance.Config.MtfSpies.Inventory.Any())
_player.ResetInventory(Instance.Config.MtfSpies.Inventory);
}

private void Update()
{
if (_player == null || _player.Role != _disguisedRoleType)
Destroy(false);
}

public void OnHurting(HurtingEventArgs ev)
{
if (ev.Attacker == ev.Target)
Expand All @@ -52,8 +53,9 @@ public void OnHurting(HurtingEventArgs ev)
{
if (Instance.Config.PreventSpyFf)
ev.Amount = 0;

ev.Attacker.Broadcast(Instance.Config.MtfSpies.SameTeamBroadcast.Duration, Instance.Config.MtfSpies.SameTeamBroadcast.Content);

ev.Attacker.Broadcast(Instance.Config.MtfSpies.SameTeamBroadcast.Duration,
Instance.Config.MtfSpies.SameTeamBroadcast.Content);
return;
}

Expand All @@ -62,13 +64,16 @@ public void OnHurting(HurtingEventArgs ev)

Destroy();
}

public void Destroy(bool reveal = true)
{
if (reveal)
{
var curItem = _player.CurrentItem;
_player.SetRole(Instance.Config.MtfSpies.HiddenRole, true);
_player.Broadcast(Instance.Config.MtfSpies.RevealBroadcast.Duration, Instance.Config.MtfSpies.RevealBroadcast.Content);
_player.Broadcast(Instance.Config.MtfSpies.RevealBroadcast.Duration,
Instance.Config.MtfSpies.RevealBroadcast.Content);
Timing.CallDelayed(0.275f, () => { _player.CurrentItem = curItem; });
}

_player.IsFriendlyFireEnabled = false;
Expand Down
1 change: 1 addition & 0 deletions Spies/Configs/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Config : IConfig
{
[Description("Prevents spys from taking damage from the faction they are undercover from.")]
public bool PreventSpyFf { get; private set; } = false;

public ChaosSpies ChaosSpies { get; private set; } = new ChaosSpies();
public MtfSpies MtfSpies { get; private set; } = new MtfSpies();
public bool IsEnabled { get; set; } = true;
Expand Down
46 changes: 28 additions & 18 deletions Spies/Configs/SubConfigs/ChaosSpies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,45 @@ namespace Spies.Configs.SubConfigs
using Exiled.API.Features;
using System.Collections.Generic;
using System.ComponentModel;

public class ChaosSpies
{
[Description("Percent chance a spy will spawn in a given respawn wave.")]
public int SpawnChance { get; private set; } = 40;

[Description("Broadcast played to the spy when they spawn.")]
public Broadcast SpawningBroadcast { get; private set; } = new Broadcast("You are a <color=green>Chaos Spy!</color>\\nPress [`] for more info.", 10);
public Broadcast SpawningBroadcast { get; private set; } =
new Broadcast("You are a <color=green>Chaos Spy!</color>\\nPress [`] for more info.", 10);

[Description("Message to send to the spys console when they spawn.")]
public string SpawningConsoleInfo { get; private set; } = "You are a Chaos Spy! You are immune to MTF for now, but as soon as you damage an insurgent, your immunity will turn off. Help Chaos win the round and kill Scientists and MTF as necessary.";
public string SpawningConsoleInfo { get; private set; } =
"You are a Chaos Spy! You are immune to MTF for now, but as soon as you damage an insurgent, your immunity will turn off. Help Chaos win the round and kill Scientists and MTF as necessary.";

[Description("Role the spy will reveal as after shooting a MTF or Scientist.")]
public RoleType HiddenRole { get; private set; } = RoleType.ChaosInsurgency;

[Description("Broadcast played to the spy when their role is revealed.")]
public Broadcast RevealBroadcast { get; private set; } = new Broadcast("You have been revealed as a spy!", 10);

[Description("Broadcast played when a ClassD or ChaosInsurgency attacks the spy.")]
public Broadcast SameTeamBroadcast { get; private set; } = new Broadcast("You have attacked a spy of your own faction!", 2);
public Broadcast SameTeamBroadcast { get; private set; } =
new Broadcast("You have attacked a spy of your own faction!", 2);

[Description("Inventory the spy will spawn with. Leave empty for the default inventory.")]
public Dictionary<RoleType, List<ItemType>> Inventory { get; private set; } = new Dictionary<RoleType, List<ItemType>>
{
{
RoleType.NtfCadet, new List<ItemType>
{
ItemType.GunProject90,
ItemType.KeycardNTFLieutenant,
ItemType.GrenadeFrag,
ItemType.GrenadeFlash,
ItemType.Disarmer,
ItemType.WeaponManagerTablet
}
}
};
public Dictionary<RoleType, List<ItemType>> Inventory { get; private set; } =
new Dictionary<RoleType, List<ItemType>>
{
{
RoleType.NtfCadet, new List<ItemType>
{
ItemType.GunProject90,
ItemType.KeycardNTFLieutenant,
ItemType.GrenadeFrag,
ItemType.GrenadeFlash,
ItemType.Disarmer,
ItemType.WeaponManagerTablet
}
}
};
}
}
17 changes: 13 additions & 4 deletions Spies/Configs/SubConfigs/MtfSpies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@ namespace Spies.Configs.SubConfigs
using Exiled.API.Features;
using System.Collections.Generic;
using System.ComponentModel;

public class MtfSpies
{
[Description("Percent chance a spy will spawn in a given respawn wave.")]
public int SpawnChance { get; private set; } = 40;

[Description("Broadcast played to the spy when they spawn.")]
public Broadcast SpawningBroadcast { get; private set; } = new Broadcast("You are an <color=blue>MTF Spy!</color>\\nPress [`] for more info.", 10);
public Broadcast SpawningBroadcast { get; private set; } =
new Broadcast("You are an <color=blue>MTF Spy!</color>\\nPress [`] for more info.", 10);

[Description("Message to send to the spys console when they spawn.")]
public string SpawningConsoleInfo { get; private set; } = "You are a MTF Spy! You are immune to chaos for now, but as soon as you damage an insurgent, your immunity will turn off. Help MTF win the round and kill ClassD and Insurgents as necessary.";
public string SpawningConsoleInfo { get; private set; } =
"You are a MTF Spy! You are immune to chaos for now, but as soon as you damage an insurgent, your immunity will turn off. Help MTF win the round and kill ClassD and Insurgents as necessary.";

[Description("Role the spy will reveal as after shooting a ClassD or ChaosInsurgency.")]
public RoleType HiddenRole { get; private set; } = RoleType.NtfScientist;

[Description("Broadcast played to the spy when their role is revealed.")]
public Broadcast RevealBroadcast { get; private set; } = new Broadcast("You have been revealed as a spy!", 10);

[Description("Broadcast played when a MTF or Scientist attacks the spy.")]
public Broadcast SameTeamBroadcast { get; private set; } = new Broadcast("You have attacked a spy of your own faction!", 2);
public Broadcast SameTeamBroadcast { get; private set; } =
new Broadcast("You have attacked a spy of your own faction!", 2);

[Description("Inventory the spy will spawn with. Leave empty for the default inventory.")]
public List<ItemType> Inventory { get; private set; } = new List<ItemType>();
}
Expand Down
30 changes: 15 additions & 15 deletions Spies/Handlers/PlayerHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
namespace Spies.Handlers
{
using Components;
using Exiled.Events.EventArgs;
using Exiled.API.Features;
namespace Spies.Handlers
{
using Components;
using Exiled.Events.EventArgs;
using Exiled.API.Features;
using System.Linq;
using System.Collections.Generic;
using MEC;

public class PlayerHandlers
{
public bool MtfExterminated;
public bool ChaosExterminated;
public void OnDied(DiedEventArgs _)
{
public class PlayerHandlers
{
public bool MtfExterminated;
public bool ChaosExterminated;

public void OnDied(DiedEventArgs _)
{
Timing.CallDelayed(0.1f, () =>
{
MtfExterminated = !Player.Get(Team.MTF).Any() && !Player.Get(Team.RSC).Any();
Expand All @@ -34,7 +34,7 @@ public void OnDied(DiedEventArgs _)
if (chaosSpy != null)
chaosSpy.Destroy();
}
});
}
}
});
}
}
}
32 changes: 17 additions & 15 deletions Spies/Handlers/ServerHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,55 @@ namespace Spies.Handlers
using Exiled.API.Features;
using Exiled.Events.EventArgs;
using Respawning;
using System;
using System.Linq;
using System;
using System.Linq;
using static Spies;

public class ServerHandlers
{
private readonly Random _random = new Random();

public void OnConsoleCommand(SendingConsoleCommandEventArgs ev)
{
if (ev.Name.ToLower() != "reveal")
if (ev.Name.ToLower() != "reveal")
return;

var chaosSpy = ev.Player.GameObject.GetComponent<ChaosSpy>();
var mtfSpy = ev.Player.GameObject.GetComponent<MtfSpy>();
if (chaosSpy != null)
chaosSpy.Destroy();
else if (mtfSpy != null)
mtfSpy.Destroy();
else
return;

return;

ev.IsAllowed = false;
ev.ReturnMessage = "Revealing your role..";
ev.Color = "white";
}

public void OnRespawningTeam(RespawningTeamEventArgs ev)
{
Instance._playerHandlers.MtfExterminated = !Player.Get(Team.MTF).Any() && !Player.Get(Team.RSC).Any();
Instance._playerHandlers.ChaosExterminated = !Player.Get(Team.CHI).Any() && !Player.Get(Team.CDP).Any();
Instance.PlayerHandlers.MtfExterminated = !Player.Get(Team.MTF).Any() && !Player.Get(Team.RSC).Any();
Instance.PlayerHandlers.ChaosExterminated = !Player.Get(Team.CHI).Any() && !Player.Get(Team.CDP).Any();


Player ply = ev.Players[_random.Next(ev.Players.Count)];
switch (ev.NextKnownTeam)
{
case SpawnableTeamType.ChaosInsurgency:
if (_random.Next(101) > Instance.Config.MtfSpies.SpawnChance || Instance._playerHandlers.MtfExterminated)
if (_random.Next(101) > Instance.Config.MtfSpies.SpawnChance ||
Instance.PlayerHandlers.MtfExterminated)
break;

var mtfSpy = ply.GameObject.AddComponent<MtfSpy>();
mtfSpy.disguisedTeam = Team.CHI;
break;
case SpawnableTeamType.NineTailedFox:
if (_random.Next(101) > Instance.Config.ChaosSpies.SpawnChance || Instance._playerHandlers.ChaosExterminated)
if (_random.Next(101) > Instance.Config.ChaosSpies.SpawnChance ||
Instance.PlayerHandlers.ChaosExterminated)
break;

var chaosSpy = ply.GameObject.AddComponent<ChaosSpy>();
chaosSpy.disguisedTeam = Team.MTF;
break;
Expand Down
Loading

0 comments on commit 2fef5cf

Please sign in to comment.