Update Villager.gd
This commit is contained in:
		
							parent
							
								
									9c90404a7f
								
							
						
					
					
						commit
						5ef736efc2
					
				
					 1 changed files with 15 additions and 6 deletions
				
			
		| 
						 | 
					@ -1,8 +1,8 @@
 | 
				
			||||||
extends CharacterBody3D
 | 
					extends CharacterBody3D
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var rng = RandomNumberGenerator.new()
 | 
					var rng = RandomNumberGenerator.new()
 | 
				
			||||||
@export var target_location_xz = global_transform.origin * Vector3(1, 0, 1)
 | 
					@export var target_location_xz = transform.origin * Vector3(1, 0, 1)
 | 
				
			||||||
@export var location_xz = global_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 target_direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
 | 
				
			||||||
@export var direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
 | 
					@export var direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
 | 
				
			||||||
var task = "idle"
 | 
					var task = "idle"
 | 
				
			||||||
| 
						 | 
					@ -29,7 +29,7 @@ func _ready():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 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 = global_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:
 | 
				
			||||||
| 
						 | 
					@ -39,7 +39,7 @@ func _process(delta):
 | 
				
			||||||
			# 3% chance to walk somewhere.
 | 
								# 3% chance to walk somewhere.
 | 
				
			||||||
			if task <= walk_chance:
 | 
								if task <= walk_chance:
 | 
				
			||||||
				task = "walk"
 | 
									task = "walk"
 | 
				
			||||||
				target_location_xz = global_transform.origin * Vector3(1, 0, 1) + (direction_xz * 16)
 | 
									target_location_xz = transform.origin * Vector3(1, 0, 1) + (direction_xz * 16)
 | 
				
			||||||
			# 6% 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"
 | 
				
			||||||
| 
						 | 
					@ -65,7 +65,12 @@ func _process(delta):
 | 
				
			||||||
				task = "idle"
 | 
									task = "idle"
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		"spin":
 | 
							"spin":
 | 
				
			||||||
			task = "idle"
 | 
								# 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"
 | 
				
			||||||
						
 | 
											
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -75,8 +80,12 @@ func _physics_process(delta):
 | 
				
			||||||
		velocity.y -= gravity * delta
 | 
							velocity.y -= gravity * delta
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
	# Walk to target location.
 | 
						# Walk to target location.
 | 
				
			||||||
	if location_xz.distance_to(target_location_xz) > (Vector2(direction_xz.x, direction_xz.z) * walk_speed).length():
 | 
						if (location_xz.distance_to(target_location_xz) > walk_speed) and task == "walk":
 | 
				
			||||||
		velocity.x = direction_xz.x * walk_speed
 | 
							velocity.x = direction_xz.x * walk_speed
 | 
				
			||||||
		velocity.z = direction_xz.z * walk_speed
 | 
							velocity.z = direction_xz.z * walk_speed
 | 
				
			||||||
 | 
						else:
 | 
				
			||||||
 | 
							velocity.x = 0
 | 
				
			||||||
 | 
							velocity.z = 0
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	move_and_slide()
 | 
						move_and_slide()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue