Tough_Crowd/godot/scenes/crowd/person.gd

202 lines
5.3 KiB
GDScript3
Raw Permalink Normal View History

2024-01-27 11:02:06 +00:00
class_name AudienceMember
2024-01-27 13:08:40 +00:00
extends Node2D
2024-01-26 21:10:03 +00:00
2024-01-27 11:02:06 +00:00
@export var mood : float = 0.
2024-01-27 14:40:59 +00:00
@export_enum("blue", "green", "red") var color
2024-01-27 11:02:06 +00:00
2024-01-26 21:10:03 +00:00
@export var head : Node2D
@export var face : Node2D
2024-01-27 14:40:59 +00:00
@export var body : Node2D
@export var body_shape : 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 is_listening : bool = false
var last_joke_heard : Joke.JokeType = -1
2024-01-26 21:10:03 +00:00
var known_faces : Array[String] = [
2024-01-28 14:50:58 +00:00
"res://scenes/faces/face_curly.tscn",
"res://scenes/faces/face_jenny.tscn",
"res://scenes/faces/face_moritz.tscn",
"res://scenes/faces/face_ronald.tscn",
# Add more scenes as needed
2024-01-26 21:10:03 +00:00
]
2024-01-27 14:40:59 +00:00
var known_bodies = {
2024-01-28 14:50:58 +00:00
"blue" : [
"res://scenes/bodies/body_blue_1.tscn",
"res://scenes/bodies/body_blue_2.tscn",
"res://scenes/bodies/body_blue_3.tscn",
# Add more scenes as needed
],
"green" : [
"res://scenes/bodies/body_green_1.tscn",
"res://scenes/bodies/body_green_2.tscn",
"res://scenes/bodies/body_green_3.tscn",
# Add more scenes as needed
],
"red" : [
"res://scenes/bodies/body_red_1.tscn",
"res://scenes/bodies/body_red_2.tscn",
"res://scenes/bodies/body_red_3.tscn",
# Add more scenes as needed
],
2024-01-27 14:40:59 +00:00
}
2024-01-28 14:16:29 +00:00
var buh_sounds = [
2024-01-28 14:50:58 +00:00
preload("res://audio/buh_1.wav"),
preload("res://audio/buh_2.wav"),
preload("res://audio/buh_3.wav"),
preload("res://audio/buh_4.wav"),
2024-01-28 14:16:29 +00:00
]
var laugh_sounds = [
2024-01-28 14:50:58 +00:00
preload("res://audio/haha_1.wav"),
preload("res://audio/haha_2.wav"),
preload("res://audio/haha_3.wav"),
preload("res://audio/haha_4.wav"),
2024-01-28 14:16:29 +00:00
]
var cough_sounds = [
2024-01-28 14:50:58 +00:00
preload("res://audio/cough_1.wav"),
preload("res://audio/cough_2.wav"),
preload("res://audio/cough_3.wav"),
2024-01-28 14:16:29 +00:00
]
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-27 15:45:22 +00:00
func _map_color_to_profile_data_index(color):
2024-01-28 14:50:58 +00:00
match color:
"green":
return 0
"blue":
return 1
"red":
return 2
return 0
2024-01-27 15:45:22 +00:00
static func get_random_color():
2024-01-28 14:50:58 +00:00
var keys = ["blue", "green", "red"]
return keys[randi() % keys.size()]
2024-01-28 14:16:29 +00:00
func get_random_cough():
2024-01-28 14:50:58 +00:00
return cough_sounds[randi() % cough_sounds.size()]
2024-01-28 14:16:29 +00:00
func get_random_buh():
2024-01-28 14:50:58 +00:00
return buh_sounds[randi() % buh_sounds.size()]
2024-01-28 14:16:29 +00:00
func get_random_laugh():
2024-01-28 14:50:58 +00:00
return laugh_sounds[randi() % laugh_sounds.size()]
2024-01-27 15:45:22 +00:00
2024-01-26 21:10:03 +00:00
# Called when the node enters the scene tree for the first time.
func _ready():
2024-01-28 14:50:58 +00:00
laughter_left = 0.
mood = randf_range(profile.lashout_threshold, profile.happy_threshold)
update_expression()
2024-01-28 14:50:58 +00:00
var profile_index = _map_color_to_profile_data_index(color)
var profile_data = AudienceProfile.get_profile_data(profile_index)
profile.load_data(profile_data)
2024-01-28 14:50:58 +00:00
if body_shape == null:
set_random_body(color)
2024-01-28 14:50:58 +00:00
if face == null:
set_random_face()
2024-01-26 21:10:03 +00:00
# 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-28 14:50:58 +00:00
if laughter_left >= 0:
laughter_left -= delta
2024-01-28 13:45:47 +00:00
if not is_listening and mood > profile.happy_threshold * .9:
2024-01-27 12:30:02 +00:00
mood -= profile.happiness_decay * delta
elif not is_listening and mood < profile.lashout_threshold * .9:
2024-01-27 12:30:02 +00:00
mood += profile.lashout_decay * delta
2024-01-28 14:50:58 +00:00
if mood < profile.lashout_threshold:
throw_bottle()
2024-01-28 14:50:58 +00:00
update_expression()
2024-01-27 11:02:06 +00:00
func _input(event):
2024-01-28 14:50:58 +00:00
var just_pressed = event.is_pressed() and not event.is_echo()
if just_pressed and OS.is_debug_build():
if Input.is_key_pressed(KEY_SPACE):
update_mood(1.)
elif Input.is_key_pressed(KEY_ENTER):
update_mood(-1.)
2024-01-27 11:02:06 +00:00
func update_mood(change: float):
2024-01-28 13:45:47 +00:00
if laughter_left <= 0 and change > 0 and (mood + change) > profile.happy_threshold :
2024-01-27 12:30:02 +00:00
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-28 14:50:58 +00:00
$Speaker.stream = get_random_laugh()
$Speaker.play()
2024-01-28 14:16:29 +00:00
2024-01-28 14:50:58 +00:00
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-28 14:16:29 +00:00
2024-01-28 14:50:58 +00:00
mood += change
2024-01-27 11:02:06 +00:00
func update_expression():
2024-01-28 14:50:58 +00:00
if laughter_left > 0:
expression = "laugh"
elif mood > profile.happy_threshold:
expression = "happy"
elif mood < profile.angry_threshold:
expression = "angry"
else:
expression = "neutral"
if randi() % 25000 == 1:
$Speaker.stream = get_random_cough()
$Speaker.play()
2024-01-27 14:40:59 +00:00
func set_random_body(for_color):
2024-01-28 14:50:58 +00:00
if str(for_color) not in known_bodies:
var keys = known_bodies.keys()
for_color = keys[randi() % keys.size()]
print(keys.size())
var body_path = known_bodies[for_color][randi() % known_bodies[for_color].size()]
var body_res = load(body_path)
body_shape = body_res.instantiate()
body.add_child(body_shape)
2024-01-26 21:10:03 +00:00
func set_random_face():
2024-01-28 14:50:58 +00:00
var face_res = load(known_faces[randi() % known_faces.size()])
face = face_res.instantiate()
head.add_child(face)
2024-01-27 12:08:34 +00:00
func on_joke_start():
2024-01-28 14:50:58 +00:00
is_listening = true
func on_hear_joke(joke: Joke):
var mood_change = profile.joke_mood_mapping.get(joke.type, 0) * joke.effectiveness
2024-01-28 13:45:47 +00:00
if joke.type == last_joke_heard and mood_change > 0:
mood_change *= .5
update_mood(mood_change)
last_joke_heard = joke.type
func on_joke_finish():
2024-01-28 14:50:58 +00:00
is_listening = false
2024-01-27 14:50:20 +00:00
func throw_bottle():
2024-01-28 13:45:47 +00:00
mood = profile.lashout_threshold * 0.99
2024-01-28 14:16:29 +00:00
2024-01-28 14:50:58 +00:00
$Speaker.stream = get_random_buh()
$Speaker.play()
2024-01-27 14:50:20 +00:00
2024-01-28 14:50:58 +00:00
var bottle_scene = preload("res://scenes/objects/bottle.tscn")
var bottle = bottle_scene.instantiate()
2024-01-27 14:50:20 +00:00
2024-01-28 14:50:58 +00:00
add_child(bottle);