示例#1
0
        public override void Process(PlayerHeldItemChanged packet)
        {
            Optional <RemotePlayer> opPlayer = playerManager.Find(packet.PlayerId);

            Validate.IsPresent(opPlayer);

            Optional <GameObject> opItem = NitroxEntity.GetObjectFrom(packet.ItemId);

            Validate.IsPresent(opItem);

            Pickupable pickupable = opItem.Value.GetComponent <Pickupable>();

            Validate.IsTrue(pickupable);

            InventoryItem inventoryItem = (InventoryItem)inventoryItemFromPickupable.GetValue(pickupable);

            Validate.NotNull(inventoryItem);

            ItemsContainer inventory = opPlayer.Value.Inventory;
            PlayerTool     tool      = opItem.Value.GetComponent <PlayerTool>();

            // Copied from QuickSlots
            switch (packet.Type)
            {
            case PlayerHeldItemChangedType.DRAW_AS_TOOL:
                Validate.IsTrue(tool);
                ModelPlug.PlugIntoSocket(tool, opPlayer.Value.ItemAttachPoint);
                Utils.SetLayerRecursively(opItem.Value, viewModelLayer);
                foreach (Animator componentsInChild in tool.GetComponentsInChildren <Animator>())
                {
                    componentsInChild.cullingMode = AnimatorCullingMode.AlwaysAnimate;
                }
                if (tool.mainCollider)
                {
                    tool.mainCollider.enabled = false;
                }
                tool.GetComponent <Rigidbody>().isKinematic = true;
                opItem.Value.SetActive(true);
                setHandIK.Invoke(tool, new object[] { true });
                SafeAnimator.SetBool(opPlayer.Value.ArmsController.GetComponent <Animator>(), $"holding_{tool.animToolName}", true);
                opPlayer.Value.AnimationController["using_tool_first"] = packet.IsFirstTime == null;

                if (opItem.Value.TryGetComponent(out FPModel fpModelDraw))     //FPModel needs to be updated
                {
                    fpModelDraw.OnEquip(null, null);
                }
                break;

            case PlayerHeldItemChangedType.HOLSTER_AS_TOOL:
                Validate.IsTrue(tool);
                opItem.Value.SetActive(false);
                Utils.SetLayerRecursively(opItem.Value, defaultLayer);
                if (tool.mainCollider)
                {
                    tool.mainCollider.enabled = true;
                }
                tool.GetComponent <Rigidbody>().isKinematic = false;
                inventoryItem.item.Reparent(inventory.tr);
                foreach (Animator componentsInChild in tool.GetComponentsInChildren <Animator>())
                {
                    componentsInChild.cullingMode = AnimatorCullingMode.CullUpdateTransforms;
                }
                SafeAnimator.SetBool(opPlayer.Value.ArmsController.GetComponent <Animator>(), $"holding_{tool.animToolName}", false);
                opPlayer.Value.AnimationController["using_tool_first"] = false;

                if (opItem.Value.TryGetComponent(out FPModel fpModelHolster))     //FPModel needs to be updated
                {
                    fpModelHolster.OnUnequip(null, null);
                }

                break;

            case PlayerHeldItemChangedType.DRAW_AS_ITEM:
                inventoryItem.item.Reparent(opPlayer.Value.ItemAttachPoint);
                inventoryItem.item.SetVisible(true);
                Utils.SetLayerRecursively(inventoryItem.item.gameObject, viewModelLayer);
                break;

            case PlayerHeldItemChangedType.HOLSTER_AS_ITEM:
                inventoryItem.item.Reparent(inventory.tr);
                inventoryItem.item.SetVisible(false);
                Utils.SetLayerRecursively(inventoryItem.item.gameObject, defaultLayer);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }