-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathui.go
453 lines (435 loc) · 14.4 KB
/
ui.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
Copyright (c) 2018, Tomasz "VedVid" Nowakowski
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
blt "bearlibterminal"
"errors"
"fmt"
"sort"
"strconv"
"unicode/utf8"
)
const (
MaxMessageBuffer = WindowSizeY - MapSizeY
)
func PrintMenu(x, y int, header string, options []string) {
/* Function PrintMenu takes four arguments: two ints that are
top-left corner of menu, header, and slice of options.
If header is empty, text is moved one tile higher to
avoid wasting space.
During execution, it joins header and all of options in
one text, with additional formatting.
For example, header "MyMenu" and options ["first", "two"]
would produce that kind of output:
MyMenu
a) first
b) two
It refreshed terminal and waits for player input at the end. */
blt.ClearArea(UIPosX, UIPosY, UISizeX, UISizeY)
if header == "" {
y--
}
txt := header
for i, v := range options {
txt = txt + "\n" + OrderToCharacter(i) + ") " + v
}
txt = txt + "\n[[ESC]] back"
blt.Print(x, y, txt)
blt.Refresh()
}
func PrintInventoryMenu(x, y int, header string, options Objects) {
/* PrintInventoryMenu is helper function that takes Objects
as its main argument, and adds their names (currently
their symbol representation, due to some strange decisions
made by dev, objects doesn't have names yet) to the opts
slice of strings, then calls PrintMenu using that list.
Unfortunately that kind of "hack" is necessary, because
Go doesn't support generics and optional arguments,
and still doesn't provide sensible alternatives.
I'd like to just pass Objects to the PrintMenu func. */
var opts = []string{}
for _, v := range options {
opts = append(opts, v.Name)
}
PrintMenu(x, y, header, opts)
}
func PrintEquipmentMenu(x, y int, header string, options Objects) {
/* Similar to PrintInventoryMenu, but it sorts options
by their Slots initially, and slot in showed before
item name.
Note that it shows Creature's slots,
not all equippable objects in inventory.
Because of this, it is necessary to find "true" length
of options, skipping all nil pointers.
Unfortunately, it may crash in future, with
more slots involved. */
var opts = []string{}
for i := 0; i < len(options); i++ {
txt := ""
if options[i] != nil {
txt = "[[" + SlotStrings[i] + "]] " + options[i].Name
} else {
txt = "[[" + SlotStrings[i] + "]] empty"
}
opts = append(opts, txt)
}
PrintMenu(x, y, header, opts)
}
func PrintEquippables(x, y int, header string, options Objects) {
/* PrintEquippables is function that prints list of equippables. */
var opts = []string{}
for _, v := range options {
opts = append(opts, v.Name)
}
PrintMenu(x, y, header, opts)
}
func PrintMessages(x, y int, header string) {
/* PrintMessages works as PrintMenu, but it
will not format text in special way. */
if header == "" {
y--
}
txt := header
for _, v := range MsgBuf {
txt = txt + "\n" + v
}
blt.Print(x, y, txt)
}
func AddMessage(message string) {
/* AddMessage is function that adds message
to the MessageBuffer. It removes the oldest
line to keep size set in MaxMessageBuffer.
But first, it checks if passed message is
not longer than whole message log.
This is mostly harmless, so AddMessage
does not returns error, but prints it
at its own. */
var err error
messageLen := utf8.RuneCountInString(message)
if messageLen > LogSizeX {
txt := MessageLengthError(message, messageLen, LogSizeX)
err = errors.New("Message is too long to fit message log. " + txt)
fmt.Println(err)
}
if len(MsgBuf) < MaxMessageBuffer {
MsgBuf = append(MsgBuf, message)
} else {
MsgBuf = append(MsgBuf[1:], message)
}
PrintLog()
blt.Refresh()
}
func RemoveLastMessage() {
/* Function RemoveLastMessage is called when it is necessary to remove
last message from buffer, even if said buffer is not full.
It removes last message, clears its area, and reprints log. */
MsgBuf = MsgBuf[:len(MsgBuf)-1]
blt.Layer(UILayer)
blt.ClearArea(LogPosX, LogPosY, LogPosX+LogSizeX, LogPosY+LogSizeY)
PrintLog()
blt.Refresh()
}
func HandleHighScores() {
Scores.Scores = append(Scores.Scores, Config.Score)
sort.Sort(sort.Reverse(sort.IntSlice(Scores.Scores)))
size := len(Scores.Scores)
if size > 10 {
size = 10
Scores.Scores = Scores.Scores[:10]
}
blt.Clear()
first := true
hs := "High scores: "
blt.Print(((WindowSizeX / 2) - (utf8.RuneCountInString(hs) / 2)), 3, hs)
for i := 0; i < size; i++ {
if Scores.Scores[i] == Config.Score && first == true {
blt.Color(blt.ColorFromName("yellow"))
first = false
}
txt := strconv.Itoa(Scores.Scores[i])
sep := " "
if i == 9 {
sep = ""
}
blt.Print(((WindowSizeX / 2) - 5), 5+i, sep+strconv.Itoa(i+1)+". "+txt)
blt.Color(blt.ColorFromName("white"))
}
exit := "Press <ENTER> to exit."
blt.Print(((WindowSizeX / 2) - (utf8.RuneCountInString(exit) / 2)), 12, exit)
for {
blt.Refresh()
key := ReadInput()
if key == blt.TK_ENTER || key == blt.TK_SPACE || key == blt.TK_ESCAPE || key == blt.TK_CLOSE {
break
}
}
SaveScores()
}
func PrintVictoryScreen() {
yourScore := Stats.Killed - Stats.Lost
if yourScore < 0 {
yourScore = 0
}
yourScore = (yourScore * Config.Score) / 100
if yourScore < 10 {
yourScore = 10
}
Config.Score = yourScore
for {
blt.Clear()
blt.Layer(UILayer)
line1 := "You did it!"
line2 := "Finally did it!"
line3 := "You killed everyone in the train,"
line4 := "reached the engine, killed the driver as well,"
line5 := "and pulled the break lever!"
line6 := "Now, you can just unload these gold filled chests"
line7 := "from this train and live rich and well."
line8 := "Your score is: " + strconv.Itoa(Config.Score)
line1len := utf8.RuneCountInString(line1)
line2len := utf8.RuneCountInString(line2)
line3len := utf8.RuneCountInString(line3)
line4len := utf8.RuneCountInString(line4)
line5len := utf8.RuneCountInString(line5)
line6len := utf8.RuneCountInString(line6)
line7len := utf8.RuneCountInString(line7)
line8len := utf8.RuneCountInString(line8)
line9 := "Press <ENTER> to proceed."
line9len := utf8.RuneCountInString(line9)
posy := (WindowSizeY / 2) - 6
blt.Print(((WindowSizeX / 2) - (line1len / 2)), posy, line1)
blt.Print(((WindowSizeX / 2) - (line2len / 2)), posy+1, line2)
blt.Print(((WindowSizeX / 2) - (line3len / 2)), posy+2, line3)
blt.Print(((WindowSizeX / 2) - (line4len / 2)), posy+3, line4)
blt.Print(((WindowSizeX / 2) - (line5len / 2)), posy+4, line5)
blt.Print(((WindowSizeX / 2) - (line6len / 2)), posy+5, line6)
blt.Print(((WindowSizeX / 2) - (line7len / 2)), posy+6, line7)
blt.Print(((WindowSizeX / 2) - (line8len / 2)), posy+8, line8)
blt.Print(((WindowSizeX / 2) - (line9len / 2)), posy+10, line9)
blt.Refresh()
key := ReadInput()
if key == blt.TK_ESCAPE || key == blt.TK_ENTER || key == blt.TK_SPACE || key == blt.TK_CLOSE {
break
}
}
HandleHighScores()
}
func DeadScreen() {
yourScore := Stats.Killed - Stats.Lost
if yourScore < 0 {
yourScore = 0
}
Config.Score = yourScore
for {
blt.Clear()
blt.Layer(UILayer)
line1 := "Maybe robbing this train was a bad idea?"
line2 := "Maybe it was a good idea, but you made a series of mistakes?"
line3 := "You'll never know."
line4 := "They got you. Ten times."
line5 := "No chances to leave this damn train alive..."
line1len := utf8.RuneCountInString(line1)
line2len := utf8.RuneCountInString(line2)
line3len := utf8.RuneCountInString(line3)
line4len := utf8.RuneCountInString(line4)
line5len := utf8.RuneCountInString(line5)
line8 := "Your score is: " + strconv.Itoa(Config.Score)
line8len := utf8.RuneCountInString(line8)
line6 := "Press <ENTER> to proceed."
line6len := utf8.RuneCountInString(line6)
posy := (WindowSizeY / 2) - 5
blt.Print(((WindowSizeX / 2) - (line1len / 2)), posy, line1)
blt.Print(((WindowSizeX / 2) - (line2len / 2)), posy+1, line2)
blt.Print(((WindowSizeX / 2) - (line3len / 2)), posy+2, line3)
blt.Print(((WindowSizeX / 2) - (line4len / 2)), posy+3, line4)
blt.Print(((WindowSizeX / 2) - (line5len / 2)), posy+4, line5)
blt.Print(((WindowSizeX / 2) - (line8len / 2)), posy+6, line8)
blt.Print(((WindowSizeX / 2) - (line6len / 2)), posy+8, line6)
blt.Refresh()
key := ReadInput()
if key == blt.TK_ESCAPE || key == blt.TK_ENTER || key == blt.TK_SPACE || key == blt.TK_CLOSE {
break
}
}
HandleHighScores()
}
func MainMenu(cfg *Cfg) {
lives := livesNormal
monsters := MonstersNormal
reloading := AmmoUnlimited
animations := AnimationsFalse
score := 100
if CfgIsHere == true {
lives = Config.Lives
monsters = Config.Monsters
//reloading = Config.Reloading
animations = Config.Animations
score = Config.Score
}
for {
blt.Clear()
blt.Color(blt.ColorFromName("#B87333"))
titleLine1 := "╦═╗╔═╗╔═╗║ ║╦═╣ ╦═╗╔═╗╦╗ ╔═╣ ═╦═╦═╗╔═╗╠╦╣╔╗╦╔═╣"
titleLine2 := "║ ║║ ║║ ║ ║║ ║ ║║ ║║║ ║ ║ ║ ║║ ║ ║ ║║║║ "
titleLine3 := "╠╦╝║ ║║ ╦║ ║╬═ ╠╦╝║ ║╠╩╗╚═╗ ║ ╠╦╝╠═╣ ║ ║║║╚═╗"
titleLine4 := "║║ ║ ║║ ║║ ║║ ║║ ║ ║║ ║ ║ ║ ║║ ║ ║ ║ ║║║ ║"
titleLine5 := "╩╚╝╚═╝╚═╝╚═╝╩═╣ ╩╚╝╚═╝╚═╝╠═╝ ╩ ╩╚╝╩ ╩╠╩╣╩╚╝╠═╝"
titlePos := 3
blt.Print(7, titlePos, titleLine1)
blt.Print(7, titlePos+1, titleLine2)
blt.Print(7, titlePos+2, titleLine3)
blt.Print(7, titlePos+3, titleLine4)
blt.Print(7, titlePos+4, titleLine5)
blt.Color(blt.ColorFromName("white"))
livesString := ""
if lives == livesNormal {
livesString = "normal"
} else if lives == livesEasy {
livesString = "easy"
} else if lives == livesHard {
livesString = "hard"
}
line1 := "<a> ← Lives: " + livesString + " → <A>"
monstersString := ""
if monsters == MonstersNormal {
monstersString = "normal"
} else if monsters == MonstersEasy {
monstersString = "fewer"
} else if monsters == MonstersHard {
monstersString = "more"
}
line2 := "<b> ← Enemies: " + monstersString + " → <B>"
/*reloadingString := ""
if reloading == AmmoUnlimited {
reloadingString = "unlimited"
} else {
reloadingString = "limited"
}
line3 := "<c> ← Reloading: " + reloadingString + " → <C>"*/
animationsString := ""
if animations == AnimationsFalse {
animationsString = "off"
} else {
animationsString = "on"
}
line4 := "<c> ← Animations: " + animationsString + " → <C>"
line5 := "Score multiplier: " + strconv.Itoa(score) + "%"
line6 := "Press <ENTER> to proceed."
line1len := utf8.RuneCountInString(line1)
line2len := utf8.RuneCountInString(line2)
//line3len := utf8.RuneCountInString(line3)
line4len := utf8.RuneCountInString(line4)
line5len := utf8.RuneCountInString(line5)
line6len := utf8.RuneCountInString(line6)
posy := (WindowSizeY / 2)
blt.Print(((WindowSizeX / 2) - (line1len / 2)), posy, line1)
blt.Print(((WindowSizeX / 2) - (line2len / 2)), posy+1, line2)
//blt.Print(((WindowSizeX / 2) - (line3len / 2)), posy+2, line3)
blt.Print(((WindowSizeX / 2) - (line4len / 2)), posy+2, line4)
blt.Print(((WindowSizeX / 2) - (line5len / 2)), posy+4, line5)
blt.Print(((WindowSizeX / 2) - (line6len / 2)), posy+6, line6)
blt.Refresh()
key := ReadInput()
if key == blt.TK_ENTER {
cfg.Lives = lives
cfg.Monsters = monsters
cfg.Reloading = reloading
cfg.Score = score
cfg.Animations = animations
break
}
if key == blt.TK_A && blt.Check(blt.TK_SHIFT) != 0 {
if lives == livesEasy {
lives = livesNormal
score += 25
} else if lives == livesNormal {
lives = livesHard
score += 25
} else {
continue
}
} else if key == blt.TK_A {
if lives == livesNormal {
lives = livesEasy
score -= 25
} else if lives == livesHard {
lives = livesNormal
score -= 25
} else {
continue
}
} else if key == blt.TK_B && blt.Check(blt.TK_SHIFT) != 0 {
if monsters == MonstersEasy {
monsters = MonstersNormal
score += 25
} else if monsters == MonstersNormal {
monsters = MonstersHard
score += 25
} else {
continue
}
} else if key == blt.TK_B {
if monsters == MonstersNormal {
monsters = MonstersEasy
score -= 25
} else if monsters == MonstersHard {
monsters = MonstersNormal
score -= 25
} else {
continue
}
/*} else if key == blt.TK_C && blt.Check(blt.TK_SHIFT) != 0 {
if reloading == AmmoUnlimited {
reloading = AmmoLimited
score += 25
} else {
reloading = AmmoUnlimited
score -= 25
}
} else if key == blt.TK_C {
if reloading == AmmoUnlimited {
reloading = AmmoLimited
score += 25
} else {
reloading = AmmoUnlimited
score -= 25
}*/
} else if key == blt.TK_C && blt.Check(blt.TK_SHIFT) != 0 {
if animations == AnimationsFalse {
animations = AnimationsTrue
} else {
animations = AnimationsFalse
}
} else if key == blt.TK_C {
if animations == AnimationsFalse {
animations = AnimationsTrue
} else {
animations = AnimationsFalse
}
}
}
err := SaveConfig()
if err != nil {
fmt.Println("Error during saving config file!")
fmt.Println(err)
}
}