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
@export var mood : float = 0.
@export var head : Node2D
@export var face : Node2D
@export var profile : AudienceProfile
@export_enum("angry", "neutral", "happy", "laugh") var expression
var known_faces : Array[String] = [
"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.
func _ready():
pass # Replace with function body.
mood = randf_range(profile.lashout_threshold, profile.happy_threshold)
update_expression()
if face == null:
set_random_face()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func _process(delta):
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():
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/audience_profile.gd" id="2_bsodr"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_kbo53"]
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")
script = ExtResource("1_mx0i8")
head = NodePath("Head")
profile = NodePath("Profile")
expression = 1
[node name="Head" type="Node2D" parent="."]
position = Vector2(0, -16)
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
@export var mood : int = 0
var last_mood : int = 0
@export_enum("angry", "neutral", "happy", "laugh") var expression
@export var sprite_laugh : Sprite2D
@export var sprite_happy : Sprite2D
@export var sprite_neutral : 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_visible : Color = Color(1, 1, 1, 1)
# Called when the node enters the scene tree for the first time.
func _ready():
mood = 0
last_mood = 0
sprite_happy.modulate.a = 0
sprite_neutral.modulate.a = 1
sprite_unhappy.modulate.a = 0
# Called every frame. 'delta' is the elapsed time since the previous frame.
person_controller = null
expression = person_controller.expression
reset_face()
func _process(_delta):
if mood != last_mood:
var tween = get_tree().create_tween().bind_node(self).set_parallel().set_trans(Tween.TRANS_LINEAR)
if mood < 0:
tween.tween_property(sprite_unhappy, "modulate", color_visible, tween_speed)
tween.tween_property(sprite_neutral, "modulate", color_transparent, tween_speed)
tween.tween_property(sprite_happy, "modulate", color_transparent, tween_speed)
elif mood > 0:
tween.tween_property(sprite_unhappy, "modulate", color_transparent, tween_speed)
tween.tween_property(sprite_neutral, "modulate", color_transparent, tween_speed)
tween.tween_property(sprite_happy, "modulate", color_visible, tween_speed)
if expression != person_controller.expression:
update_face(person_controller.expression)
expression != person_controller.expression
func reset_face():
for expr in expression_map:
if expr == expression:
expression_map[expr].modulate = color_visible
else:
tween.tween_property(sprite_unhappy, "modulate", color_transparent, tween_speed)
tween.tween_property(sprite_neutral, "modulate", color_visible, tween_speed)
tween.tween_property(sprite_happy, "modulate", color_transparent, tween_speed)
last_mood = mood
expression_map[expr].modulate = color_transparent
func update_face(new_expression: String):
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