Files
MovementTests/systems/dash/DashSystem.cs

156 lines
5.4 KiB
C#

using Godot;
namespace Movementtests.systems;
public record DashComputationRecord(bool HasHit, Vector3 Location, Vector3 CollisionPoint, Vector3 CollisionNormal);
public record DashResolveRecord(bool EndWithMantle, Vector3 DashLocation, Vector3 MantleLocation);
public partial class DashSystem: Node3D
{
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
public float DashSpeed { get; set; } = 0.05f;
[Export(PropertyHint.Range, "0,1000,1,or_greater")]
public float PostDashSpeed { get; set; } = 0f;
private Node3D _head;
private ShapeCast3D _dashCast3D;
private ShapeCast3D _playerCast3D;
private Camera3D _camera;
private TweenQueueSystem _tweenQueueSystem;
private Vector3 _dashDirection = Vector3.Zero;
private MantleSystem _mantleSystem;
private MeshInstance3D _dashTarget;
public DashResolveRecord DashResolve { get; set; }
public DashComputationRecord DashComputation { get; set; }
[Signal]
public delegate void DashStartedEventHandler();
[Signal]
public delegate void DashEndedEventHandler();
public void Init(Node3D head, Camera3D camera, TweenQueueSystem tweenQueueSystem)
{
_dashCast3D = GetNode<ShapeCast3D>("DashCast3D");
_playerCast3D = GetNode<ShapeCast3D>("PlayerShapeCast3D");
_head = head;
_camera = camera;
_tweenQueueSystem = tweenQueueSystem;
_mantleSystem = GetNode<MantleSystem>("MantleSystem");
_mantleSystem.Init(this);
_dashTarget = GetNode<MeshInstance3D>("DashTarget");
_dashTarget.SetVisible(false);
}
private DashComputationRecord ComputeDashLocation()
{
if (!_dashCast3D.IsColliding())
{
return new DashComputationRecord(false, _dashCast3D.ToGlobal(_dashCast3D.TargetPosition), Vector3.Zero, Vector3.Zero);
}
var collisionPoint = _dashCast3D.GetCollisionPoint(0);
var collisionNormal = _dashCast3D.GetCollisionNormal(0);
// var playerEndLocation = ComputeDashLocationForPlayerShape(collisionPoint, collisionNormal);
var fraction = _dashCast3D.GetClosestCollisionSafeFraction();
var globalSweepPath = _dashCast3D.ToGlobal(_dashCast3D.TargetPosition) - _dashCast3D.GlobalPosition;
var locationAlongPath = _dashCast3D.GlobalPosition + globalSweepPath * fraction;
var maxPushDownDistance = 0.9f;
var correctionProportion = (float)Mathf.Remap(collisionNormal.Y, -0.5, -1, 0, 1);
var proportion = (float) Mathf.Remap(_dashCast3D.GlobalRotation.X, 0, 1.57, 0, 1);
locationAlongPath += collisionNormal * maxPushDownDistance * proportion * correctionProportion;
var otherLocation = ComputeDashLocationForPlayerShape(collisionPoint, collisionNormal);
return new DashComputationRecord(true, locationAlongPath, collisionPoint, collisionNormal);
}
public Vector3 ComputeDashLocationForPlayerShape(Vector3 location, Vector3? normal = null)
{
if (!normal.HasValue)
return location;
var castStartLocation = location + 2 * normal.Value;
var castEndLocation = -2 * normal.Value;
_playerCast3D.SetGlobalPosition(castStartLocation);
_playerCast3D.SetTargetPosition(castEndLocation);
if (!_playerCast3D.IsColliding())
return castEndLocation;
var fraction = _playerCast3D.GetClosestCollisionSafeFraction();
var locationAlongPath = castEndLocation * fraction;
return castStartLocation + locationAlongPath;
}
public void PrepareDash()
{
_dashTarget.SetVisible(false);
_dashCast3D.SetRotation(new Vector3(
_camera.Rotation.X,
_head.Rotation.Y,
_camera.Rotation.Z));
DashComputation = ComputeDashLocation();
var (hasHit, location, collisionPoint, collisionNormal) = DashComputation;
var shouldMantle = false;
var mantleLocation = Vector3.Zero;
if (hasHit && Mathf.Abs(collisionNormal.Y) < 0.5f)
{
var mantleResult = _mantleSystem.FindMantleLocationAtPoint(collisionPoint, collisionNormal);
shouldMantle = mantleResult.IsSome(out mantleLocation);
}
var targetColor = hasHit ? new Color(1f, 0.2f, 0.2f) : new Color(1f, 1f, 1f);
targetColor = shouldMantle ? new Color(0.2f, 0.2f, 1f) : targetColor;
var targetMaterial = (StandardMaterial3D) _dashTarget.GetSurfaceOverrideMaterial(0);
targetMaterial.SetAlbedo(targetColor);
_dashTarget.SetVisible(true);
_dashTarget.SetGlobalPosition(location);
DashResolve = new DashResolveRecord(shouldMantle, location, mantleLocation);
}
public void CancelDash()
{
_dashTarget.SetVisible(false);
}
public void DashTweenEnded()
{
EmitSignal(SignalName.DashEnded);
}
public void Dash()
{
EmitSignal(SignalName.DashStarted);
_dashTarget.SetVisible(false);
var dashTweenInputs = new TweenQueueSystem.TweenInputs(DashResolve.DashLocation, 0.1f);
var dashTween = _tweenQueueSystem.TweenToLocation(dashTweenInputs);
dashTween.Finished += DashTweenEnded;
if (DashResolve.EndWithMantle)
{
_tweenQueueSystem.QueueTween(DashResolve.MantleLocation, 0.2f);
}
}
public void DashToThrownWeapon()
{
}
public void DashToPlantedWeapon()
{
}
}