Skip to content

Commit 275ec5a

Browse files
feat: add soft drop toggling
1 parent ed4d066 commit 275ec5a

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

model.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ import (
1212

1313
type Playfield [40][10]byte
1414

15+
type Fall struct {
16+
stopwatch stopwatch.Model
17+
defaultTime time.Duration
18+
softDropTime time.Duration
19+
currentMode uint8
20+
}
21+
1522
type Model struct {
1623
playfield Playfield
1724
styles *Styles
1825
help help.Model
1926
keys *KeyMap
20-
stopwatch stopwatch.Model
2127
currentTet *Tetrimino
28+
fall Fall
2229
}
2330

2431
func InitialModel() *Model {
@@ -27,14 +34,18 @@ func InitialModel() *Model {
2734
styles: DefaultStyles(),
2835
help: help.New(),
2936
keys: DefaultKeyMap(),
30-
stopwatch: stopwatch.NewWithInterval(time.Millisecond * 300),
37+
fall: Fall{
38+
defaultTime: time.Millisecond * 300,
39+
softDropTime: time.Millisecond * 100,
40+
},
3141
}
42+
m.fall.stopwatch = stopwatch.NewWithInterval(m.fall.defaultTime)
3243
m.currentTet = m.playfield.NewTetrimino()
3344
return m
3445
}
3546

3647
func (m Model) Init() tea.Cmd {
37-
return m.stopwatch.Init()
48+
return m.fall.stopwatch.Init()
3849
}
3950

4051
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -75,6 +86,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7586
}
7687
}
7788
m.currentTet = newTet
89+
case key.Matches(msg, m.keys.SoftDrop):
90+
m.fall.toggleSoftDrop()
7891
}
7992
case stopwatch.TickMsg:
8093
newTet, err := m.currentTet.MoveDown(&m.playfield)
@@ -89,10 +102,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
89102
fmt.Print(s)
90103
}
91104

92-
var stopwatchCmd tea.Cmd
93-
m.stopwatch, stopwatchCmd = m.stopwatch.Update(msg)
105+
var cmd tea.Cmd
106+
m.fall.stopwatch, cmd = m.fall.stopwatch.Update(msg)
94107

95-
return m, stopwatchCmd
108+
return m, cmd
96109
}
97110

98111
func (m Model) View() string {
@@ -121,3 +134,13 @@ func (m Model) View() string {
121134

122135
return m.styles.Program.Render(output) + "\n" + m.help.View(m.keys)
123136
}
137+
138+
func (f *Fall) toggleSoftDrop() {
139+
if f.currentMode == 0 {
140+
f.currentMode = 1
141+
f.stopwatch.Interval = f.softDropTime
142+
return
143+
}
144+
f.currentMode = 0
145+
f.stopwatch.Interval = f.defaultTime
146+
}

0 commit comments

Comments
 (0)