Added a tiny bit of air control and a turnback jump trick.
Release / Export Game (push) Successful in 1m58s
Release / Export Game (push) Successful in 1m58s
This commit is contained in:
@@ -1,22 +1,32 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
// [Export]
|
||||
// public float RunMultiplier { get; set; } = 1.84f;
|
||||
|
||||
[Signal]
|
||||
public delegate void OnTurnbackJumpEventHandler(bool onJumpPoint, bool isOnWall);
|
||||
|
||||
[ExportCategory("Accelerations and Impulses")]
|
||||
[Export]
|
||||
public float WalkAcceleration { get; set; } = 3f;
|
||||
[Export]
|
||||
public float StoppingDeceleration { get; set; } = 8f;
|
||||
[Export]
|
||||
public float CancellationAcceleration { get; set; } = 4f;
|
||||
[Export]
|
||||
public float AirAcceleration { get; set; } = 0.5f;
|
||||
|
||||
[Export]
|
||||
public float JumpHeight { get; set; } = 5.5f;
|
||||
|
||||
private float PeakTime { get; set; } = 0.1f;
|
||||
|
||||
[ExportCategory("Max Speeds")]
|
||||
[Export]
|
||||
private float MaxFallSpeed { get; set; } = 8f;
|
||||
|
||||
@@ -28,12 +38,17 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
[Export]
|
||||
private float MaxWalkSpeed { get; set; } = 8f;
|
||||
|
||||
[ExportCategory("Objects")]
|
||||
[Export]
|
||||
public Label DebugTextLabel { get; set; }
|
||||
|
||||
[Export]
|
||||
public AnimatedSprite2D Sprite { get; set; }
|
||||
|
||||
[Export]
|
||||
public PredictiveCollisions Predictor { get; set; }
|
||||
|
||||
[ExportCategory("Physics Tweaks")]
|
||||
[Export]
|
||||
public float Gravity { get; set; } = 30.625f;
|
||||
|
||||
@@ -48,6 +63,12 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
|
||||
[Export]
|
||||
public float CoyoteTiming { get; set; } = 0.1f;
|
||||
|
||||
[Export]
|
||||
public float Deadzone { get; set; } = 0.1f;
|
||||
|
||||
|
||||
|
||||
|
||||
private float _lv = 0.0f;
|
||||
|
||||
@@ -71,8 +92,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
Sprite.SpeedScale = absvel.X switch {
|
||||
< 10 => 1,
|
||||
< 150 => 2,
|
||||
< 300 => 4,
|
||||
_ => 1
|
||||
< 600 => 4,
|
||||
_ => 4
|
||||
};
|
||||
if (!Sprite.IsPlaying())
|
||||
Sprite.Play();
|
||||
@@ -95,15 +116,12 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
}
|
||||
}
|
||||
|
||||
[Export]
|
||||
public float Deadzone { get; set; } = 0.1f;
|
||||
|
||||
public override void _PhysicsProcess(double delta) {
|
||||
var xAxis = Input.GetAxis("move_left", "move_right");
|
||||
var yAxis = Input.GetAxis("look_up", "look_down");
|
||||
var yAxis = Input.GetAxis("move_up", "move_down");
|
||||
var lookAxis = Input.GetAxis("look_up", "look_down");
|
||||
// var jumpStrength = -Input.GetActionStrength("jump");
|
||||
|
||||
var lastCollision = this.IsOnCeiling();
|
||||
RecalculateLv();
|
||||
|
||||
float velx = (Velocity.X / UnitToPx);
|
||||
@@ -132,9 +150,45 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
if (Input.IsActionJustReleased("jump") && vely < 0) {
|
||||
vely *= 1/4f;
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed("jump") && vely > 0) {
|
||||
var side = velx > 0 ? PredictiveCollisions.Location.BottomRight : PredictiveCollisions.Location.BottomLeft;
|
||||
if (Predictor.AnyHit(side) && !this.IsOnWall()) {
|
||||
EmitSignal(SignalName.OnTurnbackJump, true, this.IsOnWall());
|
||||
var hits = Predictor.GetLastCastHits(side);
|
||||
_ = hits.First(x => {
|
||||
if (x.Obj is null) return false;
|
||||
var collider = x.AsGodotDictionary()["collider"].AsGodotObject();
|
||||
if (collider.HasMeta("JumpPointAnimatable")) {
|
||||
var animPath = collider.GetMeta("JumpPointAnimatable").AsNodePath();
|
||||
var anim = collider.Call(MethodName.GetNode, animPath).As<AnimatedSprite2D>();
|
||||
var frame = anim.Frame;
|
||||
void anf() {
|
||||
anim.Stop();
|
||||
anim.Frame = frame;
|
||||
anim.AnimationLooped -= anf;
|
||||
};
|
||||
anim.AnimationLooped += anf;
|
||||
anim.Play();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
// has jump point
|
||||
float theta = Mathf.Atan2(-yAxis, xAxis);
|
||||
if (theta < 0) theta = 0;
|
||||
if (theta > Mathf.Pi / 2) theta = Mathf.Pi/2;
|
||||
Vector2 turnjump = new Vector2(Mathf.Cos(theta), -Mathf.Sin(theta) - 0.2f) * _lv;
|
||||
velx = turnjump.X;
|
||||
vely = turnjump.Y;
|
||||
} else {
|
||||
// does not have jump point
|
||||
}
|
||||
}
|
||||
|
||||
velx += xAxis * AirAcceleration * (float)delta;
|
||||
}
|
||||
|
||||
if (Mathf.Abs(xAxis) < Deadzone) {
|
||||
if (grounded && Mathf.Abs(xAxis) < Deadzone) {
|
||||
var autostop = -Mathf.Sign(velx) * StoppingDeceleration * (float)delta;
|
||||
if (Mathf.Abs(autostop) > Mathf.Abs(velx)) velx = 0f;
|
||||
else velx += autostop;
|
||||
@@ -155,7 +209,8 @@ public partial class PlayerPhysicsController : CharacterBody2D {
|
||||
$"grounded: {grounded}\n" +
|
||||
$"vely: {vely}\n" +
|
||||
$"velx: {velx}\n" +
|
||||
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n";
|
||||
$"lv : {_lv:F2} or {_lv/(float)delta:F2}\n" +
|
||||
$"lcrc : {Predictor.AnyHit(PredictiveCollisions.Location.BottomLeft)}/{Predictor.AnyHit(PredictiveCollisions.Location.BottomRight)}";
|
||||
// Force = new Vector2(0, forcey);
|
||||
Velocity = new Vector2(velx, vely);
|
||||
Velocity *= UnitToPx;
|
||||
|
||||
Reference in New Issue
Block a user