Beispiel #1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            DebugInventory();
        }

        //Drop item
        if (Input.GetKeyDown(PlayerData.Instance.keybinds["Drop Item"]) && mouseInventory.inventoryReadOnly[0] != null)
        {
            ItemEntityPool.Instance.CreateItemEntity(Camera.main.ScreenToWorldPoint(Input.mousePosition), mouseInventory.inventoryReadOnly[0].item);
            mouseInventory.DecrementStack(0);
        }

        //Fire weapon
        if (Input.GetKey(PlayerData.Instance.keybinds["Fire Weapon"]))
        {
            fireWeapon = true;
        }

        if (Input.GetKeyDown(PlayerData.Instance.keybinds["Rotate Buildable"]))
        {
            placementRotation--;
            if (placementRotation < 0)
            {
                placementRotation = 7;
            }
        }


        if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            Vector3        mousePos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2        mousePos2D = new Vector2(mousePos.x, mousePos.y);
            RaycastHit2D[] hits       = new RaycastHit2D[1];
            Physics2D.Raycast(mousePos2D, Vector2.zero, filterMining, hits);

            if (mouseInventory.inventoryReadOnly[0] != null)
            {
                ItemThrowable throwable = mouseInventory.inventoryReadOnly[0].item as ItemThrowable;
                if (throwable != null && Input.GetMouseButtonDown(0)) //thrown item code
                {
                    Vector3    place  = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    ThrownItem thrown = Instantiate(throwable.thrownResult, this.transform.position, Quaternion.identity).GetComponent <ThrownItem>();
                    thrown.SetTarget(place);
                    mouseInventory.DecrementStack(0);
                }
                else if (mouseInventory.inventoryReadOnly[0].item.placeableResult != null) //place building code
                {
                    Vector2 size;
                    IRotate rotateable;
                    if (mouseInventory.inventoryReadOnly[0].item.placeableResult.TryGetComponent(out rotateable))
                    {
                        placeSpriteRenderer.sprite = rotateable.GetRotatedPlaceSprite(placementRotation);
                        size = rotateable.GetRotatedBoundingBox(placementRotation);
                    }
                    else
                    {
                        placeSpriteRenderer.sprite = mouseInventory.inventoryReadOnly[0].item.placeableResult.GetComponent <SpriteRenderer>().sprite;
                        size = mouseInventory.inventoryReadOnly[0].item.placeableResult.GetComponent <BoxCollider2D>().size;
                    }

                    Vector3 place        = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    Vector3 placeRounded = new Vector3(Mathf.Round(place.x), Mathf.Round(place.y), 0);
                    Vector3 placeVector  = placeGrid.WorldToCell(placeRounded);
                    size = new Vector2(Mathf.CeilToInt(size.x), Mathf.CeilToInt(size.y));
                    Vector3 offset = new Vector3(0, 0);
                    if (size.x % 2 != 0)
                    {
                        offset.x = 0.5f;
                    }
                    if (size.y % 2 != 0)
                    {
                        offset.y = 0.5f;
                    }

                    placeSprite.transform.position = placeVector - offset;



                    BuildingManager buildingManager;


                    if (rotateable != null && Physics2D.OverlapBox(placeVector - offset, size - new Vector2(0.1f, 0.1f), 0.0f, filter, colliders) == 0)
                    {
                        rotateable.GetRotatedPlaceSprite(placementRotation);
                        placeSpriteRenderer.color = new Color(0, 1, 0, 0.25f);
                        if (Input.GetMouseButton(0))
                        {
                            GameObject building = Instantiate(mouseInventory.inventoryReadOnly[0].item.placeableResult, placeVector - offset, Quaternion.identity);
                            building.GetComponent <Health>().alliance = characterAlliance.allianceCode;
                            building.GetComponent <IRotate>().InitializeRotated(placementRotation);
                            DestroyGrassAt(placeVector - offset, size);
                            mouseInventory.DecrementStack(0);
                            UpdateLoaders(building.transform.position, size);
                        }
                    }
                    else if (mouseInventory.inventoryReadOnly[0].item.placeableResult.TryGetComponent(out buildingManager))
                    {
                        Physics2D.OverlapBox(placeVector - offset, size - new Vector2(0.1f, 0.1f), 0.0f, filterMining, collidersList);
                        List <Mineable> mineables;
                        if (buildingManager.CheckPlacement(collidersList, out mineables, placeVector - offset))
                        {
                            placeSpriteRenderer.color = new Color(0, 1, 0, 0.25f);
                            if (Input.GetMouseButton(0))
                            {
                                GameObject building = Instantiate(mouseInventory.inventoryReadOnly[0].item.placeableResult, placeVector - offset, Quaternion.identity);
                                building.GetComponent <CraftingManagerFixed>().SetMineables(mineables);
                                building.GetComponent <Health>().alliance = characterAlliance.allianceCode;
                                DestroyGrassAt(placeVector - offset, size);
                                mouseInventory.DecrementStack(0);
                                UpdateLoaders(building.transform.position, size);
                            }
                        }
                        else
                        {
                            placeSpriteRenderer.color = new Color(1, 0, 0, 0.25f);
                        }
                    }
                    else if (Physics2D.OverlapBox(placeVector - offset, size - new Vector2(0.1f, 0.1f), 0.0f, filter, colliders) == 0)
                    {
                        placeSpriteRenderer.color = new Color(0, 1, 0, 0.25f);
                        if (Input.GetMouseButton(0))
                        {
                            GameObject building = Instantiate(mouseInventory.inventoryReadOnly[0].item.placeableResult, placeVector - offset, Quaternion.identity);
                            building.GetComponent <Health>().alliance = characterAlliance.allianceCode;
                            DestroyGrassAt(placeVector - offset, size);
                            mouseInventory.DecrementStack(0);
                            UpdateLoaders(building.transform.position, size);
                        }
                    }
                    else
                    {
                        placeSpriteRenderer.color = new Color(1, 0, 0, 0.25f);
                    }
                }
                else if (mouseInventory.inventoryReadOnly[0].item.placeableResult == null)
                {
                    placeSpriteRenderer.color = new Color(1, 1, 1, 0);
                }
            }
            else
            {
                placeSpriteRenderer.color = new Color(1, 1, 1, 0);
            }

            if (hits[0].collider != null)
            {
                #region Left Click Interaction
                if (Input.GetMouseButtonDown(0))
                {
                    Inventory             inventory             = null;
                    CraftingManagerRecipe craftingManagerRecipe = null;
                    CraftingManagerFixed  craftingManagerFixed  = null;
                    IItemFilter           itemFilter            = null;
                    if (hits[0].collider.gameObject.TryGetComponent <CraftingManagerRecipe>(out craftingManagerRecipe))
                    {
                        filterItemUI.SetFilterFocus(null);
                        machineUI.SetLinkedMachine(hits[0].collider.gameObject);
                        inventoryInteractUI.SetViewedInventory(null);
                    }
                    else if (hits[0].collider.gameObject.TryGetComponent <CraftingManagerFixed>(out craftingManagerFixed))
                    {
                        filterItemUI.SetFilterFocus(null);
                        machineUI.SetLinkedMachine(hits[0].collider.gameObject);
                        inventoryInteractUI.SetViewedInventory(null);
                    }
                    else if (hits[0].collider.gameObject.TryGetComponent <Inventory>(out inventory))
                    {
                        filterItemUI.SetFilterFocus(null);
                        inventoryInteractUI.SetViewedInventory(inventory);
                        machineUI.SetLinkedMachine(null);
                    }
                    else if (hits[0].collider.gameObject.TryGetComponent <IItemFilter>(out itemFilter))
                    {
                        filterItemUI.SetFilterFocus(itemFilter);
                        machineUI.SetLinkedMachine(null);
                        inventoryInteractUI.SetViewedInventory(null);
                    }
                    else
                    {
                        inventoryInteractUI.SetViewedInventory(null);
                        machineUI.SetLinkedMachine(null);
                    }
                }
                #endregion Left Click Interaction

                #region Right Click Interaction
                if (Input.GetMouseButtonDown(1))
                {
                    Mineable mineable;
                    if (hits[0].collider.TryGetComponent(out mineable))
                    {
                        currentMineable = mineable;
                    }
                }

                if (currentMineable != null)
                {
                    Mineable mineable;
                    if (hits[0].collider.TryGetComponent(out mineable))
                    {
                        if (!ReferenceEquals(currentMineable, mineable))
                        {
                            miningProgress  = 0;
                            currentMineable = mineable;
                        }
                    }
                }
                #endregion Right Click Interaction
            }
            else
            {
                currentMineable = null;
            }
        }
        else
        {
            placeSpriteRenderer.color = new Color(1, 1, 1, 0);
        }

        if (Input.GetKeyDown(PlayerData.Instance.keybinds["Change Ammo"]))
        {
            selectedAmmoSlot++;
            if (selectedAmmoSlot >= ammoInventory.inventoryReadOnly.Count)
            {
                selectedAmmoSlot = 0;
            }
            ammoSlotSelected.SetHighlightedSlot(selectedAmmoSlot);
        }
    }