コード例 #1
0
    public void NormalState()
    {
        Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        _rigidBody.velocity        = input * m_fMovementSpeed;
        _rigidBody.angularVelocity = 0;
        if (input.magnitude > 0)
        {
            float radianToDegrees = Mathf.Atan2(-input.x, input.y) * 180 / Mathf.PI;
            float xAngle          = Mathf.Atan2(0, input.x) * 180 / Mathf.PI;

            rotation = Quaternion.AngleAxis(radianToDegrees, Vector3.forward);

            float      tempRadian = Mathf.Atan2(input.x, -input.y) * 180 / Mathf.PI;
            Quaternion temp       = Quaternion.AngleAxis(tempRadian, Vector3.forward);
            arrowObject.transform.rotation = temp;
            this.transform.rotation        = Quaternion.AngleAxis(xAngle, Vector3.up);
            _animator.SetBool("Moving", true);
        }
        else
        {
            _animator.SetBool("Moving", false);
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            Ray          ray   = new Ray(this.transform.position, rotation * Vector3.up);
            RaycastHit2D hit2d = Physics2D.Raycast(ray.origin, ray.direction, raycastRange, 1 << 8);

            Debug.DrawRay(ray.origin, ray.direction * 50, Color.red, 100);
            if (hit2d)
            {
                BaseInteractable interactable;
                if ((interactable = hit2d.transform.gameObject.GetComponent <BaseInteractable>()))
                {
                    if (interactable is ResourceInteractable)
                    {
                        ResourceInteractable resource = (interactable as ResourceInteractable);
                        if (resource.ToolRequired)
                        {
                            if (mInventory.GetCurrentItem() != null)
                            {
                                if (mInventory.GetCurrentItem().ItemType == ItemType.TOOL && mInventory.GetCurrentItem().ToolType == resource.toolTypeRequired)
                                {
                                    ChangeState(PlayerState.HARVESTING, Vector3.zero, interactable);
                                }
                            }
                        }
                        else
                        {
                            ChangeState(PlayerState.HARVESTING, Vector3.zero, interactable);
                        }
                    }
                    else if (interactable is CraftableInteractable)
                    {
                        if (mInventory.GetCurrentItem())
                        {
                            CraftableInteractable craftable = (interactable as CraftableInteractable);
                            craftable.Interact(mInventory.GetCurrentItem());

                            Item craftedItem = null;
                            if (craftable.isCompleted(out craftedItem))
                            {
                                if (craftable.itemScriptableObject)
                                {
                                    if (mInventory.ContainsItem(craftable.itemScriptableObject))
                                    {
                                        for (int i = 0; i < mInventory.m_Items.Count; i++)
                                        {
                                            if (mInventory.m_Items[i].ItemType == ItemType.TOOL && mInventory.m_Items[i].ToolType == craftedItem.ToolType)
                                            {
                                                mInventory.m_Items[i].IncreaseQuantity(1);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //Create a new item
                                        craftedItem.IncreaseQuantity(1);
                                        mInventory.AddNewItem(craftedItem);
                                    }
                                }
                                mResources.DepleteResource(PlayerResoureType.STAMINA);
                                if (craftable.DestroyOnceComplete)
                                {
                                    Destroy(craftable.gameObject);
                                }
                            }
                        }
                    }
                    else if (interactable is PlayerResourceInteractable)
                    {
                        PlayerResourceInteractable playerResource = (interactable as PlayerResourceInteractable);
                        if (playerResource.waitForCraftable)
                        {
                            if (playerResource.isReady())
                            {
                                ChangeState(PlayerState.REPLENISHING, hit2d.point, playerResource);
                            }
                        }
                        else
                        {
                            ChangeState(PlayerState.REPLENISHING, hit2d.point, playerResource);
                        }
                    }
                    else
                    {
                        interactable.Interact();
                    }
                    //else if (interactable is CraftableInteractable)
                    //{
                    //    //TODO
                    //}
                }
            }
        }

        for (int i = 0; i < mInventory.m_Items.Count; i++)
        {
            KeyCode potentialKeyPress = (KeyCode)Enum.Parse(typeof(KeyCode), string.Format("Alpha{0}", i + 1));
            if (Input.GetKeyDown(potentialKeyPress))
            {
                mInventory.SetItemIdx(i);
            }
        }
    }