| 1 |
//go:build !tinygo |
| 2 |
// +build !tinygo |
| 3 |
|
| 4 |
package gfx |
| 5 |
|
| 6 |
import ( |
| 7 |
"image/draw" |
| 8 |
"sort" |
| 9 |
) |
| 10 |
|
| 11 |
// Blocks is a slice of blocks. |
| 12 |
type Blocks []Block |
| 13 |
|
| 14 |
// Add appends one or more blocks to the slice of Blocks. |
| 15 |
func (blocks *Blocks) Add(bs ...Block) { |
| 16 |
if len(bs) > 0 { |
| 17 |
*blocks = append(*blocks, bs...) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// AddNewBlock creates a new Block and appends it to the slice. |
| 22 |
func (blocks *Blocks) AddNewBlock(pos, size Vec3, ic BlockColor) { |
| 23 |
blocks.Add(NewBlock(pos, size, ic)) |
| 24 |
} |
| 25 |
|
| 26 |
// Draw all blocks. |
| 27 |
func (blocks Blocks) Draw(dst draw.Image, origin Vec3) { |
| 28 |
for _, block := range blocks { |
| 29 |
if block.Rect(origin).Bounds().Overlaps(dst.Bounds()) { |
| 30 |
block.Draw(dst, origin) |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// DrawPolygons draws all of the blocks on the dst image. |
| 36 |
// (using the shape, top and left polygons at the given origin) |
| 37 |
func (blocks Blocks) DrawPolygons(dst draw.Image, origin Vec3) { |
| 38 |
for _, block := range blocks { |
| 39 |
if block.Rect(origin).Bounds().Overlaps(dst.Bounds()) { |
| 40 |
block.DrawPolygons(dst, origin) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// DrawRectangles for all blocks. |
| 46 |
func (blocks Blocks) DrawRectangles(dst draw.Image, origin Vec3) { |
| 47 |
for _, block := range blocks { |
| 48 |
if block.Rect(origin).Bounds().Overlaps(dst.Bounds()) { |
| 49 |
block.DrawRectangles(dst, origin) |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// DrawBounds for all blocks. |
| 55 |
func (blocks Blocks) DrawBounds(dst draw.Image, origin Vec3) { |
| 56 |
for _, block := range blocks { |
| 57 |
if block.Rect(origin).Bounds().Overlaps(dst.Bounds()) { |
| 58 |
block.DrawBounds(dst, origin) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// DrawWireframes for all blocks. |
| 64 |
func (blocks Blocks) DrawWireframes(dst draw.Image, origin Vec3) { |
| 65 |
for _, block := range blocks { |
| 66 |
if block.Rect(origin).Bounds().Overlaps(dst.Bounds()) { |
| 67 |
block.DrawWireframe(dst, origin) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Sort blocks to be drawn starting from max X, max Y and min Z. |
| 73 |
func (blocks Blocks) Sort() { |
| 74 |
sort.Slice(blocks, func(i, j int) bool { |
| 75 |
return blocks[i].Behind(blocks[j]) |
| 76 |
}) |
| 77 |
} |