IEnumerator <float> JumpToBed()
    {
        var anim = GameLibOfMethods.animator;

        anim.Play("PrepareToSleep");
        GameLibOfMethods.animator.SetBool("Sleeping", true);
        PlayerAnimationHelper.StopPlayer();
        SpriteControler.Instance.FaceDOWN();

        yield return(0f);


        PlayerCommands.LastPositionBeforeJump = GameLibOfMethods.player.transform.position;

        while (true)
        {
            if (!anim.GetCurrentAnimatorStateInfo(0).IsName("PrepareToSleep"))
            {
                Debug.Log("Something is wrong.. this finished before firing the method. Make sure you don't call this from FixedUpdate.");
                break;
            }

            if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 80 / 120f)
            {
                break;
            }

            yield return(0f);
        }

        Physics2D.IgnoreLayerCollision(GameLibOfMethods.player.layer, 10, true);

        while (true)
        {
            Vector3 temp = new Vector3(CharacterPosition.position.x, CharacterPosition.position.y, 1);

            GameLibOfMethods.player.transform.position = Vector3.MoveTowards(GameLibOfMethods.player.transform.position, temp, 3 * Time.deltaTime);
            if (GameLibOfMethods.player.transform.position.y == CharacterPosition.position.y)
            {
                break;
            }
            yield return(0f);
        }

        yield return(0f);

        Use();
    }
Beispiel #2
0
        IEnumerator <float> WalkTo(Vector3 Position, float StartingSpeedPercentage, float MaxSpeed, System.Action Callback)
        {
            Position = new Vector3(Position.x, Position.y, 1);
            PlayerAnimationHelper.StopPlayer();
            //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
            RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
            //IgnoreAllCollisionExcept(true, 31, 9, 10);
            col.isTrigger = true;

            float   minSqrDist        = 0;
            Vector3 pathFindingCenter = Vector3.zero;

            if (currentTarget != null)
            {
                minSqrDist        = Mathf.Pow(currentTarget.interactionOptions.InteractionRange, 2);
                pathFindingCenter = currentTarget.gameObject.transform.position + (Vector3)currentTarget.interactionOptions.InteractionCenterOffset;
            }

            yield return(0f);

            if (path.Count != 0)
            {
                anim.SetBool("Walking", true);
            }
            MaxSpeed *= Stats.MoveSpeed;

            float _spd  = StartingSpeedPercentage * MaxSpeed;
            int   index = 0;

            while (this)
            {
                if (index >= path.Count)
                {
                    break;
                }

                if (!CanWalkAt(path[index]))
                {
                    yield return(0f);

                    //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
                    RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
                    index = 0;
                    continue;
                }

                Vector3 currentPos = rb.position;
                Vector3 targetPos  = grid.PosFromNode(path[index]);

                var offset = new Vector3(targetPos.x - currentPos.x, targetPos.y - currentPos.y, 1);
                if (_spd != MaxSpeed)
                {
                    _spd = Mathf.MoveTowards(_spd, MaxSpeed, 10 * MaxSpeed * GameTime.Time.fixedDeltaTime);
                }
                float currentSpeed = _spd * GameTime.Time.fixedDeltaTime;

                var posAfterMovement = Vector3.MoveTowards(currentPos, targetPos, currentSpeed);

                // Update the index and increase the movement point without losing a frame of movement.
                if (posAfterMovement == targetPos)
                {
                    index++;

                    if (index >= path.Count)
                    {
                    }
                    else
                    {
                        targetPos = new Vector3(grid.PosFromNode(path[index]).x, grid.PosFromNode(path[index]).y, 1);
                        offset    = new Vector3(targetPos.x - currentPos.x, targetPos.y - currentPos.y, 1);

                        posAfterMovement = Vector3.MoveTowards(currentPos, targetPos, currentSpeed);
                    }
                }

                /// Check if we've reached the <see cref="Objects.Functionality.InteractionOptions.InteractionRange"/>.
                if (currentTarget != null)
                {
                    float sqrDistanceToTarget = Vector2.SqrMagnitude(posAfterMovement - pathFindingCenter);
                    if (sqrDistanceToTarget <= minSqrDist)
                    {
                        break;
                    }
                }

                rb.MovePosition(posAfterMovement);

                if (index >= path.Count)
                {
                    break;
                }

                CalculateFacing(offset);

                yield return(0f);
            }

            if (!this)
            {
                yield break;
            }

            anim.SetBool("Walking", false);
            yield return(0f);

            col.isTrigger = false;
            PlayerAnimationHelper.ResetPlayer();
            currentHandle = null;
            currentTarget = null;

            if (Callback != null)
            {
                Callback();
            }
        }
        IEnumerator <float> WalkTo(Vector3 Position, float StartingSpeedPercentage, float MaxSpeed, System.Action Callback)
        {
            Position = new Vector3(Position.x, Position.y, 1);
            PlayerAnimationHelper.StopPlayer();
            //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
            RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
            //IgnoreAllCollisionExcept(true, 31, 9, 10);
            col.isTrigger = true;

            yield return(0f);

            if (path.Count != 0)
            {
                anim.SetBool("Walking", true);
            }
            MaxSpeed *= Stats.MoveSpeed;

            float _spd  = StartingSpeedPercentage * MaxSpeed;
            int   index = 0;

            while (this)
            {
                if (index >= path.Count)
                {
                    break;
                }

                if (!CanWalkAt(path[index]))
                {
                    yield return(0f);

                    //RequestPath.GetPath(rb.position, Position, path, Resolution.High);
                    RequestPath.GetPath_Avoidance(rb.position, Position, path, Resolution.High, col);
                    index = 0;
                    continue;
                }

                Vector3 targetPos = grid.PosFromNode(path[index]);

                Vector3 offset = new Vector3(targetPos.x - rb.position.x, targetPos.y - rb.position.y, 1);
                if (_spd != MaxSpeed)
                {
                    _spd = Mathf.MoveTowards(_spd, MaxSpeed, 10 * MaxSpeed * Time.fixedDeltaTime);
                }
                float currentSpeed = _spd * Time.fixedDeltaTime;

                var posAfterMovement = Vector3.MoveTowards(rb.position, targetPos, currentSpeed);

                if (posAfterMovement == targetPos)
                {
                    index++;

                    if (index >= path.Count)
                    {
                    }
                    else
                    {
                        targetPos = new Vector3(grid.PosFromNode(path[index]).x, grid.PosFromNode(path[index]).y, 1);
                        offset    = new Vector3(targetPos.x - rb.position.x, targetPos.y - rb.position.y, 1);

                        posAfterMovement = Vector3.MoveTowards(rb.position, targetPos, currentSpeed);
                    }
                }

                rb.MovePosition(posAfterMovement);

                if (index >= path.Count)
                {
                    break;
                }

                CalculateFacing(offset);

                yield return(0f);
            }

            if (!this)
            {
                yield break;
            }

            anim.SetBool("Walking", false);
            yield return(0f);

            GameLibOfMethods.player.GetComponent <Collider2D>().isTrigger = false;
            PlayerAnimationHelper.ResetPlayer();
            currentHandle = null;

            if (Callback != null)
            {
                Callback();
            }
        }
