dev: 函数统一间隔两行
This commit is contained in:
@@ -63,4 +63,4 @@ game/
|
|||||||
|
|
||||||
- 除连接了信号或 HTTPRequest、MultiplayerAPI 的函数外,任何函数都不应该以下划线(`_`)开头。
|
- 除连接了信号或 HTTPRequest、MultiplayerAPI 的函数外,任何函数都不应该以下划线(`_`)开头。
|
||||||
|
|
||||||
- 函数之间既可间隔一行,也可间隔两行。建议绑定 Godot 信号的函数间隔两行(自动空行),其余函数间隔一行,部分间隔一行的函数为遗留问题。
|
- 函数之间既可间隔一行,也可间隔两行。建议函数间隔两行,部分间隔一行的函数为遗留问题。
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ func create_file(file_path: String) -> void:
|
|||||||
var file: FileAccess = FileAccess.open(file_path, FileAccess.WRITE)
|
var file: FileAccess = FileAccess.open(file_path, FileAccess.WRITE)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
|
||||||
func download_file(server_path: String, local_path: String) -> void:
|
func download_file(server_path: String, local_path: String) -> void:
|
||||||
var http: HTTPRequest = HTTPRequest.new()
|
var http: HTTPRequest = HTTPRequest.new()
|
||||||
add_child(http)
|
add_child(http)
|
||||||
@@ -19,6 +20,7 @@ func download_file(server_path: String, local_path: String) -> void:
|
|||||||
http.request(server_path)
|
http.request(server_path)
|
||||||
await http.request_completed
|
await http.request_completed
|
||||||
|
|
||||||
|
|
||||||
func get_uuid() -> void:
|
func get_uuid() -> void:
|
||||||
var index_file: FileAccess = FileAccess.open("user://download/sources/temp/index.json", FileAccess.READ)
|
var index_file: FileAccess = FileAccess.open("user://download/sources/temp/index.json", FileAccess.READ)
|
||||||
var index_text: String = index_file.get_as_text()
|
var index_text: String = index_file.get_as_text()
|
||||||
@@ -26,6 +28,7 @@ func get_uuid() -> void:
|
|||||||
index_file.close()
|
index_file.close()
|
||||||
uuid = content["uuid"]
|
uuid = content["uuid"]
|
||||||
|
|
||||||
|
|
||||||
func download_defs(type: String, origin: String) -> void:
|
func download_defs(type: String, origin: String) -> void:
|
||||||
var list_file: FileAccess = FileAccess.open("user://download/sources/%s/%ss/list.json" % [uuid, type], FileAccess.READ)
|
var list_file: FileAccess = FileAccess.open("user://download/sources/%s/%ss/list.json" % [uuid, type], FileAccess.READ)
|
||||||
var list_text: String = list_file.get_as_text()
|
var list_text: String = list_file.get_as_text()
|
||||||
@@ -37,6 +40,7 @@ func download_defs(type: String, origin: String) -> void:
|
|||||||
var filename = list[k]
|
var filename = list[k]
|
||||||
await download_file("%s%s/id/%s" % [origin, type, k], "user://download/sources/%s/%ss/%s.json" % [uuid, type, filename])
|
await download_file("%s%s/id/%s" % [origin, type, k], "user://download/sources/%s/%ss/%s.json" % [uuid, type, filename])
|
||||||
|
|
||||||
|
|
||||||
func download_assets(origin: String) -> void:
|
func download_assets(origin: String) -> void:
|
||||||
var list_file: FileAccess = FileAccess.open("user://download/sources/%s/assets/list.json" % [uuid], FileAccess.READ)
|
var list_file: FileAccess = FileAccess.open("user://download/sources/%s/assets/list.json" % [uuid], FileAccess.READ)
|
||||||
var list_text: String = list_file.get_as_text()
|
var list_text: String = list_file.get_as_text()
|
||||||
@@ -51,6 +55,7 @@ func download_assets(origin: String) -> void:
|
|||||||
var filename = list["sounds"][k]
|
var filename = list["sounds"][k]
|
||||||
await download_file("%sasset/sound/%s" % [origin, k], "user://download/sources/%s/assets/sounds/%s" % [uuid, filename])
|
await download_file("%sasset/sound/%s" % [origin, k], "user://download/sources/%s/assets/sounds/%s" % [uuid, filename])
|
||||||
|
|
||||||
|
|
||||||
func download_from_origin() -> int:
|
func download_from_origin() -> int:
|
||||||
var origin: String = GameManager.data_origin
|
var origin: String = GameManager.data_origin
|
||||||
var http: HTTPRequest = HTTPRequest.new()
|
var http: HTTPRequest = HTTPRequest.new()
|
||||||
@@ -88,6 +93,7 @@ func download_from_origin() -> int:
|
|||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
func load_resource():
|
func load_resource():
|
||||||
var card_file: FileAccess = FileAccess.open("user://download/sources/%s/cards/list.json" % [uuid], FileAccess.READ)
|
var card_file: FileAccess = FileAccess.open("user://download/sources/%s/cards/list.json" % [uuid], FileAccess.READ)
|
||||||
GameManager.card_list = JSON.parse_string(card_file.get_as_text())
|
GameManager.card_list = JSON.parse_string(card_file.get_as_text())
|
||||||
@@ -107,6 +113,7 @@ func load_resource():
|
|||||||
GameManager.sound_list = asset_list["sounds"]
|
GameManager.sound_list = asset_list["sounds"]
|
||||||
asset_file.close()
|
asset_file.close()
|
||||||
|
|
||||||
|
|
||||||
func get_sources():
|
func get_sources():
|
||||||
if !DirAccess.dir_exists_absolute("user://download/sources/"):
|
if !DirAccess.dir_exists_absolute("user://download/sources/"):
|
||||||
DirAccess.make_dir_recursive_absolute("user://download/sources/")
|
DirAccess.make_dir_recursive_absolute("user://download/sources/")
|
||||||
|
|||||||
@@ -7,16 +7,20 @@ signal end_game
|
|||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
end_game.connect(_on_end_game)
|
end_game.connect(_on_end_game)
|
||||||
|
|
||||||
|
|
||||||
func start_game() -> void:
|
func start_game() -> void:
|
||||||
game_round = 1
|
game_round = 1
|
||||||
|
|
||||||
|
|
||||||
func settle_round() -> void:
|
func settle_round() -> void:
|
||||||
game_round += 1
|
game_round += 1
|
||||||
if check_game_end():
|
if check_game_end():
|
||||||
end_game.emit()
|
end_game.emit()
|
||||||
|
|
||||||
|
|
||||||
func check_game_end() -> bool:
|
func check_game_end() -> bool:
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
|
||||||
func _on_end_game() -> void:
|
func _on_end_game() -> void:
|
||||||
game_round = -1
|
game_round = -1
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ func add_player(id: int) -> void:
|
|||||||
player_turns[id] = 0
|
player_turns[id] = 0
|
||||||
player_hp[id] = INITIAL_HP
|
player_hp[id] = INITIAL_HP
|
||||||
|
|
||||||
|
|
||||||
func remove_player(id: int) -> void:
|
func remove_player(id: int) -> void:
|
||||||
if not players.has(id):
|
if not players.has(id):
|
||||||
return
|
return
|
||||||
@@ -36,6 +37,7 @@ func remove_player(id: int) -> void:
|
|||||||
for dict in [player_cards, player_turns, player_hp, player_username]:
|
for dict in [player_cards, player_turns, player_hp, player_username]:
|
||||||
dict.erase(id)
|
dict.erase(id)
|
||||||
|
|
||||||
|
|
||||||
func create_server(playern: int) -> void:
|
func create_server(playern: int) -> void:
|
||||||
max_players = playern
|
max_players = playern
|
||||||
if peer.create_server(PORT, playern) != OK:
|
if peer.create_server(PORT, playern) != OK:
|
||||||
@@ -43,24 +45,29 @@ func create_server(playern: int) -> void:
|
|||||||
_setup_multiplayer()
|
_setup_multiplayer()
|
||||||
add_player(1)
|
add_player(1)
|
||||||
|
|
||||||
|
|
||||||
func create_client(ip: String) -> void:
|
func create_client(ip: String) -> void:
|
||||||
if peer.create_client(ip, PORT) != OK:
|
if peer.create_client(ip, PORT) != OK:
|
||||||
return
|
return
|
||||||
_setup_multiplayer()
|
_setup_multiplayer()
|
||||||
|
|
||||||
|
|
||||||
func _setup_multiplayer() -> void:
|
func _setup_multiplayer() -> void:
|
||||||
multiplayer.multiplayer_peer = peer
|
multiplayer.multiplayer_peer = peer
|
||||||
multiplayer.peer_connected.connect(_on_peer_connected)
|
multiplayer.peer_connected.connect(_on_peer_connected)
|
||||||
multiplayer.peer_disconnected.connect(_on_peer_disconnected)
|
multiplayer.peer_disconnected.connect(_on_peer_disconnected)
|
||||||
|
|
||||||
|
|
||||||
func _on_peer_connected(id: int) -> void:
|
func _on_peer_connected(id: int) -> void:
|
||||||
add_player(id)
|
add_player(id)
|
||||||
sync_players.rpc()
|
sync_players.rpc()
|
||||||
|
|
||||||
|
|
||||||
func _on_peer_disconnected(id: int) -> void:
|
func _on_peer_disconnected(id: int) -> void:
|
||||||
remove_player(id)
|
remove_player(id)
|
||||||
sync_players.rpc()
|
sync_players.rpc()
|
||||||
|
|
||||||
|
|
||||||
func start_game() -> void:
|
func start_game() -> void:
|
||||||
if not multiplayer.is_server() or players.size() != max_players:
|
if not multiplayer.is_server() or players.size() != max_players:
|
||||||
return
|
return
|
||||||
@@ -69,6 +76,7 @@ func start_game() -> void:
|
|||||||
deal_cards()
|
deal_cards()
|
||||||
sync_game_state.rpc()
|
sync_game_state.rpc()
|
||||||
|
|
||||||
|
|
||||||
func extract() -> String:
|
func extract() -> String:
|
||||||
if cards.is_empty():
|
if cards.is_empty():
|
||||||
return ""
|
return ""
|
||||||
@@ -77,10 +85,12 @@ func extract() -> String:
|
|||||||
cards.remove_at(index)
|
cards.remove_at(index)
|
||||||
return card
|
return card
|
||||||
|
|
||||||
|
|
||||||
func deal_cards() -> void:
|
func deal_cards() -> void:
|
||||||
for player_id in players:
|
for player_id in players:
|
||||||
_draw_cards(player_id, MAX_HAND_SIZE)
|
_draw_cards(player_id, MAX_HAND_SIZE)
|
||||||
|
|
||||||
|
|
||||||
func next_round() -> void:
|
func next_round() -> void:
|
||||||
if not multiplayer.is_server():
|
if not multiplayer.is_server():
|
||||||
return
|
return
|
||||||
@@ -88,6 +98,7 @@ func next_round() -> void:
|
|||||||
server_round += 1
|
server_round += 1
|
||||||
sync_game_state.rpc()
|
sync_game_state.rpc()
|
||||||
|
|
||||||
|
|
||||||
func settle_round() -> void:
|
func settle_round() -> void:
|
||||||
for player_id in players:
|
for player_id in players:
|
||||||
var draw_count := DEFAULT_DRAW_COUNT
|
var draw_count := DEFAULT_DRAW_COUNT
|
||||||
@@ -95,6 +106,7 @@ func settle_round() -> void:
|
|||||||
draw_count = FIRST_ROUND_DRAW_COUNT
|
draw_count = FIRST_ROUND_DRAW_COUNT
|
||||||
_draw_cards(player_id, draw_count)
|
_draw_cards(player_id, draw_count)
|
||||||
|
|
||||||
|
|
||||||
func _draw_cards(player_id: int, count: int) -> void:
|
func _draw_cards(player_id: int, count: int) -> void:
|
||||||
if not player_cards.has(player_id):
|
if not player_cards.has(player_id):
|
||||||
return
|
return
|
||||||
@@ -107,18 +119,22 @@ func _draw_cards(player_id: int, count: int) -> void:
|
|||||||
break
|
break
|
||||||
hand.append(card)
|
hand.append(card)
|
||||||
|
|
||||||
|
|
||||||
func get_my_cards() -> void:
|
func get_my_cards() -> void:
|
||||||
request_cards.rpc_id(1)
|
request_cards.rpc_id(1)
|
||||||
|
|
||||||
|
|
||||||
@rpc("any_peer", "call_remote", "reliable")
|
@rpc("any_peer", "call_remote", "reliable")
|
||||||
func request_cards() -> void:
|
func request_cards() -> void:
|
||||||
var sender_id := multiplayer.get_remote_sender_id()
|
var sender_id := multiplayer.get_remote_sender_id()
|
||||||
send_cards.rpc_id(sender_id, player_cards.get(sender_id, []))
|
send_cards.rpc_id(sender_id, player_cards.get(sender_id, []))
|
||||||
|
|
||||||
|
|
||||||
@rpc("authority", "call_remote", "reliable")
|
@rpc("authority", "call_remote", "reliable")
|
||||||
func send_cards(data: Array) -> void:
|
func send_cards(data: Array) -> void:
|
||||||
my_card = data
|
my_card = data
|
||||||
|
|
||||||
|
|
||||||
func sync_game_state() -> void:
|
func sync_game_state() -> void:
|
||||||
if not multiplayer.is_server():
|
if not multiplayer.is_server():
|
||||||
return
|
return
|
||||||
@@ -131,6 +147,7 @@ func sync_game_state() -> void:
|
|||||||
game_started = game_started
|
game_started = game_started
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@rpc("authority", "call_remote", "reliable")
|
@rpc("authority", "call_remote", "reliable")
|
||||||
func sync_game_state_rpc(data: Dictionary) -> void:
|
func sync_game_state_rpc(data: Dictionary) -> void:
|
||||||
cards = data.cards
|
cards = data.cards
|
||||||
@@ -140,6 +157,7 @@ func sync_game_state_rpc(data: Dictionary) -> void:
|
|||||||
server_round = data.server_round
|
server_round = data.server_round
|
||||||
game_started = data.game_started
|
game_started = data.game_started
|
||||||
|
|
||||||
|
|
||||||
func sync_players() -> void:
|
func sync_players() -> void:
|
||||||
if not multiplayer.is_server():
|
if not multiplayer.is_server():
|
||||||
return
|
return
|
||||||
@@ -149,6 +167,7 @@ func sync_players() -> void:
|
|||||||
player_hp = player_hp
|
player_hp = player_hp
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@rpc("authority", "call_remote", "reliable")
|
@rpc("authority", "call_remote", "reliable")
|
||||||
func sync_players_rpc(data: Dictionary) -> void:
|
func sync_players_rpc(data: Dictionary) -> void:
|
||||||
players = data.players
|
players = data.players
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ func _ready():
|
|||||||
var root: Window = get_tree().root
|
var root: Window = get_tree().root
|
||||||
current_scene = root.get_child(root.get_child_count() - 1)
|
current_scene = root.get_child(root.get_child_count() - 1)
|
||||||
|
|
||||||
|
|
||||||
func goto_scene(path: String):
|
func goto_scene(path: String):
|
||||||
get_tree().change_scene_to_file("res://scenes/%s.tscn" % [path])
|
get_tree().change_scene_to_file("res://scenes/%s.tscn" % [path])
|
||||||
var root: Window = get_tree().root
|
var root: Window = get_tree().root
|
||||||
|
|||||||
@@ -4,17 +4,21 @@ var description: String
|
|||||||
var type: String
|
var type: String
|
||||||
var card_name: String
|
var card_name: String
|
||||||
|
|
||||||
|
|
||||||
func set_texture(pic: String) -> void:
|
func set_texture(pic: String) -> void:
|
||||||
var image: Image = Image.new()
|
var image: Image = Image.new()
|
||||||
image.load(pic)
|
image.load(pic)
|
||||||
$Sprite.texture = ImageTexture.create_from_image(image)
|
$Sprite.texture = ImageTexture.create_from_image(image)
|
||||||
|
|
||||||
|
|
||||||
func set_card(cname: String) -> void:
|
func set_card(cname: String) -> void:
|
||||||
card_name = cname
|
card_name = cname
|
||||||
set_texture("user://download/sources/%s/assets/pics/%s" % [DownloadManager.uuid, GameManager.pic_list[card_name]])
|
set_texture("user://download/sources/%s/assets/pics/%s" % [DownloadManager.uuid, GameManager.pic_list[card_name]])
|
||||||
|
|
||||||
|
|
||||||
func set_pos(x: int, y: int) -> void:
|
func set_pos(x: int, y: int) -> void:
|
||||||
$Sprite.position = Vector2(x, y)
|
$Sprite.position = Vector2(x, y)
|
||||||
|
|
||||||
|
|
||||||
func get_pos() -> Vector2:
|
func get_pos() -> Vector2:
|
||||||
return $Sprite.position
|
return $Sprite.position
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ func _ready() -> void:
|
|||||||
card.set_pos(300, 300)
|
card.set_pos(300, 300)
|
||||||
print(card.get_pos())
|
print(card.get_pos())
|
||||||
|
|
||||||
|
|
||||||
func init() -> void:
|
func init() -> void:
|
||||||
# 设置 UI 展示文本
|
# 设置 UI 展示文本
|
||||||
if multiplayer.is_server():
|
if multiplayer.is_server():
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ extends Node2D
|
|||||||
func _on_join_game_pressed() -> void:
|
func _on_join_game_pressed() -> void:
|
||||||
$JoinGameUI.show()
|
$JoinGameUI.show()
|
||||||
|
|
||||||
|
|
||||||
func _on_start_game_pressed() -> void:
|
func _on_start_game_pressed() -> void:
|
||||||
$CreateGameUI.show()
|
$CreateGameUI.show()
|
||||||
|
|
||||||
|
|
||||||
func _on_setting_button_pressed() -> void:
|
func _on_setting_button_pressed() -> void:
|
||||||
SceneManager.goto_scene("menus/settings")
|
SceneManager.goto_scene("menus/settings")
|
||||||
|
|
||||||
|
|
||||||
func _on_quit_game_pressed() -> void:
|
func _on_quit_game_pressed() -> void:
|
||||||
get_tree().quit()
|
get_tree().quit()
|
||||||
|
|||||||
@@ -1,20 +1,24 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
init_sources()
|
init_sources()
|
||||||
init_text()
|
init_text()
|
||||||
|
|
||||||
|
|
||||||
func init_text() -> void:
|
func init_text() -> void:
|
||||||
$DataSetting/LineEdit.text = GameManager.data_origin
|
$DataSetting/LineEdit.text = GameManager.data_origin
|
||||||
$IPBeginSetting/LineEdit.text = GameManager.ip_begin
|
$IPBeginSetting/LineEdit.text = GameManager.ip_begin
|
||||||
$UsernameSetting/LineEdit.text = GameManager.username
|
$UsernameSetting/LineEdit.text = GameManager.username
|
||||||
$LoadSource/ChooseSource.select(GameManager.source)
|
$LoadSource/ChooseSource.select(GameManager.source)
|
||||||
|
|
||||||
|
|
||||||
func init_sources() -> void:
|
func init_sources() -> void:
|
||||||
DownloadManager.get_sources()
|
DownloadManager.get_sources()
|
||||||
for source_name in GameManager.sources:
|
for source_name in GameManager.sources:
|
||||||
$LoadSource/ChooseSource.add_item(source_name)
|
$LoadSource/ChooseSource.add_item(source_name)
|
||||||
|
|
||||||
|
|
||||||
func _on_save_button_pressed() -> void:
|
func _on_save_button_pressed() -> void:
|
||||||
GameManager.data_origin = $DataSetting/LineEdit.text
|
GameManager.data_origin = $DataSetting/LineEdit.text
|
||||||
GameManager.ip_begin = $IPBeginSetting/LineEdit.text
|
GameManager.ip_begin = $IPBeginSetting/LineEdit.text
|
||||||
@@ -42,7 +46,6 @@ func _on_download_button_pressed() -> void:
|
|||||||
$Tips.text = "SETTINGS_TIP_LOADED"
|
$Tips.text = "SETTINGS_TIP_LOADED"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func _on_load_button_pressed() -> void:
|
func _on_load_button_pressed() -> void:
|
||||||
if GameManager.sources.size() == 0 or GameManager.source == -1:
|
if GameManager.sources.size() == 0 or GameManager.source == -1:
|
||||||
$Tips.text = "SETTINGS_TIP_NOLOCALSOURCE"
|
$Tips.text = "SETTINGS_TIP_NOLOCALSOURCE"
|
||||||
|
|||||||
Reference in New Issue
Block a user