gd: setup a target and a simple dash to target
This commit is contained in:
@ -3,6 +3,8 @@ using RustyOptions;
|
||||
|
||||
namespace PolarBears.PlayerControllerAddon;
|
||||
|
||||
public record DashLocation(Result<Vector3, string> Result, bool HasHit);
|
||||
|
||||
public partial class DashSystem: Node3D
|
||||
{
|
||||
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
|
||||
@ -11,25 +13,48 @@ public partial class DashSystem: Node3D
|
||||
private Node3D _head;
|
||||
private ShapeCast3D _dashCast3D;
|
||||
private Camera3D _camera;
|
||||
private MeshInstance3D _dashTarget;
|
||||
|
||||
public void Init(Node3D head, Camera3D camera)
|
||||
{
|
||||
_dashCast3D = GetNode<ShapeCast3D>("DashCast3D");
|
||||
_head = head;
|
||||
_camera = camera;
|
||||
_dashTarget = GetNode<MeshInstance3D>("DashTarget");
|
||||
_dashTarget.SetVisible(false);
|
||||
}
|
||||
|
||||
private DashLocation ComputeDashLocation()
|
||||
{
|
||||
var dashLocation = _dashCast3D.IsColliding()
|
||||
? _dashCast3D.GetCollisionPoint(0)
|
||||
: _dashCast3D.ToGlobal(_dashCast3D.TargetPosition);
|
||||
return new DashLocation(Result.Ok(dashLocation), _dashCast3D.IsColliding());
|
||||
}
|
||||
|
||||
public Result<Vector3, string> PrepareDash()
|
||||
{
|
||||
_dashTarget.SetVisible(false);
|
||||
|
||||
_dashCast3D.SetRotation(new Vector3(
|
||||
_camera.Rotation.X,
|
||||
_head.Rotation.Y,
|
||||
_camera.Rotation.Z));
|
||||
|
||||
var dashLocation = _dashCast3D.IsColliding()
|
||||
? _dashCast3D.GetCollisionPoint(0)
|
||||
: _dashCast3D.GetTargetPosition();
|
||||
var (result, hasHit) = ComputeDashLocation();
|
||||
|
||||
var targetColor = hasHit ? new Color(0.2f, 0.2f, 1f) : new Color(1f, 1f, 1f);
|
||||
var targetMaterial = (StandardMaterial3D) _dashTarget.GetSurfaceOverrideMaterial(0);
|
||||
targetMaterial.AlbedoColor = targetColor;
|
||||
|
||||
_dashTarget.SetVisible(true);
|
||||
_dashTarget.SetGlobalPosition(result.Unwrap());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return Result.Ok(dashLocation);
|
||||
public void Dash()
|
||||
{
|
||||
_dashTarget.SetVisible(false);
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
|
||||
private bool _canDoubleJump = true;
|
||||
private bool _movementEnabled = true;
|
||||
private Vector3 _dashLocation = Vector3.Zero;
|
||||
|
||||
private float _currentSpeed;
|
||||
|
||||
@ -138,9 +139,30 @@ public partial class PlayerController : CharacterBody3D
|
||||
_movementEnabled = true;
|
||||
}
|
||||
|
||||
private void TweenToLocation(Vector3 location, float duration)
|
||||
{
|
||||
Tween tween = GetTree().CreateTween();
|
||||
var callback = new Callable(this, MethodName.EnableMovement);
|
||||
|
||||
tween.TweenProperty(this, "position", location, duration);
|
||||
tween.TweenCallback(callback);
|
||||
|
||||
DisableMovement();
|
||||
tween.Play();
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var dashLocation = DashSystem.PrepareDash().Unwrap();
|
||||
if (Input.IsActionPressed("aim_dash"))
|
||||
{
|
||||
_dashLocation = DashSystem.PrepareDash().Unwrap();
|
||||
}
|
||||
|
||||
if (Input.IsActionJustReleased("aim_dash"))
|
||||
{
|
||||
DashSystem.Dash();
|
||||
TweenToLocation(_dashLocation, 0.1f);
|
||||
}
|
||||
|
||||
var mantleLocationResult = MantleSystem.CheckWallInFront();
|
||||
if (isOnFloorCustom())
|
||||
@ -169,15 +191,8 @@ public partial class PlayerController : CharacterBody3D
|
||||
{
|
||||
if (mantleLocationResult.IsOk(out var mantleLocation))
|
||||
{
|
||||
Tween tween = GetTree().CreateTween();
|
||||
var duration = 0.1f * mantleLocation.DistanceTo(Position);
|
||||
var callback = new Callable(this, MethodName.EnableMovement);
|
||||
|
||||
tween.TweenProperty(this, "position", mantleLocation, duration);
|
||||
tween.TweenCallback(callback);
|
||||
|
||||
DisableMovement();
|
||||
tween.Play();
|
||||
TweenToLocation(mantleLocation, duration);
|
||||
}
|
||||
else if (isOnFloorCustom())
|
||||
{
|
||||
|
Reference in New Issue
Block a user