/
graphics.go
113 lines (95 loc) · 2.09 KB
/
graphics.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
package consoleui
import (
"github.com/nsf/termbox-go"
"github.com/xosmig/roguelike/core/geom"
"log"
)
func (ui *consoleUi) drawMap() {
gameMap := ui.model.GetMap()
for row := 0; row < gameMap.GetHeight(); row++ {
for col := 0; col < gameMap.GetWidth(); col++ {
var ch rune
modelName := gameMap.Get(geom.Loc(row, col)).Object.ModelName()
switch modelName {
case "exit":
ch = 'O'
case "character":
ch = '@'
case "wall":
ch = '#'
case "empty":
ch = ' '
case "zombie":
ch = 'z'
case "item":
ch = '$'
default:
log.Printf("Error: unknown object model name: '%v'\n", modelName)
ch = '?'
}
ui.draw(rune(ch), 0, 0)
}
ui.nextLine()
}
}
func (ui *consoleUi) drawRules() {
ui.println("Use arrows to move, Ctrl+C to exit")
}
func (ui *consoleUi) drawHealthBar() {
char := ui.model.GetCharacter()
for i := 0; i < char.GetHP(); i++ {
ui.draw('@', termbox.ColorRed|termbox.AttrBold, 0)
}
for i := char.GetHP(); i < char.GetMaxHP(); i++ {
ui.draw('@', termbox.ColorCyan, 0)
}
ui.nextLine()
}
func (ui *consoleUi) drawInventory() {
char := ui.model.GetCharacter()
if len(char.Inventory()) == 0 {
ui.emptyLine()
ui.emptyLine()
ui.emptyLine()
return
}
ui.println("Use Ctrl + A/S/D to wear or take off items")
for i := range char.Inventory() {
ui.draw('A'+rune(i), 0, 0)
}
ui.nextLine()
for _, item := range char.Inventory() {
var ch rune
switch item.IconName() {
case "health_amulet":
ch = 'H'
default:
log.Printf("Error: unknown item icon name: '%v'\n", item.IconName())
ch = '?'
}
var attr = termbox.ColorCyan
if char.Wearing() == item {
attr = termbox.ColorRed | termbox.AttrBold
}
ui.draw(ch, attr, 0)
}
ui.nextLine()
}
func (ui *consoleUi) clear() {
termbox.Clear(0, 0)
ui.curLine = 0
}
// render clears the screen and renders the next frame.
func (ui *consoleUi) render() error {
ui.clear()
ui.drawMap()
ui.emptyLine()
ui.drawRules()
ui.drawHealthBar()
ui.emptyLine()
ui.drawInventory()
ui.emptyLine()
ui.emptyLine()
// messages will be displayed bellow
return termbox.Flush()
}