-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerManager.cs
More file actions
101 lines (85 loc) · 2.67 KB
/
Copy pathPlayerManager.cs
File metadata and controls
101 lines (85 loc) · 2.67 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.InputSystem;
public class PlayerManager : MonoBehaviour
{
[Header("Mode")]
[SerializeField] protected GameMode gameMode;
// Start is called once before the first execution of Update after the MonoBehaviour is created
// protected GameObject setupWindow;
protected Dictionary<int, InputDevice> players;
protected GameObject[] shipControllers;
void Awake()
{
// TODO: Determine scene and whether I should read from a Json or not
players = new Dictionary<int, InputDevice>();
DontDestroyOnLoad(gameObject);
FindShipControllers();
}
void FindShipControllers()
{
shipControllers = GameObject.FindGameObjectsWithTag("Player");
for (int i = 0; i < shipControllers.Length; i++)
{
for (int j = i + 1; j < shipControllers.Length; j++)
{
if (string.Compare(shipControllers[i].name, shipControllers[j].name) > 0)
{
GameObject temp = shipControllers[i];
shipControllers[i] = shipControllers[j];
shipControllers[j] = temp;
}
}
}
}
// Update is called once per frame
void Update()
{
// Debug
StartGame();
}
public void AddPlayer(int index, InputDevice device)
{
players.Remove(index);
players.Add(index, device);
}
public void RemovePlayer(int index)
{
players.Remove(index);
}
public bool SetDeviceForController(int index, InputDevice device)
{
if (index < 0 || index >= shipControllers.Length)
return false;
shipControllers[index].GetComponent<ShipController>().SetDevice(device);
Debug.Log("Player manager setting controller " + index + " to device " + device);
return true;
}
public void StartGame()
{
//FindShipControllers();
for (int i = 0; i < players.Count; i++)
{
SetDeviceForController(i, players[i]);
}
}
public void SaveIntoJson()
{
string info = JsonUtility.ToJson(players);
System.IO.File.WriteAllText(Application.persistentDataPath + "/PlayerManagerData.json", info);
}
public void LoadFromJson()
{
string infoJson = System.IO.File.ReadAllText(Application.persistentDataPath + "/PlayerManagerData.json");
players = JsonUtility.FromJson<Dictionary<int, InputDevice>>(infoJson);
}
public GameMode GetGameMode()
{
return gameMode;
}
public enum GameMode
{
TimeAttack = 0b_0,
BattleMode = 0b_1
}
}