From a8465d996234ef5eef9d74d31e4497220197a4ff Mon Sep 17 00:00:00 2001 From: ncusimano Date: Sat, 19 Aug 2023 16:08:58 -0600 Subject: [PATCH] Updates to Villager and Update to Main --- godot/Player.gd | 6 +-- godot/Villager.gd | 103 +++++++++++++++++++++++++------------------- godot/villager.tscn | 8 ++-- 3 files changed, 63 insertions(+), 54 deletions(-) diff --git a/godot/Player.gd b/godot/Player.gd index 963b947..9054565 100644 --- a/godot/Player.gd +++ b/godot/Player.gd @@ -1,6 +1,5 @@ extends CharacterBody3D - const SPEED = 5.0 const JUMP_VELOCITY = 4.5 @export var horiz_sens = 0.25 @@ -13,9 +12,6 @@ var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") func _ready(): Input.mouse_mode = Input.MOUSE_MODE_CAPTURED - playerCam = get_node("CameraMount") as Node3D - playerVisuals = get_node("grape_man") as Node3D - func _process(delta): # Brings your mouse out of the window if you press escape. Add pause screen function here? @@ -28,7 +24,7 @@ func _input(event): if event is InputEventMouseMotion: # Side to side, first player object then player visuals. rotate_y(deg_to_rad(-event.relative.x * horiz_sens)) - playerVisuals.rotate_y(deg_to_rad(event.relative.x * horiz_sens)) + $grape_man.rotate_y(deg_to_rad(event.relative.x * horiz_sens)) # Vertical camera rotation. playerCam.rotate_x(deg_to_rad(-event.relative.y * vert_sens)) diff --git a/godot/Villager.gd b/godot/Villager.gd index 4cac191..a29d2ff 100644 --- a/godot/Villager.gd +++ b/godot/Villager.gd @@ -1,67 +1,82 @@ -extends Node3D +extends CharacterBody3D var rng = RandomNumberGenerator.new() -var spin_amount = 0 -var spinning = false -var wait_time = 0 -var walking = false -var walk_dist = 0 +@export var target_location_xz = global_transform.origin * Vector3(1, 0, 1) +@export var location_xz = global_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.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) + +# 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. + pass # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): - var task = "idle" + location_xz = global_transform.origin * Vector3(1, 0, 1) + direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized() match task: "idle": - task = rng.randi_range(0, 100) + task = rng.randf_range(0, 100) - if task <= 33: + # 3% chance to walk somewhere. + if task <= walk_chance: task = "walk" - elif task > 33 and task <= 50: + target_location_xz = global_transform.origin * Vector3(1, 0, 1) + (direction_xz * 16) + # 6% chance to rotate. + elif task > walk_chance and task <= (walk_chance + spin_chance): task = "spin" - elif task > 50: - task = "wait" + 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" "walk": - if !walking: - walk_dist = rng.randi_range(0, 20) - walking = true - - if walk_dist > 0: - # Small change to turn a little while walking. - var spin_chance = rng.randi_range(0, 10) - if spin_chance <= 1: - rotate_y(5) - + # 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": - 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 - + task = "idle" + +func _physics_process(delta): + # Add the gravity. + if not is_on_floor(): + velocity.y -= gravity * delta + + # Walk to target location. + if location_xz.distance_to(target_location_xz) > (Vector2(direction_xz.x, direction_xz.z) * walk_speed).length(): + velocity.x = direction_xz.x * walk_speed + velocity.z = direction_xz.z * walk_speed + + move_and_slide() diff --git a/godot/villager.tscn b/godot/villager.tscn index d118819..118e359 100644 --- a/godot/villager.tscn +++ b/godot/villager.tscn @@ -6,13 +6,11 @@ [sub_resource type="CapsuleMesh" id="CapsuleMesh_b3srs"] -[node name="Node3D" type="Node3D"] +[node name="Villager" type="CharacterBody3D"] script = ExtResource("1_ardgw") -[node name="RigidBody3D" type="RigidBody3D" parent="."] - -[node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D"] +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] shape = SubResource("CapsuleShape3D_s4x8n") -[node name="MeshInstance3D" type="MeshInstance3D" parent="RigidBody3D/CollisionShape3D"] +[node name="MeshInstance3D" type="MeshInstance3D" parent="CollisionShape3D"] mesh = SubResource("CapsuleMesh_b3srs")