/// <summary> /// Add Collectible to the networked buffered list for tracking it on all clients. This method actually /// checks whether the Collectible is existent already and then modifies that entry, before adding it. /// The object ID has to be assigned in all cases, but the other parameters depend on the state. /// </summary> public int AddBufferedCollectible(NetworkInstanceId id, Vector3 position, NetworkInstanceId tarId) { //find existing index, if any int index = collects.Count; for (int i = 0; i < collects.Count; i++) { if (collects[i].objId == id) { index = i; break; } } //create new state with variables passed in CollectibleState state = new CollectibleState { objId = id, pos = position, targetId = tarId }; //modify or add new entry to the list if (index < collects.Count) { collects[index] = state; } else { collects.Add(state); } //return index that was modified in this operation return(index); }
/// <summary> /// Method called by the SyncList operation over the Network when its content changes. /// This is an implementation for changes to the buffered Collectibles, updating their position and assignment. /// Parameters: type of operation, index of Collectible which received updates. /// </summary> public void OnCollectibleStateChanged(SyncListStruct <CollectibleState> .Operation op, int index) { //get reference by index CollectibleState state = collects[index]; if (state.objId.Value <= 0) { return; } //get game object instance by network ID GameObject obj = ClientScene.FindLocalObject(state.objId); if (obj == null) { return; } //get Collectible component on that object Collectible colComp = obj.GetComponent <Collectible>(); if (colComp == null) { return; } //targetId is assigned: handle pickup on the corresponding player //or position is not at origin: handle drop at that position //otherwise when the entry has changed it got returned to its spawn position if (state.targetId.Value > 0) { colComp.spawner.Pickup(state.targetId); } else if (state.pos != colComp.spawner.transform.position) { colComp.spawner.Drop(state.pos); } else { colComp.spawner.Return(); } }