-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventHandler.cs
More file actions
141 lines (107 loc) · 4.74 KB
/
EventHandler.cs
File metadata and controls
141 lines (107 loc) · 4.74 KB
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
using System.Collections.Generic;
using System.Linq;
using Exiled.API.Features;
using Exiled.Events.EventArgs.Player;
using PlayerRoles;
using PlayerRoles.PlayableScps.Scp3114;
using Scp3114SpawnControl.Models;
using UnityEngine;
using Events = Exiled.Events.Handlers;
using Random = UnityEngine.Random;
namespace Scp3114SpawnControl
{
public class EventHandler
{
private const RoleTypeId scp3114Role = RoleTypeId.Scp3114;
private readonly Dictionary<Player, bool> spectatableCache = new(2);
private readonly Dictionary<Player, Quaternion> pendingRotations = new(2);
public void Subscribe()
{
Events.Player.Spawned += OnSpawned;
Events.Player.Spawning += OnSpawning;
Events.Server.WaitingForPlayers += OnWaitingforPlayers;
PlayerRoleManager.OnServerRoleSet -= Scp3114InitialRagdollSpawner.OnServerRoleSet;
}
public void Unsubscribe()
{
Events.Player.Spawned -= OnSpawned;
Events.Player.Spawning -= OnSpawning;
Events.Server.WaitingForPlayers -= OnWaitingforPlayers;
PlayerRoleManager.OnServerRoleSet += Scp3114InitialRagdollSpawner.OnServerRoleSet;
}
private void OnWaitingforPlayers()
{
spectatableCache.Clear();
pendingRotations.Clear();
}
private void OnSpawning(SpawningEventArgs ev)
{
if (ev.NewRole.Type != scp3114Role)
return;
if (!ev.NewRole.SpawnFlags.HasFlag(RoleSpawnFlags.UseSpawnpoint))
return;
List<SpawnPoint> spawnPoints = Plugin.Instance.Config.SpawnPoints;
if (spawnPoints.Count == 0)
return;
float totalChance = spawnPoints.Sum(x => x.Chance);
if (totalChance <= 0f)
return;
float randomValue = Random.Range(0f, Mathf.Max(100f, totalChance));
float cumulative = 0f;
SpawnPoint selectedSpawn = null;
foreach (SpawnPoint spawnPoint in spawnPoints)
{
cumulative += spawnPoint.Chance;
if (randomValue <= cumulative)
{
selectedSpawn = spawnPoint;
break;
}
}
if (selectedSpawn == null)
return;
Room room = Room.Get(selectedSpawn.Room);
bool roomNull = room == null;
ev.Position = roomNull ? selectedSpawn.Position : room.WorldPosition(selectedSpawn.Position);
/// Not working idk why?
//ev.HorizontalRotation = roomNull ? selectedSpawn.Rotation.y : (room.Rotation * Quaternion.Euler(selectedSpawn.Rotation)).eulerAngles.y;
pendingRotations[ev.Player] = roomNull ? Quaternion.Euler(selectedSpawn.Rotation) : room.Rotation * Quaternion.Euler(selectedSpawn.Rotation);
if (!Scp3114InitialRagdollSpawner._ragdollsSpawned && selectedSpawn.CustomRagdolls != null && selectedSpawn.CustomRagdolls.Count != 0)
{
foreach (CustomRagdolls ragdoll in selectedSpawn.CustomRagdolls)
{
Vector3 pos = roomNull ? ragdoll.Position : room.WorldPosition(ragdoll.Position);
Quaternion rot = roomNull ? Quaternion.Euler(ragdoll.Rotation) : room.Rotation * Quaternion.Euler(ragdoll.Rotation);
Scp3114InitialRagdollSpawner.ServerSpawnRagdoll(ragdoll.RoleType, pos, rot, ev.Player.ReferenceHub);
}
Scp3114InitialRagdollSpawner._ragdollsSpawned = true;
}
}
private void OnSpawned(SpawnedEventArgs ev)
{
if (ev.Player.Role.Type == scp3114Role)
{
Scp3114InitialRagdollSpawner.ServerSpawnRagdolls(ev.Player.ReferenceHub);
if (pendingRotations.TryGetValue(ev.Player, out Quaternion rot))
{
if (ev.SpawnFlags.HasFlag(RoleSpawnFlags.UseSpawnpoint))
ev.Player.Rotation = rot;
pendingRotations.Remove(ev.Player);
}
}
if (!Plugin.Instance.Config.Make3114UnSpectatable)
return;
if (ev.OldRole.Type == scp3114Role && ev.Player.Role.Type != scp3114Role && spectatableCache.TryGetValue(ev.Player, out bool previous))
{
ev.Player.IsSpectatable = previous;
spectatableCache.Remove(ev.Player);
return;
}
if (ev.Player.Role.Type == scp3114Role)
{
spectatableCache[ev.Player] = ev.Player.IsSpectatable;
ev.Player.IsSpectatable = false;
}
}
}
}