/
dialogue.go
147 lines (117 loc) · 2.88 KB
/
dialogue.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"golang.org/x/image/colornames"
)
var (
DialoguePresenter = dialoguePresent{
currentRun: &intro1,
showing: false,
}
diaIMD = imdraw.New(nil)
)
var (
intro1 = dialogue{
text: "Welcome to NoGo45, a game about starting with nothing",
next: &intro2,
initd: time.Now(),
delay: time.Second * 3,
}
intro2 = dialogue{
text: "Oh! You seem to be missing a level",
next: &intro3,
delay: time.Second * 3,
}
intro3 = dialogue{
text: "...and controls, oops!",
next: &intro4,
delay: time.Second,
}
intro4 = dialogue{
text: "Don't worry, we can fix this; we just need coins!",
next: &intro5,
delay: time.Second * 2,
}
intro5 = dialogue{
text: "To get your first coin, just press 'u' to get to the upgrade menu\nExit this dialogue first with 'Space'",
delay: time.Second,
}
afterMovement = dialogue{
text: "Now you can move your character\nUse Esc to get out the upgrade menu then try out moving with WASD",
}
)
type dialogue struct {
text string
next *dialogue
initd time.Time
delay time.Duration
}
type dialoguePresent struct {
currentRun *dialogue
showing bool
}
func (d *dialoguePresent) update(dt float64, win *pixelgl.Window) leveler {
if d.showing {
d.updateShowing(dt, win)
} else {
d.updateNotShowing(dt, win)
}
return currentLvl
}
func (d *dialoguePresent) draw(target pixel.Target) {
if !d.showing {
return
}
const diaBorder = 50.0
const diaBottom = 150
const diaHeight = 400
const diaPadding = 40
// Box
diaIMD.Clear()
diaIMD.Color = colornames.Beige
bottomLeft := cam.Unproject(pixel.V(diaBorder, diaBottom))
topLeft := cam.Unproject(pixel.V(diaBorder, diaHeight))
topRight := cam.Unproject(pixel.V(winBounds.Max.X-diaBorder, diaHeight))
diaIMD.Push(bottomLeft, topRight)
diaIMD.Rectangle(0)
diaIMD.Draw(target)
// Text
t := text.New(topLeft.Add(pixel.V(diaPadding, -diaPadding)), atlas)
t.Color = defaultTextColour
_, _ = fmt.Fprint(t, d.currentRun.text)
t.Draw(target, pixel.IM)
t = text.New(bottomLeft.Add(pixel.V(winBounds.Center().X, diaPadding)), atlas)
t.Color = defaultTextColour
_, _ = fmt.Fprint(t, "Press space to continue...")
t.Draw(target, pixel.IM)
}
func (d *dialoguePresent) updateShowing(dt float64, win *pixelgl.Window) {
if win.JustPressed(pixelgl.KeySpace) {
d.showing = false
next := d.currentRun.next
if next != nil {
next.initd = time.Now()
}
d.currentRun = next
resumeGame()
return
}
}
// queue queues up a dialogue
func (d *dialoguePresent) queue(dia dialogue) {
d.currentRun = &dia
}
func (d *dialoguePresent) updateNotShowing(dt float64, win *pixelgl.Window) {
if d.currentRun == nil {
return
}
if time.Since(d.currentRun.initd) >= d.currentRun.delay {
d.showing = true
pauseGame()
}
}