-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
80 lines (68 loc) · 1.37 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"sync"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type frameMsg struct{}
func animate() tea.Cmd {
return tea.Tick(time.Second/time.Duration(fps), func(_ time.Time) tea.Msg {
return frameMsg{}
})
}
type model struct {
cells cellbuffer
boids []boid
}
func (m model) Init() tea.Cmd {
return animate()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
return m, tea.Quit
case tea.WindowSizeMsg:
updateVars()
m.cells.init(msg.Width, msg.Height)
m.boids = initBoidsOnScreenSize(msg.Width, msg.Height)
return m, nil
case frameMsg:
if !m.cells.ready() {
return m, nil
}
m.cells.wipe()
m.updateBoids()
return m, animate()
default:
return m, nil
}
}
func (m model) View() string {
return m.cells.String()
}
func (m model) updateBoids() {
var wg sync.WaitGroup
for i := range m.boids {
wg.Add(1)
go func(id int) {
defer wg.Done()
m.boids[i].update(m.boids)
}(i)
wg.Wait()
m.boids[i].move()
drawTriangle(&m.cells, m.boids[i].pos, m.boids[i].forward)
}
}
func drawTriangle(cb *cellbuffer, centre, dir Point) {
cb.set(int(centre.x), int(centre.y), triangleRuneTable[dir])
}
var triangleRuneTable = map[Point]string{
{-1, -1}: "◤",
{-1, 0}: "◀",
{-1, 1}: "◣",
{0, 1}: "▼",
{1, 1}: "◢",
{1, 0}: "▶",
{1, -1}: "◥",
{0, -1}: "▲",
}