-
Notifications
You must be signed in to change notification settings - Fork 181
/
RoleExtensions.cs
169 lines (149 loc) · 8.15 KB
/
RoleExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// -----------------------------------------------------------------------
// <copyright file="RoleExtensions.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------
namespace Exiled.API.Extensions
{
using System;
using System.Collections.Generic;
using System.Linq;
using Enums;
using Exiled.API.Features.Spawn;
using InventorySystem;
using InventorySystem.Configs;
using PlayerRoles;
using PlayerRoles.FirstPersonControl;
using UnityEngine;
using Team = PlayerRoles.Team;
/// <summary>
/// A set of extensions for <see cref="RoleTypeId"/>.
/// </summary>
public static class RoleExtensions
{
/// <summary>
/// Gets a <see cref="RoleTypeId">role's</see> <see cref="Color"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/> to get the color of.</param>
/// <returns>The <see cref="Color"/> of the role.</returns>
public static Color GetColor(this RoleTypeId roleType) => roleType == RoleTypeId.None ? Color.white : roleType.GetRoleBase().RoleColor;
/// <summary>
/// Gets a <see cref="RoleTypeId">role's</see> <see cref="Side"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/> to check the side of.</param>
/// <returns><see cref="Side"/>.</returns>
public static Side GetSide(this RoleTypeId roleType) => PlayerRolesUtils.GetTeam(roleType).GetSide();
/// <summary>
/// Gets a <see cref="Team">team's</see> <see cref="Side"/>.
/// </summary>
/// <param name="team">The <see cref="Team"/> to get the <see cref="Side"/> of.</param>
/// <returns><see cref="Side"/>.</returns>.
public static Side GetSide(this Team team) => team switch
{
Team.SCPs => Side.Scp,
Team.FoundationForces or Team.Scientists => Side.Mtf,
Team.ChaosInsurgency or Team.ClassD => Side.ChaosInsurgency,
Team.OtherAlive => Side.Tutorial,
_ => Side.None,
};
/// <summary>
/// Gets the <see cref="Team"/> of the given <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <returns><see cref="Team"/>.</returns>
public static Team GetTeam(this RoleTypeId roleType) => roleType switch
{
RoleTypeId.ChaosConscript or RoleTypeId.ChaosMarauder or RoleTypeId.ChaosRepressor or RoleTypeId.ChaosRifleman => Team.ChaosInsurgency,
RoleTypeId.Scientist => Team.Scientists,
RoleTypeId.ClassD => Team.ClassD,
RoleTypeId.Scp049 or RoleTypeId.Scp939 or RoleTypeId.Scp0492 or RoleTypeId.Scp079 or RoleTypeId.Scp096 or RoleTypeId.Scp106 or RoleTypeId.Scp173 or RoleTypeId.Scp3114 => Team.SCPs,
RoleTypeId.FacilityGuard or RoleTypeId.NtfCaptain or RoleTypeId.NtfPrivate or RoleTypeId.NtfSergeant or RoleTypeId.NtfSpecialist => Team.FoundationForces,
RoleTypeId.Tutorial => Team.OtherAlive,
_ => Team.Dead,
};
/// <summary>
/// Gets the full name of the given <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="typeId">The <see cref="RoleTypeId"/>.</param>
/// <returns>The full name.</returns>
public static string GetFullName(this RoleTypeId typeId) => typeId.GetRoleBase().RoleName;
/// <summary>
/// Gets the base <see cref="PlayerRoleBase"/> of the given <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <returns>The <see cref="PlayerRoleBase"/>.</returns>
public static PlayerRoleBase GetRoleBase(this RoleTypeId roleType) => roleType.TryGetRoleBase(out PlayerRoleBase roleBase) ? roleBase : null;
/// <summary>
/// Tries to get the base <see cref="PlayerRoleBase"/> of the given <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <param name="roleBase">The <see cref="PlayerRoleBase"/> to return.</param>
/// <returns>The <see cref="PlayerRoleBase"/>.</returns>
public static bool TryGetRoleBase(this RoleTypeId roleType, out PlayerRoleBase roleBase) => PlayerRoleLoader.TryGetRoleTemplate(roleType, out roleBase);
/// <summary>
/// Tries to get the base <see cref="PlayerRoleBase"/> of the given <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <param name="roleBase">The <see cref="PlayerRoleBase"/> to return.</param>
/// <typeparam name="T">The type to cast the <see cref="PlayerRoleBase"/> to.</typeparam>
/// <returns>The <see cref="PlayerRoleBase"/>.</returns>
public static bool TryGetRoleBase<T>(this RoleTypeId roleType, out T roleBase)
where T : PlayerRoleBase => PlayerRoleLoader.TryGetRoleTemplate(roleType, out roleBase);
/// <summary>
/// Gets the <see cref="LeadingTeam"/>.
/// </summary>
/// <param name="team">Team.</param>
/// <returns><see cref="LeadingTeam"/>.</returns>
public static LeadingTeam GetLeadingTeam(this Team team) => team switch
{
Team.ClassD or Team.ChaosInsurgency => LeadingTeam.ChaosInsurgency,
Team.FoundationForces or Team.Scientists => LeadingTeam.FacilityForces,
Team.SCPs => LeadingTeam.Anomalies,
_ => LeadingTeam.Draw,
};
/// <summary>
/// Checks whether a <see cref="RoleTypeId"/> is an <see cref="IFpcRole"/> or not.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <returns>Returns whether <paramref name="roleType"/> is an <see cref="IFpcRole"/> or not.</returns>
public static bool IsFpcRole(this RoleTypeId roleType) => roleType.GetRoleBase() is IFpcRole;
/// <summary>
/// Gets a random spawn point of a <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/> to get the spawn point from.</param>
/// <returns>Returns a <see cref="SpawnLocation"/> representing the spawn, or <see langword="null"/> if no spawns were found.</returns>
public static SpawnLocation GetRandomSpawnLocation(this RoleTypeId roleType)
{
if (roleType.TryGetRoleBase(out FpcStandardRoleBase fpcRole) &&
fpcRole.SpawnpointHandler != null &&
fpcRole.SpawnpointHandler.TryGetSpawnpoint(out Vector3 position, out float horizontalRotation))
{
return new(roleType, position, horizontalRotation);
}
return null;
}
/// <summary>
/// Gets the starting items of a <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <returns>An <see cref="Array"/> of <see cref="ItemType"/> that the role receives on spawn. Will be empty for classes that do not spawn with items.</returns>
public static ItemType[] GetStartingInventory(this RoleTypeId roleType)
{
if (StartingInventories.DefinedInventories.TryGetValue(roleType, out InventoryRoleInfo info))
return info.Items;
return Array.Empty<ItemType>();
}
/// <summary>
/// Gets the starting ammo of a <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <returns>An <see cref="Array"/> of <see cref="ItemType"/> that the role receives on spawn. Will be empty for classes that do not spawn with ammo.</returns>
public static Dictionary<AmmoType, ushort> GetStartingAmmo(this RoleTypeId roleType)
{
if (StartingInventories.DefinedInventories.TryGetValue(roleType, out InventoryRoleInfo info))
return info.Ammo.ToDictionary(kvp => kvp.Key.GetAmmoType(), kvp => kvp.Value);
return new();
}
}
}