161 lines
5.8 KiB
C#

using CommandSystem.Commands.RemoteAdmin;
using GameCore;
using LabApi.Events.Arguments.ServerEvents;
using LabApi.Events.Handlers;
using LabApi.Features;
using LabApi.Loader.Features.Plugins;
using MEC;
using Interactables.Interobjects.DoorUtils;
using LabApi.Events.Arguments.PlayerEvents;
using LabApi.Features.Wrappers;
using MapGeneration;
using PlayerRoles;
using UnityEngine;
using Logger = LabApi.Features.Console.Logger;
using Version = System.Version;
namespace LobbyGame
{
public class LobbyGame: Plugin
{
public override string Name => "LobbyGame";
public override string Author => "Code002Lover";
public override Version Version { get; } = new(1, 0, 0);
public override string Description => "Adds a lobby minigame";
public override Version RequiredApiVersion { get; } = new(LabApiProperties.CompiledVersion);
public int RoundTimer { get; set; } = 20;
public static LobbyGame Singleton { get; private set; }
private bool _isStarted;
private Room _randomRoom;
public override void Enable()
{
ServerEvents.WaitingForPlayers += WaitingForPlayers;
PlayerEvents.Joined += PlayerJoined;
Singleton = this;
}
private static void PlayerJoined(PlayerJoinedEventArgs ev)
{
Timing.RunCoroutine(ContinuouslyTrySpawning());
return;
IEnumerator<float> ContinuouslyTrySpawning()
{
while (!RoundStart.RoundStarted)
{
GameObject.Find("StartRound").transform.localScale = Vector3.zero;
yield return Timing.WaitForSeconds(0.5f);
if(Singleton._randomRoom == null) continue;
if(!Singleton._isStarted) continue;
ev.Player.SetRole(RoleTypeId.ChaosRifleman, RoleChangeReason.None, RoleSpawnFlags.None);
ev.Player.Position = Singleton._randomRoom.Position + new Vector3(0, 1, 0);
break;
}
}
}
private static void WaitingForPlayers()
{
Timing.CallDelayed(15, () =>
{
if (Singleton._isStarted) return;
var randomRoom = Map.GetRandomRoom(FacilityZone.Entrance);
while (randomRoom is not { Zone: FacilityZone.Entrance })
{
randomRoom = Map.GetRandomRoom(FacilityZone.Entrance);
}
Logger.Debug($"Random entrance room: {randomRoom.Name}");
RoundStart.LobbyLock = true;
Singleton._randomRoom = randomRoom;
Singleton._isStarted = true;
foreach (var player in Player.ReadyList)
{
player.SetRole(RoleTypeId.ChaosRifleman, RoleChangeReason.None, RoleSpawnFlags.None);
try
{
player.Position = randomRoom.Position + new Vector3(0, 1, 0);
}
catch (Exception _)
{
player.SetRole(RoleTypeId.Spectator);
}
}
Timing.RunCoroutine(ContinuouslyUpdateRoundTimer());
GameObject.Find("StartRound").transform.localScale = Vector3.zero;
foreach (var door in Map.Doors)
{
door.IsOpened = false;
door.Lock(DoorLockReason.Lockdown2176, true);
}
return;
IEnumerator<float> ContinuouslyUpdateRoundTimer()
{
while (true)
{
yield return Timing.WaitForSeconds(1);
foreach (var player in Player.ReadyList)
{
player.SendBroadcast(
$"<size=30><color=grey>Round starts in</color> <color=red>{Singleton.RoundTimer}</color> <color=grey>seconds</color></size>",
10, Broadcast.BroadcastFlags.Normal, true);
}
Logger.Debug($"Round starts in {Singleton.RoundTimer} seconds");
if (Player.ReadyList.Count() <= 1)
{
Singleton.RoundTimer = 20;
continue;
}
Singleton.RoundTimer--;
if (Singleton.RoundTimer >= -1) continue;
Singleton.RoundTimer = 20;
foreach (var player in Player.ReadyList)
{
player.ClearInventory();
player.SetRole(RoleTypeId.Spectator);
}
foreach (var door in Map.Doors)
{
door.Lock(DoorLockReason.Lockdown2176, false);
}
foreach (var player in Player.ReadyList)
{
player.SendBroadcast(
"",
1, Broadcast.BroadcastFlags.Normal, true);
}
CharacterClassManager.ForceRoundStart();
break;
}
}
});
}
public override void Disable()
{
ServerEvents.WaitingForPlayers -= WaitingForPlayers;
PlayerEvents.Joined -= PlayerJoined;
Singleton = null;
}
}
}