From 3ccf4ebabb48dccdd88db7cf9941fa890dc91750 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 30 Aug 2025 21:38:53 -0500 Subject: [PATCH] Just making things work again --- entity/EntityMovement.gd | 97 ++++++++++++++++++++------------ entity/npc/NPCMovement.gd | 3 +- entity/player/Player.gd | 30 ++++++++++ entity/player/Player.tscn | 3 +- entity/player/PlayerInput.gd | 2 +- entity/player/PlayerMovement.gd | 3 +- map/TestMap.tscn | 1 + meta/RootScene.gd | 1 + singleton/Overworld.gd | 2 +- ui/component/AdvancedRichText.gd | 4 +- 10 files changed, 102 insertions(+), 44 deletions(-) diff --git a/entity/EntityMovement.gd b/entity/EntityMovement.gd index d749bf8..14febe5 100644 --- a/entity/EntityMovement.gd +++ b/entity/EntityMovement.gd @@ -1,6 +1,6 @@ +@tool class_name EntityMovement extends Node -const SPEED = 64.0 const FRICTION = 0.01 enum FacingDirection { @@ -24,61 +24,65 @@ const FacingDirAngle = { FacingDirection.WEST: -PI / 2 }; +var _inputDir:Vector2 = Vector2.ZERO +var _facingDir:FacingDirection = FacingDirection.SOUTH +var _running:bool = false + @export var body:CharacterBody3D @export var rotate:Node3D @export var sprite:AnimatedSprite3D +@export var walkSpeed:float = 48.0 +@export var runSpeed:float = 64.0 +@export var facingDir:FacingDirection = FacingDirection.SOUTH: + set(value): + _facingDir = value + _updateSprite() + get: + return _facingDir -var facingDir:FacingDirection = FacingDirection.SOUTH -var inputDir:Vector2 = Vector2.ZERO - -func _enter_tree() -> void: - if !sprite: +# +# Private Methods +# +func _updateSprite() -> void: + if !sprite || sprite.animation == FacingDirWalkAnimations[facingDir]: return - for dir in FacingDirWalkAnimations: - if sprite.animation != FacingDirWalkAnimations[dir]: - continue - facingDir = dir - break + sprite.animation = FacingDirWalkAnimations[facingDir] -func canMove() -> bool: - return true -func applyFacingDir() -> void: - if !sprite || inputDir.length() <= 0.01: +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: + 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: + 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: + 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: + elif _inputDir.x < 0 && facingDir == FacingDirection.WEST: facingDir = FacingDirection.WEST else: facingDir = FacingDirection.SOUTH else: facingDir = FacingDirection.SOUTH - elif inputDir.x > 0: + elif _inputDir.x > 0: facingDir = FacingDirection.EAST else: facingDir = FacingDirection.WEST - sprite.animation = FacingDirWalkAnimations[facingDir] - -func applyGravity() -> void: +func _applyGravity() -> void: if !body.is_on_floor(): body.velocity += PHYSICS.GRAVITY * get_process_delta_time() -func applyMovement() -> void: +func _applyMovement() -> void: if !canMove(): return @@ -98,7 +102,7 @@ func applyMovement() -> void: right = right.normalized() var directionAdjusted = ( - forward * inputDir.y + right * inputDir.x + forward * _inputDir.y + right * _inputDir.x ).normalized() if directionAdjusted.length() <= 0.01: return @@ -107,19 +111,38 @@ func applyMovement() -> void: var targetRot = atan2(directionAdjusted.x, directionAdjusted.z) rotate.rotation.y = targetRot - body.velocity.x = directionAdjusted.x * SPEED - body.velocity.z = directionAdjusted.z * SPEED + var speed = walkSpeed + if _running: + speed = runSpeed + + body.velocity.x = directionAdjusted.x * speed + body.velocity.z = directionAdjusted.z * speed -func applyFriction(delta:float) -> void: +func _applyFriction(delta:float) -> void: body.velocity.x *= delta * FRICTION body.velocity.z *= delta * FRICTION +# +# Protected Methods +# +func canMove() -> bool: + return true + +# +# Callbacks +# +func _enter_tree() -> void: + _updateSprite() + func _physics_process(delta:float) -> void: + if Engine.is_editor_hint(): + return + if !body: return - applyGravity() - applyFriction(delta) - applyMovement() - applyFacingDir() - body.move_and_slide() \ No newline at end of file + _applyGravity() + _applyFriction(delta) + _applyMovement() + _applyFacingDir() + body.move_and_slide() diff --git a/entity/npc/NPCMovement.gd b/entity/npc/NPCMovement.gd index a2fe5c9..a7c28f2 100644 --- a/entity/npc/NPCMovement.gd +++ b/entity/npc/NPCMovement.gd @@ -1 +1,2 @@ -class_name NPCMovement extends "res://entities/EntityMovement.gd" \ No newline at end of file +@tool +class_name NPCMovement extends "res://entity/EntityMovement.gd" \ No newline at end of file diff --git a/entity/player/Player.gd b/entity/player/Player.gd index a95f96c..8942ce1 100644 --- a/entity/player/Player.gd +++ b/entity/player/Player.gd @@ -1 +1,31 @@ +@tool class_name Player extends CharacterBody3D + +@export var _movement:PlayerMovement + +@export var facingDirection:EntityMovement.FacingDirection: + set(value): + if _movement: + _movement.facingDir = value + get: + if _movement: + return _movement.facingDir + return EntityMovement.FacingDirection.SOUTH + +@export var walkSpeed:float = 48.0: + set(value): + if _movement: + _movement.walkSpeed = value + get: + if _movement: + return _movement.walkSpeed + return 48.0 + +@export var runSpeed:float = 64.0: + set(value): + if _movement: + _movement.runSpeed = value + get: + if _movement: + return _movement.runSpeed + return 64.0 diff --git a/entity/player/Player.tscn b/entity/player/Player.tscn index 30cb4c6..6ec64de 100644 --- a/entity/player/Player.tscn +++ b/entity/player/Player.tscn @@ -64,8 +64,9 @@ animations = [{ [sub_resource type="BoxShape3D" id="BoxShape3D_g13of"] size = Vector3(10, 16, 8) -[node name="Player" type="CharacterBody3D"] +[node name="Player" type="CharacterBody3D" node_paths=PackedStringArray("_movement")] script = ExtResource("1_24gqh") +_movement = NodePath("Scripts/PlayerMovement") [node name="Scripts" type="Node" parent="."] diff --git a/entity/player/PlayerInput.gd b/entity/player/PlayerInput.gd index 594d76a..7baf995 100644 --- a/entity/player/PlayerInput.gd +++ b/entity/player/PlayerInput.gd @@ -10,4 +10,4 @@ func _process(delta: float) -> void: if Input.is_action_just_pressed("interact"): interaction.interact() - movement.inputDir = Input.get_vector("move_left", "move_right", "move_back", "move_forward").normalized() + movement._inputDir = Input.get_vector("move_left", "move_right", "move_back", "move_forward").normalized() diff --git a/entity/player/PlayerMovement.gd b/entity/player/PlayerMovement.gd index 7d3fb1a..b9710cf 100644 --- a/entity/player/PlayerMovement.gd +++ b/entity/player/PlayerMovement.gd @@ -1,4 +1,5 @@ -class_name PlayerMovement extends "res://entities/EntityMovement.gd" +@tool +class_name PlayerMovement extends "res://entity/EntityMovement.gd" func canMove() -> bool: if PAUSE.isMovementPaused(): diff --git a/map/TestMap.tscn b/map/TestMap.tscn index 18fd2b9..4872138 100644 --- a/map/TestMap.tscn +++ b/map/TestMap.tscn @@ -10,6 +10,7 @@ [node name="Player" parent="." instance=ExtResource("2_0d2qr")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 14.3947, 1.94879, -13.1025) +facingDirection = 1 [node name="NPC" parent="." instance=ExtResource("3_0vfw4")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.30029, 4.06806, 0.563562) diff --git a/meta/RootScene.gd b/meta/RootScene.gd index ebbf92e..11921bf 100644 --- a/meta/RootScene.gd +++ b/meta/RootScene.gd @@ -11,6 +11,7 @@ func _exit_tree() -> void: push_error("RootScene should not be removed from the scene tree. This is a bug.") func onSceneChange(newScene:SceneSingleton.SceneType) -> void: + print("overworld", overworld) remove_child(overworld) remove_child(initial) diff --git a/singleton/Overworld.gd b/singleton/Overworld.gd index 89f4e2d..af0d41f 100644 --- a/singleton/Overworld.gd +++ b/singleton/Overworld.gd @@ -18,7 +18,7 @@ func _exit_tree() -> void: TRANSITION.fadeOutEnd.disconnect(onFadeOutEnd) TRANSITION.fadeInEnd.disconnect(onFadeInEnd) -func _process(delta:float) -> void: +func _process(_delta:float) -> void: if(!isMapChanging()): return diff --git a/ui/component/AdvancedRichText.gd b/ui/component/AdvancedRichText.gd index 160d4a3..e16f89d 100644 --- a/ui/component/AdvancedRichText.gd +++ b/ui/component/AdvancedRichText.gd @@ -30,7 +30,7 @@ func _parseAdvancedText() -> void: key = tr(key) self.text = processInputTags(key) -func processInputTags(text:String) -> String: +func processInputTags(_text:String) -> String: var regex = RegEx.new() regex.compile(r"\[input action=(.*?)\](.*?)\[/input\]") var result = text @@ -41,4 +41,4 @@ func processInputTags(text:String) -> String: # var icon_path = get_icon_for_action(action, device) var img_tag = "[img height=%d valign=center,center]res://textures/input/%s.tres[/img]" % [ height, action ] result = result.replace(match.get_string(0), img_tag) - return result \ No newline at end of file + return result