-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.gd
74 lines (58 loc) · 2.19 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
65
66
67
68
69
70
71
72
73
74
extends Node
@onready var menu = $CanvasLayer/MainMenu
@onready var entry = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/AdressEntry
@onready var hud = $CanvasLayer/HUD
@onready var bar = $CanvasLayer/HUD/HealthBar
const PORT = 9999
const PLAYER = preload("res://player.tscn")
var enet_peer = ENetMultiplayerPeer.new()
func _unhandled_input(_event):
if Input.is_action_just_pressed("quit"):
get_tree().quit()
#if Input.is_action_just_pressed("full"):
#swap_fullscreen_mode()
func swap_fullscreen_mode():
if DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
else:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
func _on_host_button_pressed():
menu.hide()
hud.show()
enet_peer.create_server(PORT)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect(add_player)
multiplayer.peer_disconnected.connect(remove_player)
add_player(multiplayer.get_unique_id())
upnp_setup()
func _on_join_button_pressed():
menu.hide()
hud.show()
if entry.text.is_empty():
enet_peer.create_client("127.0.0.1",PORT)
else:
enet_peer.create_client(entry.text, PORT)
multiplayer.multiplayer_peer = enet_peer
func add_player(peer_id):
var player = PLAYER.instantiate()
player.name = str(peer_id)
add_child(player)
if player.is_multiplayer_authority():
player.health_changed.connect(update_bar)
func remove_player(peer_id):
var player = get_node_or_null(str(peer_id))
if player:
player.queue_free()
func update_bar(health_value):
bar.value = health_value
func _on_multiplayer_spawner_spawned(node):
if node.is_multiplayer_authority():
node.health_changed.connect(update_bar)
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)
assert(upnp.get_gateway() and upnp.get_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())