/// <summary> /// Collect given objects. /// </summary> /// <param name="gameObject">The game object to collect.</param> public void CollectGameObject(GameObject gameObject) { if (gameObject.name == "SUN" && worldTimeFsm == null) { // Yep it's called "Color" :> worldTimeFsm = Utils.GetPlaymakerScriptByName(gameObject, "Color"); if (worldTimeFsm == null) { return; } // Register refresh world time event. if (!worldTimeFsm.Fsm.HasEvent(REFRESH_WORLD_TIME_EVENT)) { FsmEvent mpRefreshWorldTimeEvent = worldTimeFsm.Fsm.GetEvent(REFRESH_WORLD_TIME_EVENT); PlayMakerUtils.AddNewGlobalTransition(worldTimeFsm, mpRefreshWorldTimeEvent, "State 1"); } // Make sure world time is up-to-date with cache. WorldTime = worldTimeCached; } else if (Utils.IsGameObjectHierarchyMatching(gameObject, "mailbox_bottom_player/Name")) { SetupMailbox(gameObject); } else if (gameObject.name == "TRAFFIC") { new TrafficManager(gameObject); } else if (gameObject.name == "STORE") { new Shop(gameObject); } else if (gameObject.name == "BOAT") { ObjectSyncComponent osc = gameObject.transform.FindChild("GFX").FindChild("Colliders").FindChild("Collider").gameObject.AddComponent<ObjectSyncComponent>(); osc.Setup(ObjectSyncManager.ObjectTypes.Boat, ObjectSyncManager.AUTOMATIC_ID); } else if (gameObject.name == "GarageDoors") { ObjectSyncComponent oscLeft = gameObject.transform.FindChild("DoorLeft").FindChild("Coll").gameObject.AddComponent<ObjectSyncComponent>(); oscLeft.Setup(ObjectSyncManager.ObjectTypes.GarageDoor, ObjectSyncManager.AUTOMATIC_ID); ObjectSyncComponent oscRight = gameObject.transform.FindChild("DoorRight").FindChild("Coll").gameObject.AddComponent<ObjectSyncComponent>(); oscRight.Setup(ObjectSyncManager.ObjectTypes.GarageDoor, ObjectSyncManager.AUTOMATIC_ID); } }
/// <summary> /// Collect given objects. /// </summary> /// <param name="gameObject">The game object to collect.</param> public void CollectGameObject(GameObject gameObject) { if (gameObject.name == "SUN" && worldTimeFsm == null) { // Yep it's called "Color" :> worldTimeFsm = Utils.GetPlaymakerScriptByName(gameObject, "Color"); if (worldTimeFsm == null) { return; } // Register refresh world time event. if (!worldTimeFsm.Fsm.HasEvent(REFRESH_WORLD_TIME_EVENT)) { FsmEvent mpRefreshWorldTimeEvent = worldTimeFsm.Fsm.GetEvent(REFRESH_WORLD_TIME_EVENT); PlayMakerUtils.AddNewGlobalTransition(worldTimeFsm, mpRefreshWorldTimeEvent, "State 1"); } // Make sure world time is up-to-date with cache. WorldTime = worldTimeCached; } else if (Utils.IsGameObjectHierarchyMatching(gameObject, "mailbox_bottom_player/Name")) { SetupMailbox(gameObject); } else if (gameObject.name == "TRAFFIC") { trafficManager.Setup(gameObject); } else if (gameObject.name == "STORE") { shopManager.Setup(gameObject); } else if (gameObject.name == "BOAT") { ObjectSyncComponent osc = gameObject.transform.FindChild("GFX/Colliders/Collider").gameObject.AddComponent <ObjectSyncComponent>(); osc.Setup(ObjectSyncManager.ObjectTypes.Boat, ObjectSyncManager.AUTOMATIC_ID); } // Garage doors. else if (gameObject.name == "GarageDoors") { ObjectSyncComponent oscLeft = gameObject.transform.FindChild("DoorLeft/Coll").gameObject.AddComponent <ObjectSyncComponent>(); oscLeft.Setup(ObjectSyncManager.ObjectTypes.GarageDoor, ObjectSyncManager.AUTOMATIC_ID); ObjectSyncComponent oscRight = gameObject.transform.FindChild("DoorRight/Coll").gameObject.AddComponent <ObjectSyncComponent>(); oscRight.Setup(ObjectSyncManager.ObjectTypes.GarageDoor, ObjectSyncManager.AUTOMATIC_ID); } // Old car shed doors. else if (gameObject.name == "Doors" && gameObject.transform.parent.name == "Shed") { PlayMakerFSM doorLeft = gameObject.transform.FindChild("DoorLeft/Mesh").gameObject.GetComponent <PlayMakerFSM>(); EventHook.AddWithSync(doorLeft, "Open door"); EventHook.AddWithSync(doorLeft, "Close door"); PlayMakerFSM doorRight = gameObject.transform.FindChild("DoorRight/Mesh").gameObject.GetComponent <PlayMakerFSM>(); EventHook.AddWithSync(doorRight, "Open door"); EventHook.AddWithSync(doorRight, "Close door"); } // Weather system. else if (gameObject.name == "Clouds" && gameObject.transform.parent.name == "CloudSystem") { ObjectSyncComponent osc = gameObject.AddComponent <ObjectSyncComponent>(); osc.Setup(ObjectSyncManager.ObjectTypes.Weather, ObjectSyncManager.AUTOMATIC_ID); } // Sewage well jobs. else if (gameObject.name.StartsWith("HouseShit")) { ObjectSyncComponent osc = gameObject.AddComponent <ObjectSyncComponent>(); osc.Setup(ObjectSyncManager.ObjectTypes.SewageWell, ObjectSyncManager.AUTOMATIC_ID); } // Phone. else if (gameObject.name == "Ring") { phoneManager.Setup(gameObject); } // Map. else if (gameObject.name == "MAP" && gameObject.transform.FindChild("Darts")) { mapManager.Setup(gameObject); } }
/// <summary> /// Constructor. /// </summary> /// <param name="netManager">Network manager owning this network world.</param> public NetWorld(NetManager netManager) { this.netManager = netManager; Instance = this; GameCallbacks.onWorldUnload += () => { OnGameWorldUnload(); }; GameCallbacks.onWorldLoad += () => { OnGameWorldLoad(); }; GameCallbacks.onPlayMakerObjectCreate += (GameObject instance, GameObject prefab) => { if (!GamePickupableDatabase.IsPickupable(instance)) { return; } var metaData = prefab.GetComponent <PickupableMetaDataComponent>(); Client.Assert(metaData != null, "Tried to spawn pickupable that has no meta data assigned."); Messages.PickupableSpawnMessage msg = new Messages.PickupableSpawnMessage(); msg.prefabId = metaData.prefabId; msg.transform.position = Utils.GameVec3ToNet(instance.transform.position); msg.transform.rotation = Utils.GameQuatToNet(instance.transform.rotation); msg.active = instance.activeSelf; // Setup sync component on object. Client.Assert(instance.GetComponent <ObjectSyncComponent>(), $"Object created but no ObjectSyncComponent could be found! Object name: {instance.name}"); ObjectSyncComponent osc = instance.GetComponent <ObjectSyncComponent>(); msg.id = osc.Setup(osc.ObjectType, ObjectSyncManager.AUTOMATIC_ID); // Determine if object should be spawned on remote client. // (Helps to avoid duplicate objects spawning) bool sendToRemote = false; if (NetManager.Instance.IsHost) { Logger.Debug("Sending new object data to client!"); sendToRemote = true; } else { // This is a hack to workout beer bottles not spawning on the remote client due to items only spawning on the host. // This will be replaced in the future. if (instance.name.StartsWith("BottleBeerFly")) { sendToRemote = true; } } if (sendToRemote) { netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); Logger.Debug("Sending new object data to client!"); } }; GameCallbacks.onPlayMakerObjectActivate += (GameObject instance, bool activate) => { if (playerIsLoading) { return; } if (activate == instance.activeSelf) { return; } if (!GamePickupableDatabase.IsPickupable(instance)) { return; } ObjectSyncComponent pickupable = GetPickupableByGameObject(instance); if (pickupable == null) { return; } if (activate) { var metaData = pickupable.gameObject.GetComponent <PickupableMetaDataComponent>(); Messages.PickupableSpawnMessage msg = new Messages.PickupableSpawnMessage(); msg.id = pickupable.ObjectID; msg.prefabId = metaData.prefabId; msg.transform.position = Utils.GameVec3ToNet(instance.transform.position); msg.transform.rotation = Utils.GameQuatToNet(instance.transform.rotation); netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); } else { Messages.PickupableActivateMessage msg = new Messages.PickupableActivateMessage(); msg.id = pickupable.ObjectID; msg.activate = false; netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); } }; GameCallbacks.onPlayMakerObjectDestroy += (GameObject instance) => { if (!GamePickupableDatabase.IsPickupable(instance)) { return; } ObjectSyncComponent pickupable = GetPickupableByGameObject(instance); if (pickupable == null) { Logger.Debug($"Pickupable {instance.name} has been destroyed however it is not registered, skipping removal."); return; } HandlePickupableDestroy(instance); }; GameCallbacks.onPlayMakerSetPosition += (GameObject gameObject, Vector3 position, Space space) => { if (!GamePickupableDatabase.IsPickupable(gameObject)) { return; } ObjectSyncComponent pickupable = GetPickupableByGameObject(gameObject); if (pickupable == null) { return; } if (space == Space.Self) { position += gameObject.transform.position; } Messages.PickupableSetPositionMessage msg = new Messages.PickupableSetPositionMessage(); msg.id = pickupable.ObjectID; msg.position = Utils.GameVec3ToNet(position); netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }; RegisterNetworkMessagesHandlers(netManager.MessageHandler); }