void deserializeMonsters(byte[] monsterBytes) { // DEBUG //string x = ""; //for (int i = 0; i < monsterBytes.Length; ++i) { x += monsterBytes[i]; } // UnityEngine.Debug.Log(playerId + " monsterbytes in: " + x); // DEBUG int index = 0; int numMonsters; Protocol.Deserialize(out numMonsters, monsterBytes, ref index); HashSet <int> newSerializeIds = new HashSet <int>(); for (int i = 0; i < numMonsters; ++i) { int serializeId; Protocol.Deserialize(out serializeId, monsterBytes, ref index); newSerializeIds.Add(serializeId); int monsterId; Protocol.Deserialize(out monsterId, monsterBytes, ref index); Monster monster; if (monsterRef.TryGetValue(serializeId, out monster)) { if (monster.monsterId != monsterId) // Destroy if wrong monster. { destroyMonster(monster); monster = null; } } if (monster == null) // No monster or wrong (destroyed) monster { monster = Instantiate(MonsterR.getById(monsterId)); monster.transform.SetParent(viewMapRef.transform); monster.gameState = this; monster.SetPath(viewMapRef.getPath()); monster.serializeId = serializeId; monsterRef[serializeId] = monster; } // There is now a valid monster. Update monster.deserializeFrom(monsterBytes, ref index); } // Remove no longer existing monsters. List <Monster> toDestroy = new List <Monster>(); foreach (KeyValuePair <int, Monster> pair in monsterRef) { if (!newSerializeIds.Contains(pair.Key)) { toDestroy.Add(pair.Value); } } foreach (Monster m in toDestroy) { destroyMonster(m); } }
public void spawnMonster(int monsterId) { if (photonView == null || photonView.isMine) { Monster monster = Instantiate(MonsterR.getById(monsterId)); monster.gameState = this; monster.SetPath(viewMapRef.getPath()); monster.serializeId = ++monsterCount; monsterRef[monster.serializeId] = monster; SoundManager.instance.PlaySpawnMonster(); } }