From 69a918d4ece2544c9d385f4815bdfecf006c434f Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 28 Feb 2024 21:44:24 +0100 Subject: [PATCH] Added chat bubbles --- client/Main.gd | 4 +++ client/network/Chat.gd | 12 ++++--- client/player/Player.gd | 2 ++ client/player/Player.tscn | 14 +++++++- client/player/chat/ChatBubble.gd | 10 ++++++ client/player/chat/ChatBubble.tscn | 46 +++++++++++++++++++++++++++ client/player/chat/ChatComponent.gd | 46 +++++++++++++++++++++++++++ client/player/chat/ChatComponent.tscn | 28 ++++++++++++++++ client/player/chat/LabelSettings.tres | 6 ++++ client/player/hud/HealthBar.gd | 2 +- client/ui/chat/Chat.gd | 4 +-- client/ui/theme/stylebox/Panel.tres | 2 +- 12 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 client/player/chat/ChatBubble.gd create mode 100644 client/player/chat/ChatBubble.tscn create mode 100644 client/player/chat/ChatComponent.gd create mode 100644 client/player/chat/ChatComponent.tscn create mode 100644 client/player/chat/LabelSettings.tres diff --git a/client/Main.gd b/client/Main.gd index 9d42843..94a1ebd 100644 --- a/client/Main.gd +++ b/client/Main.gd @@ -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) diff --git a/client/network/Chat.gd b/client/network/Chat.gd index afcd02c..39ac137 100644 --- a/client/network/Chat.gd +++ b/client/network/Chat.gd @@ -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]") diff --git a/client/player/Player.gd b/client/player/Player.gd index 1413fbe..16033d1 100644 --- a/client/player/Player.gd +++ b/client/player/Player.gd @@ -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) diff --git a/client/player/Player.tscn b/client/player/Player.tscn index d74b878..7eb1efc 100644 --- a/client/player/Player.tscn +++ b/client/player/Player.tscn @@ -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 diff --git a/client/player/chat/ChatBubble.gd b/client/player/chat/ChatBubble.gd new file mode 100644 index 0000000..5f33f0a --- /dev/null +++ b/client/player/chat/ChatBubble.gd @@ -0,0 +1,10 @@ +class_name ChatBubble +extends Control + +var label: Label + +func _ready(): + label = %Label + +func show_message(message: String): + label.text = message diff --git a/client/player/chat/ChatBubble.tscn b/client/player/chat/ChatBubble.tscn new file mode 100644 index 0000000..e904697 --- /dev/null +++ b/client/player/chat/ChatBubble.tscn @@ -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 diff --git a/client/player/chat/ChatComponent.gd b/client/player/chat/ChatComponent.gd new file mode 100644 index 0000000..246c8cb --- /dev/null +++ b/client/player/chat/ChatComponent.gd @@ -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 \ No newline at end of file diff --git a/client/player/chat/ChatComponent.tscn b/client/player/chat/ChatComponent.tscn new file mode 100644 index 0000000..142d4b0 --- /dev/null +++ b/client/player/chat/ChatComponent.tscn @@ -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 diff --git a/client/player/chat/LabelSettings.tres b/client/player/chat/LabelSettings.tres new file mode 100644 index 0000000..f508340 --- /dev/null +++ b/client/player/chat/LabelSettings.tres @@ -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) diff --git a/client/player/hud/HealthBar.gd b/client/player/hud/HealthBar.gd index f773c09..d1500e5 100644 --- a/client/player/hud/HealthBar.gd +++ b/client/player/hud/HealthBar.gd @@ -18,4 +18,4 @@ func on_health_changed(): elif value < 0.75: texture_progress = medium else: - texture_progress = high \ No newline at end of file + texture_progress = high diff --git a/client/ui/chat/Chat.gd b/client/ui/chat/Chat.gd index caf00dd..2c3a820 100644 --- a/client/ui/chat/Chat.gd +++ b/client/ui/chat/Chat.gd @@ -3,5 +3,5 @@ extends VBoxContainer signal message_submitted(message: String) -func add_message(message: String): - %Messages.append_text("%s\n" % message) \ No newline at end of file +func add_message(player: Player, message: String): + %Messages.append_text("[color=#e0e0e0]%s:[/color] %s\n" % [player.name, message]) \ No newline at end of file diff --git a/client/ui/theme/stylebox/Panel.tres b/client/ui/theme/stylebox/Panel.tres index 141f3ab..100fed9 100644 --- a/client/ui/theme/stylebox/Panel.tres +++ b/client/ui/theme/stylebox/Panel.tres @@ -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)