-
Notifications
You must be signed in to change notification settings - Fork 4
/
gamehex.go
303 lines (270 loc) · 7.97 KB
/
gamehex.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
// Package gamehex implements an example hexagonal game board and uses ebiten for rendering.
package gamehex
import (
"fmt"
"image/color"
"math"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
)
type Game struct {
currentLevel *Level
width int
height int
offscreen *ebiten.Image
clickedTile [2]int
camX float64
camY float64
camScale float64
camScaleTo float64
mousePanX int
mousePanY int
sprites *SpriteSheet
pathFinder
}
// NewGame returns a new isometric demo Game.
func NewGame() (*Game, error) {
l, err := NewLevel(10, 10)
if err != nil {
return nil, fmt.Errorf("failed to create new level: %s", err)
}
s, err := LoadSpriteSheet(l.hexRadius)
if err != nil {
return nil, fmt.Errorf("failed to load spritesheet: %s", err)
}
return &Game{
currentLevel: l,
clickedTile: [2]int{-1, -1},
camScale: 1,
camScaleTo: 1,
mousePanX: math.MinInt32,
mousePanY: math.MinInt32,
sprites: s,
pathFinder: newPathFinder(),
}, nil
}
// Update reads current user input and updates the Game state.
func (g *Game) Update() error {
// Update target zoom level.
var scrollY float64
if ebiten.IsKeyPressed(ebiten.KeyC) || ebiten.IsKeyPressed(ebiten.KeyPageDown) {
scrollY = -0.25
} else if ebiten.IsKeyPressed(ebiten.KeyE) || ebiten.IsKeyPressed(ebiten.KeyPageUp) {
scrollY = 0.25
} else {
_, scrollY = ebiten.Wheel()
if scrollY < -1 {
scrollY = -1
} else if scrollY > 1 {
scrollY = 1
}
}
g.camScaleTo += scrollY * (g.camScaleTo / 7)
// Clamp target zoom level.
if g.camScaleTo < 0.01 {
g.camScaleTo = 0.01
} else if g.camScaleTo > 100 {
g.camScaleTo = 100
}
// Smooth zoom transition.
div := 10.0
if g.camScaleTo > g.camScale {
g.camScale += (g.camScaleTo - g.camScale) / div
} else if g.camScaleTo < g.camScale {
g.camScale -= (g.camScale - g.camScaleTo) / div
}
// Pan camera via keyboard.
pan := 7.0 / g.camScale
if ebiten.IsKeyPressed(ebiten.KeyA) {
g.camX -= pan
}
if ebiten.IsKeyPressed(ebiten.KeyD) {
g.camX += pan
}
if ebiten.IsKeyPressed(ebiten.KeyS) {
g.camY -= pan
}
if ebiten.IsKeyPressed(ebiten.KeyW) {
g.camY += pan
}
// Select tiles via keyboard.
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
g.clickedTile[0]--
if g.clickedTile[0] < 0 {
g.clickedTile[0] = 0
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
g.clickedTile[0]++
if g.clickedTile[0] >= g.currentLevel.Width {
g.clickedTile[0] = g.currentLevel.Width - 1
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
g.clickedTile[1]++
if g.clickedTile[1] >= g.currentLevel.Height {
g.clickedTile[1] = g.currentLevel.Height - 1
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
g.clickedTile[1]--
if g.clickedTile[1] < 0 {
g.clickedTile[1] = 0
}
}
// Pan camera via mouse.
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
if g.mousePanX == math.MinInt32 && g.mousePanY == math.MinInt32 {
g.mousePanX, g.mousePanY = ebiten.CursorPosition()
} else {
x, y := ebiten.CursorPosition()
dx, dy := float64(g.mousePanX-x)*(pan/100), float64(g.mousePanY-y)*(pan/100)
g.camX, g.camY = g.camX-dx, g.camY+dy
}
} else if g.mousePanX != math.MinInt32 || g.mousePanY != math.MinInt32 {
g.mousePanX, g.mousePanY = math.MinInt32, math.MinInt32
}
// Clamp camera position.
// TODO: Fix actual width and height. Clamping is currently inaccurate.
worldWidth := float64(g.currentLevel.Width*g.currentLevel.hexRadius) * 1.5
worldHeight := float64(g.currentLevel.Height*g.currentLevel.hexRadius) * math.Sqrt(3)
if g.camX < 0 {
g.camX = 0
} else if g.camX > worldWidth {
g.camX = worldWidth
}
if g.camY < -worldHeight {
g.camY = -worldHeight
} else if g.camY > 0 {
g.camY = 0
}
// Handle pathfinding input.
g.handlePathInput()
// If we have a mouse click, we calculate the tile we clicked on and store it
// for rendering later.
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
g.clickedTile = g.getTileAtCursor()
}
return nil
}
func (g *Game) getTileAtCursor() [2]int {
x, y := ebiten.CursorPosition()
x, y = int(float64(x)/g.camScale), int(float64(y)/g.camScale)
x, y = x+int(g.camX), y-int(g.camY)
cX, cY := g.currentLevel.TileAtPos(x, y)
return [2]int{cX, cY}
}
// Draw draws the Game on the screen.
func (g *Game) Draw(screen *ebiten.Image) {
// Render level.
g.renderLevel(screen)
// Render path info.
g.drawPathInfo(screen)
// Print game info.
ebitenutil.DebugPrint(screen, fmt.Sprintf("KEYS WASD EC P\nFPS %0.0f\nTPS %0.0f\nSCA %0.2f\nPOS %0.0f,%0.0f", ebiten.ActualFPS(), ebiten.ActualTPS(), g.camScale, g.camX, g.camY))
}
// Layout is called when the Game's layout changes.
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
g.width, g.height = outsideWidth, outsideHeight
return g.width, g.height
}
// renderLevel draws the current Level on the screen.
func (g *Game) renderLevel(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
padding := float64(g.currentLevel.hexRadius) * g.camScale
cx, cy := 0.0, 0.0 // float64(g.width/2), float64(g.height/2)
scaleLater := g.camScale > 1
scale := g.camScale
target := screen
// When zooming in, tiles can have slight bleeding edges.
// To avoid them, render the result on an offscreen first and then scale it later.
if scaleLater {
if g.offscreen != nil {
w, h := g.offscreen.Size()
sw, sh := screen.Size()
if w != sw || h != sh {
g.offscreen.Dispose()
g.offscreen = nil
}
}
if g.offscreen == nil {
g.offscreen = ebiten.NewImage(screen.Size())
}
target = g.offscreen
target.Clear()
scale = 1
}
nbOfSelected := make(map[[2]int]bool)
if g.clickedTile[0] != -1 && g.clickedTile[1] != -1 {
for _, n := range g.currentLevel.getNeighbors(g.clickedTile[0], g.clickedTile[1]) {
nbOfSelected[n] = true
}
}
// Hacky way to check if a tile is part of the path.
isPath := make(map[[2]int]bool)
for _, p := range g.path {
isPath[p] = true
}
for y := 0; y < g.currentLevel.Height; y++ {
for x := 0; x < g.currentLevel.Width; x++ {
// Tint the tile red if it was clicked.
var c color.Color
if g.clickedTile[0] == x && g.clickedTile[1] == y {
c = color.RGBA{255, 0, 0, 255}
} else if nbOfSelected[[2]int{x, y}] {
c = color.RGBA{0, 255, 0, 255}
} else {
c = color.White
}
xi, yi := g.currentLevel.HexTileXYToPixelPos(x, y)
// Skip drawing tiles that are out of the screen.
drawX, drawY := ((float64(xi)-g.camX)*g.camScale)+cx, ((float64(yi)+g.camY)*g.camScale)+cy
if drawX+padding < 0 || drawY+padding < 0 || drawX > float64(g.width) || drawY > float64(g.height) {
continue
}
op.GeoM.Reset()
// Move to current isometric position.
op.GeoM.Translate(float64(xi), float64(yi))
// Translate camera position.
op.GeoM.Translate(-g.camX, g.camY)
// Translate from center of hexagon.
op.GeoM.Translate(-float64(g.currentLevel.hexRadius), -float64(g.currentLevel.hexRadius)*math.Sqrt(3)/2)
// Zoom.
op.GeoM.Scale(scale, scale)
// Center.
op.GeoM.Translate(cx, cy)
// Reset color matrix and tint if needed.
op.ColorM.Reset()
// Tint if it is part of the path.
if isPath[[2]int{x, y}] {
op.ColorM.Scale(1, 0.5, 0.5, 1)
}
// Draw tile.
val := g.currentLevel.Tiles[y*g.currentLevel.Width+x]
// Draw terrain.
switch val.Type() {
case TileTypeWater:
target.DrawImage(g.sprites.Water, op)
case TileTypeDirt:
target.DrawImage(g.sprites.Dirt, op)
case TileTypeGrass:
target.DrawImage(g.sprites.Grass, op)
default:
target.DrawImage(g.sprites.Snow, op)
}
// Draw trees if present.
if val.HasTrees() {
target.DrawImage(g.sprites.Trees, op)
}
g.currentLevel.drawHex(target, (float64(xi)-g.camX+cx)*scale, (float64(yi)+g.camY+cy)*scale, scale, x, y, c)
}
}
if scaleLater {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-cx, -cy)
op.GeoM.Scale(float64(g.camScale), float64(g.camScale))
op.GeoM.Translate(cx, cy)
screen.DrawImage(target, op)
}
}