summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos/PressStartToJoinExample_Assigner.cs
blob: 6b235898cf1a5fb00ca5830cf260876a960b24ae (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
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections.Generic;
using UnityEngine;

namespace Rewired.Demos;

[AddComponentMenu("")]
public class PressStartToJoinExample_Assigner : MonoBehaviour
{
	private class PlayerMap
	{
		public int rewiredPlayerId;

		public int gamePlayerId;

		public PlayerMap(int rewiredPlayerId, int gamePlayerId)
		{
			this.rewiredPlayerId = rewiredPlayerId;
			this.gamePlayerId = gamePlayerId;
		}
	}

	private static PressStartToJoinExample_Assigner instance;

	public int maxPlayers = 4;

	private List<PlayerMap> playerMap;

	private int gamePlayerIdCounter;

	public static Player GetRewiredPlayer(int gamePlayerId)
	{
		if (!ReInput.isReady)
		{
			return null;
		}
		if (instance == null)
		{
			Debug.LogError("Not initialized. Do you have a PressStartToJoinPlayerSelector in your scehe?");
			return null;
		}
		for (int i = 0; i < instance.playerMap.Count; i++)
		{
			if (instance.playerMap[i].gamePlayerId == gamePlayerId)
			{
				return ReInput.players.GetPlayer(instance.playerMap[i].rewiredPlayerId);
			}
		}
		return null;
	}

	private void Awake()
	{
		playerMap = new List<PlayerMap>();
		instance = this;
	}

	private void Update()
	{
		for (int i = 0; i < ReInput.players.playerCount; i++)
		{
			if (ReInput.players.GetPlayer(i).GetButtonDown("JoinGame"))
			{
				AssignNextPlayer(i);
			}
		}
	}

	private void AssignNextPlayer(int rewiredPlayerId)
	{
		if (playerMap.Count >= maxPlayers)
		{
			Debug.LogError("Max player limit already reached!");
			return;
		}
		int nextGamePlayerId = GetNextGamePlayerId();
		playerMap.Add(new PlayerMap(rewiredPlayerId, nextGamePlayerId));
		Player player = ReInput.players.GetPlayer(rewiredPlayerId);
		player.controllers.maps.SetMapsEnabled(state: false, "Assignment");
		player.controllers.maps.SetMapsEnabled(state: true, "Default");
		Debug.Log("Added Rewired Player id " + rewiredPlayerId + " to game player " + nextGamePlayerId);
	}

	private int GetNextGamePlayerId()
	{
		return gamePlayerIdCounter++;
	}
}