/// <summary> /// Constructor. /// </summary> /// <param name="netManager">The network manager owning this player.</param> /// <param name="netWorld">Network world owning this player.</param> /// <param name="steamId">The steam id of the player.</param> public NetLocalPlayer( NetManager netManager, NetWorld netWorld, Steamworks.CSteamID steamId) : base(netManager, netWorld, steamId) { Instance = this; steamID = steamId; GameDoorsManager.Instance.onDoorsOpen = (GameObject door) => { Messages.OpenDoorsMessage msg = new Messages.OpenDoorsMessage(); msg.position = Utils.GameVec3ToNet(door.transform.position); msg.open = true; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; GameDoorsManager.Instance.onDoorsClose = (GameObject door) => { Messages.OpenDoorsMessage msg = new Messages.OpenDoorsMessage(); msg.position = Utils.GameVec3ToNet(door.transform.position); msg.open = false; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; LightSwitchManager.Instance.onLightSwitchUsed = (GameObject lswitch, bool turnedOn) => { Messages.LightSwitchMessage msg = new Messages.LightSwitchMessage(); msg.pos = Utils.GameVec3ToNet(lswitch.transform.position); msg.toggle = turnedOn; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; if (animManager == null) { animManager = new PlayerAnimManager(); } }
/// <summary> /// Constructor. /// </summary> /// <param name="netManager">The network manager owning this player.</param> /// <param name="netWorld">Network world owning this player.</param> /// <param name="steamId">The steam id of the player.</param> public NetLocalPlayer(NetManager netManager, NetWorld netWorld, Steamworks.CSteamID steamId) : base(netManager, netWorld, steamId) { GameDoorsManager.Instance.onDoorsOpen = (GameObject door) => { Messages.OpenDoorsMessage msg = new Messages.OpenDoorsMessage(); msg.position = Utils.GameVec3ToNet(door.transform.position); msg.open = true; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; GameDoorsManager.Instance.onDoorsClose = (GameObject door) => { Messages.OpenDoorsMessage msg = new Messages.OpenDoorsMessage(); msg.position = Utils.GameVec3ToNet(door.transform.position); msg.open = false; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; GameCallbacks.onObjectPickup = (GameObject gameObj) => { Messages.PickupObjectMessage msg = new Messages.PickupObjectMessage(); msg.netId = netWorld.GetPickupableNetId(gameObj); Client.Assert(msg.netId != NetPickupable.INVALID_ID, "Tried to pickup not network pickupable."); netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; GameCallbacks.onObjectRelease = (bool drop) => { Messages.ReleaseObjectMessage msg = new Messages.ReleaseObjectMessage(); msg.drop = drop; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; BeerCaseManager.Instance.onBottleConsumed = (GameObject bcase) => { Messages.RemoveBottleMessage msg = new Messages.RemoveBottleMessage(); msg.netId = netWorld.GetPickupableNetId(bcase); Client.Assert(msg.netId != NetPickupable.INVALID_ID, "Tried to drink from not network beercase."); netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; LightSwitchManager.Instance.onLightSwitchUsed = (GameObject lswitch, bool turnedOn) => { Messages.LightSwitchMessage msg = new Messages.LightSwitchMessage(); msg.pos = Utils.GameVec3ToNet(lswitch.transform.position); msg.toggle = turnedOn; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; if (animManager == null) { animManager = new PlayerAnimManager(); } }
/// <summary> /// Write full world synchronization message. /// </summary> /// <param name="msg">The message to write to.</param> public void WriteFullWorldSync(Messages.FullWorldSyncMessage msg) { // Write time Game.GameWorld gameWorld = Game.GameWorld.Instance; msg.dayTime = gameWorld.WorldTime; msg.day = gameWorld.WorldDay; // Write mailbox name msg.mailboxName = gameWorld.PlayerLastName; // Write doors List <Game.Objects.GameDoor> doors = Game.GameDoorsManager.Instance.doors; int doorsCount = doors.Count; msg.doors = new Messages.DoorsInitMessage[doorsCount]; for (int i = 0; i < doorsCount; ++i) { var doorMsg = new Messages.DoorsInitMessage(); Game.Objects.GameDoor door = doors[i]; doorMsg.position = Utils.GameVec3ToNet(door.Position); doorMsg.open = door.IsOpen; msg.doors[i] = doorMsg; } // Write light switches. List <Game.Objects.LightSwitch> lights = Game.LightSwitchManager.Instance.lightSwitches; int lightCount = lights.Count; msg.lights = new Messages.LightSwitchMessage[lightCount]; for (int i = 0; i < lightCount; i++) { var lightMsg = new Messages.LightSwitchMessage(); Game.Objects.LightSwitch light = lights[i]; lightMsg.pos = Utils.GameVec3ToNet(light.Position); lightMsg.toggle = light.SwitchStatus; msg.lights[i] = lightMsg; } // Write weather Game.GameWeatherManager.Instance.WriteWeather(msg.currentWeather); // Write vehicles. int vehiclesCount = vehicles.Count; msg.vehicles = new Messages.VehicleInitMessage[vehiclesCount]; for (int i = 0; i < vehiclesCount; ++i) { var vehicleMsg = new Messages.VehicleInitMessage(); NetVehicle vehicle = vehicles[i]; vehicleMsg.id = vehicle.NetId; vehicleMsg.transform.position = Utils.GameVec3ToNet(vehicle.GetPosition()); vehicleMsg.transform.rotation = Utils.GameQuatToNet(vehicle.GetRotation()); msg.vehicles[i] = vehicleMsg; } // Write pickupables. var pickupableMessages = new List <Messages.PickupableSpawnMessage>(); foreach (var kv in netPickupables) { NetPickupable pickupable = kv.Value; if (pickupable.gameObject == null) { Logger.Log($"Null ptr of the pickupable game object {pickupable.NetId}"); continue; } var pickupableMsg = new Messages.PickupableSpawnMessage(); pickupableMsg.id = pickupable.NetId; var metaData = pickupable.gameObject.GetComponent <Game.Components.PickupableMetaDataComponent>(); pickupableMsg.prefabId = metaData.prefabId; Transform transform = pickupable.gameObject.transform; pickupableMsg.transform.position = Utils.GameVec3ToNet(transform.position); pickupableMsg.transform.rotation = Utils.GameQuatToNet(transform.rotation); pickupableMsg.active = pickupable.gameObject.activeSelf; List <float> data = new List <float>(); //Beercases if (metaData.PrefabDescriptor.type == Game.GamePickupableDatabase.PrefabType.BeerCase && pickupable.gameObject.name != "beer case") { Game.Objects.BeerCase beer = Game.BeerCaseManager.Instance.FindBeerCase(pickupable.gameObject); data.Add(Game.BeerCaseManager.Instance.FullCaseBottles - beer.UsedBottles); } if (data.Count != 0) { pickupableMsg.Data = data.ToArray(); } pickupableMessages.Add(pickupableMsg); } msg.pickupables = pickupableMessages.ToArray(); netManager.GetLocalPlayer().WriteSpawnState(msg); }
/// <summary> /// Write full world synchronization message. /// </summary> /// <param name="msg">The message to write to.</param> public void WriteFullWorldSync(Messages.FullWorldSyncMessage msg) { Logger.Debug("Writing full world synchronization message."); var watch = System.Diagnostics.Stopwatch.StartNew(); // 'Player is loading' is only applicable for remote client. playerIsLoading = false; // Write time Game.GameWorld gameWorld = Game.GameWorld.Instance; msg.dayTime = gameWorld.WorldTime; msg.day = gameWorld.WorldDay; // Write mailbox name msg.mailboxName = gameWorld.PlayerLastName; // Write doors List <GameDoor> doors = GameDoorsManager.Instance.doors; int doorsCount = doors.Count; msg.doors = new Messages.DoorsInitMessage[doorsCount]; Logger.Debug($"Writing state of {doorsCount} doors."); for (int i = 0; i < doorsCount; ++i) { var doorMsg = new Messages.DoorsInitMessage(); GameDoor door = doors[i]; doorMsg.position = Utils.GameVec3ToNet(door.Position); doorMsg.open = door.IsOpen; msg.doors[i] = doorMsg; } // Write light switches. List <LightSwitch> lights = Game.LightSwitchManager.Instance.lightSwitches; int lightCount = lights.Count; msg.lights = new Messages.LightSwitchMessage[lightCount]; Logger.Debug($"Writing light switches state of {lightCount}"); for (int i = 0; i < lightCount; i++) { var lightMsg = new Messages.LightSwitchMessage(); LightSwitch light = lights[i]; lightMsg.pos = Utils.GameVec3ToNet(light.Position); lightMsg.toggle = light.SwitchStatus; msg.lights[i] = lightMsg; } // Write weather GameWeatherManager.Instance.WriteWeather(msg.currentWeather); // Write objects. (Pickupables, Player vehicles, AI vehicles) var pickupableMessages = new List <Messages.PickupableSpawnMessage>(); Logger.Debug( $"Writing state of {ObjectSyncManager.Instance.ObjectIDs.Count} objects"); foreach (var kv in ObjectSyncManager.Instance.ObjectIDs) { ObjectSyncComponent osc = kv.Value; if (osc == null) { continue; } if (osc.ObjectType != ObjectSyncManager.ObjectTypes.Pickupable) { continue; } bool wasActive = true; if (!osc.gameObject.activeSelf) { wasActive = false; osc.gameObject.SetActive(true); } Logger.Log($"Writing object: {osc.gameObject.name}"); var pickupableMsg = new Messages.PickupableSpawnMessage(); var metaData = osc.gameObject.GetComponent <PickupableMetaDataComponent>(); Client.Assert(metaData != null && metaData.PrefabDescriptor != null, $"Object with broken meta data -- {osc.gameObject.name}."); pickupableMsg.prefabId = metaData.prefabId; Transform transform = osc.gameObject.transform; pickupableMsg.transform.position = Utils.GameVec3ToNet(transform.position); pickupableMsg.transform.rotation = Utils.GameQuatToNet(transform.rotation); pickupableMsg.active = osc.gameObject.activeSelf; // ObjectID pickupableMsg.id = osc.gameObject.GetComponent <ObjectSyncComponent>().ObjectID; List <float> data = new List <float>(); if (data.Count != 0) { pickupableMsg.Data = data.ToArray(); } if (!wasActive) { osc.gameObject.SetActive(false); } pickupableMessages.Add(pickupableMsg); } msg.pickupables = pickupableMessages.ToArray(); netManager.GetLocalPlayer().WriteSpawnState(msg); watch.Stop(); Logger.Debug( "World state has been written. Took " + watch.ElapsedMilliseconds + "ms"); }
/// <summary> /// Write full world synchronization message. /// </summary> /// <param name="msg">The message to write to.</param> public void WriteFullWorldSync(Messages.FullWorldSyncMessage msg) { Logger.Debug("Writing full world synchronization message."); var watch = System.Diagnostics.Stopwatch.StartNew(); // 'Player is loading' is only applicable for remote client. playerIsLoading = false; // Write time GameWorld gameWorld = GameWorld.Instance; msg.dayTime = gameWorld.WorldTime; msg.day = gameWorld.WorldDay; // Write mailbox name msg.mailboxName = gameWorld.PlayerLastName; // Write doors List <GameDoor> doors = GameDoorsManager.Instance.doors; int doorsCount = doors.Count; msg.doors = new Messages.DoorsInitMessage[doorsCount]; Logger.Debug($"Writing state of {doorsCount} doors."); for (int i = 0; i < doorsCount; ++i) { var doorMsg = new Messages.DoorsInitMessage(); GameDoor door = doors[i]; doorMsg.position = Utils.GameVec3ToNet(door.Position); doorMsg.open = door.IsOpen; msg.doors[i] = doorMsg; } // Write light switches. List <LightSwitch> lights = Game.LightSwitchManager.Instance.lightSwitches; int lightCount = lights.Count; msg.lights = new Messages.LightSwitchMessage[lightCount]; Logger.Debug($"Writing light switches state of {lightCount}"); for (int i = 0; i < lightCount; i++) { var lightMsg = new Messages.LightSwitchMessage(); LightSwitch light = lights[i]; lightMsg.pos = Utils.GameVec3ToNet(light.Position); lightMsg.toggle = light.SwitchStatus; msg.lights[i] = lightMsg; } // Write connected players. msg.connectedPlayers = new Messages.ConnectedPlayersMessage(); int[] playerIDs = new int[netManager.players.Count]; ulong[] steamIDs = new ulong[netManager.players.Count]; int index2 = 0; foreach (var connectedPlayer in netManager.players) { playerIDs[index2] = connectedPlayer.Key; steamIDs[index2] = connectedPlayer.Value.SteamId.m_SteamID; index2++; } msg.connectedPlayers.playerIDs = playerIDs; msg.connectedPlayers.steamIDs = steamIDs; // Write objects. (Pickupables, Player vehicles, AI vehicles) var pickupableMessages = new List <Messages.PickupableSpawnMessage>(); Logger.Debug($"Writing state of {ObjectSyncManager.Instance.ObjectIDs.Count} objects"); foreach (var kv in ObjectSyncManager.Instance.ObjectIDs) { ObjectSyncComponent osc = kv.Value; if (osc == null) { continue; } if (osc.ObjectType != ObjectSyncManager.ObjectTypes.Pickupable) { continue; } bool wasActive = true; if (!osc.gameObject.activeSelf) { wasActive = false; osc.gameObject.SetActive(true); } if (NetWorld.DisplayObjectRegisteringDebug) { Logger.Debug($"Writing object: {osc.gameObject.name}"); } var pickupableMsg = new Messages.PickupableSpawnMessage(); var metaData = osc.gameObject.GetComponent <PickupableMetaDataComponent>(); Client.Assert(metaData != null && metaData.PrefabDescriptor != null, $"Object with broken meta data -- {osc.gameObject.name}."); pickupableMsg.prefabId = metaData.prefabId; Transform transform = osc.gameObject.transform; pickupableMsg.transform.position = Utils.GameVec3ToNet(transform.position); pickupableMsg.transform.rotation = Utils.GameQuatToNet(transform.rotation); pickupableMsg.active = osc.gameObject.activeSelf; // ObjectID pickupableMsg.id = osc.gameObject.GetComponent <ObjectSyncComponent>().ObjectID; List <float> data = new List <float>(); if (data.Count != 0) { pickupableMsg.Data = data.ToArray(); } if (!wasActive) { osc.gameObject.SetActive(false); } pickupableMessages.Add(pickupableMsg); } // Object owners. int objectsCount = ObjectSyncManager.Instance.ObjectIDs.Count; msg.objectOwners = new Messages.ObjectOwnerSync[objectsCount]; Dictionary <NetPlayer, int> playersReversed = new Dictionary <NetPlayer, int>(); foreach (var player in netManager.players) { playersReversed.Add(player.Value, player.Key); } Logger.Debug($"Writing owners of {objectsCount} objects!"); int index = 0; foreach (var objectId in ObjectSyncManager.Instance.ObjectIDs) { var objectMsg = new Messages.ObjectOwnerSync(); objectMsg.objectID = objectId.Key; if (objectId.Value.Owner != null) { objectMsg.ownerPlayerID = playersReversed[objectId.Value.Owner]; } else { objectMsg.ownerPlayerID = -1; } msg.objectOwners[index] = objectMsg; index++; } msg.pickupables = pickupableMessages.ToArray(); netManager.GetLocalPlayer().WriteSpawnState(msg); watch.Stop(); Logger.Debug("World state has been written. Took " + watch.ElapsedMilliseconds + "ms"); // Write player keys. int[] keys = { HutongGames.PlayMaker.FsmVariables.GlobalVariables.GetFsmInt("PlayerKeyGifu").Value, HutongGames.PlayMaker.FsmVariables.GlobalVariables.GetFsmInt("PlayerKeyHayosiko").Value, HutongGames.PlayMaker.FsmVariables.GlobalVariables.GetFsmInt("PlayerKeyFerndale").Value, HutongGames.PlayMaker.FsmVariables.GlobalVariables.GetFsmInt("PlayerKeyHome").Value, HutongGames.PlayMaker.FsmVariables.GlobalVariables.GetFsmInt("PlayerKeyRuscko").Value, HutongGames.PlayMaker.FsmVariables.GlobalVariables.GetFsmInt("PlayerKeySatsuma").Value }; msg.playerKeys = keys; // Write uncle states. msg.uncleStage = UncleManager.Instance.UncleStage; msg.uncleTime = UncleManager.Instance.UncleTime; msg.uncleHome = UncleManager.Instance.UncleHome; }