示例#1
0
        public void StateControl(ControlFlags controlFlags)
        {
            // If player is attacking or throwing, control of the player is disabled
            if (_characterComponents.CharacterFlags.IsAttacking ||
                _characterComponents.CharacterFlags.IsThrowing)
            {
                return;
            }

            #region Attack
            if (controlFlags.Attack &&
                !_characterComponents.CharacterFlags.IsAttacking &&
                !_characterComponents.CharacterFlags.IsThrowing)
            {
                // Check if player was sliding to put back main settings
                if (_characterComponents.CharacterFlags.WasSliding)
                {
                    // Set sliding flag to false
                    _characterComponents.CharacterFlags.WasSliding = false;
                    // Trigger slide event for animator state changes
                    _characterComponents.CharacterEvents.OnSlideEvent.Invoke(false);
                }
                // Manage gameObject Collider2Ds
                CharacterController2D.ManageCollider2Ds(_collider2DArrary, _characterComponents.AttackCapsuleCollider2D);
                // Set "is attacking" flag to true
                _characterComponents.CharacterFlags.IsAttacking = true;

                return;
            }
            #endregion
            #region Throw
            if (controlFlags.Throw &&
                !_characterComponents.CharacterFlags.IsAttacking &&
                !_characterComponents.CharacterFlags.IsThrowing)
            {
                // Check if player was sliding to put back main settings
                if (_characterComponents.CharacterFlags.WasSliding)
                {
                    // Set sliding flag to false
                    _characterComponents.CharacterFlags.WasSliding = false;
                    // Trigger slide event for animator state changes
                    _characterComponents.CharacterEvents.OnSlideEvent.Invoke(false);
                }
                // Manage gameObject Collider2Ds
                CharacterController2D.ManageCollider2Ds(_collider2DArrary, _characterComponents.ThrowCapsuleCollider2D);
                // Set "is throwing" flag to true
                _characterComponents.CharacterFlags.IsThrowing = true;

                return;
            }
            #endregion
            #region Slide
            if (controlFlags.Slide)
            {
                if (!_characterComponents.CharacterFlags.WasSliding)
                {
                    // Set sliding flag to true
                    _characterComponents.CharacterFlags.WasSliding = true;
                    // Trigger slide event for animator state changes
                    _characterComponents.CharacterEvents.OnSlideEvent.Invoke(true);
                    // Manage gameObject Collider2Ds
                    CharacterController2D.ManageCollider2Ds(_collider2DArrary, _characterComponents.SlideCapsuleCollider2D);

                    if (Mathf.Abs(_characterComponents.Rigidbody2D.velocity.x) > 0.01f)
                    {
                        // Set slide force direction
                        float l_slideForceDirection = _characterComponents.CharacterFlags.IsFacingRight ? 1.0f : -1.0f;
                        // Apply slide force to rigid body
                        _characterComponents
                        .Rigidbody2D
                        .AddForce(new Vector2(l_slideForceDirection * _characterComponents.CharacterParams.SlideForce, 0.0f));
                    }
                }
            }
            #endregion
            #region Not slide
            else
            {
                if (_characterComponents.CharacterFlags.WasSliding)
                {
                    // Set sliding flag to false
                    _characterComponents.CharacterFlags.WasSliding = false;
                    // Trigger slide event for animator state changes
                    _characterComponents.CharacterEvents.OnSlideEvent.Invoke(false);
                }
                // Manage gameObject Collider2Ds
                CharacterController2D.ManageCollider2Ds(_collider2DArrary, _characterComponents.MainCapsuleCollider2D);
                // Apply run speed and fixed delta time to move parameter
                float move = controlFlags.HorizontalMove * _characterComponents.CharacterParams.RunSpeed * Time.fixedDeltaTime;
                // Move the character by finding the target velocity
                Vector3 l_targetVelocity = new Vector2(move * 10.0f, _characterComponents.Rigidbody2D.velocity.y);
                // And then smoothing it out and applying it to the character
                _characterComponents.Rigidbody2D.velocity =
                    Vector3
                    .SmoothDamp(_characterComponents.Rigidbody2D.velocity,
                                l_targetVelocity,
                                ref _velocity,
                                _characterComponents.CharacterParams.MovementSmoothing);
                // Manage player facing direction and relevant components
                CharacterController2D.ManagePlayerFacing(_characterComponents, _collider2DArrary, move);
            }
            #endregion
            #region Player jump
            if (controlFlags.Jump)
            {
                // Check if player was sliding to put back main settings
                if (_characterComponents.CharacterFlags.WasSliding)
                {
                    // Set sliding flag to false
                    _characterComponents.CharacterFlags.WasSliding = false;
                    // Trigger slide event for animator state changes
                    _characterComponents.CharacterEvents.OnSlideEvent.Invoke(false);
                    // Manage gameObject Collider2Ds
                    CharacterController2D.ManageCollider2Ds(_collider2DArrary, _characterComponents.MainCapsuleCollider2D);
                }
                // Add a vertical force to the player
                _characterComponents.Rigidbody2D.AddForce(new Vector2(0.0f, _characterComponents.CharacterParams.JumpForce));
            }
            #endregion
        }
