Skip to content

Commit

Permalink
Add "CHANGE NAME" button to menu
Browse files Browse the repository at this point in the history
  • Loading branch information
armsnyder committed Oct 20, 2020
1 parent b2f657c commit cf57543
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
70 changes: 43 additions & 27 deletions pkg/client/scenes/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,53 @@ import (
"github.com/nsf/termbox-go"
)

const (
buttonNewGame = iota
buttonJoinGame
buttonChangeName
)

type Menu struct {
scene
isJoinGame bool
nickname string
button int
nickname string
}

func (m *Menu) OnTerminalEvent(event termbox.Event) error {
switch dx, _ := getDirectionPressed(event); dx {
case -1:
m.isJoinGame = false
case 1:
m.isJoinGame = true
dx, dy := getDirectionPressed(event)

switch {
case dx == -1:
switch m.button {
case buttonChangeName:
m.button = buttonJoinGame
case buttonJoinGame:
m.button = buttonNewGame
}
case dx == 1:
switch m.button {
case buttonNewGame:
m.button = buttonJoinGame
case buttonJoinGame:
m.button = buttonChangeName
}
case dy == -1:
m.button = buttonChangeName
case dy == 1:
if m.button == buttonChangeName {
m.button = buttonJoinGame
}
}

if event.Key == termbox.KeyEnter {
var player int
if m.isJoinGame {
player = 2
} else {
player = 1
switch m.button {
case buttonNewGame:
return m.ChangeScene(&Game{player: 1})
case buttonJoinGame:
return m.ChangeScene(&Game{player: 2})
case buttonChangeName:
return m.ChangeScene(&Nickname{changeNickname: true, nickname: m.nickname})
}

return m.ChangeScene(&Game{player: player})
}

return nil
Expand All @@ -40,18 +64,10 @@ func (m *Menu) Draw() {

draw(topRight, normal, fmt.Sprintf("Did you know? Your name is %s!", m.nickname))

var (
newGameText = "[ NEW GAME ]"
joinGameText = "[ JOIN GAME ]"
)

var buttonColors [2]color
if m.isJoinGame {
buttonColors = [2]color{normal, inverted}
} else {
buttonColors = [2]color{inverted, normal}
}
buttonColors := [3]color{normal, normal, normal}
buttonColors[m.button] = inverted

draw(offset(centerLeft, -1, 3), buttonColors[0], newGameText)
draw(offset(centerRight, 1, 3), buttonColors[1], joinGameText)
draw(offset(centerLeft, -1, 3), buttonColors[buttonNewGame], "[ NEW GAME ]")
draw(offset(centerRight, 1, 3), buttonColors[buttonJoinGame], "[ JOIN GAME ]")
draw(offset(topRight, 0, 2), buttonColors[buttonChangeName], "[ CHANGE NAME ]")
}
7 changes: 6 additions & 1 deletion pkg/client/scenes/nickname.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ const maxNicknameLen = 10

type Nickname struct {
scene
nickname string
nickname string
changeNickname bool
}

func (n *Nickname) Setup(changeScene ChangeScene, sendMessage SendMessage) error {
if err := n.scene.Setup(changeScene, sendMessage); err != nil {
return err
}

if n.changeNickname {
return nil
}

if err := n.load(); err != nil {
return err
}
Expand Down

0 comments on commit cf57543

Please sign in to comment.