Improved platforming jump physics and added music autoplay feature. Added joystick support. Added dimmed background tiling.
Release / Export Game (push) Successful in 2m22s
Release / Export Game (push) Successful in 2m22s
This commit is contained in:
@@ -4,20 +4,16 @@ using System.Diagnostics;
|
||||
|
||||
public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
[Export]
|
||||
public float FloatingMovePenalty { get; set; } = 0.1f;
|
||||
public float FloatingWalkSpeed { get; set; } = 1f;
|
||||
[Export]
|
||||
public float FloatingJumpPenalty { get; set; } = 0.1f;
|
||||
public float RunMultiplier { get; set; } = 1.84f;
|
||||
[Export]
|
||||
public float JumpStrengthDecay { get; set; } = 2;
|
||||
[Export]
|
||||
public float MovementDecay { get; set; } = 2;
|
||||
[Export]
|
||||
public float MovementScale { get; set; } = 10;
|
||||
[Export]
|
||||
public float JumpScale { get; set; } = 12;
|
||||
public float WalkSpeed { get; set; } = 3f;
|
||||
|
||||
[Export]
|
||||
public float FrictionCoefficent { get; set; } = 0.1f;
|
||||
public float JumpHeight { get; set { field = value; RecalculateLv(); } } = 5.5f;
|
||||
|
||||
private float PeakTime { get; set { field = value; RecalculateLv(); } } = 0.1f;
|
||||
|
||||
[Export]
|
||||
public Label DebugTextLabel { get; set; }
|
||||
@@ -26,11 +22,27 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
public AnimatedSprite2D Sprite { get; set; }
|
||||
|
||||
[Export]
|
||||
public float Gravity { get; set; } = 980f;
|
||||
public float Gravity { get; set { field = value; RecalculateLv(); } } = 30f;
|
||||
|
||||
private float _timeInFlight = 0;
|
||||
[Export]
|
||||
public float UnitToPx { get; set; } = 32;
|
||||
|
||||
private float _lv = 0.0f;
|
||||
private float _invmass = 1/2f;
|
||||
[Export]
|
||||
public float Mass { get => 1/_invmass; set => _invmass = 1/value; }
|
||||
|
||||
private float epsilon = 10e-5f;
|
||||
|
||||
private void RecalculateLv() {
|
||||
_lv = Mathf.Sqrt(Mathf.Pow(Gravity * PeakTime / 2, 2) + 2 * Gravity * JumpHeight);
|
||||
}
|
||||
|
||||
public override void _Ready() {
|
||||
RecalculateLv();
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
|
||||
public override void _Process(double delta) {
|
||||
var grounded = this.IsOnFloor();
|
||||
@@ -48,38 +60,61 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
}
|
||||
}
|
||||
|
||||
private float _timeOffGround = 0.0f;
|
||||
|
||||
[Export]
|
||||
public float CoyoteTiming { get; set; } = 0.1f;
|
||||
|
||||
|
||||
public override void _PhysicsProcess(double delta) {
|
||||
var xAxis = Input.GetAxis("move_left", "move_right") * MovementScale;
|
||||
var xAxis = Input.GetAxis("move_left", "move_right");
|
||||
var yAxis = Input.GetAxis("look_up", "look_down");
|
||||
var jumpStrength = -Input.GetActionStrength("jump") * JumpScale;
|
||||
// var jumpStrength = -Input.GetActionStrength("jump");
|
||||
|
||||
var lastCollision = this.IsOnCeiling();
|
||||
RecalculateLv();
|
||||
|
||||
float velx = 0f;
|
||||
float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta;
|
||||
|
||||
var grounded = this.IsOnFloor();
|
||||
if (!grounded) {
|
||||
xAxis *= FloatingMovePenalty;
|
||||
jumpStrength *= FloatingJumpPenalty;
|
||||
var coyoteGrounded = _timeOffGround < CoyoteTiming;
|
||||
if (grounded || coyoteGrounded) {
|
||||
if (grounded) _timeOffGround = 0;
|
||||
else _timeOffGround += (float)delta;
|
||||
|
||||
if (Input.IsActionJustPressed("jump")) {
|
||||
vely = -_lv;
|
||||
}
|
||||
|
||||
if (Input.IsActionPressed("run")) {
|
||||
velx = WalkSpeed * RunMultiplier;
|
||||
} else {
|
||||
velx = WalkSpeed;
|
||||
}
|
||||
} else {
|
||||
if (Input.IsActionJustReleased("jump") && vely < 0) {
|
||||
vely *= 1/4f;
|
||||
}
|
||||
|
||||
if (Input.IsActionPressed("run")) {
|
||||
velx = FloatingWalkSpeed * RunMultiplier;
|
||||
} else {
|
||||
velx = FloatingWalkSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
if (grounded && jumpStrength < 0)
|
||||
_timeInFlight = 0;
|
||||
|
||||
jumpStrength *= Mathf.Exp(-JumpStrengthDecay * _timeInFlight);
|
||||
|
||||
// autorun
|
||||
var friction = Mathf.Abs(Velocity.X) > 125 ? FrictionCoefficent * 1/8f : FrictionCoefficent;
|
||||
// var decay = Mathf.Abs(Velocity.X) > 125 ? MovementDecay * 1/4f : MovementDecay;
|
||||
xAxis *= Mathf.Exp(-MovementDecay * Mathf.Abs(Velocity.X));
|
||||
xAxis -= Velocity.X * friction;
|
||||
velx *= xAxis;
|
||||
|
||||
DebugTextLabel.Text =
|
||||
$"grounded: {grounded}\n" +
|
||||
$"vely: {vely}\n" +
|
||||
$"velx: {velx}\n" +
|
||||
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n" +
|
||||
$"tg : {_timeOffGround}";
|
||||
// Force = new Vector2(0, forcey);
|
||||
Velocity = new Vector2(velx, vely) * UnitToPx;
|
||||
|
||||
var moveVector = new Vector2((float)(delta * xAxis), (float)( delta * (Gravity + jumpStrength)));
|
||||
DebugTextLabel.Text = $"grounded: {grounded}\nmv: {moveVector}\n vel: {Velocity}\n jumpStrength: {jumpStrength}\ntimeInFlight: {_timeInFlight}";
|
||||
Velocity += moveVector;
|
||||
MoveAndSlide();
|
||||
|
||||
if (!grounded)
|
||||
_timeInFlight += (float)delta;
|
||||
base._PhysicsProcess(delta);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user