Update Villager.gd

This commit is contained in:
ncusimano 2023-08-19 16:41:21 -06:00
parent d5d7295602
commit 9208a53c71
1 changed files with 12 additions and 9 deletions

View File

@ -13,8 +13,8 @@ var task = "idle"
@export var walk_speed = 0.5 @export var walk_speed = 0.5
# Rate at which character corrects their direction after going off course (percentage). # Rate at which character corrects their direction after going off course (decimal percentage).
@export var dir_correction_rate = 0.95 @export var spin_speed = 0.50
# Margin of accuracy to which the character will correct their direction when off course (radians). # Margin of accuracy to which the character will correct their direction when off course (radians).
@export var dir_accuracy = deg_to_rad(0.1) @export var dir_accuracy = deg_to_rad(0.1)
@ -36,17 +36,17 @@ func _process(delta):
"idle": "idle":
task = rng.randf_range(0, 100) task = rng.randf_range(0, 100)
# 3% chance to walk somewhere. # Chance to walk somewhere.
if task <= walk_chance: if task <= walk_chance:
task = "walk" task = "walk"
target_location_xz = global_transform.origin * Vector3(1, 0, 1) + (direction_xz * 16) target_location_xz = global_transform.origin * Vector3(1, 0, 1) + (direction_xz * 16)
# 6% chance to rotate. # Chance to rotate.
elif task > walk_chance and task <= (walk_chance + spin_chance): elif task > walk_chance and task <= (walk_chance + spin_chance):
task = "spin" task = "spin"
var rotation_angle = rng.randf_range(-2*PI, 2*PI) var rotation_angle = rng.randf_range(-2*PI, 2*PI)
var rotation_vector = Vector3(cos(rotation_angle), 0, sin(rotation_angle)) var rotation_vector = Vector3(cos(rotation_angle), 0, sin(rotation_angle))
target_direction_xz = direction_xz + rotation_vector target_direction_xz = direction_xz + rotation_vector
# 90% chance to idle. # Chance to idle.
elif task > (spin_chance + walk_chance): elif task > (spin_chance + walk_chance):
task = "idle" task = "idle"
@ -57,7 +57,7 @@ func _process(delta):
# Continue to correct direction to within a margin of dir_accuracy degrees. # Continue to correct direction to within a margin of dir_accuracy degrees.
if direction_xz.angle_to(target_direction_xz) >= dir_accuracy: if direction_xz.angle_to(target_direction_xz) >= dir_accuracy:
# Rotate towards destination at specified percentage rate. # Rotate towards destination at specified percentage rate.
rotate_y(direction_xz.angle_to(target_direction_xz) * dir_correction_rate) rotate_y(direction_xz.angle_to(target_direction_xz) * spin_speed)
if location_xz.distance_to(target_location_xz) <= (Vector2(direction_xz.x, direction_xz.z) * walk_speed).length(): if location_xz.distance_to(target_location_xz) <= (Vector2(direction_xz.x, direction_xz.z) * walk_speed).length():
target_direction_xz = direction_xz target_direction_xz = direction_xz
@ -65,9 +65,12 @@ func _process(delta):
task = "idle" task = "idle"
"spin": "spin":
task = "idle" if direction_xz.angle_to(target_direction_xz) >= dir_accuracy:
# Rotate towards destination at specified percentage rate.
rotate_y(direction_xz.angle_to(target_direction_xz) * spin_speed)
else:
task = "idle"
func _physics_process(delta): func _physics_process(delta):
# Add the gravity. # Add the gravity.