Files
MovementTests/systems/dash/DashSystem.cs

184 lines
6.2 KiB
C#

using Godot;
namespace Movementtests.systems;
public partial class DashSystem: Node3D
{
public record DashLocation(bool HasHit, Vector3 TargetLocation);
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
public float DashSpeed { get; set; } = 0.1f;
[Export(PropertyHint.Range, "0,1000,1,or_greater")]
public float PostDashSpeed { get; set; } = 0f;
public bool HasHit { get; set; }
public Vector3 TargetLocation { get; set; }
public Vector3 CollisionPoint { get; set; }
public Vector3 CollisionNormal { get; set; }
public Vector3 PlannedPlayerLocation { get; set; }
public bool ShouldMantle { get; set; }
public Vector3 PlannedMantleLocation { get; set; }
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;
private CpuParticles3D _dashIndicator;
private AnimationPlayer _dashIndicatorAnim;
[Export]
public PackedScene DashIndicatorScene { get; set; }
[Signal]
public delegate void DashStartedEventHandler();
[Signal]
public delegate void DashEndedEventHandler();
[Signal]
public delegate void DashProgressEventHandler(float progress);
private Vector3 _globalDashPosition = Vector3.Zero;
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);
_dashIndicator = GetNode<CpuParticles3D>("DashIndicator");
_dashIndicatorAnim = GetNode<AnimationPlayer>("DashIndicator/AnimationPlayer");
}
private DashLocation ComputeDashLocation()
{
var targetLocation = _dashCast3D.ToGlobal(_dashCast3D.TargetPosition);
var hasHit = _dashCast3D.IsColliding();
if (!hasHit)
{
return new DashLocation(false, targetLocation);
}
CollisionPoint = _dashCast3D.GetCollisionPoint(0);
CollisionNormal = _dashCast3D.GetCollisionNormal(0);
var fraction = _dashCast3D.GetClosestCollisionSafeFraction();
var globalSweepPath = TargetLocation - _dashCast3D.GlobalPosition;
var locationAlongPath = _dashCast3D.GlobalPosition + globalSweepPath * fraction;
// Pushes the point down when dashing to under a platform so head doesn't clip
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);
var finalLocation = locationAlongPath
+ CollisionNormal
* maxPushDownDistance
* Mathf.Clamp(proportion, 0, 1)
* Mathf.Clamp(correctionProportion, 0, 1);
return new DashLocation(true, finalLocation);
}
public DashLocation GetDashLocationInDirection(Vector3 direction)
{
var angle = Mathf.Atan2(direction.X, direction.Z);
GD.Print(angle);
var rotation = _head.Rotation.Y + angle*2.0f;
_dashCast3D.SetRotation(new Vector3(0, rotation, 0));
return ComputeDashLocation();
}
public void PrepareDash()
{
_dashTarget.SetVisible(false);
_dashCast3D.SetRotation(new Vector3(
_camera.Rotation.X,
_head.Rotation.Y,
_camera.Rotation.Z));
(HasHit, PlannedPlayerLocation) = ComputeDashLocation();
ShouldMantle = false;
var mantleLocation = Vector3.Zero;
if (HasHit && Mathf.Abs(CollisionNormal.Y) < 0.5f)
{
var mantleResult = _mantleSystem.FindMantleLocationAtPoint(PlannedPlayerLocation, CollisionNormal);
ShouldMantle = mantleResult.IsSome(out mantleLocation);
}
PlannedMantleLocation = 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(PlannedPlayerLocation);
}
public void CancelDash()
{
_dashTarget.SetVisible(false);
}
public void DashTweenEnded()
{
EmitSignal(SignalName.DashEnded);
_dashTarget.SetVisible(false);
}
public void Dash()
{
EmitSignal(SignalName.DashStarted);
var dashTweenInputs = new TweenQueueSystem.TweenInputs(PlannedPlayerLocation, DashSpeed);
var dashTween = _tweenQueueSystem.TweenToLocation(dashTweenInputs);
// dashTween.SetTrans(Tween.TransitionType.Cubic);
// dashTween.SetEase(Tween.EaseType.Out);
dashTween.TweenMethod(Callable.From<float>(Dashing), 0.0f, 1.0f, DashSpeed);
dashTween.Finished += DashTweenEnded;
if (ShouldMantle)
{
_tweenQueueSystem.QueueTween(PlannedMantleLocation, 0.2f);
return;
}
// var dashIndicator = (CpuParticles3D) DashIndicatorScene.Instantiate();
// GetTree().GetRoot().AddChild(dashIndicator);
// _globalDashPosition = _dashTarget.GlobalPosition + 1.5f*Vector3.Up;
// dashIndicator.GlobalPosition = _globalDashPosition;
// dashIndicator.SetEmitting(true);
}
public void Dashing(float proportion)
{
_dashTarget.SetGlobalPosition(_globalDashPosition);
EmitSignalDashProgress(proportion);
}
public void DashToThrownWeapon()
{
}
public void DashToPlantedWeapon()
{
}
}