-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move connection code to a top-level Game node
- Loading branch information
1 parent
794e3ca
commit 2976cf5
Showing
24 changed files
with
300 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
extends Node | ||
|
||
const MAX_REQUEST_ATTEMPTS := 3 | ||
const LevelScene: PackedScene = preload("res://src/World/Level.tscn") | ||
|
||
var level: Node | ||
|
||
var _server_request_attempts := 0 | ||
|
||
onready var main_menu := $MainMenu | ||
onready var character_menu := $CharacterMenu | ||
|
||
|
||
func create_level(player_data: Dictionary) -> void: | ||
level = LevelScene.instance() | ||
add_child(level) | ||
level.setup(player_data.name, player_data.color) | ||
|
||
|
||
# Asks the server to create a new character asynchronously. | ||
# Returns a dictionary with the player's name and color if it worked. | ||
# Otherwise, returns an empty dictionary. | ||
func create_character(name: String, color: Color) -> void: | ||
var result: int = yield(Connection.create_player_character_async(color, name), "completed") | ||
|
||
var data := {} | ||
if result == ERR_UNAVAILABLE: | ||
printerr("Character %s unavailable." % name) | ||
elif result == OK: | ||
data = {name = name, color = color} | ||
Connection.store_last_player_character_async(name, color) | ||
return data | ||
|
||
|
||
# Gets a player's data asynchronously by `index` from the server. | ||
func get_player(index: int) -> Dictionary: | ||
var characters: Array = yield(Connection.get_player_characters_async(), "completed") | ||
return characters[index] | ||
|
||
|
||
# Attempts to connect to the server, then to join the world match. | ||
func join_game_world() -> int: | ||
var result: int = yield(Connection.connect_to_server_async(), "completed") | ||
|
||
if result == OK: | ||
result = yield(Connection.join_world_async(), "completed") | ||
|
||
if result == OK: | ||
emit_signal("server_request_succeeded") | ||
else: | ||
emit_signal( | ||
"server_request_failed", "Error code %s: %s" % [result, Connection.error_message] | ||
) | ||
return result | ||
|
||
# Requests the server to authenticate the player using their credentials. | ||
# Attempts authentication up to `MAX_REQUEST_ATTEMPTS` times. | ||
func authenticate_user(email: String, password: String, do_remember_email: bool) -> void: | ||
var result := -1 | ||
|
||
while result != OK: | ||
if _server_request_attempts < MAX_REQUEST_ATTEMPTS: | ||
_server_request_attempts += 1 | ||
result = yield(_request_authentication(email, password, do_remember_email), "completed") | ||
else: | ||
break | ||
|
||
if result == OK: | ||
main_menu.hide() | ||
character_menu.show() | ||
else: | ||
main_menu.update_status("Error code %s: %s" % [result, Connection.error_message]) | ||
|
||
_server_request_attempts = 0 | ||
|
||
# Requests the server to authenticate the player using their credentials. | ||
func _request_authentication(email: String, password: String, do_remember_email := false) -> int: | ||
main_menu.is_enabled = false | ||
|
||
var result: int = yield(Connection.login_async(email, password), "completed") | ||
if result == OK and do_remember_email: | ||
Connection.save_email(email) | ||
|
||
main_menu.is_enabled = true | ||
return result | ||
|
||
|
||
func _on_CharacterMenu_new_character_requested(name: String, color: Color) -> void: | ||
create_character(name, color) | ||
|
||
|
||
func _on_MainMenu_login_pressed(email: String, password: String, do_remember_email: bool) -> void: | ||
authenticate_user(email, password, do_remember_email) | ||
|
||
|
||
func _on_MainMenu_register_pressed(email: String, password: String, do_remember_email: bool) -> void: | ||
main_menu.update_status("Authenticating...") | ||
main_menu.is_enabled = false | ||
|
||
var result: int = yield( | ||
Connection.register_async(email, password), "completed" | ||
) | ||
if result == OK: | ||
authenticate_user(email, password, do_remember_email) | ||
else: | ||
main_menu.update_status("Error code %s: %s" % [result, Connection.error_message]) | ||
|
||
main_menu.is_enabled = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[gd_scene load_steps=4 format=2] | ||
|
||
[ext_resource path="res://src/Main/Game.gd" type="Script" id=1] | ||
[ext_resource path="res://src/Main/MainMenu.tscn" type="PackedScene" id=2] | ||
[ext_resource path="res://src/UI/Menus/Characters/CharacterMenu.tscn" type="PackedScene" id=3] | ||
|
||
[node name="Game" type="Node"] | ||
script = ExtResource( 1 ) | ||
|
||
[node name="MainMenu" parent="." instance=ExtResource( 2 )] | ||
|
||
[node name="CharacterMenu" parent="." instance=ExtResource( 3 )] | ||
visible = false | ||
[connection signal="login_pressed" from="MainMenu" to="." method="_on_MainMenu_login_pressed"] | ||
[connection signal="register_pressed" from="MainMenu" to="." method="_on_MainMenu_register_pressed"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,53 @@ | ||
# Aggregate class that holds the login, register, and character select controls | ||
# and manages their visibility. | ||
# The game's main menu. Aggregates the LoginForm and RegisterForm. | ||
# Emits signals with relevant information for a parent node to communicate with the game server. | ||
extends Control | ||
|
||
export var CharacterSelect: PackedScene | ||
signal login_pressed(email, password, do_remember_email) | ||
signal register_pressed(email, password, do_remember_email) | ||
|
||
var is_enabled := true setget set_is_enabled | ||
|
||
onready var login_form := $LoginForm | ||
onready var register_form := $RegisterForm | ||
|
||
onready var menu_current: Control = login_form setget set_menu_current | ||
|
||
func _ready() -> void: | ||
self.menu_current = login_form | ||
|
||
|
||
func set_is_enabled(value: bool) -> void: | ||
is_enabled = value | ||
for menu in get_children(): | ||
menu.is_enabled = is_enabled | ||
|
||
func set_menu_current(value: Control) -> void: | ||
menu_current = value | ||
if not menu_current: | ||
return | ||
|
||
for child in get_children(): | ||
child.hide() | ||
for menu in get_children(): | ||
menu.hide() | ||
menu_current.show() | ||
|
||
|
||
func _on_RegisterForm_opened() -> void: | ||
login_form.hide() | ||
register_form.show() | ||
# Updates the status panel of the active menu. | ||
func update_status(text: String) -> void: | ||
menu_current.status_panel.text = text | ||
|
||
|
||
func _on_RegisterForm_closed() -> void: | ||
self.menu_current = login_form | ||
|
||
|
||
func _on_LoginForm_register_pressed() -> void: | ||
self.menu_current = register_form | ||
|
||
|
||
func _on_LoginForm_login_pressed(email: String, password: String, do_remember_email: bool) -> void: | ||
emit_signal("login_pressed", email, password, do_remember_email) | ||
|
||
|
||
func _on_RegisterForm_register_pressed(email: String, password: String, do_remember_email: bool) -> void: | ||
emit_signal("register_pressed", email, password, do_remember_email) | ||
|
||
|
||
func _on_RegisterForm_cancel_pressed() -> void: | ||
self.menu_current = login_form |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.