From e74896527b1de06e55ba939ffa0b90236d3c6c69 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 19 Aug 2025 17:32:57 -0500 Subject: [PATCH] Movement tightened up a bit --- entities/EntityMovement.gd | 125 +++++++++++++++++++++ entities/EntityMovement.gd.uid | 1 + entities/npc/NPC.tscn | 12 +- entities/npc/NPCMovement.gd | 1 + entities/npc/NPCMovement.gd.uid | 1 + entities/player/Player.tscn | 12 +- entities/player/PlayerMovement.gd | 117 +------------------ maps/TestMap.tscn | 4 +- maps/TestMapBase.tscn | 10 +- materials/WorldMaterial.tres | 10 -- materials/WorldMaterialShader.gdshader | 15 --- materials/WorldMaterialShader.gdshader.uid | 1 - singletons/GamePhysics.gd | 2 +- 13 files changed, 156 insertions(+), 155 deletions(-) create mode 100644 entities/EntityMovement.gd create mode 100644 entities/EntityMovement.gd.uid create mode 100644 entities/npc/NPCMovement.gd create mode 100644 entities/npc/NPCMovement.gd.uid delete mode 100644 materials/WorldMaterial.tres delete mode 100644 materials/WorldMaterialShader.gdshader delete mode 100644 materials/WorldMaterialShader.gdshader.uid diff --git a/entities/EntityMovement.gd b/entities/EntityMovement.gd new file mode 100644 index 0000000..d749bf8 --- /dev/null +++ b/entities/EntityMovement.gd @@ -0,0 +1,125 @@ +class_name EntityMovement extends Node + +const SPEED = 64.0 +const FRICTION = 0.01 + +enum FacingDirection { + SOUTH = 0, + EAST = 1, + NORTH = 2, + WEST = 3 +}; + +const FacingDirWalkAnimations = { + FacingDirection.SOUTH: "walk_south", + FacingDirection.EAST: "walk_east", + FacingDirection.NORTH: "walk_north", + FacingDirection.WEST: "walk_west" +}; + +const FacingDirAngle = { + FacingDirection.SOUTH: 0.0, + FacingDirection.EAST: PI / 2, + FacingDirection.NORTH: PI, + FacingDirection.WEST: -PI / 2 +}; + +@export var body:CharacterBody3D +@export var rotate:Node3D +@export var sprite:AnimatedSprite3D + +var facingDir:FacingDirection = FacingDirection.SOUTH +var inputDir:Vector2 = Vector2.ZERO + +func _enter_tree() -> void: + if !sprite: + return + for dir in FacingDirWalkAnimations: + if sprite.animation != FacingDirWalkAnimations[dir]: + continue + facingDir = dir + break + +func canMove() -> bool: + return true + +func applyFacingDir() -> void: + if !sprite || inputDir.length() <= 0.01: + return + + if inputDir.y > 0: + if facingDir != FacingDirection.NORTH && inputDir.x != 0: + if inputDir.x > 0 && facingDir == FacingDirection.EAST: + facingDir = FacingDirection.EAST + elif inputDir.x < 0 && facingDir == FacingDirection.WEST: + facingDir = FacingDirection.WEST + else: + facingDir = FacingDirection.NORTH + else: + facingDir = FacingDirection.NORTH + elif inputDir.y < 0: + if facingDir != FacingDirection.SOUTH && inputDir.x != 0: + if inputDir.x > 0 && facingDir == FacingDirection.EAST: + facingDir = FacingDirection.EAST + elif inputDir.x < 0 && facingDir == FacingDirection.WEST: + facingDir = FacingDirection.WEST + else: + facingDir = FacingDirection.SOUTH + else: + facingDir = FacingDirection.SOUTH + elif inputDir.x > 0: + facingDir = FacingDirection.EAST + else: + facingDir = FacingDirection.WEST + + sprite.animation = FacingDirWalkAnimations[facingDir] + +func applyGravity() -> void: + if !body.is_on_floor(): + body.velocity += PHYSICS.GRAVITY * get_process_delta_time() + +func applyMovement() -> void: + if !canMove(): + return + + var cameraCurrent = get_viewport().get_camera_3d() + if !cameraCurrent: + return + + # Use camera orientation for movement direction + var camBasis = cameraCurrent.global_transform.basis + + # Forward and right vectors, ignore vertical component + var forward = -camBasis.z + forward.y = 0 + forward = forward.normalized() + var right = camBasis.x + right.y = 0 + right = right.normalized() + + var directionAdjusted = ( + forward * inputDir.y + right * inputDir.x + ).normalized() + if directionAdjusted.length() <= 0.01: + return + + if rotate: + var targetRot = atan2(directionAdjusted.x, directionAdjusted.z) + rotate.rotation.y = targetRot + + body.velocity.x = directionAdjusted.x * SPEED + body.velocity.z = directionAdjusted.z * SPEED + +func applyFriction(delta:float) -> void: + body.velocity.x *= delta * FRICTION + body.velocity.z *= delta * FRICTION + +func _physics_process(delta:float) -> void: + if !body: + return + + applyGravity() + applyFriction(delta) + applyMovement() + applyFacingDir() + body.move_and_slide() \ No newline at end of file diff --git a/entities/EntityMovement.gd.uid b/entities/EntityMovement.gd.uid new file mode 100644 index 0000000..0dcf9be --- /dev/null +++ b/entities/EntityMovement.gd.uid @@ -0,0 +1 @@ +uid://b38dxsome4kiq diff --git a/entities/npc/NPC.tscn b/entities/npc/NPC.tscn index 924f803..2de3044 100644 --- a/entities/npc/NPC.tscn +++ b/entities/npc/NPC.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=12 format=3 uid="uid://kabs7mopalmo"] +[gd_scene load_steps=13 format=3 uid="uid://kabs7mopalmo"] [ext_resource type="Script" uid="uid://crw7ls7t8cwct" path="res://entities/npc/NPC.gd" id="1_00k55"] [ext_resource type="Script" uid="uid://cmwovncvo1n5o" path="res://entities/npc/NPCTest.gd" id="2_1seh5"] [ext_resource type="Script" uid="uid://b00rxpveu3v4m" path="res://InteractableArea.gd" id="2_x8luf"] +[ext_resource type="Script" uid="uid://tlfthv88ki0y" path="res://entities/npc/NPCMovement.gd" id="3_1seh5"] [ext_resource type="Texture2D" uid="uid://xx3qp5xh7tgu" path="res://entities/player/Player.png" id="4_x8luf"] [sub_resource type="BoxShape3D" id="BoxShape3D_1seh5"] @@ -70,22 +71,29 @@ script = ExtResource("1_00k55") [node name="NPCTest" type="Node" parent="Scripts"] script = ExtResource("2_1seh5") +[node name="NPCMovement" type="Node" parent="Scripts" node_paths=PackedStringArray("body", "sprite")] +script = ExtResource("3_1seh5") +body = NodePath("../..") +sprite = NodePath("../../AnimatedSprite3D") + [node name="InteractableArea" type="Area3D" parent="."] script = ExtResource("2_x8luf") metadata/_custom_type_script = "uid://b00rxpveu3v4m" [node name="CollisionShape3D" type="CollisionShape3D" parent="InteractableArea"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8, 0) shape = SubResource("BoxShape3D_1seh5") [node name="AnimatedSprite3D" type="AnimatedSprite3D" parent="."] pixel_size = 1.0 -billboard = 1 +axis = 1 double_sided = false texture_filter = 0 sprite_frames = SubResource("SpriteFrames_1seh5") animation = &"walk_west" [node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8.5, 0) shape = SubResource("SphereShape3D_x8luf") [connection signal="interactEvent" from="InteractableArea" to="Scripts/NPCTest" method="onInteract"] diff --git a/entities/npc/NPCMovement.gd b/entities/npc/NPCMovement.gd new file mode 100644 index 0000000..a2fe5c9 --- /dev/null +++ b/entities/npc/NPCMovement.gd @@ -0,0 +1 @@ +class_name NPCMovement extends "res://entities/EntityMovement.gd" \ No newline at end of file diff --git a/entities/npc/NPCMovement.gd.uid b/entities/npc/NPCMovement.gd.uid new file mode 100644 index 0000000..3c659c6 --- /dev/null +++ b/entities/npc/NPCMovement.gd.uid @@ -0,0 +1 @@ +uid://tlfthv88ki0y diff --git a/entities/player/Player.tscn b/entities/player/Player.tscn index 1ff5fb4..0f008b7 100644 --- a/entities/player/Player.tscn +++ b/entities/player/Player.tscn @@ -69,10 +69,10 @@ script = ExtResource("1_24gqh") [node name="Scripts" type="Node" parent="."] -[node name="PlayerMovement" type="Node" parent="Scripts" node_paths=PackedStringArray("player", "rotated", "sprite")] +[node name="PlayerMovement" type="Node" parent="Scripts" node_paths=PackedStringArray("body", "rotate", "sprite")] script = ExtResource("2_o7et6") -player = NodePath("../..") -rotated = NodePath("../../PlayerRotated") +body = NodePath("../..") +rotate = NodePath("../../PlayerRotated") sprite = NodePath("../../AnimatedSprite3D") [node name="PlayerInteraction" type="Node" parent="Scripts" node_paths=PackedStringArray("interactableArea", "player")] @@ -91,13 +91,15 @@ camera = NodePath("../../PlayerCamera") target = NodePath("../..") [node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8.5, 0) shape = SubResource("SphereShape3D_4pwj0") [node name="PlayerCamera" type="Camera3D" parent="."] [node name="AnimatedSprite3D" type="AnimatedSprite3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.0496178, 0, -0.00852585) pixel_size = 1.0 -billboard = 1 +axis = 1 double_sided = false texture_filter = 0 sprite_frames = SubResource("SpriteFrames_2rv2u") @@ -108,5 +110,5 @@ animation = &"walk_south" [node name="PlayerInteractableArea" type="Area3D" parent="PlayerRotated"] [node name="CollisionShape3D" type="CollisionShape3D" parent="PlayerRotated/PlayerInteractableArea"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8, 10) shape = SubResource("BoxShape3D_g13of") diff --git a/entities/player/PlayerMovement.gd b/entities/player/PlayerMovement.gd index 029f555..7d3fb1a 100644 --- a/entities/player/PlayerMovement.gd +++ b/entities/player/PlayerMovement.gd @@ -1,121 +1,6 @@ -class_name PlayerMovement extends Node - -const SPEED = 64.0 -const FRICTION = 0.01 - -enum FacingDirection { - SOUTH = 0, - EAST = 1, - NORTH = 2, - WEST = 3 -}; - -const FacingDirWalkAnimations = { - FacingDirection.SOUTH: "walk_south", - FacingDirection.EAST: "walk_east", - FacingDirection.NORTH: "walk_north", - FacingDirection.WEST: "walk_west" -}; - -@export var player:CharacterBody3D -@export var rotated:Node3D -@export var sprite:AnimatedSprite3D - -var facingDir:FacingDirection = FacingDirection.SOUTH -var inputDir:Vector2 = Vector2.ZERO - -func _enter_tree() -> void: - if !sprite: - return - for dir in FacingDirWalkAnimations: - if sprite.animation != FacingDirWalkAnimations[dir]: - continue - facingDir = dir - break +class_name PlayerMovement extends "res://entities/EntityMovement.gd" func canMove() -> bool: if PAUSE.isMovementPaused(): return false return true - -func applyFacingDir() -> void: - if !sprite || inputDir.length() <= 0.01: - return - - if inputDir.y > 0: - if facingDir != FacingDirection.NORTH && inputDir.x != 0: - if inputDir.x > 0 && facingDir == FacingDirection.EAST: - facingDir = FacingDirection.EAST - elif inputDir.x < 0 && facingDir == FacingDirection.WEST: - facingDir = FacingDirection.WEST - else: - facingDir = FacingDirection.NORTH - else: - facingDir = FacingDirection.NORTH - elif inputDir.y < 0: - if facingDir != FacingDirection.SOUTH && inputDir.x != 0: - if inputDir.x > 0 && facingDir == FacingDirection.EAST: - facingDir = FacingDirection.EAST - elif inputDir.x < 0 && facingDir == FacingDirection.WEST: - facingDir = FacingDirection.WEST - else: - facingDir = FacingDirection.SOUTH - else: - facingDir = FacingDirection.SOUTH - elif inputDir.x > 0: - facingDir = FacingDirection.EAST - else: - facingDir = FacingDirection.WEST - - sprite.animation = FacingDirWalkAnimations[facingDir] - -func applyGravity() -> void: - if !player.is_on_floor(): - player.velocity += PHYSICS.GRAVITY * get_process_delta_time() - -func applyMovement() -> void: - if !canMove(): - return - - - # Use camera orientation for movement direction - var cam_basis = get_viewport().get_camera_3d().global_transform.basis - # Forward and right vectors, ignore vertical component - - var forward = -cam_basis.z - forward.y = 0 - forward = forward.normalized() - var right = cam_basis.x - right.y = 0 - right = right.normalized() - - var directionAdjusted = ( - forward * inputDir.y + right * inputDir.x - ).normalized() - if directionAdjusted.length() <= 0.01: - return - - if rotated: - var targetRot = atan2(directionAdjusted.x, directionAdjusted.z) - rotated.rotation.y = targetRot - - player.velocity.x = directionAdjusted.x * SPEED - player.velocity.z = directionAdjusted.z * SPEED - -func applyFriction(delta:float) -> void: - player.velocity.x *= delta * FRICTION - player.velocity.z *= delta * FRICTION - -func _physics_process(delta:float) -> void: - if !player: - return - - var cameraCurrent = get_viewport().get_camera_3d() - if !cameraCurrent: - return - - applyGravity() - applyFriction(delta) - applyMovement() - applyFacingDir() - player.move_and_slide() diff --git a/maps/TestMap.tscn b/maps/TestMap.tscn index 0b378d6..c20d74c 100644 --- a/maps/TestMap.tscn +++ b/maps/TestMap.tscn @@ -9,7 +9,7 @@ [node name="TestMapBase" parent="." instance=ExtResource("1_ox0si")] [node name="Player" parent="." instance=ExtResource("2_0d2qr")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.59, 0) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 14.3947, 1.94879, -13.1025) [node name="NPC" parent="." instance=ExtResource("3_0vfw4")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.33181, 0.554601, 0.508184) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.30029, 4.06806, 0.563562) diff --git a/maps/TestMapBase.tscn b/maps/TestMapBase.tscn index bfc63cd..36fe668 100644 --- a/maps/TestMapBase.tscn +++ b/maps/TestMapBase.tscn @@ -1,10 +1,14 @@ -[gd_scene load_steps=4 format=3 uid="uid://cluuhtfjeodwb"] +[gd_scene load_steps=5 format=3 uid="uid://cluuhtfjeodwb"] -[ext_resource type="Material" uid="uid://chuogedj81c5" path="res://materials/WorldMaterial.tres" id="1_x4ibw"] +[ext_resource type="Texture2D" uid="uid://cu1hvpqmqn31n" path="res://icon.svg" id="1_x4ibw"] [sub_resource type="PlaneMesh" id="PlaneMesh_owt5q"] size = Vector2(200, 200) +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_52cv7"] +shading_mode = 0 +albedo_texture = ExtResource("1_x4ibw") + [sub_resource type="BoxShape3D" id="BoxShape3D_x4ibw"] size = Vector3(200, 0.1, 200) @@ -15,7 +19,7 @@ size = Vector3(200, 0.1, 200) [node name="MeshInstance3D" type="MeshInstance3D" parent="Ground"] mesh = SubResource("PlaneMesh_owt5q") skeleton = NodePath("../..") -surface_material_override/0 = ExtResource("1_x4ibw") +surface_material_override/0 = SubResource("StandardMaterial3D_52cv7") [node name="CollisionShape3D" type="CollisionShape3D" parent="Ground"] shape = SubResource("BoxShape3D_x4ibw") diff --git a/materials/WorldMaterial.tres b/materials/WorldMaterial.tres deleted file mode 100644 index a4202da..0000000 --- a/materials/WorldMaterial.tres +++ /dev/null @@ -1,10 +0,0 @@ -[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://chuogedj81c5"] - -[ext_resource type="Shader" uid="uid://be6ueh411xro5" path="res://materials/WorldMaterialShader.gdshader" id="1_76gxt"] -[ext_resource type="Texture2D" uid="uid://cu1hvpqmqn31n" path="res://icon.svg" id="2_7os1f"] - -[resource] -render_priority = 0 -shader = ExtResource("1_76gxt") -shader_parameter/text = ExtResource("2_7os1f") -shader_parameter/color = Color(1, 1, 1, 1) diff --git a/materials/WorldMaterialShader.gdshader b/materials/WorldMaterialShader.gdshader deleted file mode 100644 index 3c1d36b..0000000 --- a/materials/WorldMaterialShader.gdshader +++ /dev/null @@ -1,15 +0,0 @@ -shader_type spatial; -render_mode unshaded; - -uniform sampler2D text; -uniform vec4 color: source_color = vec4(1.0, 1.0, 1.0, 1.0); - -//void vertex() { - // -//} - -void fragment() { - vec4 tex_color = texture(text, UV); - ALBEDO = tex_color.rgb * color.rgb; - ALPHA = tex_color.a * color.a; -} \ No newline at end of file diff --git a/materials/WorldMaterialShader.gdshader.uid b/materials/WorldMaterialShader.gdshader.uid deleted file mode 100644 index de3c030..0000000 --- a/materials/WorldMaterialShader.gdshader.uid +++ /dev/null @@ -1 +0,0 @@ -uid://be6ueh411xro5 diff --git a/singletons/GamePhysics.gd b/singletons/GamePhysics.gd index 4cabc6a..ad920f9 100644 --- a/singletons/GamePhysics.gd +++ b/singletons/GamePhysics.gd @@ -1,3 +1,3 @@ class_name GamePhysicsSingleton extends Node -const GRAVITY = Vector3.DOWN * 9.8 +const GRAVITY = Vector3.DOWN * 100