Almost working chat and basic networking test

pull/50/head
unfa 2021-06-06 01:58:04 +02:00
parent edda7abc6f
commit 627d865d26
6 changed files with 149 additions and 35 deletions

View File

@ -113,18 +113,19 @@ func _physics_process(delta):
medium = "air"
gravity_vec += Vector3.DOWN * gravity * delta
if Input.is_action_just_pressed("move_jump") and is_on_floor():
snap = Vector3.ZERO
gravity_vec = Vector3.UP * jump
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
if Input.is_action_pressed("move_backward"):
direction += transform.basis.z
if Input.is_action_pressed("move_left"):
direction -= transform.basis.x
if Input.is_action_pressed("move_right"):
direction += transform.basis.x
if input_active:
if Input.is_action_just_pressed("move_jump") and is_on_floor():
snap = Vector3.ZERO
gravity_vec = Vector3.UP * jump
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
if Input.is_action_pressed("move_backward"):
direction += transform.basis.z
if Input.is_action_pressed("move_left"):
direction -= transform.basis.x
if Input.is_action_pressed("move_right"):
direction += transform.basis.x
if direction.length() > 0: # normalized() will return a null
direction = direction.normalized()

View File

@ -1,6 +1,22 @@
extends Control
@onready var main = get_tree().get_root().get_node("Main")
@onready var player = main.player
class chat_message:
var sender_id := 0
var team := 0
var message := ''
func _init(sender_id, team, message):
self.sender_id = sender_id
self.team = team
self.message = message
@remotesync var message: chat_message:
set(new_message):
print("message changed")
$VBoxContainer/ChatHistory.text += "\n" + str(new_message.sender_id) + ' : ' + new_message.message
enum ChatState {INACTIVE, TYPING_ALL, TYPING_TEAM}
@ -14,31 +30,48 @@ var state = ChatState.INACTIVE :
$VBoxContainer/Typing.hide()
$VBoxContainer/Typing/LineEdit.release_focus()
1: #ChatState.TYPING_ALL:
$VBoxContainer/Typing/Label.text = "all: "
$VBoxContainer/Typing.show()
$VBoxContainer/Typing/LineEdit.grab_focus()
$VBoxContainer/Typing/LineEdit.text = ''
2: #ChatState.TYPING_TEAM:
$VBoxContainer/Typing/Label.text = "team: "
$VBoxContainer/Typing.show()
$VBoxContainer/Typing/LineEdit.grab_focus()
$VBoxContainer/Typing/LineEdit.text = ''
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
pass
func _input(event) -> void:
if Input.is_action_just_pressed("say_all"):
main.focus = 1 #main.GameFocus.CHAT
state = 1 #ChatState.TYPING_ALL
if Input.is_action_just_pressed("say_team"):
main.focus = 1 #main.GameFocus.CHAT
state = 2 #ChatState.TYPING_TEAM
if Input.is_action_just_pressed("UI_Accept") and state != 0: #ChatState.INACTIVE:
$VBoxContainer/ChatHistory.text.append($VBoxContainer/Typing/LineEdit.text)
$VBoxContainer/Typing/LineEdit.text.clear()
state = 0 #ChatState.INACTIVE
main.focus = 0 #main.GameFocus.GAME
if state == ChatState.INACTIVE:
if Input.is_action_just_pressed("say_all"):
main.focus = 2 #main.GameFocus.CHAT
state = 1 #ChatState.TYPING_ALL
if Input.is_action_just_pressed("say_team"):
main.focus = 2 #main.GameFocus.CHAT
state = 2 #ChatState.TYPING_TEAM
elif Input.is_action_just_pressed("say_cancel"):
main.focus = 1 #main.GameFocus.GAME
state = 0 #ChatState.INACTIVE
func _unhandled_input(event) -> void:
if state != 0: #ChatState.INACTIVE:
get_tree().get_root().set_input_as_handled()
# doesn't work due to missing RPC implementation in Godot 4
#@remotesync func chat_message(peer_id: int, message: String) -> void:
# var player = str(peer_id)
# $VBoxContainer/ChatHistory.text += '\n' + player + " | " + message
func _on_LineEdit_text_entered(new_text):
# RPC is currently not implemented in the engine
var sender_id = get_tree().get_network_unique_id()
var new_message = chat_message.new(sender_id, 0, new_text)
rset("message", new_message)
#chat_message.rpc(get_tree().get_network_unique_id(), $VBoxContainer/Typing/LineEdit.text)
$VBoxContainer/Typing/LineEdit.text = ''
state = 0 #ChatState.INACTIVE
main.focus = 0 #main.GameFocus.GAME

