gd: dash target
This commit is contained in:
35
player_controller/Scripts/DashSystem.cs
Normal file
35
player_controller/Scripts/DashSystem.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using Godot;
|
||||
using RustyOptions;
|
||||
|
||||
namespace PolarBears.PlayerControllerAddon;
|
||||
|
||||
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;
|
||||
|
||||
public void Init(Node3D head, Camera3D camera)
|
||||
{
|
||||
_dashCast3D = GetNode<ShapeCast3D>("DashCast3D");
|
||||
_head = head;
|
||||
_camera = camera;
|
||||
}
|
||||
|
||||
public Result<Vector3, string> PrepareDash()
|
||||
{
|
||||
_dashCast3D.SetRotation(new Vector3(
|
||||
_camera.Rotation.X,
|
||||
_head.Rotation.Y,
|
||||
_camera.Rotation.Z));
|
||||
|
||||
var dashLocation = _dashCast3D.IsColliding()
|
||||
? _dashCast3D.GetCollisionPoint(0)
|
||||
: _dashCast3D.GetTargetPosition();
|
||||
|
||||
return Result.Ok(dashLocation);
|
||||
}
|
||||
}
|
1
player_controller/Scripts/DashSystem.cs.uid
Normal file
1
player_controller/Scripts/DashSystem.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://dwoppk8j5fxeg
|
@ -24,6 +24,20 @@ public partial class Mouse : Node3D
|
||||
_isPlayerDead = isDeadFunc;
|
||||
}
|
||||
|
||||
public void LookAround(Vector2 lookDir)
|
||||
{
|
||||
// Horizontal movement of head
|
||||
float angleForHorizontalRotation = lookDir.X * Sensitivity;
|
||||
_head.RotateY(angleForHorizontalRotation);
|
||||
|
||||
// Vertical movement of head
|
||||
Vector3 currentCameraRotation = _camera.Rotation;
|
||||
currentCameraRotation.X += Convert.ToSingle(lookDir.Y * Sensitivity);
|
||||
currentCameraRotation.X = Mathf.Clamp(currentCameraRotation.X, Mathf.DegToRad(-90f), Mathf.DegToRad(90f));
|
||||
|
||||
_camera.Rotation = currentCameraRotation;
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (_isPlayerDead())
|
||||
@ -33,16 +47,8 @@ public partial class Mouse : Node3D
|
||||
|
||||
if (@event is InputEventMouseMotion eventMouseMotion)
|
||||
{
|
||||
// Horizontal movement of head
|
||||
float angleForHorizontalRotation = -eventMouseMotion.Relative.X * Sensitivity;
|
||||
_head.RotateY(angleForHorizontalRotation);
|
||||
|
||||
// Vertical movement of head
|
||||
Vector3 currentCameraRotation = _camera.Rotation;
|
||||
currentCameraRotation.X += Convert.ToSingle(-eventMouseMotion.Relative.Y * Sensitivity);
|
||||
currentCameraRotation.X = Mathf.Clamp(currentCameraRotation.X, Mathf.DegToRad(-90f), Mathf.DegToRad(90f));
|
||||
|
||||
_camera.Rotation = currentCameraRotation;
|
||||
var lookDir = new Vector2(-eventMouseMotion.Relative.X, -eventMouseMotion.Relative.Y);
|
||||
LookAround(lookDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
public Stamina Stamina;
|
||||
public StairsSystem StairsSystem;
|
||||
public MantleSystem MantleSystem;
|
||||
public DashSystem DashSystem;
|
||||
public CapsuleCollider CapsuleCollider;
|
||||
public Gravity Gravity;
|
||||
public HealthSystem HealthSystem;
|
||||
@ -27,8 +28,12 @@ public partial class PlayerController : CharacterBody3D
|
||||
|
||||
[Export(PropertyHint.Range, "0,5,0.1,or_greater")]
|
||||
public float DoubleJumpSpeedFactor { get; set; } = 2f;
|
||||
|
||||
[Export(PropertyHint.Range, "1,50,1,or_greater")]
|
||||
public float ControllerSensitivity { get; set; } = 20f;
|
||||
|
||||
private bool _canDoubleJump = true;
|
||||
private bool _movementEnabled = true;
|
||||
|
||||
private float _currentSpeed;
|
||||
|
||||
@ -95,6 +100,9 @@ public partial class PlayerController : CharacterBody3D
|
||||
|
||||
MantleSystem = GetNode<MantleSystem>("MantleSystem");
|
||||
MantleSystem.Init(wallInFrontCast3D, Head, mantleCast3D);
|
||||
|
||||
DashSystem = GetNode<DashSystem>("DashSystem");
|
||||
DashSystem.Init(Head, camera);
|
||||
|
||||
CapsuleCollider = GetNode<CapsuleCollider>("CapsuleCollider");
|
||||
|
||||
@ -121,8 +129,19 @@ public partial class PlayerController : CharacterBody3D
|
||||
Mouse.Init(Head, camera, HealthSystem.IsDead);
|
||||
}
|
||||
|
||||
private void DisableMovement()
|
||||
{
|
||||
_movementEnabled = false;
|
||||
}
|
||||
public void EnableMovement()
|
||||
{
|
||||
_movementEnabled = true;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var dashLocation = DashSystem.PrepareDash().Unwrap();
|
||||
|
||||
var mantleLocationResult = MantleSystem.CheckWallInFront();
|
||||
if (isOnFloorCustom())
|
||||
{
|
||||
@ -143,14 +162,22 @@ public partial class PlayerController : CharacterBody3D
|
||||
|
||||
bool isPlayerDead = HealthSystem.IsDead();
|
||||
|
||||
// Handle Jumping
|
||||
// Handle Jump input
|
||||
if (Input.IsActionJustPressed("jump")
|
||||
&& !doesCapsuleHaveCrouchingHeight
|
||||
&& !isPlayerDead)
|
||||
{
|
||||
if (mantleLocationResult.IsOk(out var mantleLocation))
|
||||
{
|
||||
Position = 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();
|
||||
}
|
||||
else if (isOnFloorCustom())
|
||||
{
|
||||
@ -170,7 +197,6 @@ public partial class PlayerController : CharacterBody3D
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool isHeadTouchingCeiling = IsHeadTouchingCeiling();
|
||||
bool doesCapsuleHaveDefaultHeight = CapsuleCollider.IsDefaultHeight();
|
||||
|
||||
@ -209,6 +235,9 @@ public partial class PlayerController : CharacterBody3D
|
||||
{
|
||||
_currentSpeed = SprintSpeed;
|
||||
}
|
||||
|
||||
Vector2 inputLookDir = Input.GetVector("look_left", "look_right", "look_up", "look_down");
|
||||
Mouse.LookAround(-1 * ControllerSensitivity * inputLookDir);
|
||||
|
||||
// Get the input direction
|
||||
Vector2 inputDir = Input.GetVector("left", "right", "up", "down");
|
||||
|
Reference in New Issue
Block a user