예제 #1
0
        public void Update(Entity entity, Level level, float deltaTime)
        {
            if (!level.PhysicsWorld.TryGetBody(entity.BodyID, out Body? body))
            {
                return;
            }

            if (level.EntityWorld.TryGetEntity(_torchEntityID, out Entity? torchEntity))
            {
                if (torchEntity.IsPutOut)
                {
                    entity.HasLostAllHope = true;

                    body.Velocity = body.Velocity.SetX(0f);
                    return;
                }
            }

            float speedModifier = 1f;

            float waterHeight = level.TileMap.Height * PhysicsConstants.TileSize - level.WaterLevel;

            if (body.Position.Y + body.Bounds.Center.Y > waterHeight)
            {
                body.Position = body.Position.SetY(waterHeight - body.Bounds.Center.Y);

                if (body.Velocity.Y > 0f)
                {
                    body.Velocity = body.Velocity.SetY(0f);
                }

                speedModifier = 0.5f;
                _jumpsLeft    = _maxJumps;
            }

            float speed = body.Velocity.Length();

            body.IgnoresPlatforms = _bindings.IsPressed(Bindings.Drop);

            if (_graceTimer > 0f)
            {
                _graceTimer -= deltaTime;
            }

            if (speed < entity.DangerSpeed && entity.DashTimer <= 0f)
            {
                if (_bindings.IsPressed(Bindings.MoveRight))
                {
                    body.Velocity = body.Velocity.SetX(_movementSpeed * speedModifier);
                }
                if (_bindings.IsPressed(Bindings.MoveLeft))
                {
                    body.Velocity = body.Velocity.SetX(-_movementSpeed * speedModifier);
                }

                if (!_bindings.IsPressed(Bindings.MoveRight) && !_bindings.IsPressed(Bindings.MoveLeft))
                {
                    body.Velocity = body.Velocity.SetX(0f);
                }
            }

            if (body.Contact.Y > 0f)
            {
                _jumpsLeft  = _maxJumps;
                _graceTimer = _gracePeriod;
            }

            if (_dashCooldownTimer > 0f)
            {
                _dashCooldownTimer -= deltaTime;
            }

            if (entity.DashTimer <= 0f)
            {
                _hasBumped = false;

                if (_bindings.JustPressed(Bindings.Dash) && _jumpsLeft > 0 && _dashCooldownTimer <= 0f)
                {
                    _dashDir = new Vector2();
                    if (_bindings.IsPressed(Bindings.MoveRight))
                    {
                        _dashDir.X++;
                    }
                    if (_bindings.IsPressed(Bindings.MoveLeft))
                    {
                        _dashDir.X--;
                    }

                    if (_dashDir.X != 0f)
                    {
                        entity.DashTimer = _dashTime;

                        _jumpsLeft--;

                        _dashCooldownTimer = _dashCooldown;
                    }
                }
            }

            if (entity.KickTimer <= 0f)
            {
                _hasKicked = false;
            }

            if (_bindings.JustPressed(Bindings.Jump) && _jumpsLeft > 0)
            {
                body.Velocity = body.Velocity.SetY(0f);
                body.Impulse += new Vector2(0f, -_jumpImpulse * Math.Min(Math.Max(_jumpTime - _jumpTimer, 0f), deltaTime) / _jumpTime);

                _jumpTimer = deltaTime;
                _isJumping = true;
                _jumpsLeft--;

                BeginKick(entity, body, level);
            }

            if (_bindings.IsPressed(Bindings.Jump) && _isJumping && _jumpTimer < _jumpTime)
            {
                body.Impulse += new Vector2(0f, -_jumpImpulse * Math.Min(Math.Max(_jumpTime - _jumpTimer, 0f), deltaTime) / _jumpTime);

                _jumpTimer += deltaTime;
            }

            if (_jumpTimer >= _jumpTime)
            {
                _isJumping = false;
            }

            if (!_bindings.IsPressed(Bindings.Jump))
            {
                if (body.Velocity.Y < 0f)
                {
                    body.Velocity = body.Velocity.SetY(0f);
                }
                _jumpTimer = 0f;
            }

            if (entity.DashTimer > 0f)
            {
                entity.DashTimer -= deltaTime;

                body.Velocity = _dashDir * _dashSpeed;

                BumpTorch(entity, body, level);
            }

            if (entity.KickTimer > 0f)
            {
                entity.KickTimer -= deltaTime;

                KickTorch(entity, body, level);
            }
        }