SlimeoosOdyssey/godot/Villager.gd

72 lines
2.3 KiB
GDScript3
Raw Normal View History

2023-08-19 20:42:02 -06:00
extends SharedSlime
2023-08-19 23:07:52 -06:00
class_name Villager
2023-08-19 11:04:45 -06:00
# Called when the node enters the scene tree for the first time.
func _ready():
2023-08-19 18:43:22 -06:00
pass
2023-08-19 11:04:45 -06:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
2023-08-19 18:43:22 -06:00
func _process(_delta):
location_xz = transform.origin * Vector3(1, 0, 1)
direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
2023-08-19 11:17:09 -06:00
match task:
"idle":
2023-08-19 20:45:30 -06:00
idle()
2023-08-19 18:43:22 -06:00
task = rng.randf_range(0, 100)
2023-08-19 11:17:09 -06:00
2023-08-19 18:43:22 -06:00
# 3% chance to walk somewhere.
if task <= walk_chance:
2023-08-19 11:17:09 -06:00
task = "walk"
target_location_xz = transform.origin * Vector3(1, 0, 1) + (direction_xz * 4)
2023-08-19 18:43:22 -06:00
# 6% chance to rotate.
elif task > walk_chance and task <= (walk_chance + spin_chance):
2023-08-19 11:17:09 -06:00
task = "spin"
2023-08-19 18:43:22 -06:00
var rotation_angle = rng.randf_range(-2*PI, 2*PI)
var rotation_vector = Vector3(cos(rotation_angle), 0, sin(rotation_angle))
target_direction_xz = direction_xz + rotation_vector
# 90% chance to idle.
elif task > (spin_chance + walk_chance):
task = "idle"
2023-08-19 11:17:09 -06:00
"walk":
2023-08-19 20:45:30 -06:00
walk()
2023-08-19 18:43:22 -06:00
# Get the direction to the target in z-x plane.
target_direction_xz = (location_xz.direction_to(target_location_xz) * Vector3(1, 0, 1)).normalized()
# Continue to correct direction to within a margin of dir_accuracy degrees.
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) * dir_correction_rate)
if location_xz.distance_to(target_location_xz) <= (Vector2(direction_xz.x, direction_xz.z) * walk_speed).length():
target_direction_xz = direction_xz
target_location_xz = location_xz
task = "idle"
2023-08-19 11:17:09 -06:00
"spin":
2023-08-19 18:43:22 -06:00
# Continue to correct direction to within a margin of dir_accuracy degrees.
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) * dir_correction_rate)
2023-08-19 11:17:09 -06:00
else:
task = "idle"
2023-08-19 18:43:22 -06:00
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
2023-08-19 11:17:09 -06:00
# Walk to target location.
if (location_xz.distance_to(target_location_xz) > walk_speed) and task == "walk":
velocity.x = direction_xz.x * walk_speed
velocity.z = direction_xz.z * walk_speed
else:
velocity.x = 0
velocity.z = 0
move_and_slide()