local Multiplayer support

This commit is contained in:
2024-12-04 08:48:48 +01:00
parent 15be351c86
commit bb7dbb054a
13 changed files with 233 additions and 24 deletions

51
lobby.gd Normal file
View File

@@ -0,0 +1,51 @@
extends Control
@export var max_players = 4
const game_scene = "res://main.tscn" # Path to the main game scene
var ready_states = {} # Dictionary to track ready states for each player ID
@onready var player_list = $PlayerList
@onready var start_button = $StartButton
func _ready() -> void:
start_button.disabled = true
start_button.connect("pressed", _on_start_game)
func _process(delta: float) -> void:
# Handle player joining
if (Input.is_action_just_pressed("throw_bomb")):
add_player(-1) # -1 for keyboard
for i in range(0, 7): # 0-7 for controllers
if (i >= 0 and Input.is_joy_button_pressed(i, JOY_BUTTON_A)):
add_player(i)
func add_player(input_id: int) -> void:
# Prevent duplicate players
if input_id in Global.player_ids:
return
if len(Global.player_ids) >= max_players:
return # Max players reached
# Add player to the list
Global.player_ids.append(input_id)
# Create a new label to display the player ID
var label = Label.new()
label.text = "Player ID: %d" % input_id
player_list.add_child(label)
# Track ready state for the player
ready_states[input_id] = false
# Update the start button state
check_all_ready()
func check_all_ready() -> void:
# Enable the start button if at least one player has joined
start_button.disabled = len(Global.player_ids) == 0
func _on_start_game() -> void:
get_tree().change_scene_to_file(game_scene)