Ejemplo n.º 1
0
    public void PlayerStateMachine()
    {
        switch (state)
        {
        case playerState.Idle:
            if (Input.GetButtonDown("Horizontal") || Input.GetButtonDown("Vertical"))
            {
                state = playerState.Moving;
            }
            else if (Input.GetButtonDown("Map"))
            {
                state = playerState.Map;
                mainCamera.orthographicSize = maxZoom;
            }

            else if (Input.GetButtonDown("Inventory"))
            {
                inventoryUI.alpha          = 1f;
                inventoryUI.blocksRaycasts = true;
                //inventoryScript.getItems(items);
                //inventoryScript.getTools(tools);
                state = playerState.Inventory;
            }

            else if (Input.GetButtonDown("Craft"))
            {
                crafting.alpha          = 1f;
                crafting.blocksRaycasts = true;
                state = playerState.Crafting;
            }

            else if (Input.GetButtonDown("Harvest"))
            {
                state = playerState.Harvesting;
            }
            break;

        case playerState.Moving:
            xAxis = Input.GetAxisRaw("Horizontal");
            yAxis = Input.GetAxisRaw("Vertical");

            Vector3Int gridPosition = map.WorldToCell(transform.position);
            if (map.GetSprite(gridPosition).name == "rock" || map.GetSprite(gridPosition).name == "snowyRock")
            {
                speedModifier = rockMovement;
            }
            else
            {
                speedModifier = originalSpeedModifier;
            }

            float diagonalSpeed = (float)Mathf.Sqrt((yAxis * yAxis) + (xAxis * xAxis));
            if (diagonalSpeed == 0f)
            {
                diagonalSpeed = 1;
            }


            Vector3 movementVector = new Vector3(xAxis, yAxis, 0) * Time.deltaTime * (speed / diagonalSpeed) * speedModifier;
            Vector3 bufferVector   = movementVector;
            bufferVector.x = createBuffer(movementVector.x) / 2;
            bufferVector.y = createBuffer(movementVector.y);
            Vector3Int whereToMove = nodeMap.WorldToCell(transform.position + bufferVector);

            if (nodeMap.GetTile(whereToMove) == null && map.GetTile(whereToMove) != null)
            {
                transform.position           += movementVector;
                mainCamera.transform.position = transform.position + cameraOffset;
            }

            if (xAxis == 0 && yAxis == 0 && Input.GetButtonDown("Horizontal") == false && Input.GetButtonDown("Vertical") == false)
            {
                state = playerState.Idle;
            }
            break;

        case playerState.Map:
            xAxis = Input.GetAxis("Horizontal");
            yAxis = Input.GetAxis("Vertical");
            mainCamera.transform.position += new Vector3(xAxis, yAxis, 0) * Time.deltaTime * 2 * speed;

            if (Input.GetButtonDown("Map"))
            {
                state = playerState.Idle;
                mainCamera.transform.position = transform.position + cameraOffset;
                mainCamera.orthographicSize   = minZoom;
            }
            break;

        case playerState.Inventory:
            inventoryScript.MoveHighlight();
            if (Input.GetButtonDown("Inventory"))
            {
                inventoryUI.alpha          = 0f;
                inventoryUI.blocksRaycasts = false;
                state = playerState.Idle;
            }
            break;

        case playerState.Crafting:
            if (Input.GetButtonDown("Craft"))
            {
                crafting.alpha          = 0f;
                crafting.blocksRaycasts = false;
                state = playerState.Idle;
            }
            break;

        case playerState.Harvesting:
            List <Tile> nodes          = new List <Tile> ();
            var         playerPosition = map.WorldToCell(transform.position);
            playerPosition.x = playerPosition.x - 1;
            playerPosition.y = playerPosition.y - 1;
            area.position    = map.WorldToCell(playerPosition);

            var temp = nodeMap.GetTilesBlock(area);
            foreach (var tile in temp)
            {
                if (tile != null)
                {
                    nodes.Add((Tile)tile);
                }
            }

            // If there are no nodes around, skip this
            if (nodes.Count != 0)
            {
                List <JsonData.GainedResources> resources = new List <JsonData.GainedResources>();
                Debug.Log(nodes[0].name);
                TileScript nodesScript  = nodes[0].gameObject.GetComponent <TileScript>();
                Thread     gatherThread = new Thread(() => { resources = nodesScript.Gather(); });
                gatherThread.Start();
                while (gatherThread.IsAlive)
                {
                    // TODO: interrupt with certain key event
                    if (Input.GetButtonDown("Interrupt"))
                    {
                        gatherThread.Abort();
                    }
                }
                gatherThread.Join();
                foreach (JsonData.GainedResources resource in resources)
                {
                    if (Random.Range(0, 100) < resource.gatherChance)
                    {
                        // Add each successful gather into the list
                        inventoryScript.AddItems(resource.resource,
                                                 Random.Range(resource.gatherAmountMin,
                                                              resource.gatherAmountMax));
                    }
                }

                inventoryScript.inventoryChanged = true;
                inventoryScript.GetItems();
                //inventoryScript.valueNeedsUpdating("minerals", amount);
            }
            state = playerState.Idle;
            break;
        }
    }