Updates to Villager and Update to Main

This commit is contained in:
ncusimano 2023-08-19 16:08:58 -06:00
parent e52f893517
commit a8465d9962
3 changed files with 63 additions and 54 deletions

View File

@ -1,6 +1,5 @@
extends CharacterBody3D extends CharacterBody3D
const SPEED = 5.0 const SPEED = 5.0
const JUMP_VELOCITY = 4.5 const JUMP_VELOCITY = 4.5
@export var horiz_sens = 0.25 @export var horiz_sens = 0.25
@ -13,9 +12,6 @@ var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready(): func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
playerCam = get_node("CameraMount") as Node3D
playerVisuals = get_node("grape_man") as Node3D
func _process(delta): func _process(delta):
# Brings your mouse out of the window if you press escape. Add pause screen function here? # 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: if event is InputEventMouseMotion:
# Side to side, first player object then player visuals. # Side to side, first player object then player visuals.
rotate_y(deg_to_rad(-event.relative.x * horiz_sens)) 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. # Vertical camera rotation.
playerCam.rotate_x(deg_to_rad(-event.relative.y * vert_sens)) playerCam.rotate_x(deg_to_rad(-event.relative.y * vert_sens))

View File

@ -1,67 +1,82 @@
extends Node3D extends CharacterBody3D
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var spin_amount = 0 @export var target_location_xz = global_transform.origin * Vector3(1, 0, 1)
var spinning = false @export var location_xz = global_transform.origin * Vector3(1, 0, 1)
var wait_time = 0 @export var target_direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
var walking = false @export var direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
var walk_dist = 0 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. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
pass # Replace with function body. pass
# 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):
var task = "idle" location_xz = global_transform.origin * Vector3(1, 0, 1)
direction_xz = (transform.basis * Vector3(1, 0, 1)).normalized()
match task: match task:
"idle": "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" 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" task = "spin"
elif task > 50: var rotation_angle = rng.randf_range(-2*PI, 2*PI)
task = "wait" 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": "walk":
if !walking: # Get the direction to the target in z-x plane.
walk_dist = rng.randi_range(0, 20) target_direction_xz = (location_xz.direction_to(target_location_xz) * Vector3(1, 0, 1)).normalized()
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)
# 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": "spin":
if !spinning: task = "idle"
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
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()

View File

@ -6,13 +6,11 @@
[sub_resource type="CapsuleMesh" id="CapsuleMesh_b3srs"] [sub_resource type="CapsuleMesh" id="CapsuleMesh_b3srs"]
[node name="Node3D" type="Node3D"] [node name="Villager" type="CharacterBody3D"]
script = ExtResource("1_ardgw") script = ExtResource("1_ardgw")
[node name="RigidBody3D" type="RigidBody3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
[node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D"]
shape = SubResource("CapsuleShape3D_s4x8n") shape = SubResource("CapsuleShape3D_s4x8n")
[node name="MeshInstance3D" type="MeshInstance3D" parent="RigidBody3D/CollisionShape3D"] [node name="MeshInstance3D" type="MeshInstance3D" parent="CollisionShape3D"]
mesh = SubResource("CapsuleMesh_b3srs") mesh = SubResource("CapsuleMesh_b3srs")