|
| 1 | +extends Node2D |
| 2 | + |
| 3 | + |
| 4 | +@export var services := "username" |
| 5 | +@export var multiplayer_channel_name := "test_multiplayer_channel" |
| 6 | + |
| 7 | +var peer := TaloMultiplayerPeer.new() |
| 8 | + |
| 9 | +func _ready() -> void: |
| 10 | + assert(not multiplayer_channel_name.is_empty()) |
| 11 | + %IdentifyButton.pressed.connect(_on_identify_btn_pressed) |
| 12 | + |
| 13 | +func _spawn_player(peer_id: int) -> void: |
| 14 | + var player := preload("../player.tscn").instantiate() |
| 15 | + player.name = str(peer_id) |
| 16 | + player.setup(peer_id) |
| 17 | + add_child(player) |
| 18 | + if player.is_multiplayer_authority(): |
| 19 | + var vp_center := get_tree().root.size * 0.5 |
| 20 | + var spawn_range := vp_center * 0.4 |
| 21 | + var spawn_position := vp_center + Vector2( |
| 22 | + randf_range(-spawn_range.x, spawn_range.x), |
| 23 | + randf_range(-spawn_range.y, spawn_range.y) |
| 24 | + ) |
| 25 | + player.position = spawn_position |
| 26 | + |
| 27 | +func _on_identify_btn_pressed() -> void: |
| 28 | + var msg_label := %MessageLabel as Label |
| 29 | + var btn := %IdentifyButton as Button |
| 30 | + |
| 31 | + btn.disabled = true |
| 32 | + # Identify |
| 33 | + if Talo.identity_check(false) != OK: |
| 34 | + var identifier := (%IdentifierLineEdit.text as String).strip_edges() |
| 35 | + if identifier.is_empty(): |
| 36 | + msg_label.text = "Identify failed: identifier is empty." |
| 37 | + btn.disabled = false |
| 38 | + return |
| 39 | + |
| 40 | + await Talo.players.identify(services, identifier) |
| 41 | + |
| 42 | + if Talo.identity_check(false) != OK: |
| 43 | + msg_label.text = "Identify failed: please check output for more details if logging is enabled." |
| 44 | + btn.disabled = false |
| 45 | + return |
| 46 | + |
| 47 | + |
| 48 | + # Channels |
| 49 | + var channel_id := -1 |
| 50 | + # Find exists channel. |
| 51 | + var page := 0 |
| 52 | + while true: |
| 53 | + var res := await Talo.channels.get_channels(page) |
| 54 | + if not is_instance_valid(res): |
| 55 | + msg_label.text = "Join channel failed: please check output for more details if logging is enabled." |
| 56 | + btn.disabled = false |
| 57 | + return |
| 58 | + |
| 59 | + for channel: TaloChannel in res.channels: |
| 60 | + if channel.display_name == multiplayer_channel_name: |
| 61 | + channel_id = channel.id |
| 62 | + break |
| 63 | + |
| 64 | + if res.last_page: |
| 65 | + # Last page. |
| 66 | + break |
| 67 | + |
| 68 | + page += 1 |
| 69 | + |
| 70 | + # Create new channel if not exists. |
| 71 | + if channel_id < 0: |
| 72 | + var channel := await Talo.channels.create(multiplayer_channel_name, true) |
| 73 | + if not is_instance_valid(channel): |
| 74 | + msg_label.text = "Join channel failed: please check output for more details if logging is enabled." |
| 75 | + btn.disabled = false |
| 76 | + return |
| 77 | + |
| 78 | + channel_id = channel.id |
| 79 | + |
| 80 | + # Join to the channel if need. |
| 81 | + var subscribed_channels := await Talo.channels.get_subscribed_channels() |
| 82 | + if subscribed_channels.all(func(channel: TaloChannel) -> bool: return channel.id != channel_id): |
| 83 | + var joined_channel := await Talo.channels.join(channel_id) |
| 84 | + if not is_instance_valid(joined_channel): |
| 85 | + msg_label.text = "Join channel failed: please check output for more details if logging is enabled." |
| 86 | + btn.disabled = false |
| 87 | + return |
| 88 | + |
| 89 | + |
| 90 | + # Join successful, setup TaloMultiplayerPeer |
| 91 | + get_tree().get_multiplayer().multiplayer_peer = peer |
| 92 | + if peer.listen_channel(channel_id) != OK: |
| 93 | + btn.disabled = false |
| 94 | + return |
| 95 | + |
| 96 | + peer.peer_connected.connect(_on_peer_connected) |
| 97 | + peer.peer_disconnected.connect(_on_peer_disconnected) |
| 98 | + |
| 99 | + %Identify.hide() |
| 100 | + _spawn_player(peer.get_unique_id()) |
| 101 | + |
| 102 | +func _on_peer_connected(id: int) -> void: |
| 103 | + _spawn_player(id) |
| 104 | + |
| 105 | +func _on_peer_disconnected(id: int) -> void: |
| 106 | + for node in get_children(): |
| 107 | + if node.name != str(id): |
| 108 | + continue |
| 109 | + remove_child(node) |
| 110 | + node.queue_free() |
| 111 | + return |
0 commit comments