Added chat bubbles

This commit is contained in:
Eduard Urbach 2024-02-28 21:44:24 +01:00
parent db3ee3a685
commit 69a918d4ec
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
12 changed files with 167 additions and 9 deletions

View File

@ -24,6 +24,7 @@ func connect_events():
%Client.upload_changed.connect(ui.network.set_upload)
%Chat.message_received.connect(ui.chat.add_message)
%Chat.message_received.connect(show_chat_bubble)
ui.chat.message_submitted.connect( %Chat.send_message)
func start():
@ -45,6 +46,9 @@ func start_offline():
func start_online():
%Login.send_login()
func show_chat_bubble(player: Player, message: String):
player.chat.show_message(message)
func pause(enabled: bool):
get_tree().paused = enabled
Audio.mute(enabled)

View File

@ -1,6 +1,6 @@
extends PacketHandler
signal message_received(message: String)
signal message_received(player: Player, message: String)
func send_message(message: String):
var buffer := StreamPeerBuffer.new()
@ -14,14 +14,18 @@ func handle_packet(data: PackedByteArray):
var player_id := buffer.get_string()
var player := Global.players.get_player(player_id)
if !player:
return
var message := buffer.get_utf8_string()
message = escape_bbcode(message)
Log.info("%s: %s" % [player.name, message])
emit.call_deferred(player.name, message)
emit.call_deferred(player, message)
func emit(player_name: String, message: String):
message_received.emit("[color=#e0e0e0]%s:[/color] %s" % [player_name, message])
func emit(player: Player, message: String):
message_received.emit(player, message)
func escape_bbcode(text: String) -> String:
return text.replace("[", "[lb]")

View File

