/
tiles.go
258 lines (221 loc) · 7.12 KB
/
tiles.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
package gui
/*
The file implements functions for visually representing tiles and elements in a game's graphical user interface (GUI).
Additionally, shadows are added based on the active player's visibility, and in fire mode, tiles in the fire area are
highlighted. Debug information such as coordinates, supply values, and visibility are also displayed on screen when needed.
*/
import (
"fmt"
"github.com/SchnorcherSepp/TankWars2/core"
"github.com/SchnorcherSepp/TankWars2/gui/resources"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
"image/color"
"sort"
)
// drawTiles draws the tiles on the screen, including their images, ownership indicators, and supply information.
func (g *Game) drawTiles(screen *ebiten.Image) {
for xCol := 0; xCol < g.xWidth; xCol++ {
for yRow := 0; yRow < g.yHeight; yRow++ {
posX, posY := calcScreenPosition(xCol, yRow, false)
// image config
op := new(ebiten.DrawImageOptions)
op.GeoM.Translate(posX, posY)
op.Filter = ebiten.FilterLinear
// load image
tile := g.world.Tile(xCol, yRow)
img := resources.Imgs.ErrorTile
switch tile.Type {
case core.BASE:
list := resources.Imgs.TilesBase
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.DIRT:
list := resources.Imgs.TilesDirt
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.FOREST:
list := resources.Imgs.TilesForest
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.GRASS:
list := resources.Imgs.TilesGrass
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.HILL:
list := resources.Imgs.TilesHill
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.HOLE:
list := resources.Imgs.TilesHole
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.MOUNTAIN:
list := resources.Imgs.TilesMountain
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.STRUCTURE:
list := resources.Imgs.TilesStructure
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
case core.WATER:
list := resources.Imgs.TilesWater
id := int(tile.ImageID) % len(list)
img = list[id] // load random image
}
// draw tile
screen.DrawImage(img, op)
// draw base owner
if tile.Type == core.BASE && tile.Owner > 0 {
clr := color.RGBA{R: 77, G: 77, B: 77, A: 222}
x := posX + tileX/2
y := posY + 14
vector.DrawFilledCircle(screen, float32(x), float32(y), float32(10), clr, false)
txt := fmt.Sprintf("%d", tile.Owner)
ebitenutil.DebugPrintAt(screen, txt, int(x-3), int(y-8))
}
// draw supply
if g.activePlayer > 0 && len(tile.Supply) > 0 {
supply := tile.Supply[g.activePlayer]
const size = 16
if supply > 0 {
clr := valueToColor(supply, core.MaxSupply, false)
x := posX + tileX - size - 3
y := posY + tileY/4 + 3
vector.DrawFilledRect(screen, float32(x), float32(y), size, size, clr, false)
txt := fmt.Sprintf("%d", supply)
ebitenutil.DebugPrintAt(screen, txt, int(x)-(3*len(txt))+size/2, int(y-8+size/2))
}
}
}
}
}
// drawShadow draws shadows over the tiles based on the visibility of the active player.
func (g *Game) drawShadow(screen *ebiten.Image) {
if g.activePlayer > 0 {
img := resources.Imgs.EmptyTile
dark := ebiten.NewImageFromImage(img)
changeColorsExceptTransparent(dark, color.RGBA{A: 150})
light := ebiten.NewImageFromImage(img)
changeColorsExceptTransparent(light, color.RGBA{R: 44, G: 44, B: 44})
for xCol := 0; xCol < g.xWidth; xCol++ {
for yRow := 0; yRow < g.yHeight; yRow++ {
posX, posY := calcScreenPosition(xCol, yRow, false)
// image config
op := new(ebiten.DrawImageOptions)
op.GeoM.Translate(posX, posY)
op.Filter = ebiten.FilterLinear
// load image
tile := g.world.Tile(xCol, yRow)
// draw shadow
switch tile.Visibility[g.activePlayer] {
case core.FogOfWar:
screen.DrawImage(dark, op)
case core.CloseView:
screen.DrawImage(light, op)
}
}
}
}
}
// drawFireMode highlights the tiles within the firing range of the active unit when in fire mode.
func (g *Game) drawFireMode(screen *ebiten.Image) {
activeTile := g.activeTile
if g.fireMode && activeTile != nil {
unit := activeTile.Unit
if unit != nil {
// load image
img := resources.Imgs.EmptyTile
img = ebiten.NewImageFromImage(img)
changeColorsExceptTransparent(img, color.RGBA{R: 188, G: 1, B: 1, A: 22})
// all tiles in fire range
neighbors := g.world.ExtNeighbors(activeTile, unit.FireRange)
for _, tmp := range neighbors {
for _, t := range tmp {
posX, posY := calcScreenPosition(t.XCol, t.YRow, false)
// image config
op := new(ebiten.DrawImageOptions)
op.GeoM.Translate(posX, posY)
op.Filter = ebiten.FilterLinear
// draw tile
screen.DrawImage(img, op)
}
}
}
}
}
// drawActiveTile highlights the currently active tile on the screen.
func (g *Game) drawActiveTile(screen *ebiten.Image) {
if g.activeTile == nil {
return
}
posX, posY := calcScreenPosition(g.activeTile.XCol, g.activeTile.YRow, true)
bgColor := color.RGBA{R: 66, G: 66, B: 0, A: 0}
vector.DrawFilledCircle(screen, float32(posX), float32(posY), tileX/2, bgColor, false)
}
// writeTileText writes debug information about each tile on the screen, including coordinates, supply values, and visibility.
func (g *Game) writeTileText(screen *ebiten.Image) {
for xCol := 0; xCol < g.xWidth; xCol++ {
for yRow := 0; yRow < g.yHeight; yRow++ {
tile := g.world.Tile(xCol, yRow)
txt := ""
// coordinates
if g.toggleCoordinates {
txt += fmt.Sprintf("(%d,%d)", tile.XCol, tile.YRow)
}
// supply
if g.toggleSupply {
// add new line
if len(txt) > 0 {
txt += "\n"
}
// get all Keys
keys := make([]uint8, 0, len(tile.Supply))
for key := range tile.Supply {
keys = append(keys, key)
}
// sort keys
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
// add text
for _, key := range keys {
if g.activePlayer == 0 || g.activePlayer == key {
txt += fmt.Sprintf("%d:%d ", key, tile.Supply[key])
}
}
}
// visibility
if g.toggleVisibility {
// add new line
if len(txt) > 0 {
txt += "\n"
}
// get all Keys
keys := make([]uint8, 0, len(tile.Visibility))
for key := range tile.Visibility {
keys = append(keys, key)
}
// sort keys
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
// add text
for _, key := range keys {
value := tile.Visibility[key]
if value > 0 && (g.activePlayer == 0 || g.activePlayer == key) {
txt += fmt.Sprintf("%d:%d ", key, value)
}
}
}
// write text
if len(txt) > 0 {
posX, posY := calcScreenPosition(xCol, yRow, true)
posX = posX - (6 / 2 * float64(len(txt)))
posY = posY - 8
ebitenutil.DebugPrintAt(screen, txt, int(posX), int(posY))
}
}
}
}