2020-08-17 00:53:39 +02:00
|
|
|
package gamemap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2020-08-17 04:11:52 +02:00
|
|
|
// WorldMap is the data structure used game world
|
2020-08-17 00:53:39 +02:00
|
|
|
type WorldMap struct {
|
2020-08-17 01:17:15 +02:00
|
|
|
data [64][64]int
|
2020-08-17 00:53:39 +02:00
|
|
|
max_x int
|
|
|
|
max_y int
|
|
|
|
}
|
|
|
|
|
2020-08-17 04:11:52 +02:00
|
|
|
// GetTile method is used to grab a tile value with error checking
|
2020-08-17 00:53:39 +02:00
|
|
|
func (m WorldMap) GetTile(x int, y int) (int, error) {
|
|
|
|
if x > m.max_x || y > m.max_y {
|
|
|
|
return -1, fmt.Errorf("Map out of bounds error: %d, %d", x, y)
|
|
|
|
}
|
|
|
|
return m.data[x][y], nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 04:11:52 +02:00
|
|
|
// GetJSONRegion method returns a JSON object containing the tile values of everything
|
|
|
|
// within a given range
|
|
|
|
func (m WorldMap) GetJSONRegion(startX, endX, startY, endY int) ([]byte, error) {
|
2020-08-17 01:42:35 +02:00
|
|
|
regionMap := map[int]map[int]int{}
|
2020-08-17 04:11:52 +02:00
|
|
|
for x := startX; x < endX; x++ {
|
2020-08-17 01:42:35 +02:00
|
|
|
regionMap[x] = map[int]int{}
|
2020-08-17 04:11:52 +02:00
|
|
|
for y := startY; y < endY; y++ {
|
2020-08-17 00:53:39 +02:00
|
|
|
if result, err := m.GetTile(x, y); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
regionMap[x][y] = result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonString, err := json.Marshal(regionMap)
|
|
|
|
return jsonString, err
|
|
|
|
}
|
|
|
|
|
2020-08-17 04:11:52 +02:00
|
|
|
// GetJSONRegionAround returns a JSON object of tile data from a center point
|
|
|
|
func (m WorldMap) GetJSONRegionAround(centerX, centerY, regionRadius int) ([]byte, error) {
|
|
|
|
jsonString, err := m.GetJSONRegion(centerX-regionRadius, centerX+regionRadius, centerY-regionRadius, centerY+regionRadius)
|
|
|
|
return jsonString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntializeMap is a method that helps easily
|
|
|
|
// generate WorldMap objects
|
2020-08-17 00:53:39 +02:00
|
|
|
func IntializeMap() *WorldMap {
|
|
|
|
worldMap := new(WorldMap)
|
|
|
|
worldMap.max_x = 64
|
|
|
|
worldMap.max_y = 64
|
2020-08-17 01:17:15 +02:00
|
|
|
worldMap.data = [64][64]int{}
|
2020-08-17 00:53:39 +02:00
|
|
|
for x := 0; x < worldMap.max_x; x++ {
|
|
|
|
for y := 0; y < worldMap.max_y; y++ {
|
|
|
|
worldMap.data[x][y] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add dot at top left for testing
|
|
|
|
worldMap.data[0][0] = 1
|
|
|
|
|
|
|
|
return worldMap
|
|
|
|
}
|