public override void UpdateEntityInputs(float delta) { if (NoControl) return; var h = 0; var jump = false; var run = false; if (Input.IsActionPressed("ui_left")) h--; if (Input.IsActionPressed("ui_right")) h++; jump = Input.IsActionPressed("pl_jump"); run = Input.IsActionPressed("pl_run"); _inputs = new EntityInputs(jump, run, h); }
public override void UpdateEntityInputs(float delta) { if (Velocity.x < 0 && _hDir > 0) { _hDir = _ground ? -1 : 0; } if (Velocity.x > 0 && _hDir < 0) { _hDir = _ground ? 1 : 0; } _inputs = new EntityInputs(false, false, _hDir); }
public override void _PhysicsProcess(float delta) { UpdateEntityInputs(delta); Vector2 velocity = Velocity; var groundTest = MoveAndCollide(new Vector2(0, 0.1f), testOnly: true); if (!_ground || groundTest == null) { velocity.y += Gravity * delta; } float speedUp = (_inputs.r ? RunAccel : Accel) * delta; float maxSpeed = (_inputs.r ? MaxRunSpeed : MaxSpeed) * delta; switch (_inputs.h) { case -1: { if (velocity.x >= -maxSpeed) { if (velocity.x > 0) { velocity = ApplyFriction(velocity, delta); } velocity.x = Math.Max(-maxSpeed, velocity.x - speedUp); } else if (_ground) { velocity = ApplyFriction(velocity, delta); } break; } case 1: { if (velocity.x <= maxSpeed) { if (velocity.x < 0) { velocity = ApplyFriction(velocity, delta); } velocity.x = Math.Min(maxSpeed, velocity.x + speedUp); } else if (_ground) { velocity = ApplyFriction(velocity, delta); } break; } } if (_ground) { _jumpExpired = false; } if (_inputs.h == 0) { velocity = ApplyFriction(velocity, delta); } var forceJump = _forceJump > 0; if (forceJump || _inputs.j) { velocity = DoJump(forceJump, velocity, delta); } else { _jumpTime = 0; _jumpExpired = true; if (ResetJumpFinishJump) { ResetJump(); } } _forceJump -= delta; _floatTime += delta; foreach (var area in _collisions) { var g = GlobalPosition.x - area.GlobalPosition.x; velocity.x += Math.Min(20, Math.Max(-20, 1 / g * delta * 4000)); if (Math.Abs(g) <= 3) { if (g < 0) { velocity.x = Math.Min(velocity.x, 0); } else if (g > 0) { velocity.x = Math.Max(velocity.x, 0); } velocity.x += g * delta * 500; } } PreMove(velocity, delta); var add = new Vector2(0, _above.Count > 0 ? Math.Max(0, _above.Min(t => t.Velocity.y)) : 0); var newVel = MoveAndSlide(velocity + add, Vector2.Up, false, 2, 0.785398f, false) - add; if (newVel.y != 0 && newVel.y != velocity.y) { if (velocity.y < 0) { _jumpTime = MaxJumpTime + 1; } newVel.x = velocity.x; } if (Velocity.y < 0 && newVel.y == 0 && _inputs.j) { _jumpTime = MaxJumpTime + 1; } _ground = (_ground && newVel.y == 0) || (!_ground && Velocity.y > 0 && newVel.y == 0) || _above.Count > 0; Velocity = newVel; PostMove(newVel, delta); UpdateAnimation(delta); _prevInputs = _inputs; ResetStuff(delta); }