gridpathingtest/sauce/world.go
2024-02-02 15:59:24 -08:00

66 lines
1.0 KiB
Go

package sauce
type World struct {
Chunks []*Chunk
}
func NewWorld(width, height int) *World {
w := &World{}
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
w.Chunks = append(w.Chunks, NewChunk(10, 10))
w.Chunks[len(w.Chunks)-1].X = x * 10
w.Chunks[len(w.Chunks)-1].Y = y * 10
}
}
return w
}
func (w *World) At(x, y int) *Cell {
for _, chunk := range w.Chunks {
if x >= chunk.X && x < chunk.X+chunk.Width() && y >= chunk.Y && y < chunk.Y+chunk.Height() {
return chunk.At(x-chunk.X, y-chunk.Y)
}
}
return nil
}
func (w *World) HueristicLine(x1, y1, x2, y2 int, h uint8) {
// Draw a line from x1, y1 to x2, y2 applying the hueristic value to each cell.
dx := x2 - x1
dy := y2 - y1
x := x1
y := y1
sx := 1
sy := 1
if dx < 0 {
sx = -1
dx = -dx
}
if dy < 0 {
sy = -1
dy = -dy
}
err := dx - dy
for {
cell := w.At(x, y)
if cell != nil {
cell.Hueristic = h
}
if x == x2 && y == y2 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x += sx
}
if e2 < dx {
err += dx
y += sy
}
}
}