-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.gd
70 lines (53 loc) · 2.43 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
extends Node
@onready var main_menu = $CanvasLayer/MainMenu
@onready var address_entry = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/AddressEntry
@onready var upnp_toggle = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/UpnpToggle
@onready var port_entry = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/PortEntry
@onready var hud = $CanvasLayer/HUD
@onready var health_bar = $CanvasLayer/HUD/HealthBar
@onready var moneydisplay = $CanvasLayer/HUD/Label
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("fullscreen"):
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN) if DisplayServer.window_get_mode() != DisplayServer.WINDOW_MODE_FULLSCREEN else DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
func _on_host_button_pressed():
main_menu.hide()
hud.show()
enet_peer.create_server(port_entry.value)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect(add_player)
multiplayer.peer_disconnected.connect(remove_player)
add_player(multiplayer.get_unique_id())
if upnp_toggle.button_pressed:
upnp_setup()
func _on_join_button_pressed():
main_menu.hide()
hud.show()
enet_peer.create_client(address_entry.text, port_entry.value)
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_health_bar)
func remove_player(peer_id):
var player = get_node_or_null(str(peer_id))
if player:
player.queue_free()
func update_health_bar(health_value):
health_bar.value = health_value
func _on_multiplayer_spawner_spawned(node):
if node.is_multiplayer_authority() and node.is_in_group("player"):
node.health_changed.connect(update_health_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_entry.value)
assert(map_result == UPNP.UPNP_RESULT_SUCCESS, "UPNP Port Mapping Failed! Error %s" % map_result)
print("Success! Join Address: %s" % upnp.query_external_address())