main.go
This file
Path
examples/doom-fire/main.go
133 lines · 2.8 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
1package main23import (4 "fmt"5 "math/rand"6 "strings"7 "time"89 tea "charm.land/bubbletea/v2"10 "charm.land/lipgloss/v2"11)1213// This Doom Fire implementation was ported from @const-void's Node version.14// See https://github.com/const-void/DOOM-fire-node1516var whiteFg = lipgloss.NewStyle().Foreground(lipgloss.White)1718type model struct {19 screenBuf []int20 width int21 height int22 firePalette []int23 startTime time.Time24}2526func (m model) Init() tea.Cmd {27 return tick28}2930func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {31 switch msg := msg.(type) {32 case tea.KeyPressMsg:33 if msg.String() == "q" || msg.String() == "ctrl+c" {34 return m, tea.Quit35 }36 case tickMsg:37 m.spreadFire()38 return m, tick39 case tea.WindowSizeMsg:40 m.width = msg.Width41 m.height = msg.Height * 2 // Double height for half-block characters42 m.screenBuf = make([]int, m.width*m.height)43 // Initialize the bottom row with white (maximum intensity)44 for i := range m.width {45 m.screenBuf[(m.height-1)*m.width+i] = len(m.firePalette) - 146 }47 }48 return m, nil49}5051func (m model) View() tea.View {52 if m.width == 0 {53 return tea.NewView("Initializing...")54 }5556 var s strings.Builder5758 for y := 0; y < m.height-2; y += 2 {59 for x := range m.width {60 pixelHi := m.screenBuf[y*m.width+x]61 pixelLo := m.screenBuf[(y+1)*m.width+x]6263 hiColor := m.firePalette[pixelHi]64 loColor := m.firePalette[pixelLo]6566 s.WriteString(lipgloss.NewStyle().67 Foreground(lipgloss.ANSIColor(hiColor)).68 Background(lipgloss.ANSIColor(loColor)).69 Render("▀"))70 }71 if y < m.height-2 {72 s.WriteByte('\n')73 }74 }7576 elapsed := time.Since(m.startTime)77 s.WriteString(whiteFg.Render("Press q or ctrl+c to quit. " + fmt.Sprintf("Elapsed: %s", elapsed.Round(time.Second))))78 v := tea.NewView(s.String())79 v.AltScreen = true80 return v81}8283func (m *model) spreadFire() {84 for x := range m.width {85 for y := range m.height {86 m.spreadPixel(y*m.width + x)87 }88 }89}9091func (m *model) spreadPixel(idx int) {92 if idx < m.width {93 return94 }9596 pixel := m.screenBuf[idx]97 if pixel == 0 {98 m.screenBuf[idx-m.width] = 099 return100 }101102 rnd := rand.Intn(3)103 dst := idx - rnd + 1104 if dst-m.width >= 0 && dst-m.width < len(m.screenBuf) {105 decay := rnd & 1106 newValue := max(pixel-decay, 0)107 m.screenBuf[dst-m.width] = newValue108 }109}110111type tickMsg time.Time112113func tick() tea.Msg {114 time.Sleep(time.Millisecond * 50)115 return tickMsg(time.Now())116}117118func initialModel() model {119 // Same color palette as the original120 palette := []int{0, 233, 234, 52, 53, 88, 89, 94, 95, 96, 130, 131, 132, 133, 172, 214, 215, 220, 220, 221, 3, 226, 227, 230, 231, 7}121122 return model{123 firePalette: palette,124 startTime: time.Now(),125 }126}127128func main() {129 p := tea.NewProgram(initialModel())130 if _, err := p.Run(); err != nil {131 fmt.Printf("Error running program: %v", err)132 }133}