Beispiel #4
0
        IEnumerator <float> JumpToBed()
        {
            Player.anim.Play("PrepareToSleep");
            Player.anim.SetBool("Sleeping", true);
            PlayerAnimationHelper.StopPlayer();
            SpriteControler.Instance.FaceDOWN();

            yield return(0f);

            PlayerCommands.LastPositionBeforeJump = GameLibOfMethods.player.transform.position;
            //SpriteControler.Instance.ChangeSortingOrder(1);
            GameLibOfMethods.player.transform.parent.transform.parent = transform;
            while (true)
            {
                var state = Player.anim.GetCurrentAnimatorStateInfo(0);
                if (!state.IsName("PrepareToSleep"))
                {
                    Debug.Log("Something is wrong.. this finished before firing the method. Make sure you don't call this from FixedUpdate.");
                    break;
                }

                if (Player.anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 62 / 120f)
                {
                    break;
                }

                yield return(0f);
            }


            Player.col.enabled = false;


            Vector2 targetPos  = useableFunctionality.PlayerStandPosition;
            Vector2 currentPos = GameLibOfMethods.player.transform.position;

            while (true)
            {
                currentPos = Vector2.MoveTowards(currentPos, targetPos, .8f * GameTime.Time.deltaTime);
                GameLibOfMethods.player.transform.position = currentPos;
                if (currentPos == targetPos)
                {
                    break;
                }
                yield return(0f);
            }

            yield return(0f);

            if (Random.Range(0f, 1f) > 0.5f)
            {
                PlayerBuff.Add <DeepSleep>();
            }

            GameLibOfMethods.isSleeping = true;
            GameLibOfMethods.cantMove   = true;
            HandlePlayerSprites(enable: false);
            StartCoroutine(SleepSpeedChangeDelay());
            //Covers.sortingOrder = 2;
            useState = ObjectUseState.InUse;
            CurrentAction.BeginAction();
        }