View File

@ -206,8 +206,9 @@ __meta__ = {
[node name="LineEdit" type="LineEdit" parent="Chat/VBoxContainer/Typing"]
offset_left = 38.0
offset_right = 108.0
offset_right = 238.0
offset_bottom = 33.0
rect_min_size = Vector2( 200, 0 )
context_menu_enabled = false
virtual_keyboard_enabled = false
shortcut_keys_enabled = false
@ -218,3 +219,5 @@ script = null
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="text_entered" from="Chat/VBoxContainer/Typing/LineEdit" to="Chat" method="_on_LineEdit_text_entered"]

View File

@ -2,22 +2,66 @@ extends Node
enum GameFocus {MENU, GAME, CHAT, AWAY}
const NET_PORT = 12597
var peer = NetworkedMultiplayerENet.new()
@onready var gui = $GUI
@onready var hud = $HUD
@onready var player = $Level/Player
@onready var chat = hud.get_node("Chat")
var focus = GameFocus.MENU
var focus = GameFocus.MENU :
set(new_focus):
match new_focus:
0:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
gui.show()
player.input_active = false
1:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
gui.hide()
player.input_active = true
2:
player.input_active = false
3:
player.input_active = true
func _input(event) -> void:
focus = new_focus
func _input(_event) -> void:
if Input.is_action_just_pressed("ui_cancel"):
if focus == GameFocus.GAME:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
gui.show()
player.input_active = false
focus = GameFocus.MENU
elif focus == GameFocus.MENU:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
gui.hide()
player.input_active = true
focus = GameFocus.GAME
func _on_Host_pressed():
peer.create_server(NET_PORT, 16)
get_tree().network_peer = peer
func _on_Connect_pressed():
peer.create_client('localhost', NET_PORT)
get_tree().network_peer = peer
func _player_connected(id) -> void:
print("player connected, id: ", id)
func _player_disconnected(id) -> void:
print("player disconnected, id: ", id)
func _connected_ok() -> void:
print("connected to server")
func _connected_fail() -> void:
print("connection to server failed")
func _server_disconnected() -> void:
print("server disconnected")
func _ready() -> void:
get_tree().connect("network_peer_connected", self._player_connected)
get_tree().connect("network_peer_disconnected", self._player_disconnected)
get_tree().connect("connected_to_server", self._connected_ok)
get_tree().connect("connection_failed", self._connected_fail)
get_tree().connect("server_disconnected", self._server_disconnected)

View File

@ -13,3 +13,31 @@ script = ExtResource( 3 )
[node name="GUI" parent="." instance=ExtResource( 2 )]
[node name="Level" parent="." instance=ExtResource( 1 )]
[node name="NetworkTesting" type="VBoxContainer" parent="."]
anchor_bottom = 1.0
offset_right = 100.0
offset_bottom = -500.0
script = null
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Host" type="Button" parent="NetworkTesting"]
offset_right = 100.0
offset_bottom = 29.0
text = "Host"
script = null
[node name="Connect" type="Button" parent="NetworkTesting"]
offset_top = 33.0
offset_right = 100.0
offset_bottom = 62.0
text = "Connect"
script = null
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="pressed" from="NetworkTesting/Host" to="." method="_on_Host_pressed"]
[connection signal="pressed" from="NetworkTesting/Connect" to="." method="_on_Connect_pressed"]

View File

@ -116,6 +116,11 @@ say_send={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"pressed":false,"keycode":16777221,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
]
}
say_cancel={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"pressed":false,"keycode":16777217,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
]
}
[rendering]