From 71db52c5443a7bf82d9a23a770994a42b043be04 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Thu, 27 Jul 2023 22:11:31 +0100 Subject: Initial commit --- code/phase/AssignPhase.cs | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 code/phase/AssignPhase.cs (limited to 'code/phase/AssignPhase.cs') diff --git a/code/phase/AssignPhase.cs b/code/phase/AssignPhase.cs new file mode 100644 index 0000000..26c9fc1 --- /dev/null +++ b/code/phase/AssignPhase.cs @@ -0,0 +1,89 @@ +using Sandbox; +using Sandbox.UI; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MurderGame; + +public class AssignPhase : BasePhase +{ + public override string Title => "Assigning teams"; + public int TicksElapsed; + + public override void Activate() + { + foreach (var entity in Entity.All.OfType()) + { + entity.Delete(); + } + + var detectivesNeeded = 1; + var murderersNeeded = 1; + + List spawnpoints = Entity.All.OfType().OrderBy( x => Guid.NewGuid() ).ToList(); + var clients = Game.Clients.ToList(); + foreach ( int i in Enumerable.Range( 0, clients.Count ).OrderBy( x => Guid.NewGuid() ) ) + { + var client = clients[i]; + if (client.Pawn != null) + { + ((Player) client.Pawn).Cleanup(); + client.Pawn.Delete(); + } + Player pawn = new(); + client.Pawn = pawn; + if (spawnpoints.Count == 0) + { + ChatBox.Say( "Could not spawn " + client.Name + " as there are not enough spawn points." ); + pawn.CurrentTeam = Team.Spectator; + continue; + } + pawn.DressFromClient( client ); + + if (murderersNeeded > 0) + { + pawn.CurrentTeam = Team.Murderer; + --murderersNeeded; + } + else if (detectivesNeeded > 0) + { + pawn.CurrentTeam = Team.Detective; + --detectivesNeeded; + } + else + { + pawn.CurrentTeam = Team.Bystander; + } + Log.Info( "Assigning " + client.Name + " to team " + TeamOperations.GetTeamName( pawn.CurrentTeam ) ); + + var spawnpoint = spawnpoints[0]; + spawnpoints.RemoveAt( 0 ); + var tx = spawnpoint.Transform; + tx.Position = tx.Position + Vector3.Up * 50.0f; + pawn.Transform = tx; + + RoleOverlay.Show( To.Single( client ) ); + } + base.TimeLeft = 5; + } + + public override void Deactivate() + { + foreach (var client in Game.Clients) + { + RoleOverlay.Hide( To.Single( client ) ); + } + } + + public override void Tick() + { + ++TicksElapsed; + if ( base.TimeLeft != -1 && TicksElapsed % Game.TickRate == 0 && --base.TimeLeft == 0 ) + { + base.IsFinished = true; + base.NextPhase = new PlayPhase(); + } + } + +} -- cgit v1.2.3-70-g09d2