Ejemplo n.º 1
0
    public bool isInRange(int targetX, int targetY, int range)
    {
        float dist = PointFunctions.DistanceTo(new Vector2(m_x, m_y), new Vector2(targetX, targetY));

        if (dist <= range)
        {
            return(true);
        }

        return(false);
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (m_isDead && m_myTurn)
        {
            m_battleSystem.EndTurn();
            return;
        }

        if (m_playingSkillAnimation || m_playingItemAnimation)
        {
            return;
        }

        //move animation
        if (moveTimer > 0)
        {
            r_myTurnMarker.SetActive(false);
            moveTimer -= Time.deltaTime * 2;
            gameObject.transform.SetPositionAndRotation(Vector3.Lerp(newPos, oldPos, moveTimer), gameObject.transform.rotation);
        }
        else if (moveTimer < 0)
        {
            r_myTurnMarker.SetActive(true);
            moveTimer = 0;
            gameObject.transform.SetPositionAndRotation(Vector3.Lerp(newPos, oldPos, moveTimer), gameObject.transform.rotation);
        }

        //attackAnimation
        if (attackTimer > 0 && m_attackHasHit == false)
        {
            r_myTurnMarker.SetActive(false);
            attackTimer -= Time.deltaTime * 4;
            gameObject.transform.SetPositionAndRotation(Vector3.Lerp(attackPos, oldPos, attackTimer), gameObject.transform.rotation);
        }
        else if (attackTimer < 0 && m_attackHasHit == false)
        {
            r_myTurnMarker.SetActive(false);
            m_attackHasHit = true;
            //deal Damage
            m_battleSystem.m_battleSpaces[m_targetX, m_targetY].m_pawn.TakeDamage(this, DamageType.DAMAGETYPE_PHYSICAL, 1.0f);
            if (gameObject.tag == "Player")
            {
                m_battleSystem.AttackEnd(false);
            }
        }
        else if (attackTimer > 1 && m_attackHasHit == true)
        {
            r_myTurnMarker.SetActive(true);
            attackTimer = 1;
            gameObject.transform.SetPositionAndRotation(Vector3.Lerp(attackPos, oldPos, attackTimer), gameObject.transform.rotation);
        }
        else if (attackTimer < 1 && m_attackHasHit == true)
        {
            r_myTurnMarker.SetActive(false);
            attackTimer += Time.deltaTime * 3;
            gameObject.transform.SetPositionAndRotation(Vector3.Lerp(attackPos, oldPos, attackTimer), gameObject.transform.rotation);
        }



        if (m_HP <= 0)
        {
            Death();
            return;
        }

        if (!m_myTurn)
        {
            r_myTurnMarker.SetActive(false);
            return;
        }

        if (m_AP == 0 && m_myTurn)
        {
            EndTurn();
            return;
        }

        if (gameObject.tag == "Enemy" && m_myTurn)
        {
            m_enemyActionTimer += Time.deltaTime;
            if (m_enemyActionTimer < m_enemyActionDelay)
            {
                return;
            }
            //check if player is in range
            m_enemyActionTimer = 0;
            //attack an adjecent enemy
            {
                //right space
                if (m_x < 7)
                {
                    if (m_battleSystem.m_battleSpaces[m_x + 1, m_y].m_occupied)
                    {
                        if (m_battleSystem.m_battleSpaces[m_x + 1, m_y].m_pawn.gameObject.tag == "Player")
                        {
                            Attack(m_x + 1, m_y, m_battleSystem.m_battleSpaces[m_x + 1, m_y].m_pawn.gameObject.transform.position);
                            m_AP--;
                            return;
                        }
                    }
                }
                //up space
                if (m_y < 3)
                {
                    if (m_battleSystem.m_battleSpaces[m_x, m_y + 1].m_occupied)
                    {
                        if (m_battleSystem.m_battleSpaces[m_x, m_y + 1].m_pawn.gameObject.tag == "Player")
                        {
                            Attack(m_x, m_y + 1, m_battleSystem.m_battleSpaces[m_x, m_y + 1].m_pawn.gameObject.transform.position);
                            m_AP--;
                            return;
                        }
                    }
                }
                //left space
                if (m_x > 0)
                {
                    if (m_battleSystem.m_battleSpaces[m_x - 1, m_y].m_occupied)
                    {
                        if (m_battleSystem.m_battleSpaces[m_x - 1, m_y].m_pawn.gameObject.tag == "Player")
                        {
                            Attack(m_x - 1, m_y, m_battleSystem.m_battleSpaces[m_x - 1, m_y].m_pawn.gameObject.transform.position);
                            m_AP--;
                            return;
                        }
                    }
                }

                //down space
                if (m_y > 0)
                {
                    if (m_battleSystem.m_battleSpaces[m_x, m_y - 1].m_occupied)
                    {
                        if (m_battleSystem.m_battleSpaces[m_x, m_y - 1].m_pawn.gameObject.tag == "Player")
                        {
                            Attack(m_x, m_y - 1, m_battleSystem.m_battleSpaces[m_x, m_y - 1].m_pawn.gameObject.transform.position);
                            m_AP--;
                            return;
                        }
                    }
                }
            }
            //if no adjecent enemy, move towards target
            List <BattleSpace> targetSpaces = r_targetPlayer.GetAdjacentSpaces();
            List <float>       distences    = new List <float>();

            int i = 0;
            //right space
            if (r_targetPlayer.m_x > 0)
            {
                distences.Add(PointFunctions.DistanceTo(new Vector2(m_x, m_y), new Vector2(targetSpaces[i].x, targetSpaces[i].y)));
                i++;
            }
            //up space
            if (r_targetPlayer.m_y < 3)
            {
                distences.Add(PointFunctions.DistanceTo(new Vector2(m_x, m_y), new Vector2(targetSpaces[i].x, targetSpaces[i].y)));
                i++;
            }
            //left space
            if (r_targetPlayer.m_x < 7)
            {
                distences.Add(PointFunctions.DistanceTo(new Vector2(m_x, m_y), new Vector2(targetSpaces[i].x, targetSpaces[i].y)));
                i++;
            }

            //down space
            if (r_targetPlayer.m_y > 0)
            {
                distences.Add(PointFunctions.DistanceTo(new Vector2(m_x, m_y), new Vector2(targetSpaces[i].x, targetSpaces[i].y)));
                i++;
            }
            bool found = false;

            float lowestDist = 0;
            int   lowestJ    = 0;

            while (!found && distences.Count >= 0)
            {
                lowestJ    = 0;
                lowestDist = 0;
                for (int j = 0; j < distences.Count; j++)
                {
                    if (distences[j] < lowestDist)
                    {
                        lowestJ    = j;
                        lowestDist = distences[j];
                    }
                }

                if (targetSpaces[lowestJ].m_occupied)
                {
                    targetSpaces.RemoveAt(lowestJ);
                    distences.RemoveAt(lowestJ);
                }
                else
                {
                    found = true;
                }
            }

            if (found)
            {
                BattleSpace targetSpace = targetSpaces[lowestJ];
                //if the target space is to the right move right
                if (targetSpace.x > m_x)
                {
                    //only if right is unocupied
                    if (m_battleSystem.m_battleSpaces[m_x + 1, m_y].m_occupied == false)
                    {
                        MoveTo(m_x + 1, m_y, new Vector3(m_battleSystem.m_battleSpaces[m_x + 1, m_y].m_cube.transform.position.x,
                                                         gameObject.transform.position.y,
                                                         m_battleSystem.m_battleSpaces[m_x + 1, m_y].m_cube.transform.position.z));
                        m_AP--;
                        return;
                    }
                }

                //if it is left move left
                if (targetSpace.x < m_x)
                {
                    //and if left is unocupied
                    if (m_battleSystem.m_battleSpaces[m_x - 1, m_y].m_occupied == false)
                    {
                        MoveTo(m_x - 1, m_y, new Vector3(m_battleSystem.m_battleSpaces[m_x - 1, m_y].m_cube.transform.position.x,
                                                         gameObject.transform.position.y,
                                                         m_battleSystem.m_battleSpaces[m_x - 1, m_y].m_cube.transform.position.z));
                        m_AP--;
                        return;
                    }
                }
                // if it is up
                if (targetSpace.y > m_y)
                {
                    if (m_battleSystem.m_battleSpaces[m_x, m_y + 1].m_occupied == false)
                    {
                        MoveTo(m_x, m_y + 1, new Vector3(m_battleSystem.m_battleSpaces[m_x, m_y + 1].m_cube.transform.position.x,
                                                         gameObject.transform.position.y,
                                                         m_battleSystem.m_battleSpaces[m_x, m_y + 1].m_cube.transform.position.z));
                        m_AP--;
                        return;
                    }
                }

                // if it is down
                if (targetSpace.y < m_y)
                {
                    if (m_battleSystem.m_battleSpaces[m_x, m_y - 1].m_occupied == false)
                    {
                        MoveTo(m_x, m_y - 1, new Vector3(m_battleSystem.m_battleSpaces[m_x, m_y - 1].m_cube.transform.position.x,
                                                         gameObject.transform.position.y,
                                                         m_battleSystem.m_battleSpaces[m_x, m_y - 1].m_cube.transform.position.z));
                        m_AP--;
                        return;
                    }
                }
            }



            EndTurn();
        }
    }