aboutsummaryrefslogtreecommitdiff
path: root/client/menu/lobby.gd
blob: ae7548b70b6d0440e2b9a3780d95bf6497a9d141 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Hurry Curry! - a game about cooking
# Copyright 2024 tpart
# Copyright 2024 nokoe
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends Control

const PLAYER = preload("res://menu/lobby/player.tscn")

var map_count
var selected_map := 0
var selected_map_name: String
var difficulty_names = [tr("Easy"), tr("Moderate"), tr("Hard"), tr("Very hard"), tr("Unplayable")]

@onready var game: Game = $"../Game"
@onready var player_container = $VBoxContainer/Top/MarginContainer/VBoxContainer/Players

@onready var map_name_label = $VBoxContainer/Bottom/MarginContainer/VBoxContainer/HBoxContainer/Map/Name
@onready var map_player_label = $VBoxContainer/Bottom/MarginContainer/VBoxContainer/HBoxContainer/Map/Players
@onready var map_difficulty_label = $VBoxContainer/Bottom/MarginContainer/VBoxContainer/HBoxContainer/Map/Difficulty

@onready var map_selector = $VBoxContainer/Bottom/MarginContainer/VBoxContainer/HBoxContainer
@onready var prev_map = $VBoxContainer/Bottom/MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/Left
@onready var next_map = $VBoxContainer/Bottom/MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer2/Right
@onready var start_button = $VBoxContainer/Bottom/MarginContainer/VBoxContainer/CenterContainer/ControllerButton

func _ready():
	game.update_players.connect(update_players)
	initialize()
	game.data_updated.connect(initialize)
	game.joined.connect(_on_game_joined)
	game.left.connect(_on_game_left)

func initialize():
	map_count = game.maps.size()
	select_map(0)

func select_map(i: int):
	if i >= map_count:
		return
	selected_map = i
	var k = game.maps.keys()[i]
	var map_data: Dictionary = game.maps[k]
	map_name_label.text = map_data["name"]
	map_player_label.text = tr("%d players recommended") % map_data["players"]
	map_difficulty_label.text = difficulty_names[map_data["difficulty"] - 1]
	selected_map_name = k

func update_players(player_list: Dictionary):
	for i in player_container.get_children():
		i.queue_free()

	for i in player_list.keys():
		var p: PlayerTag = PLAYER.instantiate()
		player_container.add_child(p)
		p.setup(player_list[i].username)

func _input(_event):
	if not visible:
		return

	if Input.is_action_just_pressed("previous") and not prev_map.disabled:
		prev_map.emit_signal("pressed")
	elif Input.is_action_just_pressed("next") and not next_map.disabled:
		next_map.emit_signal("pressed")

func _on_game_joined():
	map_selector.show()
	start_button.text = tr("Start Game")
	start_button.disabled = false

func _on_game_left():
	map_selector.hide()
	start_button.text = tr("Join Game")

func _on_left_pressed():
	selected_map = (selected_map - 1) % map_count
	select_map(selected_map)

func _on_right_pressed():
	selected_map = (selected_map + 1) % map_count
	select_map(selected_map)

func _on_controller_button_pressed():
	if game.is_joined:
		if selected_map_name != null:
			game.mp.send_chat("/start %s" % selected_map_name)
	elif not game.join_sent:
		start_button.disabled = true
		game.join()