new mood logic

This commit is contained in:
Mikhail Aristov 2024-01-27 12:02:06 +01:00
parent bdb583d206
commit 6489729878
5 changed files with 120 additions and 57 deletions

View File

@ -0,0 +1,17 @@
class_name AudienceProfile
extends Node2D
@export var happy_threshold : float
@export var angry_threshold : float
@export var lashout_threshold : float
@export var happiness_decay : float
@export var lashout_decay : float
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass

View File

@ -1,7 +1,13 @@
class_name AudienceMember
extends Sprite2D extends Sprite2D
@export var mood : float = 0.
@export var head : Node2D @export var head : Node2D
@export var face : Node2D @export var face : Node2D
@export var profile : AudienceProfile
@export_enum("angry", "neutral", "happy", "laugh") var expression
var known_faces : Array[String] = [ var known_faces : Array[String] = [
"res://scenes/faces/face_curly.tscn", "res://scenes/faces/face_curly.tscn",
@ -12,13 +18,36 @@ var known_faces : Array[String] = [
# 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. mood = randf_range(profile.lashout_threshold, profile.happy_threshold)
update_expression()
if face == null: if face == null:
set_random_face() set_random_face()
# 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):
pass if mood > profile.happy_threshold:
mood = max(mood - profile.happiness_decay * delta, profile.happy_threshold)
elif mood < profile.lashout_threshold:
mood = min(mood + profile.lashout_decay * delta, profile.lashout_threshold)
func hear_joke():
var change = randf_range(-3., 3.)
update_mood(change)
func update_mood(change: float):
if mood > profile.happy_threshold and change > 0:
pass #toggle laugh
mood += change
update_expression()
func update_expression():
if mood > profile.happy_threshold:
expression = "happy"
elif mood < profile.angry_threshold:
expression = "angry"
else:
expression = "neutral"
func set_random_face(): func set_random_face():
var face_res = load(known_faces[randi() % known_faces.size()]) var face_res = load(known_faces[randi() % known_faces.size()])

View File

@ -1,15 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://cl4fax7fbh2g7"] [gd_scene load_steps=4 format=3 uid="uid://cl4fax7fbh2g7"]
[ext_resource type="Script" path="res://scenes/crowd/person.gd" id="1_mx0i8"] [ext_resource type="Script" path="res://scenes/crowd/person.gd" id="1_mx0i8"]
[ext_resource type="Script" path="res://scenes/crowd/audience_profile.gd" id="2_bsodr"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_kbo53"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_kbo53"]
size = Vector2(20, 30) size = Vector2(20, 30)
[node name="Person" type="Sprite2D" node_paths=PackedStringArray("head")] [node name="Person" type="Sprite2D" node_paths=PackedStringArray("head", "profile")]
texture = SubResource("PlaceholderTexture2D_kbo53") texture = SubResource("PlaceholderTexture2D_kbo53")
script = ExtResource("1_mx0i8") script = ExtResource("1_mx0i8")
head = NodePath("Head") head = NodePath("Head")
profile = NodePath("Profile")
expression = 1
[node name="Head" type="Node2D" parent="."] [node name="Head" type="Node2D" parent="."]
position = Vector2(0, -16) position = Vector2(0, -16)
scale = Vector2(0.3, 0.3) scale = Vector2(0.3, 0.3)
[node name="Profile" type="Node2D" parent="."]
script = ExtResource("2_bsodr")
happy_threshold = 3.0
angry_threshold = -3.0
lashout_threshold = -10.0
happiness_decay = 1.0
lashout_decay = 10.0

View File

@ -1,38 +1,44 @@
extends Node2D extends Node2D
@export var mood : int = 0 @export_enum("angry", "neutral", "happy", "laugh") var expression
var last_mood : int = 0
@export var sprite_laugh : Sprite2D
@export var sprite_happy : Sprite2D @export var sprite_happy : Sprite2D
@export var sprite_neutral : Sprite2D @export var sprite_neutral : Sprite2D
@export var sprite_unhappy : Sprite2D @export var sprite_unhappy : Sprite2D
const tween_speed : float = 1.5 var person_controller : AudienceMember
var expression_map = {
"angry" : sprite_unhappy,
"neutral" : sprite_neutral,
"happy" : sprite_happy,
"laugh" : sprite_laugh,
}
const expression_change_speed : float = 1.5
const color_transparent : Color = Color(1, 1, 1, 0) const color_transparent : Color = Color(1, 1, 1, 0)
const color_visible : Color = Color(1, 1, 1, 1) const color_visible : Color = Color(1, 1, 1, 1)
# 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():
mood = 0 person_controller = null
last_mood = 0 expression = person_controller.expression
sprite_happy.modulate.a = 0 reset_face()
sprite_neutral.modulate.a = 1
sprite_unhappy.modulate.a = 0
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta): func _process(_delta):
if mood != last_mood: if expression != person_controller.expression:
var tween = get_tree().create_tween().bind_node(self).set_parallel().set_trans(Tween.TRANS_LINEAR) update_face(person_controller.expression)
if mood < 0: expression != person_controller.expression
tween.tween_property(sprite_unhappy, "modulate", color_visible, tween_speed)
tween.tween_property(sprite_neutral, "modulate", color_transparent, tween_speed) func reset_face():
tween.tween_property(sprite_happy, "modulate", color_transparent, tween_speed) for expr in expression_map:
elif mood > 0: if expr == expression:
tween.tween_property(sprite_unhappy, "modulate", color_transparent, tween_speed) expression_map[expr].modulate = color_visible
tween.tween_property(sprite_neutral, "modulate", color_transparent, tween_speed)
tween.tween_property(sprite_happy, "modulate", color_visible, tween_speed)
else: else:
tween.tween_property(sprite_unhappy, "modulate", color_transparent, tween_speed) expression_map[expr].modulate = color_transparent
tween.tween_property(sprite_neutral, "modulate", color_visible, tween_speed)
tween.tween_property(sprite_happy, "modulate", color_transparent, tween_speed) func update_face(new_expression: String):
last_mood = mood var tween = get_tree().create_tween().bind_node(self).set_parallel().set_trans(Tween.TRANS_LINEAR)
tween.tween_property(expression_map[expression], "modulate", color_transparent, expression_change_speed)
tween.tween_property(expression_map[new_expression], "modulate", color_visible, expression_change_speed)

File diff suppressed because one or more lines are too long