SlimeoosOdyssey/godot/Villager.gd

85 lines
2.3 KiB
GDScript

extends Node3D
var rng = RandomNumberGenerator.new()
@export var target_location_xz = transform.origin * Vector3(1, 0, 1)
@export var location_xz = transform.origin * Vector3(1, 0, 1)
@export var target_direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
@export var direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
var task = "idle"
# Percentage chances of the character performing certain actions while idle.
@export var walk_chance = 0.5
@export var spin_chance = 0.8
@export var walk_speed = 1
# Rate at which character corrects their direction after going off course (decimal percentage).
@export var spin_speed = 0.90
# Margin of accuracy to which the character will correct their direction when off course (radians).
@export var dir_accuracy = deg_to_rad(0.1)
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
location_xz = transform.origin * Vector3(1, 0, 1)
direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
match task:
"idle":
task = rng.randi_range(0, 100)
target_location_xz = location_xz
# Chance to walk somewhere.
if task <= walk_chance:
task = "walk"
target_location_xz = transform.origin * Vector3(1, 0, 1) + (direction_xz * 4)
# Chance to rotate.
elif task > walk_chance and task <= (walk_chance + spin_chance):
task = "spin"
elif task > 50:
task = "wait"
"walk":
pass
"spin":
if !spinning:
var spin_amount = rng.randi_range(-360, 360)
spinning = true
if spin_amount > 0:
rotate_y(5)
spin_amount -= 5
elif spin_amount < 0:
rotate_y(-5)
spin_amount += 5
else:
task = "idle"
spinning = false
"wait":
# Idle Animation goes here!
wait_time += 1
if wait_time == 100:
task = "idle"
wait_time = 0
# 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()