Skip to content

Commit

Permalink
feat: implement sprint game mode (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Broderick-Westrope authored Aug 21, 2024
1 parent 0b78a5a commit f59b998
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
16 changes: 10 additions & 6 deletions cmd/tetrigo/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ type PlayCmd struct {
}

func (c *PlayCmd) Run(globals *GlobalVars) error {
switch c.GameMode {
case "marathon":
return launchStarter(globals, common.ModeUltra, common.NewSingleInput(common.ModeMarathon, c.Level, c.Name))
case "ultra":
return launchStarter(globals, common.ModeUltra, common.NewSingleInput(common.ModeUltra, c.Level, c.Name))
default:
singlePlayerModes := map[string]common.Mode{
"marathon": common.ModeMarathon,
"sprint": common.ModeSprint,
"ultra": common.ModeUltra,
}

mode, ok := singlePlayerModes[c.GameMode]
if !ok {
return fmt.Errorf("invalid game mode: %s", c.GameMode)
}

return launchStarter(globals, mode, common.NewSingleInput(common.ModeMarathon, c.Level, c.Name))
}

type LeaderboardCmd struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/tui/common/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ type Mode int
const (
ModeMenu = Mode(iota)
ModeMarathon
ModeSprint
ModeUltra
ModeLeaderboard
)

var modeToStrMap = map[Mode]string{
ModeMenu: "Menu",
ModeMarathon: "Marathon",
ModeSprint: "Sprint",
ModeUltra: "Ultra",
ModeLeaderboard: "Leaderboard",
}
Expand Down
7 changes: 2 additions & 5 deletions internal/tui/menu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewModel(_ *common.MenuInput) *Model {
nameInput := textinput.NewModel("Enter your name", 20, 20)
modePicker := hpicker.NewModel([]hpicker.KeyValuePair{
{Key: "Marathon", Value: common.ModeMarathon},
// {Key: "Sprint (40 Lines)", Value: "sprint"},
{Key: "Sprint (40 Lines)", Value: common.ModeSprint},
{Key: "Ultra (Time Trial)", Value: common.ModeUltra},
})
levelPicker := hpicker.NewModel(nil, hpicker.WithRange(1, 15))
Expand Down Expand Up @@ -167,10 +167,7 @@ func (m Model) startGame() (tea.Cmd, error) {
}

switch mode {
case common.ModeMarathon:
in := common.NewSingleInput(mode, level, playerName)
return common.SwitchModeCmd(mode, in), nil
case common.ModeUltra:
case common.ModeMarathon, common.ModeSprint, common.ModeUltra:
in := common.NewSingleInput(mode, level, playerName)
return common.SwitchModeCmd(mode, in), nil
case common.ModeMenu, common.ModeLeaderboard:
Expand Down
23 changes: 20 additions & 3 deletions internal/tui/single/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"github.com/charmbracelet/lipgloss"
)

const (
timerUpdateInterval = time.Millisecond * 13
)

var _ tea.Model = &Model{}

type Model struct {
Expand Down Expand Up @@ -59,16 +63,29 @@ func NewModel(in *common.SingleInput, cfg *config.Config) (*Model, error) {
MaxLevel: cfg.MaxLevel,
IncreaseLevel: true,
EndOnMaxLevel: cfg.EndOnMaxLevel,
GhostEnabled: cfg.GhostEnabled,

GhostEnabled: cfg.GhostEnabled,
}
m.gameStopwatch = stopwatch.NewWithInterval(timerUpdateInterval)
case common.ModeSprint:
gameIn = &single.Input{
Level: in.Level,
MaxLevel: cfg.MaxLevel,
IncreaseLevel: true,

MaxLines: 40,
EndOnMaxLines: true,

GhostEnabled: cfg.GhostEnabled,
}
m.gameStopwatch = stopwatch.NewWithInterval(time.Millisecond * 13)
m.gameStopwatch = stopwatch.NewWithInterval(timerUpdateInterval)
case common.ModeUltra:
gameIn = &single.Input{
Level: in.Level,
GhostEnabled: cfg.GhostEnabled,
}
m.useTimer = true
m.gameTimer = timer.NewWithInterval(time.Minute*2, time.Millisecond*13)
m.gameTimer = timer.NewWithInterval(time.Minute*2, timerUpdateInterval)
case common.ModeMenu, common.ModeLeaderboard:
return nil, fmt.Errorf("invalid single player game mode: %v", in.Mode)
default:
Expand Down
6 changes: 3 additions & 3 deletions internal/tui/starter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func (m *Model) setChild(mode common.Mode, switchIn common.SwitchModeInput) erro
return ErrInvalidSwitchModeInput
}
m.child = menu.NewModel(menuIn)
case common.ModeMarathon, common.ModeUltra:
ultraIn, ok := switchIn.(*common.SingleInput)
case common.ModeMarathon, common.ModeSprint, common.ModeUltra:
singleIn, ok := switchIn.(*common.SingleInput)
if !ok {
return ErrInvalidSwitchModeInput
}
child, err := single.NewModel(ultraIn, m.cfg)
child, err := single.NewModel(singleIn, m.cfg)
if err != nil {
return err
}
Expand Down

0 comments on commit f59b998

Please sign in to comment.