Movement tightened up a bit
This commit is contained in:
125
entities/EntityMovement.gd
Normal file
125
entities/EntityMovement.gd
Normal file
@@ -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()
|
1
entities/EntityMovement.gd.uid
Normal file
1
entities/EntityMovement.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b38dxsome4kiq
|
@@ -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"]
|
||||
|
1
entities/npc/NPCMovement.gd
Normal file
1
entities/npc/NPCMovement.gd
Normal file
@@ -0,0 +1 @@
|
||||
class_name NPCMovement extends "res://entities/EntityMovement.gd"
|
1
entities/npc/NPCMovement.gd.uid
Normal file
1
entities/npc/NPCMovement.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://tlfthv88ki0y
|
@@ -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()
|
||||
|
@@ -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)
|
||||
|
@@ -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")
|
||||
|
@@ -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)
|
@@ -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;
|
||||
}
|
@@ -1 +0,0 @@
|
||||
uid://be6ueh411xro5
|
@@ -1,3 +1,3 @@
|
||||
class_name GamePhysicsSingleton extends Node
|
||||
|
||||
const GRAVITY = Vector3.DOWN * 9.8
|
||||
const GRAVITY = Vector3.DOWN * 100
|
||||
|
Reference in New Issue
Block a user