Skip to content

Commit

Permalink
Fix host returning to multiplayer screen without an opponent
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Nov 15, 2024
1 parent d7d0fc5 commit 5f8c208
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions screen/multiplayer_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type MultiplayerHostScreen struct {
back *turdgl.Button
buttonBackground *turdgl.CurvedRect

server *turdserve.Server
opponentIsConnected bool
server *turdserve.Server
opponentIsInLobby bool
}

// NewMultiplayerHostScreen constructs an uninitialised multiplayer host screen.
Expand Down Expand Up @@ -101,7 +101,7 @@ func (s *MultiplayerHostScreen) Enter(_ InitData) {
Y: s.buttonBackground.Pos.Y + TileSizePx*TileBoundryFactor,
},
func() {
if !s.opponentIsConnected {
if !s.opponentIsInLobby {
// Make the opponent status text briefly change colour
s.opponentStatus.SetColour(common.Tile64Colour)
go func() {
Expand Down Expand Up @@ -163,6 +163,7 @@ func (s *MultiplayerHostScreen) Enter(_ InitData) {
// Exit deinitialises the screen.
func (s *MultiplayerHostScreen) Exit() {
s.win.UnregisterKeybind(turdgl.KeyEscape, turdgl.KeyRelease)
s.opponentIsInLobby = false
}

// Update updates and draws multiplayer host screen.
Expand Down Expand Up @@ -249,7 +250,7 @@ func (s *MultiplayerHostScreen) handlePlayerData(data comms.PlayerData) error {
s.opponentStatus.SetText(
fmt.Sprintf("\"%s\" has joined the game. Press Start to begin", s.opponentName),
)
s.opponentIsConnected = true
s.opponentIsInLobby = true

// Send host player data to client
if err := s.sendPlayerData(); err != nil {
Expand All @@ -262,7 +263,7 @@ func (s *MultiplayerHostScreen) handlePlayerData(data comms.PlayerData) error {
// handleOpponentDisconnect handles the opponent disconnecting from the server.
func (s *MultiplayerHostScreen) handleOpponentDisconnect() {
s.opponentStatus.SetText(fmt.Sprintf("Waiting for opponent to join \"%s\"", getIPAddr()))
s.opponentIsConnected = false
s.opponentIsInLobby = false
}

// getIPAddr returns the IP address of the host.
Expand All @@ -284,7 +285,7 @@ const serverKey = "server"
// startGame attempts to start a multiplayer game.
func (s *MultiplayerHostScreen) startGame() error {
// Check opponent is connected
if !s.opponentIsConnected {
if !s.opponentIsInLobby {
return errors.New("opponent is not connected")
}

Expand Down

0 comments on commit 5f8c208

Please sign in to comment.