This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
culling.go
93 lines (75 loc) · 2.01 KB
/
culling.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package goga
const (
culling_2d_name = "culling2d"
)
type Cullable struct {
*Actor
*Pos2D
}
type Culling2D struct {
cullables []Cullable
viewport Vec4
}
// Creates a new sprite culling system.
// To update the viewport, call SetViewport().
func NewCulling2D(x, y, width, height int) *Culling2D {
culling := &Culling2D{}
culling.cullables = make([]Cullable, 0)
culling.viewport = Vec4{float64(x), float64(y), float64(width), float64(height)}
return culling
}
// Sets the culling outer bounds.
// Actors outside of this box won't be rendered.
func (c *Culling2D) SetViewport(x, y, width, height int) {
c.viewport = Vec4{float64(x), float64(y), float64(width), float64(height)}
}
func (c *Culling2D) Cleanup() {}
// Adds actor with Pos2D to the system.
func (c *Culling2D) Add(actor *Actor, pos *Pos2D) bool {
id := actor.GetId()
for _, cull := range c.cullables {
if id == cull.Actor.GetId() {
return false
}
}
c.cullables = append(c.cullables, Cullable{actor, pos})
return true
}
// Removes actor with Pos2D from system.
func (c *Culling2D) Remove(actor *Actor) bool {
return c.RemoveById(actor.GetId())
}
// Removes actor with Pos2D from system by ID.
func (c *Culling2D) RemoveById(id ActorId) bool {
for i, cull := range c.cullables {
if cull.GetId() == id {
c.cullables = append(c.cullables[:i], c.cullables[i+1:]...)
return true
}
}
return false
}
// Removes all cullable objects.
func (c *Culling2D) RemoveAll() {
c.cullables = make([]Cullable, 0)
}
// Returns number of cullable objects.
func (c *Culling2D) Len() int {
return len(c.cullables)
}
func (c *Culling2D) GetName() string {
return culling_2d_name
}
// Updates visibility of all contained sprites.
func (c *Culling2D) Update(delta float64) {
for _, cullable := range c.cullables {
if cullable.Pos.X > c.viewport.Z ||
cullable.Pos.X+cullable.Size.X < c.viewport.X ||
cullable.Pos.Y > c.viewport.W ||
cullable.Pos.Y+cullable.Size.Y < c.viewport.Y {
cullable.Visible = false
} else {
cullable.Visible = true
}
}
}