|  | 
|  | 1 | +package tetris | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"strconv" | 
|  | 5 | +	"strings" | 
|  | 6 | +	"time" | 
|  | 7 | + | 
|  | 8 | +	"github.com/Kaamkiya/gg/internal/app/tetris/color" | 
|  | 9 | +	"github.com/Kaamkiya/gg/internal/app/tetris/shape" | 
|  | 10 | +	tea "github.com/charmbracelet/bubbletea" | 
|  | 11 | +	"github.com/charmbracelet/lipgloss" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +type gameProgressTick struct{} | 
|  | 15 | + | 
|  | 16 | +func initialModel() gameState { | 
|  | 17 | +	return gameState{ | 
|  | 18 | +		nil, | 
|  | 19 | +		nil, | 
|  | 20 | +		newGameboard(color.Colors), | 
|  | 21 | +		shape.NewRandomizer(), | 
|  | 22 | +		0, | 
|  | 23 | +		&difficulty{ | 
|  | 24 | +			initialDifficulyCountDown, | 
|  | 25 | +			initialDifficulyLevel, | 
|  | 26 | +			initialGameProgressTickDelay, | 
|  | 27 | +		}, | 
|  | 28 | +		false, | 
|  | 29 | +		dropFinished, | 
|  | 30 | +	} | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +func (gs *gameState) Init() tea.Cmd { | 
|  | 34 | +	return func() tea.Msg { | 
|  | 35 | +		return gameProgressTick{} | 
|  | 36 | +	} | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +// Update implements the game loop by handling the tea.Msg structs. There are the following flows: | 
|  | 40 | +//   - Base loop: gameProgressTick -> handleGameProgress -> gameProgressTick | 
|  | 41 | +//   - Line complete: gameProgressTick -> handleGameProgress -> lineAnimationTick | 
|  | 42 | +//   - Line animation ongoing: lineAnimationTick -> handleLineAnimation -> lineAnimationTick | 
|  | 43 | +//   - Line animation finished: lineAnimationTick -> handleLineAnimation -> gameProgressTick | 
|  | 44 | +func (gs *gameState) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | 
|  | 45 | +	switch msg := msg.(type) { | 
|  | 46 | +	case tea.KeyMsg: | 
|  | 47 | +		if msg.String() == "ctrl+c" || msg.String() == "q" || msg.String() == "Q" { | 
|  | 48 | +			return gs, tea.Quit | 
|  | 49 | +		} else if !gs.isPaused { | 
|  | 50 | +			switch msg.String() { | 
|  | 51 | +			case "h", "H", "left": | 
|  | 52 | +				gs.handleLeft() | 
|  | 53 | +			case "l", "L", "right": | 
|  | 54 | +				gs.handleRight() | 
|  | 55 | +			case "j", "J", "down": | 
|  | 56 | +				gs.handleDrop() | 
|  | 57 | +			case "z", "Z": | 
|  | 58 | +				gs.handleLeftRotate() | 
|  | 59 | +			case "x", "X": | 
|  | 60 | +				gs.handleRightRotate() | 
|  | 61 | +			case "p", "P": | 
|  | 62 | +				gs.isPaused = true | 
|  | 63 | +				return gs, nil | 
|  | 64 | +			} | 
|  | 65 | +		} else { | 
|  | 66 | +			if msg.String() == "p" || msg.String() == "P" { | 
|  | 67 | +				gs.isPaused = false | 
|  | 68 | +				return gs, tea.Tick(gs.currentDifficulty.gameProgressTickDelay, func(time.Time) tea.Msg { return gameProgressTick{} }) | 
|  | 69 | +			} | 
|  | 70 | +		} | 
|  | 71 | +	case gameProgressTick: | 
|  | 72 | +		if gs.isPaused { | 
|  | 73 | +			return gs, nil | 
|  | 74 | +		} | 
|  | 75 | +		return gs, gs.handleGameProgressTick() | 
|  | 76 | +	case lineAnimationTick: | 
|  | 77 | +		return gs, gs.handleLineAnimationTick(msg) | 
|  | 78 | +	} | 
|  | 79 | + | 
|  | 80 | +	return gs, nil | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | +// View method creates the view by generating the play area and the sidebar. Although the Tetris board size is | 
|  | 84 | +// defined by Height and Width, the play area is larger. Each Tetris box is 4 characters wide and 2 characters tall | 
|  | 85 | +// so the total play area size is 2 * Height * 4 * Width characters. On each line of the play area, a sidebar | 
|  | 86 | +// line is appended. | 
|  | 87 | +func (gs *gameState) View() string { | 
|  | 88 | +	boardBuilder := strings.Builder{} | 
|  | 89 | +	boardBuilder.Grow((height+2)*(width+2)*8 + 22*14) | 
|  | 90 | + | 
|  | 91 | +	borderStyle := lipgloss.NewStyle(). | 
|  | 92 | +		BorderStyle(lipgloss.NormalBorder()). | 
|  | 93 | +		BorderForeground(lipgloss.AdaptiveColor{Light: "#200C0C", Dark: "#BEC1C6"}) | 
|  | 94 | + | 
|  | 95 | +	gameGridLines := buildGameGrid(gs) | 
|  | 96 | +	sideBarLines := buildSidebar(gs) | 
|  | 97 | + | 
|  | 98 | +	for i := range height * 2 { | 
|  | 99 | +		var playAreaStr string | 
|  | 100 | + | 
|  | 101 | +		if i == 0 { | 
|  | 102 | +			playAreaStr = borderStyle. | 
|  | 103 | +				BorderTop(true). | 
|  | 104 | +				BorderRight(true). | 
|  | 105 | +				BorderLeft(true). | 
|  | 106 | +				Render(gameGridLines[i]) | 
|  | 107 | +		} else if i == height*2-1 { | 
|  | 108 | +			playAreaStr = borderStyle. | 
|  | 109 | +				BorderRight(true). | 
|  | 110 | +				BorderBottom(true). | 
|  | 111 | +				BorderLeft(true). | 
|  | 112 | +				Render(gameGridLines[i]) | 
|  | 113 | +		} else { | 
|  | 114 | +			playAreaStr = borderStyle.BorderLeft(true).BorderRight(true).Render(gameGridLines[i]) | 
|  | 115 | +		} | 
|  | 116 | + | 
|  | 117 | +		boardBuilder.WriteString(playAreaStr) | 
|  | 118 | + | 
|  | 119 | +		if i < len(sideBarLines) { | 
|  | 120 | +			boardBuilder.WriteString(sideBarLines[i]) | 
|  | 121 | +		} | 
|  | 122 | + | 
|  | 123 | +		boardBuilder.WriteString("\n") | 
|  | 124 | +	} | 
|  | 125 | + | 
|  | 126 | +	return boardBuilder.String() | 
|  | 127 | +} | 
|  | 128 | + | 
|  | 129 | +func buildGameGrid(gs *gameState) [height * 2]string { | 
|  | 130 | +	gridLines := [height * 2]string{} | 
|  | 131 | + | 
|  | 132 | +	for i := range height { | 
|  | 133 | +		lineBuilder := strings.Builder{} | 
|  | 134 | +		lineBuilder.Grow(width * 4) | 
|  | 135 | + | 
|  | 136 | +		for j := range width { | 
|  | 137 | +			nextChar := gs.gameBoard.Colors[gs.gameBoard.Grid[i][j]].Render("    ") | 
|  | 138 | +			lineBuilder.WriteString(nextChar) | 
|  | 139 | +		} | 
|  | 140 | + | 
|  | 141 | +		line := lineBuilder.String() | 
|  | 142 | +		gridLines[2*i] = line | 
|  | 143 | +		gridLines[2*i+1] = line | 
|  | 144 | +	} | 
|  | 145 | + | 
|  | 146 | +	return gridLines | 
|  | 147 | +} | 
|  | 148 | + | 
|  | 149 | +func buildSidebar(gs *gameState) [14]string { | 
|  | 150 | +	sidebarLines := [14]string{} | 
|  | 151 | +	sidebarLines[0] = "      Next Shape      " | 
|  | 152 | +	sidebarLines[1] = "                      " | 
|  | 153 | + | 
|  | 154 | +	if gs.nextShape != nil { | 
|  | 155 | +		grid := gs.nextShape.GetGrid() | 
|  | 156 | + | 
|  | 157 | +		for i := range 4 { | 
|  | 158 | +			if i >= len(grid) { | 
|  | 159 | +				sidebarLines[i+2] = "                      " | 
|  | 160 | +			} else { | 
|  | 161 | +				lineBuilder := strings.Builder{} | 
|  | 162 | +				spaceLength := (22 - len(grid[i])) / 2 | 
|  | 163 | +				lineBuilder.WriteString(strings.Repeat(" ", spaceLength)) | 
|  | 164 | + | 
|  | 165 | +				for j := range grid[i] { | 
|  | 166 | +					if grid[i][j] { | 
|  | 167 | +						lineBuilder.WriteString(gs.gameBoard.Colors[gs.nextShape.GetColor()].Render(" ")) | 
|  | 168 | +					} else { | 
|  | 169 | +						lineBuilder.WriteString(" ") | 
|  | 170 | +					} | 
|  | 171 | +				} | 
|  | 172 | +				lineBuilder.WriteString(strings.Repeat(" ", spaceLength)) | 
|  | 173 | + | 
|  | 174 | +				sidebarLines[i+2] = lineBuilder.String() | 
|  | 175 | +			} | 
|  | 176 | +		} | 
|  | 177 | +	} | 
|  | 178 | + | 
|  | 179 | +	scoreStr := strconv.FormatUint(uint64(gs.score), 10) | 
|  | 180 | +	sidebarLines[6] = "                      " | 
|  | 181 | +	sidebarLines[7] = "   Your score is      " | 
|  | 182 | +	sidebarLines[8] = strings.Repeat(" ", 22-len(scoreStr)) + scoreStr | 
|  | 183 | +	sidebarLines[9] = "                      " | 
|  | 184 | +	sidebarLines[10] = "  hjl/←↓→ to move    " | 
|  | 185 | +	sidebarLines[11] = "  z,x to rotate      " | 
|  | 186 | +	sidebarLines[12] = "  q/ctl+c to quit    " | 
|  | 187 | +	sidebarLines[13] = "  p to pause         " | 
|  | 188 | + | 
|  | 189 | +	return sidebarLines | 
|  | 190 | +} | 
0 commit comments