@@ -12,13 +12,20 @@ import (
12
12
13
13
type Playfield [40 ][10 ]byte
14
14
15
+ type Fall struct {
16
+ stopwatch stopwatch.Model
17
+ defaultTime time.Duration
18
+ softDropTime time.Duration
19
+ currentMode uint8
20
+ }
21
+
15
22
type Model struct {
16
23
playfield Playfield
17
24
styles * Styles
18
25
help help.Model
19
26
keys * KeyMap
20
- stopwatch stopwatch.Model
21
27
currentTet * Tetrimino
28
+ fall Fall
22
29
}
23
30
24
31
func InitialModel () * Model {
@@ -27,14 +34,18 @@ func InitialModel() *Model {
27
34
styles : DefaultStyles (),
28
35
help : help .New (),
29
36
keys : DefaultKeyMap (),
30
- stopwatch : stopwatch .NewWithInterval (time .Millisecond * 300 ),
37
+ fall : Fall {
38
+ defaultTime : time .Millisecond * 300 ,
39
+ softDropTime : time .Millisecond * 100 ,
40
+ },
31
41
}
42
+ m .fall .stopwatch = stopwatch .NewWithInterval (m .fall .defaultTime )
32
43
m .currentTet = m .playfield .NewTetrimino ()
33
44
return m
34
45
}
35
46
36
47
func (m Model ) Init () tea.Cmd {
37
- return m .stopwatch .Init ()
48
+ return m .fall . stopwatch .Init ()
38
49
}
39
50
40
51
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) {
75
86
}
76
87
}
77
88
m .currentTet = newTet
89
+ case key .Matches (msg , m .keys .SoftDrop ):
90
+ m .fall .toggleSoftDrop ()
78
91
}
79
92
case stopwatch.TickMsg :
80
93
newTet , err := m .currentTet .MoveDown (& m .playfield )
@@ -89,10 +102,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
89
102
fmt .Print (s )
90
103
}
91
104
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 )
94
107
95
- return m , stopwatchCmd
108
+ return m , cmd
96
109
}
97
110
98
111
func (m Model ) View () string {
@@ -121,3 +134,13 @@ func (m Model) View() string {
121
134
122
135
return m .styles .Program .Render (output ) + "\n " + m .help .View (m .keys )
123
136
}
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