public void Initialize(string ipAdress = "127.0.0.1", int ipPort = 6576) { NetManager.ConnectToMaster(ipAdress, ipPort, new ClientListenEvents() { onRecPlayerInfo = (header, connection, playerInfo) => { currentPlayerInfo = playerInfo; Debug.Log($"Sync Player Info ID: {currentPlayerInfo.PlayerID}"); }, onRecGeneratePlayerVehicle = (header, connection, generatePlayerVehicle) => { mainThreadTasks.Enqueue(() => { var isLocalPlayer = currentPlayerInfo.PlayerID == generatePlayerVehicle.OwnerPlayerID; var netType = isLocalPlayer ? InstanceNetType.GameNetWorkOffline : InstanceNetType.GameNetworkBotOffline; var thinkLogic = isLocalPlayer ? null : ScriptableObject.CreateInstance <ClientOtherPlayerVehicleLogic>(); var vehicle = VehicleUtility.CreateVehicle(generatePlayerVehicle.VehicleName, netType, thinkLogic); generatePlayerVehicle.tankInitSystem = vehicle; var vehicleStatus = new VehicleStatus() { isLocalPlayer = isLocalPlayer, OwnerPlayerID = generatePlayerVehicle.OwnerPlayerID, VehicleID = generatePlayerVehicle.VehicleID, tankInitSystem = vehicle, syncVehicle = new SyncVehicle(generatePlayerVehicle.VehicleID, new ProtobufVector3(0, 0, 0), new ProtobufVector3(0, 0, 0), new ProtobufQuaternion(0, 0, 0, 1), new ProtobufVector3(0, 0, 0)) }; if (isLocalPlayer) { currentPlayerVehicleStatus = vehicleStatus; } vehicleStatusList.Add(vehicleStatus); Debug.Log($"isLocalPlayer : {isLocalPlayer} VehicleID {generatePlayerVehicle.VehicleID}"); }); } , onRecSyncVehicle = (header, connection, syncVehicle) => { var toSyncVehicleStatus = vehicleStatusList.Find(val => val.VehicleID == syncVehicle.VehicleID); if (toSyncVehicleStatus != null) { toSyncVehicleStatus.syncVehicle = syncVehicle; } }, onRecDestoryPlayerVehicle = (header, connection, destroyInfo) => { //TODO: Something wrong var vehicle = vehicleStatusList.Find(val => val.VehicleID == destroyInfo.VehicleID); if (vehicle != null) { mainThreadTasks.Enqueue(() => { VehicleUtility.RemoveVehicle(vehicle.tankInitSystem); Debug.Log($"Remove Player {destroyInfo.VehicleID}"); }); vehicleStatusList.Remove(vehicle); } } }); StartCoroutine(SyncInputToMaster()); }
public void Initialize(int port = 6576) { var freeCamera = new GameObject("Free Camera", typeof(FreeCamera), typeof(Camera)); //Notify New Player OnNewPlayerConnected += (playerInfo) => { Debug.Log($"New Player With UID:{playerInfo.Uid} Joined the room. Assigned ID : {playerInfo.PlayerID}"); }; //Spawn Player Vehicle once They joined. You can change the spawn policy to your perference OnNewPlayerConnected += (playerInfo) => { mainThreadTasks.Enqueue(() => { StartCoroutine(SpawnVehicle(playerInfo)); }); }; //Notfiy New Disconnected OnPlayerDisconnected += (playerInfo) => { Debug.Log($"Player: {playerInfo.PlayerID} has left the room!"); }; OnPlayerDisconnected += (playerInfo) => { PlayerList.Remove(playerInfo); var playerVehicle = VehicleList.Find(val => val.OwnerPlayerID == playerInfo.PlayerID); //Player Vehicle May Destroyed if (playerVehicle != null) { Debug.Log($"Remove Player Vehicle!{playerVehicle.VehicleID}"); VehicleList.Remove(playerVehicle); mainThreadTasks.Enqueue(() => { VehicleUtility.RemoveVehicle(playerVehicle.tankInitSystem); }); } foreach (var player in PlayerList) { player.clientConnection.SendObject("DestroyPlayerVehicle", new DestroyPlayerVehicle(playerVehicle.VehicleID)); } }; NetManager.StartAsMaster(port, new ServerListenEvents() { onRecLoginInfo = (header, connection, loginInfo) => { //Assign PlayerInfo var playerID = allocatedPlayerID; var playerInfo = new PlayerInfo(loginInfo.Uid, playerID, TeamManager.Team.red, connection); allocatedPlayerID += 1; PlayerList.Add(playerInfo); OnNewPlayerConnected?.Invoke(playerInfo); connection.SendObject("PlayerInfo", playerInfo); connection.AppendShutdownHandler(onShutDown => { OnPlayerDisconnected?.Invoke(playerInfo); }); }, onRecSyncPlayerInput = (header, connection, syncPlayerInput) => { mainThreadTasks.Enqueue(() => { var vehicle = VehicleList.Find(v => v.VehicleID == syncPlayerInput.VehicleID); var ptc = vehicle?.tankInitSystem?.vehicleComponents?.playerTracksController; if (ptc != null) { ptc.accelG = syncPlayerInput.Xinput; ptc.steerG = syncPlayerInput.Yinput; var turretTarget = vehicle.tankInitSystem.vehicleComponents.mainTurretController?.target; if (turretTarget != null) { turretTarget.position = syncPlayerInput.LookTargetPos.CovertToUnityV3(); } } }); } }); StartCoroutine(SyncVehicleLoop()); }