Tough_Crowd/godot/scenes/crowd/person.gd

77 lines
2.1 KiB
GDScript3
Raw Normal View History

2024-01-27 11:02:06 +00:00
class_name AudienceMember
2024-01-26 21:10:03 +00:00
extends Sprite2D
2024-01-27 11:02:06 +00:00
@export var mood : float = 0.
2024-01-26 21:10:03 +00:00
@export var head : Node2D
@export var face : Node2D
2024-01-27 11:02:06 +00:00
@export var profile : AudienceProfile
@export_enum("angry", "neutral", "happy", "laugh") var expression
2024-01-26 21:10:03 +00:00
var known_faces : Array[String] = [
"res://scenes/faces/face_curly.tscn",
2024-01-27 12:36:08 +00:00
"res://scenes/faces/face_jenny.tscn",
2024-01-26 21:10:03 +00:00
"res://scenes/faces/face_moritz.tscn",
"res://scenes/faces/face_ronald.tscn",
# Add more scenes as needed
]
2024-01-27 12:30:02 +00:00
const laughter_duration : float = 2. # seconds
const laughter_bobs : int = 4
var laughter_left : float = 0.
2024-01-26 21:10:03 +00:00
# Called when the node enters the scene tree for the first time.
func _ready():
2024-01-27 12:30:02 +00:00
laughter_left = 0.
2024-01-27 11:02:06 +00:00
mood = randf_range(profile.lashout_threshold, profile.happy_threshold)
update_expression()
2024-01-26 21:10:03 +00:00
if face == null:
set_random_face()
# Called every frame. 'delta' is the elapsed time since the previous frame.
2024-01-27 11:02:06 +00:00
func _process(delta):
2024-01-27 12:30:02 +00:00
if laughter_left >= 0:
laughter_left -= delta
if mood > profile.happy_threshold * .5:
mood -= profile.happiness_decay * delta
elif mood < profile.lashout_threshold * .9:
mood += profile.lashout_decay * delta
if Input.is_key_pressed(KEY_SPACE) and OS.is_debug_build():
update_mood(1.)
update_expression()
2024-01-27 11:02:06 +00:00
func hear_joke():
var change = randf_range(-3., 3.)
update_mood(change)
func update_mood(change: float):
2024-01-27 12:30:02 +00:00
if mood > profile.happy_threshold and change > 0 and laughter_left <= 0:
laughter_left = laughter_duration
# bob head
var tween = get_tree().create_tween().bind_node(self).set_loops(laughter_bobs)
var bob_duration = laughter_duration / laughter_bobs / 2
tween.tween_property(face, "position", Vector2.UP * 20, bob_duration).set_delay(randf_range(0, bob_duration))
tween.tween_property(face, "position", Vector2.ZERO, bob_duration)
2024-01-27 11:02:06 +00:00
mood += change
func update_expression():
2024-01-27 12:30:02 +00:00
if laughter_left > 0:
expression = "laugh"
elif mood > profile.happy_threshold:
2024-01-27 11:02:06 +00:00
expression = "happy"
elif mood < profile.angry_threshold:
expression = "angry"
else:
expression = "neutral"
2024-01-26 21:10:03 +00:00
func set_random_face():
var face_res = load(known_faces[randi() % known_faces.size()])
face = face_res.instantiate()
head.add_child(face)