46 lines
991 B
Go
46 lines
991 B
Go
package entities
|
|
|
|
import "github.com/go-gl/mathgl/mgl64"
|
|
|
|
const (
|
|
TileSize = 32.0 // Varje block ar 32x32 world units
|
|
)
|
|
|
|
type World struct {
|
|
Width, Height, Depth int
|
|
Grid [][][]Entity
|
|
}
|
|
|
|
func NewWorld(w, h, d int) *World {
|
|
grid := make([][][]Entity, w)
|
|
for x := 0; x < w; x++ {
|
|
grid[x] = make([][]Entity, h)
|
|
for y := 0; y < h; y++ {
|
|
grid[x][y] = make([]Entity, d)
|
|
}
|
|
}
|
|
return &World{Width: w, Height: h, Depth: d, Grid: grid}
|
|
}
|
|
|
|
func (w *World) GetEntityAt(x, y, z int) Entity {
|
|
if x < 0 || x >= w.Width || y < 0 || y >= w.Height || z < 0 || z >= w.Depth {
|
|
return nil
|
|
}
|
|
return w.Grid[x][y][z]
|
|
}
|
|
|
|
func (w *World) SetEntityAt(x, y, z int, e Entity) {
|
|
if x < 0 || x >= w.Width || y < 0 || y >= w.Height || z < 0 || z >= w.Depth {
|
|
return
|
|
}
|
|
w.Grid[x][y][z] = e
|
|
if e != nil {
|
|
e.SetPos(mgl64.Vec3{float64(x), float64(y), float64(z)})
|
|
}
|
|
}
|
|
|
|
// Convert absolute 3D position to Grid index
|
|
func ToGridIndex(pos mgl64.Vec3) (int, int, int) {
|
|
return int(pos[0]), int(pos[1]), int(pos[2])
|
|
}
|