示例#2
0
        public void StateControl(ControlFlags controlFlags)
        {
            #region Not grounded
            // If air control is enabled
            if (_characterComponents.CharacterParams.AirControl)
            {
                float l_move = 0.0f;

                // Give the player the possibility to decrease the speed in mid air
                if (Math.Sign(_characterComponents.Rigidbody2D.velocity.x) != Math.Sign(controlFlags.HorizontalMove) &&
                    Math.Abs(controlFlags.HorizontalMove) > 0.1f)
                {
                    l_move = Math.Sign(_characterComponents.Rigidbody2D.velocity.x) * _characterComponents.CharacterParams.JumpControlTargetHorizontalSpeed;

                    _characterComponents.Rigidbody2D.velocity =
                        Vector3.SmoothDamp(
                            _characterComponents.Rigidbody2D.velocity,
                            new Vector3(l_move, _characterComponents.Rigidbody2D.velocity.y, 0.0f),
                            ref _velocity,
                            _characterComponents.CharacterParams.JumpControlMovementSmoothing);
                }

                // Give the player a little bit of control in mid air
                if (controlFlags.HorizontalMove != 0.0f)
                {
                    if (Mathf.Abs(_characterComponents.Rigidbody2D.velocity.x) < _characterComponents.CharacterParams.JumpControlTargetHorizontalSpeed)
                    {
                        l_move = controlFlags.HorizontalMove * _characterComponents.CharacterParams.JumpControlTargetHorizontalSpeed;

                        _characterComponents.Rigidbody2D.velocity =
                            Vector3.SmoothDamp(
                                _characterComponents.Rigidbody2D.velocity,
                                new Vector3(l_move, _characterComponents.Rigidbody2D.velocity.y, 0.0f),
                                ref _velocity,
                                _characterComponents.CharacterParams.JumpControlMovementSmoothing);
                    }
                }
            }

            #region Double jump
            if (controlFlags.Jump && !_characterComponents.CharacterFlags.HasDoubleJumped && !_characterComponents.CharacterFlags.WasGliding)
            {
                // Put double jump flag to true
                _characterComponents.CharacterFlags.HasDoubleJumped = true;
                // Trigger double jump event for animator state changes
                _characterComponents.CharacterEvents.OnDoubleJumpEvent.Invoke();
                // Zero out y velocity before applying jump force
                _characterComponents.Rigidbody2D.velocity = new Vector2(_characterComponents.Rigidbody2D.velocity.x, 0.0f);
                // Add a vertical force to the player
                _characterComponents.Rigidbody2D.AddForce(new Vector2(0.0f, _characterComponents.CharacterParams.JumpForce));
            }
            #endregion

            #region Glide
            if (controlFlags.Glide)
            {
                if (!_characterComponents.CharacterFlags.WasGliding)
                {
                    // Zero out y velocity before applying glide
                    _characterComponents.Rigidbody2D.velocity = new Vector2(_characterComponents.Rigidbody2D.velocity.x, 0.0f);
                    // Manage gameObject Collider2Ds
                    CharacterController2D.ManageCollider2Ds(_collider2DArrary, _characterComponents.GlideCapsuleCollider2D);
                }
                // Put glide flag to true
                _characterComponents.CharacterFlags.WasGliding = true;
                // Trigger glide event for animator state changes
                _characterComponents.CharacterEvents.OnGlideEvent.Invoke(true);

                // Gliding horizontal movement control
                float l_move = controlFlags.HorizontalMove * _characterComponents.CharacterParams.GlideHorizontalSpeed;
                // Apply smooth transition to player glide horizontal velocity
                _characterComponents.Rigidbody2D.velocity =
                    Vector3.SmoothDamp(
                        _characterComponents.Rigidbody2D.velocity,
                        new Vector3(l_move, _characterComponents.Rigidbody2D.velocity.y, 0.0f),
                        ref _velocity,
                        _characterComponents.CharacterParams.GlideMovementSmoothing);
                // Manage player facing direction and relevant components
                CharacterController2D.ManagePlayerFacing(_characterComponents, _collider2DArrary, l_move);

                #region GIZ
                // If player is in a glide impulse zone let the area effector apply any forces
                if (_characterComponents.CharacterFlags.IsInGlideImpulseZone)
                {
                    // Limit player glide vertical velocity up
                    if (_characterComponents.Rigidbody2D.velocity.y >= _characterComponents.CharacterParams.GlideVerticalSpeedUp)
                    {
                        _characterComponents.Rigidbody2D.velocity =
                            new Vector2(
                                _characterComponents.Rigidbody2D.velocity.x,
                                _characterComponents.CharacterParams.GlideVerticalSpeedUp);
                    }

                    return;
                }
                #endregion

                // Apply smooth transition to player glide vertical velocity down
                _characterComponents.Rigidbody2D.velocity =
                    Vector3
                    .SmoothDamp(_characterComponents.Rigidbody2D.velocity,
                                new Vector3(_characterComponents.Rigidbody2D.velocity.x, _characterComponents.CharacterParams.GlideVerticalSpeedDown, 0.0f),
                                ref _velocity,
                                _characterComponents.CharacterParams.MovementSmoothing);
            }
            else
            {
                // Put glide flag to false
                _characterComponents.CharacterFlags.WasGliding = false;
                // Manage gameObject Collider2Ds
                CharacterController2D.ManageCollider2Ds(_collider2DArrary, _characterComponents.MainCapsuleCollider2D);
                // Trigger glide event for animator state changes
                _characterComponents.CharacterEvents.OnGlideEvent.Invoke(false);
            }
            #endregion
            #endregion
        }