-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
end_turn_anim.go
93 lines (79 loc) · 2.29 KB
/
end_turn_anim.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
81
82
83
84
85
86
87
88
89
90
91
92
93
package gameview
import (
"fmt"
"github.com/BigJk/end_of_eden/ui"
"github.com/BigJk/end_of_eden/ui/style"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"math/rand"
"time"
)
var round = `▄▄▄ ▄• ▄▌ ▐ ▄ ·▄▄▄▄
▀▄ █·▪ █▪██▌•█▌▐███▪ ██
▐▀▀▄ ▄█▀▄ █▌▐█▌▐█▐▐▌▐█· ▐█▌
▐█•█▌▐█▌.▐▌▐█▄█▌██▐█▌██. ██
.▀ ▀ ▀█▄▀▪ ▀▀▀ ▀▀ █▪▀▀▀▀▀• `
type EndTurnAnimationFrame string
type EndTurnAnimationModel struct {
id string
width int
height int
started bool
elapsed float64
turn int
}
func NewEndTurnAnimationModel(width int, height int, turn int) EndTurnAnimationModel {
return EndTurnAnimationModel{
id: fmt.Sprint(rand.Intn(100000)),
width: width,
height: height,
turn: turn,
}
}
func (m EndTurnAnimationModel) SetSize(width int, height int) EndTurnAnimationModel {
m.width = width
m.height = height
return m
}
func (m EndTurnAnimationModel) Init() tea.Cmd {
return nil
}
func (m EndTurnAnimationModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m = m.SetSize(msg.Width, msg.Height)
case tea.Key:
if m.elapsed > 0.1 && (msg.Type == tea.KeyEnter || msg.Type == tea.KeySpace) {
return nil, nil
}
case tea.MouseMsg:
if m.elapsed > 0.1 && (msg.Action == tea.MouseActionRelease && msg.Button == tea.MouseButtonLeft) {
return nil, nil
}
case EndTurnAnimationFrame:
if string(msg) == m.id {
m.elapsed += 1.0 / 30.0
if m.elapsed > 0.5 {
return nil, nil
}
return m, tea.Tick(time.Second/time.Duration(30), func(t time.Time) tea.Msg {
return EndTurnAnimationFrame(m.id)
})
}
}
// Send first tick
if !m.started {
m.started = true
return m, tea.Tick(time.Second/time.Duration(30), func(t time.Time) tea.Msg {
return EndTurnAnimationFrame(m.id)
})
}
return m, nil
}
func (m EndTurnAnimationModel) View() string {
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, lipgloss.JoinVertical(
lipgloss.Center,
lipgloss.NewStyle().Foreground(style.BaseRed).Margin(0, 0, 3, 0).Render(round),
style.RedText.Render(ui.GetNumber(m.turn)),
))
}