Just making things work again
This commit is contained in:
@@ -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()
|
||||
_applyGravity()
|
||||
_applyFriction(delta)
|
||||
_applyMovement()
|
||||
_applyFacingDir()
|
||||
body.move_and_slide()
|
||||
|
@@ -1 +1,2 @@
|
||||
class_name NPCMovement extends "res://entities/EntityMovement.gd"
|
||||
@tool
|
||||
class_name NPCMovement extends "res://entity/EntityMovement.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
|
||||
|
@@ -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="."]
|
||||
|
||||
|
@@ -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()
|
||||
|
@@ -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():
|
||||
|
Reference in New Issue
Block a user