void UpdateClients() { foreach (var netPlayer in networkPlayers) { // don't update players that are no longer participating if (!netPlayer.isActive) { continue; } var netPlayerController = netPlayer.playerController as TankPlayerController; var netPlayerPawn = (netPlayer.playerController.PawnMove as TankMovement).gameObject; var percepts = netPlayerPawn.GetComponent <TankPercepts>(); int packetSize = DataUtils.SizeOf <TankBattleStateData>() + DataUtils.SizeOf <TankTacticalInfo>() * percepts.reconInfo.Count; var stateMsg = new TankBattleStateData(); stateMsg.messageLength = packetSize; stateMsg.playerID = netPlayerController.pid; stateMsg.currentHealth = netPlayerController.m_Health.m_CurrentHealth; stateMsg.position = netPlayerController.PawnMove.position; stateMsg.forward = netPlayerController.PawnMove.forward; stateMsg.cannonForward = netPlayerController.TankGun.forward; stateMsg.canFire = netPlayerController.TankFire.CanFire() ? 1 : 0; stateMsg.perceptCount = percepts.reconInfo.Count; stateMsg.percepts = new TankTacticalInfo[3]; stateMsg.playerCount = gameManager.m_CombatantCount; int perceptSlot = 0; foreach (var reconRecord in percepts.reconInfo.Values) { // currently only space for three percepts if (!(perceptSlot < 3)) { break; } stateMsg.percepts[perceptSlot] = reconRecord; perceptSlot++; } // pack reconnaissance MemoryStream packetStream = new MemoryStream(DataUtils.SizeOf <TankBattleStateData>() + DataUtils.SizeOf <TankTacticalInfo>() * percepts.reconInfo.Count); packetStream.Write(DataUtils.GetBytes(stateMsg), 0, DataUtils.SizeOf <TankBattleStateData>()); foreach (var reconRecord in percepts.reconInfo) { packetStream.Write(DataUtils.GetBytes(reconRecord.Value), 0, DataUtils.SizeOf <TankTacticalInfo>()); } connectionSocket.Send(netPlayer, packetStream.GetBuffer()); } }
private void OnNetPlayerConnected(NetworkPlayer netPlayer) { var newPlayerController = AddPlayer(netPlayer); var welcomeMsg = new TankBattleStateData(); welcomeMsg.playerID = newPlayerController.pid; connectionSocket.Send(netPlayer, DataUtils.GetBytes(welcomeMsg)); Debug.Log("New player initialized at ID " + newPlayerController.pid + "."); }
private void OnNetPlayerConnected(NetworkPlayer netPlayer) { var newPlayerController = AddPlayer(netPlayer); // are we at the max player count? if (newPlayerController == null) { RemovePlayer(netPlayer); return; } var welcomeMsg = new TankBattleStateData(); welcomeMsg.playerID = newPlayerController.pid; connectionSocket.Send(netPlayer, DataUtils.GetBytes(welcomeMsg)); Debug.Log("New player initialized at ID " + newPlayerController.pid + "."); }
void UpdateClients() { foreach(var netPlayer in networkPlayers) { // don't update players that are no longer participating if (!netPlayer.isActive) continue; var netPlayerController = netPlayer.playerController as TankPlayerController; var netPlayerPawn = (netPlayer.playerController.PawnMove as TankMovement).gameObject; var percepts = netPlayerPawn.GetComponent<TankPercepts>(); int packetSize = DataUtils.SizeOf<TankBattleStateData>() + DataUtils.SizeOf<TankTacticalInfo>() * percepts.reconInfo.Count; var stateMsg = new TankBattleStateData(); stateMsg.messageLength = packetSize; stateMsg.playerID = netPlayerController.pid; stateMsg.currentHealth = netPlayerController.m_Health.m_CurrentHealth; stateMsg.position = netPlayerController.PawnMove.position; stateMsg.forward = netPlayerController.PawnMove.forward; stateMsg.cannonForward = netPlayerController.TankGun.forward; stateMsg.canFire = netPlayerController.TankFire.CanFire() ? 1 : 0; stateMsg.perceptCount = percepts.reconInfo.Count; stateMsg.percepts = new TankTacticalInfo[3]; int perceptSlot = 0; foreach(var reconRecord in percepts.reconInfo.Values) { // currently only space for three percepts if (!(perceptSlot < 3)) break; stateMsg.percepts[perceptSlot] = reconRecord; perceptSlot++; } // pack reconnaissance MemoryStream packetStream = new MemoryStream(DataUtils.SizeOf<TankBattleStateData>() + DataUtils.SizeOf<TankTacticalInfo>() * percepts.reconInfo.Count); packetStream.Write(DataUtils.GetBytes(stateMsg), 0, DataUtils.SizeOf<TankBattleStateData>()); foreach (var reconRecord in percepts.reconInfo) { packetStream.Write(DataUtils.GetBytes(reconRecord.Value), 0, DataUtils.SizeOf<TankTacticalInfo>()); } connectionSocket.Send(netPlayer, packetStream.GetBuffer()); } }