GOingTunneling/map.go

155 lines
3.5 KiB
Go

package main
import (
"github.com/aquilax/go-perlin"
"github.com/veandco/go-sdl2/sdl"
"image/color"
"sync"
"time"
)
type GameMap struct {
tiles [][]uint8
//tile 0 - air
//tile 1 - rock
//tile 2 - soil 1
//tile 3 - soil 2
//tile 4 - base
//tile 5 - world edge (should not be present in the map object)
width int32
height int32
}
func GetTileColorValue(value uint8) color.Color {
switch value {
case 0:
return color.RGBA{}
case 1:
return color.RGBA{R: 154, G: 154, B: 154, A: 255}
case 2:
return color.RGBA{R: 195, G: 121, B: 48, A: 255}
case 3:
return color.RGBA{R: 186, G: 89, B: 4, A: 255}
case 4:
return color.RGBA{R: 255, G: 44, B: 255, A: 255}
case 5:
return color.RGBA{R: 86, G: 86, B: 86, A: 255}
default:
return color.RGBA{R: 255, G: 255, B: 255, A: 255}
}
}
func (gameMap *GameMap) getTileColor(x int32, y int32) color.Color {
if x < 0 || x >= gameMap.width || y < 0 || y >= gameMap.height {
return GetTileColorValue(5)
} else {
tileValue := gameMap.tiles[x][y]
return GetTileColorValue(tileValue)
}
}
func (gameMap *GameMap) createGameMap(width int32, height int32) {
// Initialize the 2D slice for game tiles
gameMap.tiles = make([][]uint8, width)
// Create Perlin noise generators
perlinNoiseRock := perlin.NewPerlin(2, 4, 8, time.Now().Unix())
perlinNoiseSoil := perlin.NewPerlin(2, 8, 4, time.Now().Unix())
// WaitGroup to synchronize goroutines
var wg sync.WaitGroup
// Loop over the rows
for i := range gameMap.tiles {
// Increment WaitGroup counter for each goroutine
wg.Add(1)
// Launch a goroutine for each row
go func(i int) {
defer wg.Done() // Signal that this goroutine is done
// Initialize the row
gameMap.tiles[i] = make([]uint8, height)
// Process each tile in the row
for j := range gameMap.tiles[i] {
perlinRock := perlinNoiseRock.Noise2D(float64(i)/float64(width)*6, float64(j)/float64(height)*6)
if perlinRock > 0.2 {
gameMap.tiles[i][j] = 1
} else {
perlinSoil := perlinNoiseSoil.Noise2D(float64(i)/float64(width)*10, float64(j)/float64(height)*10)
if perlinSoil >= 0 {
gameMap.tiles[i][j] = 2
} else {
gameMap.tiles[i][j] = 3
}
}
}
}(i)
}
// Wait for all goroutines to finish
wg.Wait()
// Set the dimensions of the game map
gameMap.width = width
gameMap.height = height
}
func (gameMap *GameMap) render(camera *sdl.Rect, surface *sdl.Surface) {
fromX := camera.X
fromY := camera.Y
toX := camera.X + camera.W
toY := camera.Y + camera.H
// Create a WaitGroup to wait for all goroutines to finish
var wg sync.WaitGroup
// Process columns instead of individual pixels
for x := fromX; x < toX; x++ {
// Increment the WaitGroup counter
wg.Add(1)
// Launch a goroutine for each column
go func(x int32) {
defer wg.Done() // Decrement the counter when the goroutine completes
for y := fromY; y < toY; y++ {
cameraY := y - camera.Y
cameraX := x - camera.X
pixelColor := gameMap.getTileColor(x, y)
surface.Set(int(cameraX), int(cameraY), pixelColor)
}
}(x)
}
// Wait for all goroutines to finish
wg.Wait()
}
func (gameMap *GameMap) checkCollision(posX, posY int32) uint8 {
//0 no collision
//1 destructible collision
//2 rock collision
//3 indestructible collision
if posX >= gameMap.width || posX < 0 || posY >= gameMap.height || posY < 0 {
return 3
}
switch gameMap.tiles[posX][posY] {
case 0:
return 0
case 1:
return 2
case 2:
return 1
case 3:
return 1
case 4:
return 3
case 5:
return 3
default:
return 3
}
}