From 1d8a8c7423f610c4a36b37d0a40e2aaab9f4057b Mon Sep 17 00:00:00 2001 From: Minimata Date: Mon, 26 May 2025 16:33:52 +0200 Subject: [PATCH] gd: setup a target and a simple dash to target --- main.tscn | 1 + player_controller/PlayerController.tscn | 11 ++++++- player_controller/Scripts/DashSystem.cs | 33 ++++++++++++++++--- player_controller/Scripts/PlayerController.cs | 33 ++++++++++++++----- project.godot | 5 +++ 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/main.tscn b/main.tscn index dd31427..fa91c6c 100644 --- a/main.tscn +++ b/main.tscn @@ -26,6 +26,7 @@ shadow_enabled = true [node name="Greybox" type="CSGCombiner3D" parent="."] use_collision = true +collision_layer = 3 [node name="CSGBox3D" type="CSGBox3D" parent="Greybox"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.09619, -0.472656, -0.958893) diff --git a/player_controller/PlayerController.tscn b/player_controller/PlayerController.tscn index c9f43a9..c5ba0c9 100644 --- a/player_controller/PlayerController.tscn +++ b/player_controller/PlayerController.tscn @@ -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") diff --git a/player_controller/Scripts/DashSystem.cs b/player_controller/Scripts/DashSystem.cs index cb9f60b..c12bd85 100644 --- a/player_controller/Scripts/DashSystem.cs +++ b/player_controller/Scripts/DashSystem.cs @@ -3,6 +3,8 @@ using RustyOptions; namespace PolarBears.PlayerControllerAddon; +public record DashLocation(Result 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("DashCast3D"); _head = head; _camera = camera; + _dashTarget = GetNode("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 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); } } diff --git a/player_controller/Scripts/PlayerController.cs b/player_controller/Scripts/PlayerController.cs index 8bc688e..a937500 100644 --- a/player_controller/Scripts/PlayerController.cs +++ b/player_controller/Scripts/PlayerController.cs @@ -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()) { diff --git a/project.godot b/project.godot index d45625e..8d2f904 100644 --- a/project.godot +++ b/project.godot @@ -85,6 +85,11 @@ look_down={ "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null) ] } +aim_dash={ +"deadzone": 0.2, +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":4,"axis_value":1.0,"script":null) +] +} [physics]