@ -10,6 +10,7 @@ var state: StateComponent
var performance: PerformanceComponent
var animation: AnimationComponent
var physics: CharacterBody3D
var chat: ChatComponent
func _enter_tree():
movement = $Movement
@ -17,6 +18,7 @@ func _enter_tree():
performance = $Performance
animation = $Animation
physics = $Physics
chat = $Chat
## Name
signal name_changed(new_name: String)

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=32 format=3 uid="uid://2lcnu3dy54lx"]
[gd_scene load_steps=33 format=3 uid="uid://2lcnu3dy54lx"]
[ext_resource type="Script" path="res://player/Player.gd" id="1_8gebs"]
[ext_resource type="PackedScene" uid="uid://c8j7t4yg7anb0" path="res://assets/female/Female.blend" id="2_8nah6"]
@ -12,6 +12,7 @@
[ext_resource type="PackedScene" uid="uid://6jpnl6c4fdvo" path="res://player/hud/HUDComponent.tscn" id="7_fwgtd"]
[ext_resource type="PackedScene" uid="uid://qecdmrg6mbws" path="res://assets/swords/heirloom/Heirloom.blend" id="7_u8433"]
[ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://player/movement/MovementComponent.tscn" id="8_25qd0"]
[ext_resource type="PackedScene" uid="uid://urt8b7grv0qq" path="res://player/chat/ChatComponent.tscn" id="8_pmnom"]
[ext_resource type="PackedScene" uid="uid://clnfp813k7lep" path="res://player/footsteps/FootstepsComponent.tscn" id="8_xklbo"]
[ext_resource type="PackedScene" uid="uid://d0onbq0ad1ap4" path="res://player/rotation/RotationComponent.tscn" id="9_agxqu"]
[ext_resource type="PackedScene" uid="uid://bivxnxwi863o0" path="res://player/animation/AnimationComponent.tscn" id="10_bcaeg"]
@ -105,12 +106,23 @@ bone_idx = 44
transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0.197644, 0, 0)
[node name="HUD" parent="." node_paths=PackedStringArray("health") instance=ExtResource("7_fwgtd")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.8, 0)
visible = false
layers = 512
visibility_range_end = 50.0
visibility_range_end_margin = 10.0
visibility_range_fade_mode = 1
health = NodePath("../Health")
[node name="Chat" parent="." instance=ExtResource("8_pmnom")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.37, 0)
visible = false
layers = 512
visibility_range_end = 50.0
visibility_range_end_margin = 10.0
visibility_range_fade_mode = 1
offset = Vector2(200, -44)
[node name="Attractor" type="GPUParticlesAttractorSphere3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
strength = 15.0

View File

@ -0,0 +1,10 @@
class_name ChatBubble
extends Control
var label: Label
func _ready():
label = %Label
func show_message(message: String):
label.text = message

View File

@ -0,0 +1,46 @@
[gd_scene load_steps=4 format=3 uid="uid://epb22atgo7af"]
[ext_resource type="Script" path="res://player/chat/ChatBubble.gd" id="1_dscmj"]
[ext_resource type="LabelSettings" uid="uid://cvqtha83d263j" path="res://player/chat/LabelSettings.tres" id="1_u5tc1"]
[ext_resource type="Texture2D" uid="uid://d22tgtevi4ukp" path="res://assets/ui/chat/speech-bubble-blur.png" id="2_e5alo"]
[node name="ChatBubble" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_dscmj")
[node name="TextureRect" type="TextureRect" parent="."]
layout_mode = 1
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -512.0
offset_right = 512.0
grow_vertical = 0
texture = ExtResource("2_e5alo")
[node name="Label" type="Label" parent="."]
unique_name_in_owner = true
layout_mode = 1
anchors_preset = -1
anchor_left = -0.00520833
anchor_top = 1.00556
anchor_right = 0.00885417
anchor_bottom = 0.991667
offset_left = 82.0
offset_top = -374.0
offset_right = 427.0
offset_bottom = -139.0
grow_vertical = 0
text = "I think we gotta do this in hard mode."
label_settings = ExtResource("1_u5tc1")
horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 3
clip_text = true
text_overrun_behavior = 3
metadata/_edit_use_anchors_ = true

View File

@ -0,0 +1,46 @@
class_name ChatComponent
extends Sprite3D
@export var duration: float
@export var alpha_curve: Curve
var sub_viewport: SubViewport
var bubble: ChatBubble
var time := 0.0
func _ready():
sub_viewport = %SubViewport
bubble = %ChatBubble
assert(alpha_curve)
assert(duration > 0)
assert(sub_viewport)
assert(bubble)
texture = sub_viewport.get_texture()
func show_message(message: String):
bubble.show_message(message)
time = 0
set_enabled(true)
func _process(delta):
time += delta
if time > duration:
time = duration
set_enabled(false)
modulate.a = alpha_curve.sample(time / duration)
func set_enabled(enabled: bool):
visible = enabled
set_process(enabled)
match enabled:
true:
sub_viewport.render_target_clear_mode = SubViewport.CLEAR_MODE_ALWAYS
sub_viewport.render_target_update_mode = SubViewport.UPDATE_WHEN_VISIBLE
false:
sub_viewport.render_target_clear_mode = SubViewport.CLEAR_MODE_NEVER
sub_viewport.render_target_update_mode = SubViewport.UPDATE_DISABLED

View File

@ -0,0 +1,28 @@
[gd_scene load_steps=4 format=3 uid="uid://urt8b7grv0qq"]
[ext_resource type="Script" path="res://player/chat/ChatComponent.gd" id="1_3o8hk"]
[ext_resource type="PackedScene" uid="uid://epb22atgo7af" path="res://player/chat/ChatBubble.tscn" id="2_yfr7m"]
[sub_resource type="Curve" id="Curve_426w0"]
_data = [Vector2(0, 0), 0.0, 0.0, 0, 0, Vector2(0.05, 1), 0.0, 0.0, 0, 0, Vector2(0.8, 1), 0.0, 0.0, 0, 0, Vector2(1, 0), 0.0, 0.0, 0, 0]
point_count = 4
[node name="Chat" type="Sprite3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.83994, 0)
centered = false
offset = Vector2(400, -30)
pixel_size = 0.001
billboard = 1
render_priority = 1
region_rect = Rect2(0, 0, 84, 26)
script = ExtResource("1_3o8hk")
duration = 3.5
alpha_curve = SubResource("Curve_426w0")
[node name="SubViewport" type="SubViewport" parent="."]
unique_name_in_owner = true
disable_3d = true
transparent_bg = true
[node name="ChatBubble" parent="SubViewport" instance=ExtResource("2_yfr7m")]
unique_name_in_owner = true

View File

@ -0,0 +1,6 @@
[gd_resource type="LabelSettings" format=3 uid="uid://cvqtha83d263j"]
[resource]
font_size = 54
font_color = Color(0.12549, 0.12549, 0.12549, 1)
shadow_color = Color(0, 0, 0, 0.12549)

View File

@ -18,4 +18,4 @@ func on_health_changed():
elif value < 0.75:
texture_progress = medium
else:
texture_progress = high
texture_progress = high

View File

@ -3,5 +3,5 @@ extends VBoxContainer
signal message_submitted(message: String)
func add_message(message: String):
%Messages.append_text("%s\n" % message)
func add_message(player: Player, message: String):
%Messages.append_text("[color=#e0e0e0]%s:[/color] %s\n" % [player.name, message])

View File

@ -12,4 +12,4 @@ texture_margin_left = 24.0
texture_margin_top = 24.0
texture_margin_right = 24.0
texture_margin_bottom = 24.0
modulate_color = Color(1, 1, 1, 0.752941)
modulate_color = Color(1, 1, 1, 0.878431)