Ejemplo n.º 1
0
    public void ClearPath()
    {
        for (int i = 0; i < m_PathNodes.Count; i++)
        {
            Destroy(m_PathNodes[i].gameObject);
            Destroy(m_PathNodeShadows[i].gameObject);
        }

        m_PathNodes.Clear();
        m_PathNodeShadows.Clear();

        m_TargetNodeIndex = -1;
        HasPath           = false;
        OnPathRemoved?.Invoke();
    }
Ejemplo n.º 2
0
    void GeneratePath(int a_Index)
    {
        for (int i = 0; i < m_PathNodes.Count; i++)
        {
            Destroy(m_PathNodes[i].gameObject);
            Destroy(m_PathNodeShadows[i].gameObject);
        }

        m_PathNodes.Clear();
        m_PathNodeShadows.Clear();

        if (!m_PathableArea.ContainsKey(a_Index))
        {
            m_TargetNodeIndex = -1;
            HasPath           = false;
            OnPathRemoved?.Invoke();
            return;
        }

        Pathfinding.PrecomputedNode _CurrentNode = m_PathableArea[a_Index];

        m_Path.Clear();

        while (_CurrentNode.Parent != null)
        {
            m_Path.Add(_CurrentNode);

            _CurrentNode = _CurrentNode.Parent;
        }

        m_Path.Reverse();

        for (int i = 0; i < m_Path.Count; i++)
        {
            Vector3        _Position   = new Vector3(m_Path[i].PosX, -m_Path[i].PosY, 0);
            SpriteRenderer _Node       = Instantiate(m_PathNodePrefab, _Position, Quaternion.identity, transform.parent);
            SpriteRenderer _NodeShadow = Instantiate(m_PathNodeShadowPrefab, _Position, Quaternion.identity, transform.parent);

            Vector2Int _PreviousNodePos;
            Vector2Int _NextNodePos;

            if (i > 0)
            {
                _PreviousNodePos = new Vector2Int(m_Path[i - 1].PosX, m_Path[i - 1].PosY);
            }
            else
            {
                _PreviousNodePos   = m_PathfindingPos;
                _PreviousNodePos.x = Mathf.Abs(_PreviousNodePos.x);
                _PreviousNodePos.y = Mathf.Abs(_PreviousNodePos.y);
            }

            _PreviousNodePos -= new Vector2Int(m_Path[i].PosX, m_Path[i].PosY);

            if (i == m_Path.Count - 1)
            {
                _Node.sprite       = m_PathSprites[0];
                _NodeShadow.sprite = m_PathSprites[0];
            }
            else
            {
                _NextNodePos  = new Vector2Int(m_Path[i + 1].PosX, m_Path[i + 1].PosY);
                _NextNodePos -= new Vector2Int(m_Path[i].PosX, m_Path[i].PosY);

                if (_NextNodePos.x == -1 &&
                    _NextNodePos.y == -1)
                {
                    if (_PreviousNodePos.x == 1 &&
                        _PreviousNodePos.y == 1)
                    {
                        _Node.sprite       = m_PathSprites[16];
                        _NodeShadow.sprite = m_PathSprites[16];
                    }
                    else
                    {
                        if (_PreviousNodePos.x == -1)
                        {
                            _Node.sprite       = m_PathSprites[8];
                            _NodeShadow.sprite = m_PathSprites[8];
                        }
                        else if (_PreviousNodePos.y == -1)
                        {
                            _Node.sprite       = m_PathSprites[24];
                            _NodeShadow.sprite = m_PathSprites[24];
                        }
                        else if (_PreviousNodePos.x == 1)
                        {
                            _Node.sprite       = m_PathSprites[24];
                            _NodeShadow.sprite = m_PathSprites[24];
                        }
                        else
                        {
                            _Node.sprite       = m_PathSprites[8];
                            _NodeShadow.sprite = m_PathSprites[8];
                        }
                    }
                }
                else if (_NextNodePos.x == 1 &&
                         _NextNodePos.y == 1)
                {
                    if (_PreviousNodePos.x == -1 &&
                        _PreviousNodePos.y == -1)
                    {
                        _Node.sprite       = m_PathSprites[12];
                        _NodeShadow.sprite = m_PathSprites[12];
                    }
                    else
                    {
                        if (_PreviousNodePos.x == 1)
                        {
                            _Node.sprite       = m_PathSprites[4];
                            _NodeShadow.sprite = m_PathSprites[4];
                        }
                        else if (_PreviousNodePos.y == 1)
                        {
                            _Node.sprite       = m_PathSprites[20];
                            _NodeShadow.sprite = m_PathSprites[20];
                        }
                        else if (_PreviousNodePos.x == -1)
                        {
                            _Node.sprite       = m_PathSprites[20];
                            _NodeShadow.sprite = m_PathSprites[20];
                        }
                        else
                        {
                            _Node.sprite       = m_PathSprites[4];
                            _NodeShadow.sprite = m_PathSprites[4];
                        }
                    }
                }
                else if (_NextNodePos.x == -1 &&
                         _NextNodePos.y == 1)
                {
                    if (_PreviousNodePos.x == 1 &&
                        _PreviousNodePos.y == -1)
                    {
                        _Node.sprite       = m_PathSprites[14];
                        _NodeShadow.sprite = m_PathSprites[14];
                    }
                    else
                    {
                        if (_PreviousNodePos.x == -1)
                        {
                            _Node.sprite       = m_PathSprites[22];
                            _NodeShadow.sprite = m_PathSprites[22];
                        }
                        else if (_PreviousNodePos.y == 1)
                        {
                            _Node.sprite       = m_PathSprites[6];
                            _NodeShadow.sprite = m_PathSprites[6];
                        }
                        else if (_PreviousNodePos.x == 1)
                        {
                            _Node.sprite       = m_PathSprites[6];
                            _NodeShadow.sprite = m_PathSprites[6];
                        }
                        else
                        {
                            _Node.sprite       = m_PathSprites[22];
                            _NodeShadow.sprite = m_PathSprites[22];
                        }
                    }
                }
                else if (_NextNodePos.x == 1 &&
                         _NextNodePos.y == -1)
                {
                    if (_PreviousNodePos.x == -1 &&
                        _PreviousNodePos.y == 1)
                    {
                        _Node.sprite       = m_PathSprites[10];
                        _NodeShadow.sprite = m_PathSprites[10];
                    }
                    else
                    {
                        if (_PreviousNodePos.x == 1)
                        {
                            _Node.sprite       = m_PathSprites[18];
                            _NodeShadow.sprite = m_PathSprites[18];
                        }
                        else if (_PreviousNodePos.y == -1)
                        {
                            _Node.sprite       = m_PathSprites[2];
                            _NodeShadow.sprite = m_PathSprites[2];
                        }
                        else if (_PreviousNodePos.x == -1)
                        {
                            _Node.sprite       = m_PathSprites[2];
                            _NodeShadow.sprite = m_PathSprites[2];
                        }
                        else
                        {
                            _Node.sprite       = m_PathSprites[18];
                            _NodeShadow.sprite = m_PathSprites[18];
                        }
                    }
                }
                else if (_NextNodePos.x == 1)
                {
                    if (_PreviousNodePos.y == 1)
                    {
                        _Node.sprite       = m_PathSprites[19];
                        _NodeShadow.sprite = m_PathSprites[19];
                    }
                    else if (_PreviousNodePos.y == -1)
                    {
                        _Node.sprite       = m_PathSprites[3];
                        _NodeShadow.sprite = m_PathSprites[3];
                    }
                    else
                    {
                        _Node.sprite       = m_PathSprites[11];
                        _NodeShadow.sprite = m_PathSprites[11];
                    }
                }
                else if (_NextNodePos.x == -1)
                {
                    if (_PreviousNodePos.y == 1)
                    {
                        _Node.sprite       = m_PathSprites[7];
                        _NodeShadow.sprite = m_PathSprites[7];
                    }
                    else if (_PreviousNodePos.y == -1)
                    {
                        _Node.sprite       = m_PathSprites[23];
                        _NodeShadow.sprite = m_PathSprites[23];
                    }
                    else
                    {
                        _Node.sprite       = m_PathSprites[15];
                        _NodeShadow.sprite = m_PathSprites[15];
                    }
                }
                else if (_NextNodePos.y == 1)
                {
                    if (_PreviousNodePos.x == 1)
                    {
                        _Node.sprite       = m_PathSprites[5];
                        _NodeShadow.sprite = m_PathSprites[5];
                    }
                    else if (_PreviousNodePos.x == -1)
                    {
                        _Node.sprite       = m_PathSprites[21];
                        _NodeShadow.sprite = m_PathSprites[21];
                    }
                    else
                    {
                        _Node.sprite       = m_PathSprites[13];
                        _NodeShadow.sprite = m_PathSprites[13];
                    }
                }
                else
                {
                    if (_PreviousNodePos.x == 1)
                    {
                        _Node.sprite       = m_PathSprites[17];
                        _NodeShadow.sprite = m_PathSprites[17];
                    }
                    else if (_PreviousNodePos.x == -1)
                    {
                        _Node.sprite       = m_PathSprites[1];
                        _NodeShadow.sprite = m_PathSprites[1];
                    }
                    else
                    {
                        _Node.sprite       = m_PathSprites[9];
                        _NodeShadow.sprite = m_PathSprites[9];
                    }
                }
            }

            m_PathNodes.Add(_Node);
            m_PathNodeShadows.Add(_NodeShadow);
        }

        m_TargetNodeIndex = a_Index;

        HasPath = true;
        OnPathCreated?.Invoke();
    }
