Movement tightened up a bit
This commit is contained in:
@@ -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")
|
||||
|
@@ -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()
|
||||
|
Reference in New Issue
Block a user