gd: setup a target and a simple dash to target

This commit is contained in:
2025-05-26 16:33:52 +02:00
parent 6ed8ebff26
commit 1d8a8c7423
5 changed files with 69 additions and 14 deletions

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=25 format=3 uid="uid://bei4nhkf8lwdo"]
[gd_scene load_steps=27 format=3 uid="uid://bei4nhkf8lwdo"]
[ext_resource type="Script" uid="uid://bbbrf5ckydfna" path="res://player_controller/Scripts/PlayerController.cs" id="1_poq2x"]
[ext_resource type="Material" uid="uid://dtq8i1ka1f2pn" path="res://player_controller/Assets/Materials/Health/CameraVignette.tres" id="2_6hee7"]
@ -30,6 +30,10 @@ shader_parameter/blur = 0.0
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_qu4wy"]
height = 1.5
[sub_resource type="SphereMesh" id="SphereMesh_qu4wy"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_v31n3"]
[sub_resource type="Animation" id="Animation_vcu7l"]
length = 0.001
tracks/0/type = "bezier"
@ -215,8 +219,13 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.68, 0)
shape = SubResource("CapsuleShape3D_qu4wy")
target_position = Vector3(0, 0, -20)
max_results = 1
collision_mask = 2
debug_shape_custom_color = Color(0.911631, 0.11884, 0.656218, 1)
[node name="DashTarget" type="MeshInstance3D" parent="DashSystem"]
mesh = SubResource("SphereMesh_qu4wy")
surface_material_override/0 = SubResource("StandardMaterial3D_v31n3")
[node name="StairsSystem" type="Node3D" parent="."]
script = ExtResource("7_bmt5a")

View File

@ -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);
}
}

View File

@ -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())
{