From 093ed20a0b30f9e17b25b20b830e0cf5cd7843d8 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 29 Mar 2026 17:58:57 -0400 Subject: [PATCH] got tilting and flipping finished --- debug_object.gd | 34 +++++++++++++++++++++++++ debug_object.gd.uid | 1 + debug_object.tscn | 14 +++++++++++ debug_section.gd | 37 +++++++++++++++++++++++++++ debug_section.gd.uid | 1 + debug_section.tscn | 12 +++++++++ project.godot | 17 +++++++------ shark.gd | 59 ++++++++++++++++++++++++++++++++------------ shark.tscn | 14 +++++++++-- 9 files changed, 163 insertions(+), 26 deletions(-) create mode 100644 debug_object.gd create mode 100644 debug_object.gd.uid create mode 100644 debug_object.tscn create mode 100644 debug_section.gd create mode 100644 debug_section.gd.uid create mode 100644 debug_section.tscn diff --git a/debug_object.gd b/debug_object.gd new file mode 100644 index 0000000..4896836 --- /dev/null +++ b/debug_object.gd @@ -0,0 +1,34 @@ +#@tool +#@icon(icon_path: String) +class_name DebugObject +extends HBoxContainer +## Documentation comments + +#signal +#enum +#const +var target: Node +var variable: StringName + +#@onready var +@onready var debug_label: Label = $DebugLabel +@onready var debug_value: Label = $DebugValue + +## OVERRIDES + +func _ready() -> void: + if target and variable: + debug_label.text = variable.to_pascal_case() + ": " + +func _process(_delta: float) -> void: + if target: + debug_value.text = str(target.get(variable)) + + +## CORE + +## PRIVATE/HELPER + +## RECEIVERS + +## SETTERS/GETTERS diff --git a/debug_object.gd.uid b/debug_object.gd.uid new file mode 100644 index 0000000..151f056 --- /dev/null +++ b/debug_object.gd.uid @@ -0,0 +1 @@ +uid://dnw30w3hpedur diff --git a/debug_object.tscn b/debug_object.tscn new file mode 100644 index 0000000..fc4fafb --- /dev/null +++ b/debug_object.tscn @@ -0,0 +1,14 @@ +[gd_scene format=3 uid="uid://ctpm1isydoxy"] + +[ext_resource type="Script" uid="uid://dnw30w3hpedur" path="res://debug_object.gd" id="1_vr84h"] + +[node name="DebugObject" type="HBoxContainer" unique_id=670838278] +script = ExtResource("1_vr84h") + +[node name="DebugLabel" type="Label" parent="." unique_id=204413732] +layout_mode = 2 +text = "SomeVariable: " + +[node name="DebugValue" type="Label" parent="." unique_id=1444599920] +layout_mode = 2 +text = "SomeValue" diff --git a/debug_section.gd b/debug_section.gd new file mode 100644 index 0000000..7e0ae50 --- /dev/null +++ b/debug_section.gd @@ -0,0 +1,37 @@ +@tool +#@icon(icon_path: String) +#class_name MyNode +extends VBoxContainer +## Documentation comments + +#signal +#enum +#const +const DEBUG_OBJECT = preload("uid://ctpm1isydoxy") + +@export var target_node: Node +@export var variables_to_track: Array[StringName] +#var + +@onready var debug_title: Label = $DebugTitle + +## OVERRIDES + +func _ready() -> void: + if target_node: + debug_title.text = "Debug info for %s" % target_node.name + if variables_to_track: + for variable in variables_to_track: + var new_debug_object: DebugObject = DEBUG_OBJECT.instantiate() + new_debug_object.target = target_node + new_debug_object.variable = variable + add_child(new_debug_object) + + +## CORE + +## PRIVATE/HELPER + +## RECEIVERS + +## SETTERS/GETTERS diff --git a/debug_section.gd.uid b/debug_section.gd.uid new file mode 100644 index 0000000..4b1e614 --- /dev/null +++ b/debug_section.gd.uid @@ -0,0 +1 @@ +uid://4tr1rjqc0ebl diff --git a/debug_section.tscn b/debug_section.tscn new file mode 100644 index 0000000..435039f --- /dev/null +++ b/debug_section.tscn @@ -0,0 +1,12 @@ +[gd_scene format=3 uid="uid://dr4342v00t7ei"] + +[ext_resource type="Script" uid="uid://4tr1rjqc0ebl" path="res://debug_section.gd" id="1_ut6yq"] + +[node name="DebugSection" type="VBoxContainer" unique_id=305062788] +offset_right = 40.0 +offset_bottom = 40.0 +script = ExtResource("1_ut6yq") + +[node name="DebugTitle" type="Label" parent="." unique_id=1354024129] +layout_mode = 2 +text = "Debug info for SomeNode" diff --git a/project.godot b/project.godot index 9ce196e..0ff1a2a 100644 --- a/project.godot +++ b/project.godot @@ -18,26 +18,22 @@ config/icon="res://icon.svg" up={ "deadzone": 0.2, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } down={ "deadzone": 0.2, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } left={ "deadzone": 0.2, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } right={ "deadzone": 0.2, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } bite={ @@ -45,6 +41,11 @@ bite={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":66,"key_label":0,"unicode":98,"location":0,"echo":false,"script":null) ] } +swim={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null) +] +} [physics] diff --git a/shark.gd b/shark.gd index f08ca8f..e442188 100644 --- a/shark.gd +++ b/shark.gd @@ -9,11 +9,13 @@ enum FaceDirection {LEFT, RIGHT} #const @export_range(0, 360, 1.0, "radians_as_degrees") var rotation_speed: float = 0.0 ## degrees per second -var current_direction: FaceDirection = FaceDirection.LEFT: set = set_direction var input_vector: Vector2 +var facing_right: bool = true: set = set_facing_right +var tilt: float = 0.0 +var tilt_power: float = 0.0 @onready var shark_sprite: AnimatedSprite2D = $SharkSprite -@onready var shark_collider: CollisionPolygon2D = $SharkCollider +@onready var shark_collider: CollisionShape2D = $SharkCollider @onready var shark_cam: Camera2D = $SharkCam ## OVERRIDES @@ -22,16 +24,38 @@ func _ready() -> void: pass func _process(_delta: float) -> void: - pass + if facing_right: + tilt = clamp(rotation, -1.0, 1.0) + else: + tilt = -clamp(rotation, -1.0, 1.0) func _physics_process(delta: float) -> void: - input_vector = Input.get_vector("left", "right", "up", "down") - if input_vector.x > 0: - current_direction = FaceDirection.RIGHT - elif input_vector.x < 0: - current_direction = FaceDirection.LEFT - - rotate(deg_to_rad(input_vector.y * rotation_speed * delta)) + # handle left-right movement + input_vector.x = Input.get_axis("left", "right") + # flip sprite if needed + if abs(input_vector.x) > 0: # moving horizontally + if input_vector.x > 0: # moving right + facing_right = true + elif input_vector.x < 0: # moving left + facing_right = false + # handle up/down tilt + input_vector.y = Input.get_axis("up", "down") + if abs(input_vector.y) > 0: # attempting to tilt + if input_vector.y < 0: # attempting to tilt up (up is negative) + if tilt > -1.0: # able to tilt further up + #tilt = rotate_toward(tilt, (-PI/2), rotation_speed * delta) + if facing_right: # tilting up means negative rotation + rotate(-rotation_speed * delta) + else: # tilting up means positive rotation + rotate(rotation_speed * delta) + elif input_vector.y > 0: # attempting to tilt down (up is negative) + if tilt < 1.0: # able to tilt further down + #tilt = rotate_toward(tilt, (PI/2), rotation_speed * delta) + if facing_right: # tilting down means positive rotation + rotate(rotation_speed * delta) + else: # tilting down means negative rotation + rotate(-rotation_speed * delta) + ## CORE @@ -40,9 +64,12 @@ func _physics_process(delta: float) -> void: ## RECEIVERS ## SETTERS/GETTERS -func set_direction(new_direction: FaceDirection) -> void: - if new_direction == FaceDirection.LEFT: - shark_sprite.flip_h = false - else: - shark_sprite.flip_h = true - current_direction = new_direction +func set_facing_right(is_facing_right: bool) -> void: + if facing_right != is_facing_right: + rotation = -rotation + if is_facing_right == true: + shark_sprite.flip_h = true + else: + shark_sprite.flip_h = false + facing_right = is_facing_right + diff --git a/shark.tscn b/shark.tscn index 01b1c3a..ed399b6 100644 --- a/shark.tscn +++ b/shark.tscn @@ -4,6 +4,7 @@ [ext_resource type="Script" uid="uid://c2phpu1wyi63u" path="res://shark.gd" id="1_uce6o"] [ext_resource type="Texture2D" uid="uid://b62iwqkbendj3" path="res://spritesheets/__dark_blue_shark_swim_snapping.png" id="2_26j10"] [ext_resource type="Texture2D" uid="uid://b8uwcr02vroko" path="res://spritesheets/__dark_blue_shark_swim.png" id="3_mdx7w"] +[ext_resource type="PackedScene" uid="uid://dr4342v00t7ei" path="res://debug_section.tscn" id="5_l26on"] [sub_resource type="AtlasTexture" id="AtlasTexture_odtda"] atlas = ExtResource("2_26j10") @@ -337,7 +338,7 @@ height = 500.0 [node name="Shark" type="CharacterBody2D" unique_id=868425079] script = ExtResource("1_uce6o") -rotation_speed = 6.283185307179586 +rotation_speed = 1.5707963267948966 [node name="SharkSprite" type="AnimatedSprite2D" parent="." unique_id=1862357568] sprite_frames = SubResource("SpriteFrames_odtda") @@ -347,6 +348,15 @@ flip_h = true [node name="SharkCam" type="Camera2D" parent="." unique_id=692363117] -[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1437907772] +[node name="SharkCollider" type="CollisionShape2D" parent="." unique_id=1437907772] rotation = 1.5707964 shape = SubResource("CapsuleShape2D_uce6o") + +[node name="RayCast2D" type="RayCast2D" parent="." unique_id=1636588841] +target_position = Vector2(500, 0) + +[node name="SharkDebugLayer" type="CanvasLayer" parent="." unique_id=359221405] + +[node name="DebugSection" parent="SharkDebugLayer" unique_id=305062788 node_paths=PackedStringArray("target_node") instance=ExtResource("5_l26on")] +target_node = NodePath("../..") +variables_to_track = Array[StringName]([&"input_vector", &"facing_right", &"tilt", &"rotation_speed", &"rotation"])