Beispiel #1
0
        void Update()
        {
            transform.position = PlayerControlsMouse.Get().GetPointingPos();
            transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up);

            PlayerCharacter player   = PlayerCharacter.GetFirst();
            PlayerControls  controls = PlayerControls.Get(player ? player.player_id : 0);

            MAction maction = current_slot != null && current_slot.GetItem() != null?current_slot.GetItem().FindMergeAction(current_select) : null;

            title.enabled = maction != null && player != null && maction.CanDoAction(player, current_slot, current_select);
            title.text    = maction != null ? maction.title : "";

            bool active = current_slot != null && controls != null && !controls.IsGamePad();

            if (active != icon_group.activeSelf)
            {
                icon_group.SetActive(active);
            }

            icon.enabled = false;
            if (current_slot != null && current_slot.GetItem())
            {
                icon.sprite  = current_slot.GetItem().icon;
                icon.enabled = true;
            }

            timer += Time.deltaTime;
            if (timer > refresh_rate)
            {
                timer = 0f;
                SlowUpdate();
            }
        }
        //When the character interact with this selectable, check all the actions and see if any should be triggered.
        public void Use(PlayerCharacter character, Vector3 pos)
        {
            if (enabled)
            {
                PlayerUI ui   = PlayerUI.Get(character.player_id);
                ItemSlot slot = ui != null?ui.GetSelectedSlot() : null;

                MAction maction = slot != null && slot.GetItem() != null?slot.GetItem().FindMergeAction(this) : null;

                AAction aaction = FindAutoAction(character);
                if (maction != null && maction.CanDoAction(character, slot, this))
                {
                    maction.DoAction(character, slot, this);
                    PlayerUI.Get(character.player_id)?.CancelSelection();
                }
                else if (aaction != null && aaction.CanDoAction(character, this))
                {
                    aaction.DoAction(character, this);
                }
                else if (actions.Length > 0)
                {
                    ActionSelector.Get(character.player_id)?.Show(character, this, pos);
                }

                if (onUse != null)
                {
                    onUse.Invoke(character);
                }
            }
        }
        public void MergeSlots(ItemSlot selected_slot, ItemSlot clicked_slot)
        {
            if (selected_slot != null && clicked_slot != null && current_player != null)
            {
                ItemSlot slot1   = selected_slot;
                ItemSlot slot2   = clicked_slot;
                ItemData item1   = slot1.GetItem();
                ItemData item2   = slot2.GetItem();
                MAction  action1 = item1 != null?item1.FindMergeAction(item2) : null;

                MAction action2 = item2 != null?item2.FindMergeAction(item1) : null;

                if (item1 != null && item2 != null)
                {
                    //Same slot, cancel select
                    if (slot1 == slot2)
                    {
                        CancelSelection();
                        return;
                    }
                    //Same item, combine stacks
                    else if (item1 == item2 && !limit_one_item)
                    {
                        CombineItems(selected_slot, clicked_slot);
                        return;
                    }
                    //Else, use merge action
                    else if (action1 != null && action1.CanDoAction(current_player, slot1, slot2))
                    {
                        DoMergeAction(action1, slot1, slot2);
                        return;
                    }

                    else if (action2 != null && action2.CanDoAction(current_player, slot2, slot1))
                    {
                        DoMergeAction(action2, slot2, slot1);
                        return;
                    }
                }

                if (item1 != item2)
                {
                    //Swap
                    bool quantity_is_1 = slot1.GetQuantity() <= 1 && slot2.GetQuantity() <= 1;
                    bool should_swap   = !limit_one_item || slot1.GetInventory() == slot2.GetInventory() || quantity_is_1;
                    if (item2 == null && !should_swap)
                    {
                        SetOneItem(selected_slot, clicked_slot);
                        return;
                    }

                    if (should_swap)
                    {
                        SwapItems(selected_slot, clicked_slot);
                        return;
                    }
                }
            }
        }
        void Update()
        {
            if (target == null)
            {
                Destroy(gameObject);
                return;
            }

            if (icon_group.activeSelf)
            {
                icon_group.SetActive(false);
            }

            if (!target.IsActive())
            {
                return;
            }

            transform.position = target.transform.position;
            transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up);

            ItemSlot selected = ItemSlotPanel.GetSelectedSlotInAllPanels();

            if (selected != null && selected.GetItem() != null)
            {
                MAction action = selected.GetItem().FindMergeAction(target);
                foreach (PlayerCharacter player in PlayerCharacter.GetAll())
                {
                    if (player != null && action != null && action.CanDoAction(player, selected, target))
                    {
                        icon.sprite = selected.GetItem().icon;
                        title.text  = action.title;
                        icon_group.SetActive(true);
                    }
                }
            }
        }