-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotonRoomWordPuzzle.cs
153 lines (127 loc) · 4.96 KB
/
PhotonRoomWordPuzzle.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using System.Collections.Generic;
namespace MRTK.Tutorials.MultiUserCapabilities
{
public class PhotonRoomWordPuzzle : MonoBehaviourPunCallbacks, IInRoomCallbacks
{
public static PhotonRoomWordPuzzle Room;
[SerializeField] private GameObject photonUserPrefab = default;
[SerializeField] private List<GameObject> interactablePrefabs = new List<GameObject>(); // Store multiple prefabs
[SerializeField] private List<Transform> interactableLocations = new List<Transform>(); // Store corresponding locations
private Player[] photonPlayers;
private int playersInRoom;
private int myNumberInRoom;
public override void OnPlayerEnteredRoom(Player newPlayer)
{
base.OnPlayerEnteredRoom(newPlayer);
photonPlayers = PhotonNetwork.PlayerList;
playersInRoom++;
}
private void Awake() // I CHANGED WITH A
{
if (Room == null)
{
Room = this;
}
else
{
if (Room != this)
{
Destroy(Room.gameObject);
Room = this;
}
}
}
public override void OnEnable()
{
base.OnEnable();
PhotonNetwork.AddCallbackTarget(this);
}
public override void OnDisable()
{
base.OnDisable();
PhotonNetwork.RemoveCallbackTarget(this);
}
private void Start()
{
// Allow prefabs not in a Resources folder
if (PhotonNetwork.PrefabPool is DefaultPool pool)
{
if (photonUserPrefab != null) pool.ResourceCache.Add(photonUserPrefab.name, photonUserPrefab);
interactablePrefabs.ForEach(prefab => {
if (prefab != null) pool.ResourceCache.Add(prefab.name, prefab);
});
}
}
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
photonPlayers = PhotonNetwork.PlayerList;
playersInRoom = photonPlayers.Length;
myNumberInRoom = playersInRoom;
PhotonNetwork.NickName = myNumberInRoom.ToString();
StartGame();
}
private void StartGame()
{
CreatePlayer();
if (!PhotonNetwork.IsMasterClient) return;
if (TableAnchor.Instance != null) CreateInteractableObjects();
}
private void CreatePlayer()
{
if (photonUserPrefab == null)
{
Debug.LogError("Photon User Prefab is null.");
return;
}
PhotonNetwork.Instantiate(photonUserPrefab.name, Vector3.zero, Quaternion.identity);
}
private void CreateInteractableObjects()
{
// Check if the lists are properly assigned and have the same number of elements
if (interactablePrefabs.Count != interactableLocations.Count)
{
Debug.LogError("Interactables and locations lists do not match in size.");
return;
}
// Loop through all prefabs and instantiate them at their locations
for (int i = 0; i < interactablePrefabs.Count; i++)
{
if (interactablePrefabs[i] != null && interactableLocations[i] != null)
{
if (interactablePrefabs[i] == null || interactableLocations[i] == null)
{
Debug.LogError($"Interactable or location is null at index: {i}");
continue; // Skip this iteration
}
var position = interactableLocations[i].position;
var positionOnTopOfSurface = new Vector3(position.x, position.y + interactableLocations[i].localScale.y / 2,
position.z);
GameObject instantiatedObj = PhotonNetwork.Instantiate(
interactablePrefabs[i].name,
positionOnTopOfSurface,
interactableLocations[i].rotation
);
if (instantiatedObj == null)
{
Debug.LogError($"Failed to instantiate object at index: {i}");
continue; // Skip this iteration
}
if (CollectData.Instance == null || instantiatedObj == null)
{
Debug.LogError("CollectData instance or instantiated object is null.");
continue; // Skip this iteration
}
CollectData.Instance.RegisterPoster(instantiatedObj); // Register the object with CollectData
}
else
{
Debug.LogError($"Interactable or location is null at index: {i}");
}
}
}
}
}