summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos/PressAnyButtonToJoinExample_Assigner.cs
blob: 83c25e8a115c2bf0ea2a68065aa5c6501f579b16 (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
using System.Collections.Generic;
using UnityEngine;

namespace Rewired.Demos;

[AddComponentMenu("")]
public class PressAnyButtonToJoinExample_Assigner : MonoBehaviour
{
	private void Update()
	{
		if (ReInput.isReady)
		{
			AssignJoysticksToPlayers();
		}
	}

	private void AssignJoysticksToPlayers()
	{
		IList<Joystick> joysticks = ReInput.controllers.Joysticks;
		for (int i = 0; i < joysticks.Count; i++)
		{
			Joystick joystick = joysticks[i];
			if (!ReInput.controllers.IsControllerAssigned(joystick.type, joystick.id) && joystick.GetAnyButtonDown())
			{
				Player player = FindPlayerWithoutJoystick();
				if (player == null)
				{
					return;
				}
				player.controllers.AddController(joystick, removeFromOtherPlayers: false);
			}
		}
		if (DoAllPlayersHaveJoysticks())
		{
			ReInput.configuration.autoAssignJoysticks = true;
			base.enabled = false;
		}
	}

	private Player FindPlayerWithoutJoystick()
	{
		IList<Player> players = ReInput.players.Players;
		for (int i = 0; i < players.Count; i++)
		{
			if (players[i].controllers.joystickCount <= 0)
			{
				return players[i];
			}
		}
		return null;
	}

	private bool DoAllPlayersHaveJoysticks()
	{
		return FindPlayerWithoutJoystick() == null;
	}
}