Files
MovementTests/player_controller/Scripts/DashSystem.cs

61 lines
1.7 KiB
C#

using Godot;
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")]
public float DashSpeed { get; set; } = 0.05f;
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 (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;
}
public void Dash()
{
_dashTarget.SetVisible(false);
}
}