Ejemplo n.º 3
0
    IEnumerator Move()
    {
        if (m_Path.Count == 0)
        {
            yield break;
        }

        IsAsleep = false;

        m_StopMovement = false;

        IsMoving = true;

        m_FlagRenderer.enabled = false;

        HeroFlagVisualData _FlagData = m_PlayerColors.Flags[PlayerIndex];

        m_DynamicObstacle.ClearNodes();

        Pathfinding.PrecomputedNode _Node = m_Path[0];

        Destroy(m_PathNodes[0]);
        Destroy(m_PathNodeShadows[0]);

        m_PathNodes.RemoveAt(0);
        m_PathNodeShadows.RemoveAt(0);

        int _AnimationIndex = 0;
        int _Frame          = 0;

        m_GameReferences.InputBlocker.SetActive(true);

        Vector3 _MovementDirection;

        void SetDirection()
        {
            _MovementDirection = new Vector3
                                 (
                m_Path[0].PosX - m_PathfindingPos.x,
                -(m_Path[0].PosY - m_PathfindingPos.y),
                0
                                 ) / 4;

            if (_MovementDirection.x > 0)
            {
                if (_MovementDirection.y > 0)
                {
                    m_Direction = DIRECTION_NE;
                }
                else if (_MovementDirection.y < 0)
                {
                    m_Direction = DIRECTION_SE;
                }
                else
                {
                    m_Direction = DIRECTION_E;
                }
            }
            else if (_MovementDirection.x < 0)
            {
                if (_MovementDirection.y > 0)
                {
                    m_Direction = DIRECTION_NW;
                }
                else if (_MovementDirection.y < 0)
                {
                    m_Direction = DIRECTION_SW;
                }
                else
                {
                    m_Direction = DIRECTION_W;
                }
            }
            else if (_MovementDirection.y > 0)
            {
                m_Direction = DIRECTION_N;
            }
            else
            {
                m_Direction = DIRECTION_S;
            }
        }

        SetDirection();

        OnStartMovement?.Invoke();

        // We don't check IsMoving here, because when cancelled, we still want to finish walking to the next tile
        while (true)
        {
            if (_Frame == 4)
            {
                _Frame = 0;

                m_PathfindingPos = new Vector2Int(_Node.PosX, _Node.PosY);
                m_Path.RemoveAt(0);

                if (m_Path.Count == 0 ||
                    m_StopMovement)
                {
                    break;
                }

                Destroy(m_PathNodes[0]);
                Destroy(m_PathNodeShadows[0]);

                m_PathNodes.RemoveAt(0);
                m_PathNodeShadows.RemoveAt(0);

                _Node = m_Path[0];

                SetDirection();
            }

            m_HeroRenderer.sprite       = Hero.HeroVisualData.MovingSprites[m_Direction].Array[_AnimationIndex];
            m_HeroShadowRenderer.sprite = Hero.HeroVisualData.ShadowMovingSprites[m_Direction].Array[_AnimationIndex];
            m_FlagSpriteRenderer.sprite = _FlagData.MovingSprites[m_Direction].Array[_AnimationIndex];

            m_HeroRenderer.flipX       = HeroVisualData.SPRITES_FLIPPED[m_Direction];
            m_HeroShadowRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction];
            m_FlagSpriteRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction];

            if (m_HeroRenderer.flipX)
            {
                m_HeroRenderer.transform.localPosition       = new Vector3(-3, 0, 0);
                m_HeroShadowRenderer.transform.localPosition = new Vector3(-3, 0, 0);
                m_FlagSpriteRenderer.transform.localPosition = new Vector2(-3, 0) + Hero.HeroVisualData.MovingFlagOffsets[m_Direction];
            }
            else
            {
                m_HeroRenderer.transform.localPosition       = Vector3.zero;
                m_HeroShadowRenderer.transform.localPosition = Vector3.zero;
                m_FlagSpriteRenderer.transform.localPosition = Hero.HeroVisualData.MovingFlagOffsets[m_Direction];
            }

            transform.position += _MovementDirection;

            yield return(new WaitForSeconds(0.05f));

            _AnimationIndex++;
            _Frame++;

            if (_AnimationIndex == 8)
            {
                _AnimationIndex = 0;
            }
        }

        m_GameReferences.InputBlocker.SetActive(false);

        m_FlagRenderer.enabled = true;

        m_HeroRenderer.sprite       = Hero.HeroVisualData.IdleSprites[m_Direction];
        m_HeroShadowRenderer.sprite = Hero.HeroVisualData.ShadowIdleSprites[m_Direction];

        m_HeroRenderer.flipX       = HeroVisualData.SPRITES_FLIPPED[m_Direction];
        m_HeroShadowRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction];
        m_FlagSpriteRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction];

        if (m_HeroRenderer.flipX)
        {
            m_HeroRenderer.transform.localPosition       = new Vector3(-3, 0, 0);
            m_HeroShadowRenderer.transform.localPosition = new Vector3(-3, 0, 0);
            m_FlagSpriteRenderer.transform.localPosition = new Vector2(-3, 0) + Hero.HeroVisualData.IdleFlagOffsets[m_Direction];
        }
        else
        {
            m_HeroRenderer.transform.localPosition       = Vector3.zero;
            m_HeroShadowRenderer.transform.localPosition = Vector3.zero;
            m_FlagSpriteRenderer.transform.localPosition = Hero.HeroVisualData.IdleFlagOffsets[m_Direction];
        }

        Pathfinding.Node _FinalNode = m_GameReferences.Pathfinding.GetNode(m_PathfindingPos, IsUnderground);

        m_DynamicObstacle.AddInteractedNode(_FinalNode);

        for (int i = 0; i < _FinalNode.InteractionObjects.Count; i++)
        {
            MapTown _Town = _FinalNode.InteractionObjects[i] as MapTown;

            if (_Town != null)
            {
                m_GameReferences.TownScreen.OpenTown(_Town);
            }
        }

        m_PathableArea = m_GameReferences.Pathfinding.GetPathableArea(m_PathfindingPos, IsUnderground);

        if (m_Path.Count == 0)
        {
            m_TargetNodeIndex = -1;
            HasPath           = false;
            OnPathRemoved?.Invoke();
        }

        IsMoving = false;
    }