commandeuropa/world/map.go

166 lines
4.3 KiB
Go
Raw Permalink Normal View History

2022-11-10 00:36:36 +00:00
package world
import (
2022-11-10 07:36:00 +00:00
"image/color"
"math/rand"
2022-11-10 00:36:36 +00:00
"github.com/hajimehoshi/ebiten/v2"
)
2022-11-15 08:07:21 +00:00
const MapSize = 128
const tileDivisions = 4
const numTiles = MapSize * MapSize * tileDivisions * tileDivisions
2022-11-18 22:12:00 +00:00
func environmentTile(r, g, b uint8) *ebiten.Image {
img := ebiten.NewImage(TileSizeEnvironment, TileSizeEnvironment)
2022-11-10 07:36:00 +00:00
img.Fill(color.RGBA{r, g, b, 255})
return img
}
var (
2022-11-18 22:12:00 +00:00
lightBlueTile1 = environmentTile(174, 208, 218)
lightBlueTile2 = environmentTile(203, 234, 229)
lightBlueTile3 = environmentTile(154, 202, 206)
2022-11-10 07:36:00 +00:00
lightBlueTiles = []*ebiten.Image{lightBlueTile1, lightBlueTile1, lightBlueTile2, lightBlueTile2, lightBlueTile2, lightBlueTile2, lightBlueTile2, lightBlueTile2, lightBlueTile2, lightBlueTile3}
2022-11-18 22:12:00 +00:00
mediumBlueTile1 = environmentTile(0, 38, 117)
mediumBlueTile2 = environmentTile(9, 69, 131)
mediumBlueTile3 = environmentTile(25, 71, 148)
2022-11-10 07:36:00 +00:00
mediumBlueTiles = []*ebiten.Image{mediumBlueTile1, mediumBlueTile2, mediumBlueTile3}
2022-11-18 22:12:00 +00:00
darkBlueTile1 = environmentTile(0, 9, 88)
darkBlueTile2 = environmentTile(2, 3, 67)
darkBlueTile3 = environmentTile(0, 0, 72)
2022-11-10 07:36:00 +00:00
darkBlueTiles = []*ebiten.Image{darkBlueTile1, darkBlueTile1, darkBlueTile2, darkBlueTile3}
2022-11-18 22:12:00 +00:00
redTile1 = environmentTile(109, 5, 0)
redTile2 = environmentTile(135, 13, 8)
redTile3 = environmentTile(118, 0, 6)
2022-11-10 07:36:00 +00:00
redTiles = []*ebiten.Image{redTile1, redTile1, redTile2, redTile3}
)
2022-11-15 08:07:21 +00:00
func NewGameMap(seed int64) []Tile {
2022-11-10 07:36:00 +00:00
r := rand.New(rand.NewSource(seed))
2022-11-15 08:07:21 +00:00
tiles := make([]Tile, numTiles)
{
2022-11-18 21:19:58 +00:00
tx, ty := float64(0), float64(0)
2022-11-15 08:07:21 +00:00
for i := range tiles {
tiles[i].X, tiles[i].Y = tx, ty
tiles[i].Walkable = true
tx++
if tx == MapSize*4 {
tx = 0
ty++
}
}
}
2022-11-18 21:19:58 +00:00
for x := 0.0; x < MapSize; x++ {
for y := 0.0; y < MapSize; y++ {
2022-11-15 08:07:21 +00:00
index := TileIndex(x, y)
tiles[index].Sprite = lightBlueTiles[r.Intn(len(lightBlueTiles))]
}
}
2022-11-18 21:19:58 +00:00
setWalkable := func(tx, ty float64, walkable bool) {
2022-11-15 08:07:21 +00:00
tileIndexes := PathTilesAtMapTile(tx, ty)
for _, index := range tileIndexes {
tiles[index].Walkable = walkable
}
2022-11-10 07:36:00 +00:00
}
numMediumBlue := r.Intn(10) + 7
for i := 0; i < numMediumBlue; i++ {
2022-11-18 21:19:58 +00:00
bx, by := float64(r.Intn(132)-4), float64(r.Intn(132)-4)
bSizeX := float64(r.Intn(TileSize) + 7)
bSizeY := float64(r.Intn(TileSize) + 7)
for offsetX := 0.0; offsetX < bSizeX; offsetX++ {
for offsetY := 0.0; offsetY < bSizeY; offsetY++ {
2022-11-15 08:07:21 +00:00
tx, ty := bx+offsetX, by+offsetY
index := TileIndex(tx, ty)
2022-11-10 07:36:00 +00:00
if index == -1 {
continue
}
2022-11-15 08:07:21 +00:00
setWalkable(tx, ty, false)
2022-11-10 07:36:00 +00:00
tiles[index].Sprite = mediumBlueTiles[r.Intn(len(mediumBlueTiles))]
}
}
}
numDarkBlue := r.Intn(7) + 4
for i := 0; i < numDarkBlue; i++ {
2022-11-18 21:19:58 +00:00
bx, by := float64(r.Intn(132)-4), float64(r.Intn(132)-4)
bSizeX := float64(r.Intn(12) + 7)
bSizeY := float64(r.Intn(12) + 7)
for offsetX := 0.0; offsetX < bSizeX; offsetX++ {
for offsetY := 0.0; offsetY < bSizeY; offsetY++ {
2022-11-15 08:07:21 +00:00
tx, ty := bx+offsetX, by+offsetY
index := TileIndex(tx, ty)
2022-11-10 07:36:00 +00:00
if index == -1 {
continue
}
2022-11-15 08:07:21 +00:00
setWalkable(tx, ty, false)
2022-11-10 07:36:00 +00:00
tiles[index].Sprite = darkBlueTiles[r.Intn(len(darkBlueTiles))]
}
}
}
numStreaks := r.Intn(3) + 5
for i := 0; i < numStreaks; i++ {
2022-11-18 21:19:58 +00:00
bx, by := float64(r.Intn(132)-4), float64(r.Intn(132)-4)
2022-11-10 07:36:00 +00:00
vertical := r.Intn(2) == 0
2022-11-18 21:19:58 +00:00
bSize := float64(rand.Intn(14) + 32)
for offset := 0.0; offset < bSize; offset++ {
var tx, ty float64
2022-11-10 07:36:00 +00:00
if vertical {
2022-11-15 08:07:21 +00:00
tx, ty = bx, by+offset
2022-11-10 07:36:00 +00:00
} else {
2022-11-15 08:07:21 +00:00
tx, ty = bx+offset, by
2022-11-10 07:36:00 +00:00
}
2022-11-15 08:07:21 +00:00
index := TileIndex(tx, ty)
if index == -1 || !tiles[index].Walkable {
2022-11-10 07:36:00 +00:00
continue
}
2022-11-15 08:07:21 +00:00
setWalkable(tx, ty, false)
2022-11-10 07:36:00 +00:00
tiles[index].Sprite = redTiles[r.Intn(len(redTiles))]
}
2022-11-10 00:36:36 +00:00
}
2022-11-10 07:36:00 +00:00
2022-11-10 00:36:36 +00:00
return tiles
}
2022-11-10 07:36:00 +00:00
2022-11-18 21:19:58 +00:00
func PathTileIndex(x, y float64) int {
2022-11-15 08:07:21 +00:00
if x < 0 || y < 0 || x >= MapSize*tileDivisions || y >= MapSize*tileDivisions {
return -1
}
2022-11-18 21:19:58 +00:00
return int(y*MapSize*tileDivisions) + int(x)
2022-11-15 08:07:21 +00:00
}
2022-11-18 21:19:58 +00:00
func TileIndex(x, y float64) int {
2022-11-10 07:36:00 +00:00
if x < 0 || y < 0 || x >= MapSize || y >= MapSize {
return -1
}
2022-11-18 21:19:58 +00:00
return int(y*tileDivisions*MapSize*tileDivisions) + int(x*tileDivisions)
2022-11-15 08:07:21 +00:00
}
func ValidXY(x, y int) bool {
return x >= 0 && y >= 0 && x < MapSize && y < MapSize
}
2022-11-18 21:19:58 +00:00
func PathTilesAtMapTile(tx, ty float64) []int {
2022-11-15 08:07:21 +00:00
pi := TileIndex(tx, ty)
tiles := make([]int, 16)
i := 0
for offsetY := 0; offsetY < 4; offsetY++ {
for offsetX := 0; offsetX < 4; offsetX++ {
tiles[i] = pi + (offsetY * MapSize * 4) + offsetX
i++
}
}
return tiles
2022-11-10 07:36:00 +00:00
}