-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.go
266 lines (251 loc) · 9.01 KB
/
render.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
/*
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"
"strconv"
)
const (
/* Constant values for layers. Their usage is optional,
but (for now, at leas) recommended, because default
rendering functions depends on these values.
They are important for proper clearing characters
that should not be displayed, as, for example,
bracelet under the monster. */
_ = iota
UILayer
BoardLayer
DeadLayer
ObjectsLayer
CreaturesLayer
PlayerLayer
LookLayer
MenuLayer
)
const CockedIcon = "Φ"
func PrintBoard(b Board, c Creatures) {
/* Function PrintBoard is used in RenderAll function.
Takes level map and list of monsters as arguments
and iterates through Board.
It has to check for "]" and "[" characters, because
BearLibTerminal uses these symbols for config.
Instead of checking it here, one could just remember to
always pass "]]" instead of "]".
Prints every tile on its coords if certain conditions are met:
is Explored already, and:
- is in player's field of view (prints "normal" color) or
- is AlwaysVisible (prints dark color). */
for x := 0; x < MapSizeX; x++ {
for y := 0; y < MapSizeY; y++ {
// Technically, "t" is new variable with own memory address...
t := b[x][y] // Should it be *b[x][y]?
blt.Layer(t.Layer)
if t.Explored == true {
ch := t.Char
if t.Char == "[" || t.Char == "]" {
ch = t.Char + t.Char
}
if IsInFOV(b, c[0].X, c[0].Y, t.X, t.Y) == true {
glyph := "[color=" + t.Color + "]" + ch
blt.Print(t.X, t.Y, glyph)
} else {
if t.AlwaysVisible == true {
glyph := "[color=" + t.ColorDark + "]" + ch
blt.Print(t.X, t.Y, glyph)
}
}
}
}
}
}
func PrintObjects(b Board, o Objects, c Creatures) {
/* Function PrintObjects is used in RenderAll function.
Takes map of level, slice of objects, and all monsters
as arguments.
Iterates through Objects.
It has to check for "]" and "[" characters, because
BearLibTerminal uses these symbols for config.
Instead of checking it here, one could just remember to
always pass "]]" instead of "]".
Prints every object on its coords if certain conditions are met:
AlwaysVisible bool is set to true, or is in player fov. */
for _, v := range o {
if (IsInFOV(b, c[0].X, c[0].Y, v.X, v.Y) == true) ||
((v.AlwaysVisible == true) && (b[v.X][v.Y].Explored == true)) {
blt.Layer(v.Layer)
ch := v.Char
if v.Char == "]" || v.Char == "[" {
ch = v.Char + v.Char
}
for l := 0; l < v.Layer; l++ {
blt.Layer(l)
blt.ClearArea(v.X, v.Y, 1, 1)
}
glyph := "[color=" + v.Color + "]" + ch
blt.Print(v.X, v.Y, glyph)
}
}
}
func PrintAliveCreatures(b Board, c Creatures) {
/* Function PrintCreatures is used in RenderAll function.
Takes map of level and slice of Creatures as arguments.
Iterates through Creatures.
It has to check for "]" and "[" characters, because
BearLibTerminal uses these symbols for config.
Instead of checking it here, one could just remember to
always pass "]]" instead of "]".
Checks for every creature on its coords if certain conditions are met:
AlwaysVisible bool is set to true, or is in player fov. */
for _, v := range c {
if ((IsInFOV(b, c[0].X, c[0].Y, v.X, v.Y) == true) ||
(v.AlwaysVisible == true)) && v.HPCurrent > 0 {
blt.Layer(v.Layer)
ch := v.Char
if v.Char == "]" || v.Char == "[" {
ch = v.Char + v.Char
}
for l := 0; l < v.Layer; l++ {
blt.Layer(l)
blt.ClearArea(v.X, v.Y, 1, 1)
}
glyph := "[color=" + v.Color + "]" + ch
blt.Print(v.X, v.Y, glyph)
}
}
}
func PrintDeadCreatures(b Board, c Creatures) {
/* Function PrintCreatures is used in RenderAll function.
Takes map of level and slice of Creatures as arguments.
Iterates through Creatures.
It has to check for "]" and "[" characters, because
BearLibTerminal uses these symbols for config.
Instead of checking it here, one could just remember to
always pass "]]" instead of "]".
Checks for every creature on its coords if certain conditions are met:
AlwaysVisible bool is set to true, or is in player fov. */
for _, v := range c {
if ((IsInFOV(b, c[0].X, c[0].Y, v.X, v.Y) == true) ||
(v.AlwaysVisible == true)) && v.HPCurrent <= 0 {
blt.Layer(v.Layer)
ch := v.Char
if v.Char == "]" || v.Char == "[" {
ch = v.Char + v.Char
}
for l := 0; l < v.Layer; l++ {
blt.Layer(l)
blt.ClearArea(v.X, v.Y, 1, 1)
}
glyph := "[color=" + v.Color + "]" + ch
blt.Print(v.X, v.Y, glyph)
}
}
}
func PrintUI(c *Creature) {
/* Function PrintUI takes *Creature (it's supposed to be player) as argument.
It prints UI infos on the right side of screen.
For now its functionality is very modest, but it will expand when
new elements of game mechanics will be introduced. So, for now, it
provides only one basic, yet essential information: player's HP. */
blt.Layer(UILayer)
name := "Player"
blt.Print(UIPosX, UIPosY, name)
hpBar := "[color=red]"
for hp := 1; hp <= c.HPMax; hp++ {
character := "♥"
if hp > c.HPCurrent {
character = "♡"
}
hpBar = hpBar + character
}
blt.Print(UIPosX, UIPosY+1, hpBar)
pos := 3
for i := 0; i < len(c.Equipment); i++ {
color := "[color=gray]"
if i == c.ActiveWeapon {
color = "[color=white]"
}
blt.Print(UIPosX, UIPosY+pos,
color+strconv.Itoa(i+1)+". "+c.Equipment[i].Name)
for j := 0; j < c.Equipment[i].AmmoCurrent; j++ {
blt.Print(UIPosX+j+4+3+1, UIPosY+pos+1, "[color=dark yellow]|")
}
for k := c.Equipment[i].AmmoCurrent; k < c.Equipment[i].AmmoMax; k++ {
blt.Print(UIPosX+k+4+3+1, UIPosY+pos+1, "[color=darkest yellow]|")
}
if c.Equipment[i].Cock == true {
cockedPosX := UIPosX + c.Equipment[i].AmmoMax + 1 + 4 + 3 + 1
cockedIcon := CockedIcon
if c.Equipment[i].Cocked == true {
cockedIcon = "[color=dark green]" + CockedIcon
} else {
cockedIcon = "[color=dark red]" + CockedIcon
}
blt.Print(cockedPosX, UIPosY+pos+1, cockedIcon)
}
var ranges = []string{"▇", "▇", "▇"}
rangesStr := ""
for j, _ := range ranges {
if c.Equipment[i].Slot == SlotWeaponMelee {
break
}
val := c.Equipment[i].Ranges[j]
if val < 25 {
rangesStr = rangesStr + "[color=darker red]▁[/color]"
} else if val < 50 {
rangesStr = rangesStr + "[color=darker flame]▃[/color]"
} else if val < 75 {
rangesStr = rangesStr + "[color=darker yellow]▅[/color]"
} else {
rangesStr = rangesStr + "[color=darker green]▇[/color]"
}
}
blt.Print(UIPosX+3, UIPosY+pos+1, rangesStr)
pos += 2
}
instructions := "[color=#EDEAE0]─────────────────────\n1: grab rifle |\n2: grab revolver -\n3: grab melee }\nf: target/fire/cock\nTAB: switch target\nr: reload\ni: inspect\ng: pick up\np: pull lever\n─────────────────────"
blt.Print(UIPosX, UIPosY+pos, instructions)
}
func PrintLog() {
/* Function PrintLog prints game messages at the bottom of screen. */
blt.Layer(UILayer)
PrintMessages(LogPosX, LogPosY, "")
}
func RenderAll(b Board, o Objects, c Creatures) {
/* Function RenderAll prints every tile and character on game screen.
Takes board slice (ie level map), slice of objects, and slice of creatures
as arguments.
At first, it clears whole terminal window, then uses arguments:
CastRays (for raycasting FOV) of first object (assuming that it is player),
then calls functions for printing map, objects and creatures.
Calls PrintLog that writes message log.
At the end, RenderAll calls blt.Refresh() that makes
changes to the game window visible. */
blt.Clear()
CastRays(b, c[0].X, c[0].Y)
PrintBoard(b, c)
PrintDeadCreatures(b, c)
PrintObjects(b, o, c)
PrintAliveCreatures(b, c)
PrintUI((c)[0])
PrintLog()
blt.Refresh()
}