-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
death_anim.go
112 lines (96 loc) · 2.96 KB
/
death_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gameview
import (
"fmt"
"github.com/BigJk/end_of_eden/game"
"github.com/BigJk/end_of_eden/system/audio"
"github.com/BigJk/end_of_eden/ui/animation"
"github.com/BigJk/end_of_eden/ui/style"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"math/rand"
"time"
)
type DeathAnimationFrame string
type DeathAnimationModel struct {
id string
width int
height int
target game.Actor
enemy *game.Enemy
death game.StateEventDeathData
progress float64
started bool
}
func NewDeathAnimationModel(width int, height int, target game.Actor, targetEnemy *game.Enemy, death game.StateEventDeathData) DeathAnimationModel {
return DeathAnimationModel{
id: fmt.Sprint(rand.Intn(100000)),
width: width,
height: height,
target: target,
enemy: targetEnemy,
death: death,
}
}
func (m DeathAnimationModel) SetSize(width int, height int) DeathAnimationModel {
m.width = width
m.height = height
return m
}
func (m DeathAnimationModel) Init() tea.Cmd {
return nil
}
func (m DeathAnimationModel) 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.progress > 0.1 && (msg.Type == tea.KeyEnter || msg.Type == tea.KeySpace) {
return nil, nil
}
case tea.MouseMsg:
if m.progress > 0.1 && (msg.Action == tea.MouseActionRelease && msg.Button == tea.MouseButtonLeft) {
return nil, nil
}
case DeathAnimationFrame:
if string(msg) != m.id {
return m, nil
}
if m.progress == 0 {
audio.Play("death_scream_1")
}
elapsed := 1.0 / 30.0 / 5.0
m.progress += elapsed
if m.progress >= 1.0 {
return nil, nil
}
return m, tea.Tick(time.Second/time.Duration(30), func(t time.Time) tea.Msg {
return DeathAnimationFrame(m.id)
})
}
if !m.started {
m.started = true
return m, tea.Tick(time.Second/time.Duration(30), func(t time.Time) tea.Msg {
return DeathAnimationFrame(m.id)
})
}
return m, nil
}
const killedText = `▄ •▄ ▪ ▄▄▌ ▄▄▌ ▄▄▄ .·▄▄▄▄ ▄▄ ▄▄
█▌▄▌▪██ ██• ██• ▀▄.▀·██▪ ██ ██▌██▌
▐▀▀▄·▐█·██▪ ██▪ ▐▀▀▪▄▐█· ▐█▌▐█·▐█·
▐█.█▌▐█▌▐█▌▐▌▐█▌▐▌▐█▄▄▌██. ██ .▀ .▀
·▀ ▀▀▀▀.▀▀▀ .▀▀▀ ▀▀▀ ▀▀▀▀▀• ▀ ▀`
func (m DeathAnimationModel) View() string {
killedText := animation.JitterText(killedText, m.progress*2.5, 0, 10)
face := faceStyle.Copy().BorderForeground(style.BaseRed).Foreground(lipgloss.Color(m.enemy.Color)).Render(m.enemy.Look)
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center,
lipgloss.JoinVertical(
lipgloss.Center,
style.RedText.Render(`━╋━
┃`),
face,
style.BaseText.Render(m.target.Name),
lipgloss.NewStyle().Margin(2, 0, 0, 0).Foreground(style.BaseRed).Render(killedText),
),
)
}