gl01/internal/world/chunk.go

80 lines
1.6 KiB
Go
Raw Normal View History

2022-01-20 21:58:50 +08:00
package world
import (
"encoding/gob"
"io"
"log"
)
const (
ChunkSizeX = 16
ChunkSizeY = 128
ChunkSizeZ = 16
)
type Chunk struct {
X, Z int // Chunk coordinate in global coordinate (y is always zero)
Id, Aux [ChunkSizeX][ChunkSizeY][ChunkSizeZ]uint16
// render data kept unexported (therefore excluded from gob encoding)
renderChanged bool
vao, vbo uint32
vbolen int
2022-01-28 15:24:03 +08:00
water struct {
vao, vbo uint32
vbolen int
}
vertUpdate chan []Vertex
world *World
2022-01-20 21:58:50 +08:00
}
func (c *Chunk) SetChunkID(x, z int) {
c.X = x
c.Z = z
c.renderChanged = true
}
2022-01-28 15:24:03 +08:00
func (c *Chunk) SetBlock(x, y, z int, id, aux int) {
2022-01-20 21:58:50 +08:00
c.Id[x][y][z] = uint16(id)
2022-01-28 15:24:03 +08:00
c.Aux[x][y][z] = uint16(aux)
2022-01-20 21:58:50 +08:00
c.renderChanged = true
}
// InvalidateRender should be called if a block inside the chunk has changed
// and the chunk needs to re-rendered.
func (c *Chunk) InvalidateRender() {
c.renderChanged = true
}
// LoadFromGob loads the chunk from a gob-encoded file.
func (c *Chunk) LoadFromGob(file io.Reader) {
d := gob.NewDecoder(file)
err := d.Decode(c)
if err != nil {
log.Print("LoadFromGob Error: ", err)
}
}
// LoadFromGobIndexed loads the chunk from a gob-encoded file, with the
// chunk Index overwritten.
func (c *Chunk) LoadFromGobIndexed(file io.Reader, x, z int) {
d := gob.NewDecoder(file)
err := d.Decode(c)
if err != nil {
log.Printf("LoadFromGobIndexed(x=%d, z=%d) Error: %s", x, z, err)
}
c.X = x
c.Z = z
}
// WriteToGob writes the chunk to a gob-encoded file.
func (c *Chunk) WriteToGob(file io.Writer) {
e := gob.NewEncoder(file)
err := e.Encode(c)
if err != nil {
log.Printf("WriteToGob(x=%d, z=%d) Error: %s", c.X, c.Z, err)
}
}