2023-08-19 19:22:57 -06:00
|
|
|
extends CharacterBody3D
|
2023-08-19 20:42:02 -06:00
|
|
|
class_name SharedSlime
|
2023-08-19 20:18:07 -06:00
|
|
|
|
|
|
|
var is_holdable: bool = true
|
2023-08-20 16:58:05 -06:00
|
|
|
var is_killable: bool = true
|
2023-08-19 20:18:07 -06:00
|
|
|
|
2023-08-20 00:00:17 -06:00
|
|
|
var rng = RandomNumberGenerator.new()
|
|
|
|
@onready var target_location_xz = transform.origin * Vector3(1, 0, 1)
|
|
|
|
@onready var location_xz = transform.origin * Vector3(1, 0, 1)
|
|
|
|
@onready var target_direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
|
|
|
|
@onready 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.1
|
|
|
|
@export var spin_chance = 0.2
|
|
|
|
|
|
|
|
@export var walk_speed = 0.5
|
|
|
|
|
|
|
|
# Rate at which character corrects their direction after going off course (percentage).
|
|
|
|
@export var dir_correction_rate = 0.95
|
|
|
|
|
|
|
|
# Margin of accuracy to which the character will correct their direction when off course (radians).
|
|
|
|
@export var dir_accuracy = deg_to_rad(0.1)
|
|
|
|
|
2023-08-20 15:43:03 -06:00
|
|
|
var wall_timer
|
|
|
|
|
2023-08-20 00:00:17 -06:00
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
|
2023-08-19 20:18:07 -06:00
|
|
|
func _ready():
|
|
|
|
$slime.idle()
|
2023-08-20 15:43:03 -06:00
|
|
|
wall_timer = Timer.new()
|
|
|
|
wall_timer.wait_time = 5
|
|
|
|
wall_timer.one_shot = true
|
|
|
|
wall_timer.timeout.connect(_on_timer_timeout)
|
|
|
|
add_child(wall_timer)
|
2023-08-19 20:18:07 -06:00
|
|
|
|
|
|
|
func walk():
|
|
|
|
$slime.walk()
|
2023-08-20 00:00:17 -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)
|
|
|
|
|
2023-08-20 15:03:09 -06:00
|
|
|
if location_xz.distance_to(target_location_xz) <= 0.1:
|
2023-08-20 00:00:17 -06:00
|
|
|
target_direction_xz = direction_xz
|
|
|
|
target_location_xz = location_xz
|
|
|
|
task = "idle"
|
|
|
|
|
2023-08-20 15:43:03 -06:00
|
|
|
if !wall_timer.is_stopped():
|
|
|
|
wall_timer.stop()
|
|
|
|
|
2023-08-20 00:00:17 -06:00
|
|
|
func spin():
|
|
|
|
# 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)
|
|
|
|
else:
|
|
|
|
task = "idle"
|
2023-08-19 20:18:07 -06:00
|
|
|
|
|
|
|
func idle():
|
|
|
|
$slime.idle()
|
2023-08-20 00:00:17 -06:00
|
|
|
task = rng.randf_range(0, 100)
|
|
|
|
|
|
|
|
# 3% chance to walk somewhere.
|
|
|
|
if task <= walk_chance:
|
|
|
|
task = "walk"
|
|
|
|
target_location_xz = transform.origin * Vector3(1, 0, 1) + (direction_xz * 4)
|
2023-08-20 15:43:03 -06:00
|
|
|
wall_timer.start()
|
2023-08-20 00:00:17 -06:00
|
|
|
# 6% chance to rotate.
|
|
|
|
elif task > walk_chance and task <= (walk_chance + spin_chance):
|
|
|
|
task = "spin"
|
|
|
|
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-20 15:43:03 -06:00
|
|
|
|
|
|
|
# Stops walking if cannot reach destination in 5 sec.
|
|
|
|
func _on_timer_timeout():
|
|
|
|
# Try to turn around before walking again.
|
|
|
|
if task == "walk":
|
|
|
|
task = "spin"
|
|
|
|
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
|
2023-08-20 00:00:17 -06:00
|
|
|
|
|
|
|
# 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":
|
|
|
|
idle()
|
|
|
|
"walk":
|
|
|
|
walk()
|
|
|
|
"spin":
|
|
|
|
spin()
|
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta):
|
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity.y -= gravity * delta
|
|
|
|
|
|
|
|
# Walk to target location.
|
2023-08-20 15:03:09 -06:00
|
|
|
if (location_xz.distance_to(target_location_xz) >= 0.1) and task == "walk":
|
2023-08-20 00:00:17 -06:00
|
|
|
velocity.x = direction_xz.x * walk_speed
|
|
|
|
velocity.z = direction_xz.z * walk_speed
|
|
|
|
else:
|
|
|
|
velocity.x = 0
|
|
|
|
velocity.z = 0
|
|
|
|
|
|
|
|
move_and_slide()
|