-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.go
278 lines (243 loc) · 6.53 KB
/
level.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
package main
import (
_ "image/png"
"log"
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/colorm"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/norendren/go-fov/fov"
)
type GameData struct {
ScreenWidth int
ScreenHeight int
TileWidth int
TileHeight int
MaxRoomSize int
MinRoomSize int
MaxRooms int
}
// Creates a new instance of the static game data.
func NewGameData() GameData {
gd := GameData{
ScreenWidth: 80,
ScreenHeight: 50,
TileWidth: 16,
TileHeight: 16,
MaxRoomSize: 10,
MinRoomSize: 6,
MaxRooms: 30,
}
return gd
}
type MapTile struct {
PixelX int
PixelY int
Blocked bool
Opaque bool
Seen bool
Image *ebiten.Image
}
const (
TileFloor string = "floor"
TileWall string = "wall"
)
// Creates a MapTile of a given type at pixels x, y.
//
// Supported types include 'Floor' and 'Wall'
func NewTile(x int, y int, tileType string) (MapTile, error) {
blocked := true
opaque := true
image, _, err := ebitenutil.NewImageFromFile("assets/" + tileType + ".png")
if err != nil {
return MapTile{}, err
}
if tileType == TileFloor {
blocked = false
opaque = false
}
tile := MapTile{
PixelX: x,
PixelY: y,
Blocked: blocked,
Opaque: opaque,
Seen: false,
Image: image,
}
return tile, nil
}
// Returns the logical index of the map slice from given X and Y tile coordinates.
func GetIndexFromCoords(x int, y int) int {
gd := NewGameData()
return (y * gd.ScreenWidth) + x
}
type Level struct {
Tiles []MapTile
Rooms []RectangularRoom
PlayerView *fov.View
}
// Creates a new Level object
func NewLevel() Level {
l := Level{}
l.createTiles()
l.PlayerView = fov.New()
return l
}
// Creates a map of tiles.
func (level *Level) createTiles() {
gd := NewGameData()
tiles := make([]MapTile, gd.ScreenHeight*gd.ScreenWidth)
// Fill with wall tiles
for x := 0; x < gd.ScreenWidth; x++ {
for y := 0; y < gd.ScreenHeight; y++ {
idx := GetIndexFromCoords(x, y)
wall, err := NewTile(x*gd.TileWidth, y*gd.TileHeight, TileWall)
if err != nil {
log.Fatal(err)
}
tiles[idx] = wall
}
}
level.Tiles = tiles
for i := 0; i <= gd.MaxRooms; i++ {
// generate width and height as random numbers between gd.MinRoomSize and gd.MaxRoomSize
width := rand.Intn(gd.MaxRoomSize-gd.MinRoomSize+1) + gd.MinRoomSize
height := rand.Intn(gd.MaxRoomSize-gd.MinRoomSize+1) + gd.MinRoomSize
xPos := rand.Intn(gd.ScreenWidth - width)
yPos := rand.Intn(gd.ScreenHeight - height)
newRoom := NewRectangularRoom(xPos, yPos, width, height)
isOkay := true
for _, room := range level.Rooms {
// check through all existing rooms to ensure newRoom doesn't intersect
if newRoom.IntersectsWith(room) {
isOkay = false
break
}
}
if isOkay {
level.Rooms = append(level.Rooms, newRoom)
}
}
// Carve out rooms
for roomNum, room := range level.Rooms {
x1, x2, y1, y2 := room.Interior()
for x := x1; x <= x2; x++ {
for y := y1; y <= y2; y++ {
idx := GetIndexFromCoords(x, y)
floor, err := NewTile(x*gd.TileWidth, y*gd.TileHeight, TileFloor)
if err != nil {
log.Fatal(err)
}
level.Tiles[idx] = floor
}
}
if roomNum > 0 {
level.tunnelBetween(&level.Rooms[roomNum-1], &level.Rooms[roomNum])
}
}
}
// Draw the current level to 'screen'.
func (level *Level) Draw(screen *ebiten.Image) {
gd := NewGameData()
for x := 0; x < gd.ScreenWidth; x++ {
for y := 0; y < gd.ScreenHeight; y++ {
idx := GetIndexFromCoords(x, y)
tile := level.Tiles[idx]
if level.PlayerView.IsVisible(x, y) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(tile.PixelX), float64(tile.PixelY))
screen.DrawImage(tile.Image, op)
level.Tiles[idx].Seen = true
} else if tile.Seen {
op := &colorm.DrawImageOptions{}
var colorM colorm.ColorM
op.GeoM.Translate(float64(tile.PixelX), float64(tile.PixelY))
colorM.Translate(0, 0, 50, 0.75)
colorm.DrawImage(screen, tile.Image, colorM, op)
}
}
}
}
type RectangularRoom struct {
X1 int
Y1 int
X2 int
Y2 int
}
// Create a new RectangularRoom structure.
func NewRectangularRoom(x int, y int, width int, height int) RectangularRoom {
return RectangularRoom{
X1: x,
Y1: y,
X2: x + width,
Y2: y + height,
}
}
// Returns the tile coordinates of the center of the RectangularRoom.
func (r *RectangularRoom) Center() (int, int) {
centerX := (r.X1 + r.X2) / 2
centerY := (r.Y1 + r.Y2) / 2
return centerX, centerY
}
// Returns the tile coordinates of the interior of the RectangularRoom.
func (r *RectangularRoom) Interior() (int, int, int, int) {
return r.X1 + 1, r.X2 - 1, r.Y1 + 1, r.Y2 - 1
}
// Determines if this room intersects with otherRoom.
func (r *RectangularRoom) IntersectsWith(otherRoom RectangularRoom) bool {
return r.X1 <= otherRoom.X2 && r.X2 >= otherRoom.X1 && r.Y1 <= otherRoom.Y2 && r.Y2 >= otherRoom.Y1
}
// Create a vertical tunnel.
func (level *Level) createVerticalTunnel(y1 int, y2 int, x int) {
gd := NewGameData()
for y := min(y1, y2); y < max(y1, y2)+1; y++ {
idx := GetIndexFromCoords(x, y)
if idx > 0 && idx < gd.ScreenHeight*gd.ScreenWidth {
floor, err := NewTile(x*gd.TileWidth, y*gd.TileHeight, TileFloor)
if err != nil {
log.Fatal(err)
}
level.Tiles[idx] = floor
}
}
}
// Create a horizontal tunnel.
func (level *Level) createHorizontalTunnel(x1 int, x2 int, y int) {
gd := NewGameData()
for x := min(x1, x2); x < max(x1, x2)+1; x++ {
idx := GetIndexFromCoords(x, y)
if idx > 0 && idx < gd.ScreenHeight*gd.ScreenWidth {
floor, err := NewTile(x*gd.TileWidth, y*gd.TileHeight, TileFloor)
if err != nil {
log.Fatal(err)
}
level.Tiles[idx] = floor
}
}
}
// Tunnel from this first room to second room.
func (level *Level) tunnelBetween(first *RectangularRoom, second *RectangularRoom) {
startX, startY := first.Center()
endX, endY := second.Center()
if rand.Intn(2) == 0 {
// Tunnel horizontally, then vertically
level.createHorizontalTunnel(startX, endX, startY)
level.createVerticalTunnel(startY, endY, endX)
} else {
// Tunnel vertically, then horizontally
level.createVerticalTunnel(startY, endY, startX)
level.createHorizontalTunnel(startX, endX, endY)
}
}
// Determine if tile coordinates are on the screen.
func (level Level) InBounds(x int, y int) bool {
gd := NewGameData()
if x < 0 || x > gd.ScreenWidth || y < 0 || y > gd.ScreenHeight {
return false
}
return true
}
// Determines if a tile at a given coordinate can be seen through.
func (level Level) IsOpaque(x int, y int) bool {
return level.Tiles[GetIndexFromCoords(x, y)].Opaque
}