This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -3,12 +3,14 @@ using System;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
public partial class PlayerPhysicsController : CharacterBody2D {
|
public partial class PlayerPhysicsController : CharacterBody2D {
|
||||||
[Export]
|
// [Export]
|
||||||
public float FloatingWalkAcceleration { get; set; } = 1f;
|
// public float RunMultiplier { get; set; } = 1.84f;
|
||||||
[Export]
|
|
||||||
public float RunMultiplier { get; set; } = 1.84f;
|
|
||||||
[Export]
|
[Export]
|
||||||
public float WalkAcceleration { get; set; } = 3f;
|
public float WalkAcceleration { get; set; } = 3f;
|
||||||
|
[Export]
|
||||||
|
public float StoppingDeceleration { get; set; } = 8f;
|
||||||
|
[Export]
|
||||||
|
public float CancellationAcceleration { get; set; } = 4f;
|
||||||
|
|
||||||
[Export]
|
[Export]
|
||||||
public float JumpHeight { get; set; } = 5.5f;
|
public float JumpHeight { get; set; } = 5.5f;
|
||||||
@@ -16,15 +18,15 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
|||||||
private float PeakTime { get; set; } = 0.1f;
|
private float PeakTime { get; set; } = 0.1f;
|
||||||
|
|
||||||
[Export]
|
[Export]
|
||||||
public float DragArea { get; set; } = 4f;
|
private float MaxFallSpeed { get; set; } = 8f;
|
||||||
|
|
||||||
[Export]
|
[Export]
|
||||||
public float AirDensity { get; set; } = 37.26982f;
|
private float MaxRiseSpeed { get; set; } = 16f;
|
||||||
|
|
||||||
[Export]
|
[Export]
|
||||||
public float DragCoefficient { get; set; } = 1.3f;
|
private float MaxRunSpeed { get; set; } = 16f;
|
||||||
[Export]
|
[Export]
|
||||||
public float FloatingDragCoefficientMultiplier { get; set; } = 0.8f;
|
private float MaxWalkSpeed { get; set; } = 8f;
|
||||||
|
|
||||||
[Export]
|
[Export]
|
||||||
public Label DebugTextLabel { get; set; }
|
public Label DebugTextLabel { get; set; }
|
||||||
@@ -42,33 +44,17 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
|||||||
[Export]
|
[Export]
|
||||||
public float UnitToPx { get; set; } = 32;
|
public float UnitToPx { get; set; } = 32;
|
||||||
|
|
||||||
private float _lv = 0.0f;
|
private float _timeOffGround = 0.0f;
|
||||||
private float _invmass = 1/2f;
|
|
||||||
[Export]
|
|
||||||
public float Mass { get => 1/_invmass; set => _invmass = 1/value; }
|
|
||||||
|
|
||||||
private float epsilon = 10e-5f;
|
[Export]
|
||||||
|
public float CoyoteTiming { get; set; } = 0.1f;
|
||||||
|
|
||||||
|
private float _lv = 0.0f;
|
||||||
|
|
||||||
private void RecalculateLv() {
|
private void RecalculateLv() {
|
||||||
_lv = Mathf.Sqrt(Mathf.Pow(Gravity * PeakTime / 2, 2) + 2 * Gravity * JumpHeight);
|
_lv = Mathf.Sqrt(Mathf.Pow(Gravity * PeakTime / 2, 2) + 2 * Gravity * JumpHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
private float CalculateDrag(float velocity, float delta) {
|
|
||||||
var minopposing = Mathf.Abs(velocity * 0.99f);
|
|
||||||
return Mathf.Clamp(
|
|
||||||
0.5f *
|
|
||||||
(this.IsOnFloor() ? DragCoefficient : DragCoefficient * FloatingDragCoefficientMultiplier) *
|
|
||||||
(Mathf.Pow(TilesToMeters, 3) * AirDensity) *
|
|
||||||
(Mathf.Pow(TilesToMeters, 2) * DragArea) *
|
|
||||||
Mathf.Pow(velocity * PxToMeters, 2) *
|
|
||||||
_invmass *
|
|
||||||
-Mathf.Sign(velocity) *
|
|
||||||
delta *
|
|
||||||
(1f / TilesToMeters),
|
|
||||||
-minopposing,
|
|
||||||
minopposing);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void _Ready() {
|
public override void _Ready() {
|
||||||
RecalculateLv();
|
RecalculateLv();
|
||||||
base._Ready();
|
base._Ready();
|
||||||
@@ -88,12 +74,16 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
|||||||
< 300 => 4,
|
< 300 => 4,
|
||||||
_ => 1
|
_ => 1
|
||||||
};
|
};
|
||||||
|
if (!Sprite.IsPlaying())
|
||||||
|
Sprite.Play();
|
||||||
} else {
|
} else {
|
||||||
Sprite.SpeedScale = 1;
|
Sprite.SpeedScale = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (grounded && absvel.X < 0.1) {
|
if (grounded && absvel.X < 0.1) {
|
||||||
Sprite.Animation = "idle";
|
Sprite.Animation = "idle";
|
||||||
|
if (!Sprite.IsPlaying())
|
||||||
|
Sprite.Play();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!grounded && Velocity.Y > 0.1) {
|
if (!grounded && Velocity.Y > 0.1) {
|
||||||
@@ -105,11 +95,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private float _timeOffGround = 0.0f;
|
|
||||||
|
|
||||||
[Export]
|
[Export]
|
||||||
public float CoyoteTiming { get; set; } = 0.1f;
|
public float Deadzone { get; set; } = 0.1f;
|
||||||
|
|
||||||
|
|
||||||
public override void _PhysicsProcess(double delta) {
|
public override void _PhysicsProcess(double delta) {
|
||||||
var xAxis = Input.GetAxis("move_left", "move_right");
|
var xAxis = Input.GetAxis("move_left", "move_right");
|
||||||
@@ -119,10 +106,10 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
|||||||
var lastCollision = this.IsOnCeiling();
|
var lastCollision = this.IsOnCeiling();
|
||||||
RecalculateLv();
|
RecalculateLv();
|
||||||
|
|
||||||
float velx = (Velocity.X / UnitToPx) + CalculateDrag(Velocity.X, (float)delta);
|
float velx = (Velocity.X / UnitToPx);
|
||||||
float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta + CalculateDrag(Velocity.Y, (float)delta);
|
float vely = (Velocity.Y / UnitToPx) + Gravity * (float)delta;
|
||||||
|
|
||||||
if (Mathf.Abs(velx) < 0.3) velx = 0;
|
if (Mathf.Abs(velx) < 0.1) velx = 0;
|
||||||
|
|
||||||
var grounded = this.IsOnFloor();
|
var grounded = this.IsOnFloor();
|
||||||
var coyoteGrounded = _timeOffGround < CoyoteTiming;
|
var coyoteGrounded = _timeOffGround < CoyoteTiming;
|
||||||
@@ -138,32 +125,40 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
|||||||
vely *= 1/4f;
|
vely *= 1/4f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input.IsActionPressed("run")) {
|
var accel = WalkAcceleration;
|
||||||
velx += xAxis * WalkAcceleration * (float)delta * RunMultiplier;
|
if (xAxis * Velocity.X < 0) accel = StoppingDeceleration;
|
||||||
} else {
|
velx += xAxis * accel * (float)delta;
|
||||||
velx += xAxis * WalkAcceleration * (float)delta;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (Input.IsActionJustReleased("jump") && vely < 0) {
|
if (Input.IsActionJustReleased("jump") && vely < 0) {
|
||||||
vely *= 1/4f;
|
vely *= 1/4f;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (Input.IsActionPressed("run")) {
|
if (Mathf.Abs(xAxis) < Deadzone) {
|
||||||
velx += xAxis * FloatingWalkAcceleration * (float)delta * RunMultiplier;
|
var autostop = -Mathf.Sign(velx) * StoppingDeceleration * (float)delta;
|
||||||
} else {
|
if (Mathf.Abs(autostop) > Mathf.Abs(velx)) velx = 0f;
|
||||||
velx += xAxis * FloatingWalkAcceleration * (float)delta;
|
else velx += autostop;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var maxXSpeed = Input.IsActionPressed("run") ? MaxRunSpeed : MaxWalkSpeed;
|
||||||
|
var maxYSpeed = vely > 0 ? MaxFallSpeed : MaxRiseSpeed;
|
||||||
|
var (absvelx, absvely) = (Mathf.Abs(velx), Mathf.Abs(vely));
|
||||||
|
float xcancel = 0, ycancel = 0;
|
||||||
|
if (absvelx > maxXSpeed) xcancel = -Math.Sign(velx) * CancellationAcceleration * (float)delta;
|
||||||
|
if (absvely > maxYSpeed) ycancel = -Math.Sign(vely) * CancellationAcceleration * (float)delta;
|
||||||
|
if (Mathf.Abs(xcancel) + 0.1 > absvelx) velx = 0;
|
||||||
|
else velx += xcancel;
|
||||||
|
if (Mathf.Abs(ycancel) + 0.1 > absvely) vely = 0;
|
||||||
|
else vely += ycancel;
|
||||||
|
|
||||||
DebugTextLabel.Text =
|
DebugTextLabel.Text =
|
||||||
$"grounded: {grounded}\n" +
|
$"grounded: {grounded}\n" +
|
||||||
$"vely: {vely}\n" +
|
$"vely: {vely}\n" +
|
||||||
$"velx: {velx}\n" +
|
$"velx: {velx}\n" +
|
||||||
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n" +
|
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n";
|
||||||
$"rd : {CalculateDrag(Velocity.X, (float)delta):F2}, {CalculateDrag(Velocity.Y, (float)delta):F2}";
|
|
||||||
// Force = new Vector2(0, forcey);
|
// Force = new Vector2(0, forcey);
|
||||||
Velocity = new Vector2(velx, vely) * UnitToPx;
|
Velocity = new Vector2(velx, vely);
|
||||||
|
Velocity *= UnitToPx;
|
||||||
MoveAndSlide();
|
MoveAndSlide();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user