diff --git a/godot/Models/grape_man.tscn b/godot/Models/grape_man.tscn index 56a4369..92f2645 100644 --- a/godot/Models/grape_man.tscn +++ b/godot/Models/grape_man.tscn @@ -154,7 +154,7 @@ transform = Transform3D(0.065, 0, 0, 0, 0.065, 0, 0, 0, 0.065, 0, 0, 0) script = ExtResource("2_ksje8") [node name="Skeleton3D" parent="rig" index="0"] -bones/0/rotation = Quaternion(0.0861766, 0, 0, 0.99628) +bones/0/rotation = Quaternion(0.10507, 0, 0, 0.994465) bones/1/rotation = Quaternion(-0.0891896, 1.18734e-07, -1.06322e-08, 0.996015) bones/2/rotation = Quaternion(0.0133933, -1.19199e-07, 1.5966e-09, 0.99991) bones/3/rotation = Quaternion(-0.00997737, 0.100365, -0.694951, 0.711948) diff --git a/godot/Player.gd b/godot/Player.gd index b7670f6..15bd727 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 @@ -26,6 +25,7 @@ func _process(_delta): func _input(event): # Handle the rotation of the camera using mouse movement. if event is InputEventMouseMotion: + # Side to side, first player object then player visuals. rotate_y(deg_to_rad(-event.relative.x * horiz_sens)) $CameraMount.rotate_x(deg_to_rad(-event.relative.y * vert_sens)) $grape_man.rotate_y(deg_to_rad(event.relative.x * horiz_sens)) diff --git a/godot/Villager.gd b/godot/Villager.gd index 91a9960..a29d2ff 100644 --- a/godot/Villager.gd +++ b/godot/Villager.gd @@ -1,55 +1,82 @@ -extends Node3D +extends CharacterBody3D var rng = RandomNumberGenerator.new() -var spin_amount = 0 -var spinning = false -var wait_time = 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": - 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": - 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")