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