This commit is contained in:
Spencer Killen 2023-08-19 18:44:52 -06:00
commit db3240dd4c
Signed by: sjkillen
GPG Key ID: F307025B65C860BA
1 changed files with 40 additions and 34 deletions

View File

@ -1,4 +1,4 @@
extends Node3D extends CharacterBody3D
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
@export var target_location_xz = transform.origin * Vector3(1, 0, 1) @export var target_location_xz = transform.origin * Vector3(1, 0, 1)
@ -8,13 +8,13 @@ var rng = RandomNumberGenerator.new()
var task = "idle" var task = "idle"
# Percentage chances of the character performing certain actions while idle. # Percentage chances of the character performing certain actions while idle.
@export var walk_chance = 0.5 @export var walk_chance = 0.1
@export var spin_chance = 0.8 @export var spin_chance = 0.2
@export var walk_speed = 1 @export var walk_speed = 0.5
# Rate at which character corrects their direction after going off course (decimal percentage). # Rate at which character corrects their direction after going off course (percentage).
@export var spin_speed = 0.90 @export var dir_correction_rate = 0.95
# 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)
@ -24,54 +24,60 @@ var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
pass # Replace with function body. pass
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(_delta):
location_xz = transform.origin * Vector3(1, 0, 1) location_xz = transform.origin * Vector3(1, 0, 1)
direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized() direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
match task: match task:
"idle": "idle":
task = rng.randi_range(0, 100) task = rng.randf_range(0, 100)
target_location_xz = location_xz # 3% chance to walk somewhere.
# Chance to walk somewhere.
if task <= walk_chance: if task <= walk_chance:
task = "walk" task = "walk"
target_location_xz = transform.origin * Vector3(1, 0, 1) + (direction_xz * 4) target_location_xz = transform.origin * Vector3(1, 0, 1) + (direction_xz * 4)
# Chance to rotate. # 6% 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"
elif task > 50: var rotation_angle = rng.randf_range(-2*PI, 2*PI)
task = "wait" 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"
"walk": "walk":
pass # 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"
"spin": "spin":
if !spinning: # Continue to correct direction to within a margin of dir_accuracy degrees.
var spin_amount = rng.randi_range(-360, 360) if direction_xz.angle_to(target_direction_xz) >= dir_accuracy:
spinning = true # Rotate towards destination at specified percentage rate.
rotate_y(direction_xz.angle_to(target_direction_xz) * dir_correction_rate)
if spin_amount > 0:
rotate_y(5)
spin_amount -= 5
elif spin_amount < 0:
rotate_y(-5)
spin_amount += 5
else: else:
task = "idle" task = "idle"
spinning = false
"wait":
# Idle Animation goes here!
wait_time += 1 func _physics_process(delta):
if wait_time == 100: # Add the gravity.
task = "idle" if not is_on_floor():
wait_time = 0 velocity.y -= gravity * delta
# Walk to target location. # Walk to target location.
if (location_xz.distance_to(target_location_xz) > walk_speed) and task == "walk": if (location_xz.distance_to(target_location_xz) > walk_speed) and task == "walk":