Update Villager.gd
Updates to Villager Script
This commit is contained in:
		
							parent
							
								
									0633136cf9
								
							
						
					
					
						commit
						8dad9b406c
					
				
					 1 changed files with 37 additions and 8 deletions
				
			
		| 
						 | 
				
			
			@ -1,10 +1,26 @@
 | 
			
		|||
extends Node3D
 | 
			
		||||
 | 
			
		||||
var rng = RandomNumberGenerator.new()
 | 
			
		||||
var spin_amount = 0
 | 
			
		||||
var spinning = false
 | 
			
		||||
var wait_time = 0
 | 
			
		||||
@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():
 | 
			
		||||
| 
						 | 
				
			
			@ -13,15 +29,21 @@ func _ready():
 | 
			
		|||
 | 
			
		||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
 | 
			
		||||
func _process(delta):
 | 
			
		||||
	var task = "idle"
 | 
			
		||||
	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)
 | 
			
		||||
			
 | 
			
		||||
			if task <= 33:
 | 
			
		||||
			target_location_xz = location_xz
 | 
			
		||||
			
 | 
			
		||||
			# Chance to walk somewhere.
 | 
			
		||||
			if task <= walk_chance:
 | 
			
		||||
				task = "walk"
 | 
			
		||||
			elif task > 33 and task <= 50:
 | 
			
		||||
				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"
 | 
			
		||||
| 
						 | 
				
			
			@ -51,5 +73,12 @@ func _process(delta):
 | 
			
		|||
				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()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue