예제 #1
0
    IEnumerator DigDelay(Vector2 dir, DiggableTile dt)
    {
        bool _okayToDig = true;

        StopMoving();
        if (dt.isDiggable())
        {
            if (!dt.isOpen())
            {
                if (_PlayerStats.getStamina() >= _TunnelCost)
                {
                    _PlayerStats.IncrementStamina(-_TunnelCost);
                    //play Anim
                    _animator.SetTrigger("doDig");
                    _animMoveDelay = true;
                    StopMoving();
                    yield return(new WaitForSeconds(2f));

                    _animMoveDelay = false;
                    dt.DigTile();
                }
                else
                {
                    _okayToDig = false;
                }
            }
            if (_okayToDig)
            {
                _controlled = false;
                if (!_InGround)
                {
                    _animator.SetBool("InGround", true);
                    _InGround = true;
                }
                _CurrentTunnelTile = dt;
                if (dir == Vector2.right)
                {
                    _horizontalMove = 0.75f;
                }
                else if (dir == Vector2.left)
                {
                    _horizontalMove = -0.75f;
                }
                else if (dir == Vector2.down || dir == Vector2.up)
                {
                    // Debug.Log("DT:" + dt.gameObject + "  @" + dt.transform.position.y);
                    // Debug.Log("OUR:" + "king" + "  @" + this.transform.position.y);

                    //Calculate distance because of weird anchor points? - keep player in middle of tile
                    float newY = (this.transform.position.y - dt.transform.position.y) / 2; //can Div by 2 to alter if sprite is wrong pos
                    if (dir == Vector2.up)
                    {
                        newY = newY - 2;
                    }

                    this.transform.position = new Vector3(dt.transform.position.x, this.transform.position.y - newY, 0);
                }
            }
        }
    }
예제 #2
0
    public bool CheckTile(string Direction)
    {
        // Debug.Log("ourPos:" + this.transform.position);
        Vector3 location;
        Vector2 directionVector = Vector2.zero;

        if (_CurrentTunnelTile != null)
        {
            location = _CurrentTunnelTile.transform.position;
        }
        else
        {
            location = _CurrentSoilTile.transform.position;
        }



        if (Direction.Equals("right"))
        {
            directionVector = Vector2.right;
            // location += new Vector3(-1f, 0, 0);
        }
        else if (Direction.Equals("left"))
        {
            directionVector = Vector2.left;
            //Character is oddly offset due to tail?
            //location += new Vector3(1, 0, 0);
        }
        else if (Direction.Equals("up"))
        {
            directionVector = Vector2.up;
            // location += new Vector3(0.8f, 0, 0);
        }
        else if (Direction.Equals("down"))
        {
            directionVector = Vector2.down;
            // location += new Vector3(0.8f, 0, 0);
        }
        LayerMask _LayerMask = (1 << 11);

        // initial hit has to stay right.. or it misses left.. idk wtf its doing
        RaycastHit2D initialHit = Physics2D.Raycast(location, directionVector, 2f, _LayerMask);

        RaycastHit2D[] hits = Physics2D.RaycastAll(location, directionVector, 8f, _LayerMask);

        //Debug stuff

        Vector2 LocRaised = new Vector2(location.x - 0.1f, location.y + 0.2f);
        Vector3 pos       = (LocRaised + (directionVector * 2));

        Debug.DrawLine(LocRaised, pos, Color.blue, 3f);
        Vector3 pos2 = (new Vector2(location.x, location.y) + (directionVector * 8));

        Debug.DrawLine(location, pos2, Color.red, 3f);



        GameObject localCurrentTile;

        //Have to be able to find the current tile were on first (Note: _CurrentTile Global is next tile at this point)
        if (initialHit.collider)
        {
            //Debug.Log("Local current tile=" + initialHit.collider.gameObject);
            localCurrentTile = initialHit.collider.gameObject;

            foreach (RaycastHit2D h in hits)
            {
                if (h.collider.gameObject != localCurrentTile)
                {
                    if (h.collider.gameObject.GetComponent <DiggableTile>())
                    {
                        //  Debug.Log("Found DiggableTile:" + h.collider.gameObject);
                        DiggableTile dt = h.collider.gameObject.GetComponent <DiggableTile>();
                        if (dt.isDiggable())
                        {
                            // Debug.Log("Its True:" + h.collider.gameObject);
                            if ((directionVector == Vector2.right || directionVector == Vector2.left))
                            {
                                if (!dt.isTopSoil())
                                {
                                    StartCoroutine(DigDelay(directionVector, dt));
                                    _MoveLocation.transform.position = h.collider.gameObject.transform.position;
                                    return(true);
                                }
                            }
                            else // up and down
                            {
                                StartCoroutine(DigDelay(directionVector, dt));
                                return(true);
                            }
                        }
                    }
                }
            }
        }

        if (directionVector == Vector2.up && _CurrentTunnelTile.isTopSoil() && _CurrentTunnelTile.isDiggable())
        {
            // Debug.Log("IsPassable Top tile:" +_CurrentTopTile);
            //go up to initial ground level
            this.transform.position          = new Vector3(this.transform.position.x, _YHeight, 0);
            _MoveLocation.transform.position = new Vector3(this.transform.position.x, _YHeight, 0);
            _InGround = false;
            _animator.SetBool("InGround", false);
        }



        return(false);
    }