public void PickUpItem()
    {
        player.current_item = (o_item)Grid.ObjectFromWorld(player.transform.position, 1);
        Grid.DespawnObject(Grid.ObjectFromWorld(player.transform.position, 1));

        STATES = STATEMACHINE.SELECT_CHAR;
    }
 public void EndTurn()
 {
     if (GetWinner == VICTORY_DICTATOR.PLAYER)
     {
         StartCoroutine(NextMap());
         return;
     }
     if (TURNSTATE == TURNS.PLAYER)
     {
         foreach (o_character p in Enemies)
         {
             //for now they will recover all AP
             p.actionPoints = p.maxActionPoints;
         }
         EnemyTurn();
         TURNSTATE = TURNS.ENEMY;
     }
     else
     {
         foreach (o_character p in Players)
         {
             //for now they will recover all AP
             p.actionPoints = p.maxActionPoints;
         }
         IsSkipped = false;
         TURNSTATE = TURNS.PLAYER;
         STATES    = STATEMACHINE.IDLE;
     }
 }
 void NextEnemy()
 {
     player = null;
     EnemyQueue.Dequeue();
     STATES = STATEMACHINE.IDLE;
     //print("Progress: " + EnemyQueue.Count);
 }
    public void ConfirmMovement()
    {
        player.actionPoints -= pathfcost;
        pathfcost            = 0;

        if (!IsSkipped)
        {
            STATES = STATEMACHINE.WALK;
            Grid.UnpaintAllNodes();
            StartCoroutine(player.MoveToPosition(this));
        }
        else   //If the player is walking then stop.
        {
            player.Teleport(this);
        }
        //Disable CancelMove and Confirm
    }
    public void UseItem()
    {
        //Play some anim
        o_item ite = player.current_item;

        switch (ite.ITEM_TYPE)
        {
        case o_item.IT_TYPE.AP:
            player.actionPoints += ite.amount;
            break;

        case o_item.IT_TYPE.HEALTH:
            player.health += ite.amount;
            break;
        }
        player.health       = Mathf.Clamp(player.health, 0, player.maxHealth);
        player.current_item = null;
        //Grid.DespawnObject(Grid.ObjectFromWorld(player.transform.position, 1));
        STATES = STATEMACHINE.SELECT_CHAR;
    }
    private void Update()
    {
        if (PlayerPrefs.GetInt("MonsterMode") == 0)
        {
            if (Players.Count < 10)
            {
                PlayerCount.text = "0" + Players.Count;
            }
            else
            {
                PlayerCount.text = "" + Players.Count;
            }

            if (Enemies.Count < 10)
            {
                EnemyCount.text = "0" + Enemies.Count;
            }
            else
            {
                EnemyCount.text = "" + Enemies.Count;
            }
        }
        else
        {
            if (Players.Count < 10)
            {
                PlayerCount.text = "0" + Enemies.Count;
            }
            else
            {
                PlayerCount.text = "" + Enemies.Count;
            }

            if (Enemies.Count < 10)
            {
                EnemyCount.text = "0" + Players.Count;
            }
            else
            {
                EnemyCount.text = "" + Players.Count;
            }
        }

        Vector3 mousePositon = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (player == null)
        {
            pathfcost  = 0;
            stats.text = "Nothing";
        }

        if (GetWinner == VICTORY_DICTATOR.ENEMY)
        {
            s_camera.staticCam.StartCoroutine(s_camera.staticCam.Fade(Color.black, 0.3f));

            game_on = false;
            if (s_camera.staticCam.isfaded)
            {
                UnityEngine.SceneManagement.SceneManager.LoadScene("Title");
            }
        }


        if (game_on)
        {
            switch (STATES)
            {
            case STATEMACHINE.IDLE:

                switch (TURNSTATE)
                {
                case TURNS.PLAYER:
                    if (Input.GetMouseButtonDown(0))
                    {
                        if (CheckForObject(mousePositon))
                        {
                            s_object obj = GetObjectFromWorld(mousePositon);
                            if (IsCharacter(mousePositon, obj))
                            {
                                o_character obselect = obj.GetComponent <o_character>();
                                if (obselect.health > 0)
                                {
                                    if (obselect.playable)
                                    {
                                        pathfcost = 0;
                                        SwitchCharacter((o_character)obj);
                                        CheckCharacterSurroundings(false);
                                    }
                                    player = obselect;
                                    STATES = STATEMACHINE.SELECT_CHAR;
                                }
                            }
                        }
                    }
                    break;

                    #region ENEMY
                case TURNS.ENEMY:

                    if (EnemyQueue.Count > 0)
                    {
                        //print("Chara " + EnemyQueue.Peek());
                        player = EnemyQueue.Peek();
                        //print("pl " + player);
                        STATES = STATEMACHINE.SELECT_CHAR;
                    }
                    else
                    {
                        s_camera.staticCam.StartCoroutine(s_camera.staticCam.Fade(Color.clear, 4));
                        if (s_camera.staticCam.isfaded)
                        {
                            print("Progress END: " + EnemyQueue.Count);
                            EndTurn();
                        }
                    }

                    break;
                    #endregion
                }

                break;

            case STATEMACHINE.SELECT_CHAR:

                switch (TURNSTATE)
                {
                case TURNS.PLAYER:

                    if (player.current_item != null)
                    {
                        stats.text = player.name + "\n" + "Item: " + player.current_item.name;
                    }
                    else
                    {
                        stats.text = player.name;
                    }

                    //actionpoints = "AP: " + player.actionPoints + "/" + player.maxActionPoints;

                    if (CheckForObject(mousePositon))
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            CheckCharacterSurroundings(false);
                            s_object obj = GetObjectFromWorld(mousePositon);

                            if (obj != null)
                            {
                                if (obj.GetType() == typeof(o_character))
                                {
                                    o_character chara = obj.GetComponent <o_character>();
                                    if (targets.Contains(chara))
                                    {
                                        if (!chara.playable)
                                        {
                                            targetToAttack = chara;
                                            STATES         = STATEMACHINE.ATTACK_SELECT;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (Input.GetMouseButtonDown(0))
                    {
                        Vector2 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                        if (player.MoveCost(mousepos) != -1)
                        {
                            Grid.UnpaintAllNodes();
                            pathfcost = player.MoveCost(mousepos);
                            player.SetDirections(Grid.NodeFromWorld(mousepos));
                            List <o_node> dir = player.directions;

                            foreach (o_node d in dir)
                            {
                                Grid.PaintNode(Grid.NodePositionFromWorld(d), Color.yellow);
                            }
                            STATES = STATEMACHINE.MOVE_TO;
                        }
                    }

                    break;

                    #region ENEMY
                case TURNS.ENEMY:

                    if (player.current_item != null)
                    {
                        UseItem();
                    }

                    /*if (targetToAttack != null)
                     *  if (targetToAttack.health == 0 && !targetToAttack.IsDisappear())
                     *      return;
                     */
                    CheckCharacterSurroundings(true);
                    if (targets.Count == 0)
                    {
                        STATES = STATEMACHINE.MOVE_TO;
                    }
                    else
                    {
                        STATES = STATEMACHINE.ATTACK_SELECT;
                    }
                    break;
                    #endregion
                }

                //Have a cancel button where this player is no longer the focus

                //Attack button where it checks enemies

                break;

            case STATEMACHINE.MOVE_TO:

                switch (TURNSTATE)
                {
                case TURNS.PLAYER:
                    if (Input.GetMouseButtonDown(0))
                    {
                        Vector2 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                        if (player.playable)
                        {
                            if (player.actionPoints >= pathfcost)
                            {
                                if (Grid.NodeFromWorld(mousepos) == player.directions[player.directions.Count - 1])
                                {
                                    ConfirmMovement();
                                    return;
                                }
                            }
                        }

                        if (player.MoveCost(mousepos) != -1)
                        {
                            Grid.UnpaintAllNodes();
                            pathfcost = player.MoveCost(mousepos);
                            player.SetDirections(Grid.NodeFromWorld(mousepos));
                            List <o_node> dir = player.directions;

                            foreach (o_node d in dir)
                            {
                                Grid.PaintNode(Grid.NodePositionFromWorld(d), Color.yellow);
                            }
                        }
                    }

                    if (Input.GetMouseButtonDown(1))
                    {
                        Grid.UnpaintAllNodes();
                        LooseFocus();
                    }

                    break;

                    #region ENEMY
                case TURNS.ENEMY:

                    int         newcost = int.MaxValue;
                    o_character tar     = null;

                    foreach (o_character e in Players)
                    {
                        //Check around the target and pathfind for each position.
                        HashSet <o_node> aroundChar = Grid.CheckAroundNode(Grid.NodeFromWorld(e.transform.position));

                        int potentialMoveCost = int.MaxValue;
                        e.GetComponent <SpriteRenderer>().color = Color.red;
                        o_node nof = null;

                        foreach (o_node no in aroundChar)
                        {
                            if (!no.walkable)
                            {
                                continue;
                            }

                            int comp = player.MoveCost(no.position);
                            if (comp == -1)
                            {
                                continue;
                            }

                            potentialMoveCost = Mathf.Min(potentialMoveCost, comp);
                            if (potentialMoveCost == comp)
                            {
                                nof = no;
                            }
                        }

                        e.GetComponent <SpriteRenderer>().color = Color.white;
                        if (potentialMoveCost <= player.range)
                        {
                            if (potentialMoveCost <= player.actionPoints)
                            {
                                tar     = e;
                                newcost = Mathf.Min(newcost, potentialMoveCost);
                                if (newcost == potentialMoveCost)
                                {
                                    if (nof != null)
                                    {
                                        player.SetDirections(nof);
                                    }
                                }
                            }
                        }
                        else
                        {
                            continue;
                        }
                        Grid.UnpaintAllNodes();
                    }

                    if (newcost != int.MaxValue)
                    {
                        Grid.UnpaintAllNodes();
                        pathfcost = newcost;
                        if (pathfcost > 0)
                        {
                            ConfirmMovement();
                        }
                        else
                        {
                            NextEnemy();
                        }
                    }
                    else
                    {
                        NextEnemy();
                    }

                    break;
                    #endregion
                }
                //Confirm?
                //Cancecl Move

                break;

            case STATEMACHINE.ATTACK_SELECT:

                switch (TURNSTATE)
                {
                case TURNS.PLAYER:


                    if (targetToAttack != null)
                    {
                        targetToAttack.renderer.color = Color.magenta;
                    }

                    if (Input.GetMouseButtonDown(0))
                    {
                        s_object obj = GetObjectFromWorld(mousePositon);
                        if (obj != targetToAttack)
                        {
                            o_character newtarg = targets.Find(x => obj == x);

                            if (newtarg != null)
                            {
                                targetToAttack.renderer.color = Color.white;
                                targetToAttack = newtarg;
                                return;
                            }
                        }
                    }


                    if (targetToAttack.health > 0 && player.actionPoints >= 2)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            targetToAttack.renderer.color = Color.white;
                            STATES = STATEMACHINE.ATTACK_EXECUTE;
                        }
                    }

                    if (targetToAttack.health <= 0 || player.actionPoints < 2)
                    {
                        targetToAttack.renderer.color = Color.white;
                        LooseFocus();
                    }

                    if (Input.GetMouseButtonDown(1))
                    {
                        if (targetToAttack != null)
                        {
                            targetToAttack.renderer.color = Color.white;
                        }
                        LooseFocus();
                    }
                    break;

                    #region ENEMY
                case TURNS.ENEMY:

                    CheckCharacterSurroundings(true);
                    if (targets.Count > 0)
                    {
                        //The enemy can attack the player again if they have sufficent points
                        if (player.actionPoints >= 2)
                        {
                            targetToAttack = GetTargetWithLowestHealth();

                            if (targetToAttack.health > 0)
                            {
                                if (!isattacking && !IsSkipped)
                                {
                                    StartCoroutine(AttackingAnim());
                                }

                                if (IsSkipped)
                                {
                                    AttackCalculations(targetToAttack);
                                }
                            }
                        }
                        else
                        {
                            NextEnemy();
                        }
                    }
                    else
                    {
                        STATES = STATEMACHINE.SELECT_CHAR;
                    }

                    break;
                    #endregion
                }

                break;

            case STATEMACHINE.ATTACK_EXECUTE:

                if (!isattacking)
                {
                    StartCoroutine(AttackingAnim());
                }
                else if (TURNSTATE == TURNS.PLAYER)
                {
                    STATES = STATEMACHINE.ATTACK_SELECT;
                }

                break;

            case STATEMACHINE.WALK:
                //Call Player enumarator to walk
                //Once done go back to select char

                break;
            }
        }
    }
 public void SwitchStateOnClick(int e)
 {
     STATES = (STATEMACHINE)e;
 }
 public void LooseFocus()
 {
     STATES = STATEMACHINE.IDLE;
     player = null;
 }
 public void DropItem()
 {
     Grid.SpawnObject(player.current_item.name, player.transform.position);
     player.current_item = null;
     STATES = STATEMACHINE.SELECT_CHAR;
 }