124 lines
3.8 KiB
GDScript
124 lines
3.8 KiB
GDScript
class_name LobbyScreen
|
||
extends Control
|
||
## Netzwerkspiel im lokalen Netz: Spiel eröffnen oder beitreten.
|
||
|
||
var app
|
||
var net: Net
|
||
var status_label: Label
|
||
var list_label: Label
|
||
var ip_box: LineEdit
|
||
var b_host: Button
|
||
var b_join: Button
|
||
var b_watch: Button
|
||
var b_start: Button
|
||
var ai_buttons: Array = []
|
||
var n_ai := 0
|
||
var addr_label: Label
|
||
|
||
|
||
func _ready() -> void:
|
||
net = app.net
|
||
net.my_name = "Spieler"
|
||
net.my_pick = {"looks": app.profile.looks()}
|
||
var col := Style.column(self, 760, 40, 10)
|
||
Style.clabel(col, "Netzwerkspiel", 36, Style.GOLD, "center", true)
|
||
Style.spacer(col, 10)
|
||
var hr := Style.row(col, 12)
|
||
hr.alignment = BoxContainer.ALIGNMENT_CENTER
|
||
b_host = Style.cbutton(hr, "Spiel eröffnen", Vector2(200, 46), _host, Color("285a3c"))
|
||
ip_box = LineEdit.new()
|
||
ip_box.custom_minimum_size = Vector2(220, 46)
|
||
ip_box.placeholder_text = "Adresse des Hosts"
|
||
ip_box.text = "127.0.0.1"
|
||
hr.add_child(ip_box)
|
||
b_join = Style.cbutton(hr, "Beitreten", Vector2(160, 46), _join)
|
||
b_watch = Style.cbutton(hr, "Zuschauen", Vector2(150, 46), _watch)
|
||
addr_label = Style.clabel(col, "", 14, Style.MUTED)
|
||
status_label = Style.clabel(col, "", 16, Style.BLUE)
|
||
Style.spacer(col, 6)
|
||
Style.clabel(col, "Am Tisch", 20, Style.TEXT, "center", true)
|
||
list_label = Style.clabel(col, "", 16, Style.TEXT)
|
||
Style.spacer(col, 6)
|
||
Style.clabel(col, "KI-Gegner", 16, Style.MUTED)
|
||
var ar := Style.row(col, 10)
|
||
ar.alignment = BoxContainer.ALIGNMENT_CENTER
|
||
for n in range(0, 5):
|
||
ai_buttons.append(Style.cbutton(ar, "%d KI" % n if n > 0 else "keine KI", Vector2(120, 40), func(): _set_ai(n)))
|
||
Style.spacer(col, 10)
|
||
var br := Style.row(col, 20)
|
||
br.alignment = BoxContainer.ALIGNMENT_CENTER
|
||
b_start = Style.cbutton(br, "Start", Vector2(200, 48), _start, Color("285a3c"))
|
||
Style.cbutton(br, "Zurück", Vector2(160, 48), _back)
|
||
net.lobby_changed.connect(refresh)
|
||
net.started.connect(_started)
|
||
net.ended.connect(func(_reason): refresh())
|
||
refresh()
|
||
|
||
|
||
func _exit_tree() -> void:
|
||
if net.lobby_changed.is_connected(refresh):
|
||
net.lobby_changed.disconnect(refresh)
|
||
if net.started.is_connected(_started):
|
||
net.started.disconnect(_started)
|
||
|
||
|
||
func _host() -> void:
|
||
net.host()
|
||
refresh()
|
||
|
||
|
||
func _join() -> void:
|
||
net.join(ip_box.text.strip_edges())
|
||
refresh()
|
||
|
||
|
||
## Als Zuschauer beitreten: sieht die Partie ohne Hände, spielt nicht mit.
|
||
func _watch() -> void:
|
||
net.spectator = true
|
||
net.join(ip_box.text.strip_edges())
|
||
refresh()
|
||
|
||
|
||
func _set_ai(n: int) -> void:
|
||
n_ai = n
|
||
refresh()
|
||
|
||
|
||
func _start() -> void:
|
||
net.start_battle(n_ai)
|
||
|
||
|
||
func _started(seat: int) -> void:
|
||
app.show_screen(BattleScreen.online(seat))
|
||
|
||
|
||
func _back() -> void:
|
||
net.leave()
|
||
app.show_screen(MenuScreen.new())
|
||
|
||
|
||
func refresh() -> void:
|
||
status_label.text = net.status
|
||
var lines: Array[String] = []
|
||
var ids: Array = net.names.keys()
|
||
ids.sort()
|
||
for id in ids:
|
||
lines.append("%s%s" % [net.names[id], " (Host)" if id == 1 else ""])
|
||
for id in net.spectators:
|
||
lines.append("%s (%s)" % [net.spectators[id], tr("Zuschauer")])
|
||
list_label.text = "\n".join(lines) if not lines.is_empty() else "–"
|
||
b_host.disabled = net.connected
|
||
b_join.disabled = net.connected
|
||
b_watch.disabled = net.connected
|
||
ip_box.editable = not net.connected
|
||
var humans := ids.size()
|
||
b_start.disabled = not (net.is_host and humans >= 2 and humans + n_ai <= Game.MAX_OPPONENTS + 1)
|
||
for i in ai_buttons.size():
|
||
ai_buttons[i].disabled = not net.is_host or i == n_ai or humans + i > Game.MAX_OPPONENTS + 1
|
||
addr_label.text = ("Deine Adressen: " + ", ".join(net.local_addresses())) if net.is_host else ""
|
||
|
||
|
||
func _input(event: InputEvent) -> void:
|
||
if event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE and not ip_box.has_focus():
|
||
_back()
|