aboutsummaryrefslogtreecommitdiffstats
path: root/code/phase/WaitPhase.cs
blob: 9eab094f75f85ab9cf8dec5c79c4d70d6ada1407 (plain)
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
using Sandbox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MurderGame;

public class WaitPhase : BasePhase
{
	public override string Title => "Waiting for players";
	public bool CountIn { get; set; }
	public int TicksElapsed { get; set; }

	private bool _isCountDown { get; set; }

	public override void Tick()
	{
		if (Game.Clients.Count >= MurderGame.MinPlayers)
		{
			if (!CountIn || (_isCountDown && ++TicksElapsed % Game.TickRate == 0 && --base.TimeLeft == 0))
			{
				base.NextPhase = new AssignPhase();
				base.IsFinished = true;
				return;
			}
			else if (CountIn && !_isCountDown)
			{
				_isCountDown = true;
				base.TimeLeft = 10;
			}
		} else if (CountIn && _isCountDown)
		{
			_isCountDown = false;
			base.TimeLeft = -1;
		}

		foreach (var client in Game.Clients)
		{
			if (client.Pawn == null)
			{
				var pawn = new Player();
				client.Pawn = pawn;

				var spawnpoints = Entity.All.OfType<SpawnPoint>();
				var randomSpawnPoint = spawnpoints.OrderBy( x => Guid.NewGuid() ).FirstOrDefault();
				if ( randomSpawnPoint != null )
				{
					var tx = randomSpawnPoint.Transform;
					tx.Position = tx.Position + Vector3.Up * 50.0f;
					pawn.Transform = tx;
				}

				pawn.CurrentTeam = Team.Bystander;
				pawn.Spawn();
				pawn.Respawn();
			} else
			{
				var pawn = (Player)client.Pawn;
				if (pawn.LifeState == LifeState.Dead)
				{
					pawn.CurrentTeam = Team.Bystander;
					pawn.Respawn();
				}
			}
		}
	}

	public override void HandleClientJoin( ClientJoinedEvent e )
	{
		
	}
}