Beispiel #1
0
    /**
     * Let the given player pick up this object.
     */
    public void Pickup(PlayerController carrier)
    {
        if (!IsBeingCarried)
        {
            // A player who is already carrying something can not pickup something else
            if (!carrier.IsCarryingSomething)
            {
                _carrier       = carrier;
                IsBeingCarried = true;

                foreach (var interactive in GetComponents <Interactive>())
                {
                    interactive.enabled = false;
                }

                // Cache original layer and change layer to "Carried", see description of _cachedLayer field for explanation
                _cachedLayer          = this.gameObject.layer;
                this.gameObject.layer = LayerMask.NameToLayer("Carried");

                // Cache original mass and change mass to 0, see description of _cachedMass field for explanation
                _cachedMass = _physicsEffects.rigidbody2D.mass;
                _physicsEffects.rigidbody2D.mass = 0;

                _hingeJoint2D.connectedBody = _carrier.Rigidbody2D;
                Physics2D.IgnoreCollision(_collider, _carrier.Collider);
                // temporally change sorting order to draw carried gameobject on top
                _renderer.sortingOrder++;
                _physicsEffects.Teleport(_carrier.CarryPosition);

                _carrier.InitCarryingState(this);
                _carrier.Input.EnableButtons((Button.Throw, true));

                // While being thrown, we might loose contact to safe terrain, so we register this behavior as terrain unsafe
                // for respawning
                if (TryGetComponent <Spawnable>(out var spawnable))
                {
                    spawnable.RegisterTouchingUnsafeTerrain(this);
                }

                OnPickedUp?.Invoke(_carrier);

                StartCoroutine(PickUpCoroutine());
            }
        }

        else
        {
            Debug.LogWarning("Tried to pick up object, which is already being carried.");
        }
    }
    /**
     * Call this to let the player fall
     * (play animation, sound and let player die)
     *
     * This method is for example used by the `Chasm` class
     */
    public void InitiateFall()
    {
        if (_playerState != PlayerState.falling && _playerState != PlayerState.thrown)
        {
            // While falling, the player is probably not on safe terrain, so we tell Spawnable to not safe any respawn
            // positions
            // (since we change layers during falling, Chasm may not register as unsafe terrain while falling)
            spawnable.RegisterTouchingUnsafeTerrain(this);

            _playerState = PlayerState.falling;
            // If the player is being moved by a platform, they should no longer be moved when falling
            PhysicsEffects.RemoveCustomOrigin();
            // We place the player at the center of its collider for the falling animation to get a position somewhat
            // centered around the point where the player entered the chasm
            PhysicsEffects.Teleport(Collider.bounds.center);

            Animator.SetTrigger(FallTrigger);
        }
    }