-
Notifications
You must be signed in to change notification settings - Fork 1
/
world.gd
64 lines (47 loc) · 1.69 KB
/
world.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
extends Node
@onready var main_menu = $CanvasLayer/MainMenu
@onready var address_entry = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/AddressEntry
const Player = preload("res://player.tscn")
const PORT = 9999
var enet_peer = ENetMultiplayerPeer.new()
var me = Player.instantiate()
var am_host = false
# Called when the node enters the scene tree for the first time.
func _unhandled_input(event):
if Input.is_action_just_pressed("quit"):
get_tree().quit()
func _on_host_button_pressed():
main_menu.hide()
enet_peer.create_server(PORT)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect(add_player)
multiplayer.peer_disconnected.connect(remove_player)
me.name = str(multiplayer.get_unique_id())
me.is_host = false
add_child(me)
upnp_setup()
func _on_join_button_pressed():
main_menu.hide()
print("Joining game at %s" % address_entry.text)
enet_peer.create_client(address_entry.text, PORT)
multiplayer.multiplayer_peer = enet_peer
func add_player(peer_id):
var enemy = Player.instantiate()
enemy.name = str(peer_id)
add_child(enemy)
func remove_player(peer_id):
var player = get_node_or_null(str(peer_id))
if player:
player.queue_free()
func upnp_setup():
var upnp = UPNP.new()
var discover_result = upnp.discover()
assert(discover_result == UPNP.UPNP_RESULT_SUCCESS, \
"UPNP Discover Failed! Error %s" % discover_result)
var gateway = upnp.get_gateway()
assert(gateway and gateway.is_valid_gateway(), \
"UPNP Invalid Gateway!")
var map_result = upnp.add_port_mapping(PORT)
assert(map_result == UPNP.UPNP_RESULT_SUCCESS, \
"UPNP Port Mapping Failed! Error %s" % map_result)
print("Success! Join Address: %s" % upnp.query_external_address())