-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.go
More file actions
90 lines (77 loc) · 1.9 KB
/
scene.go
File metadata and controls
90 lines (77 loc) · 1.9 KB
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
package engine
import (
"fmt"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type Scene struct {
root Node
}
func NewScene() *Scene {
return &Scene{
root: NewNode("root"),
}
}
func (s *Scene) AddNode(node Node) {
node.SetParent(s.root)
s.root.AddChild(node)
}
func (s *Scene) Update() error {
if err := s.root.Update(); err != nil {
return err
}
colliders := GetAllNodesWithTag(s.root, "Collider")
for i, collider := range colliders {
for j := i + 1; j < len(colliders); j++ {
otherCollider := colliders[j]
if collider == otherCollider {
continue
}
if circle, ok := collider.(*CircleCollider); ok {
if otherCircle, ok := otherCollider.(*CircleCollider); ok {
if circle.CollidesWithCircle(otherCircle) {
circle.OnCollision.Emit(otherCircle)
otherCircle.OnCollision.Emit(circle)
}
}
if aabb, ok := otherCollider.(*AABBCollider); ok {
if circle.CollidesWithAABB(aabb) {
circle.OnCollision.Emit(aabb)
aabb.OnCollision.Emit(circle)
}
}
}
if aabb, ok := collider.(*AABBCollider); ok {
if otherCircle, ok := otherCollider.(*CircleCollider); ok {
if aabb.CollidesWithCircle(otherCircle) {
aabb.OnCollision.Emit(otherCircle)
otherCircle.OnCollision.Emit(aabb)
}
}
if otherAABB, ok := otherCollider.(*AABBCollider); ok {
if aabb.CollidesWithAABB(otherAABB) {
aabb.OnCollision.Emit(otherAABB)
otherAABB.OnCollision.Emit(aabb)
}
}
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyF11) {
s.PrintNodeTree()
}
return nil
}
func (s *Scene) Draw(screen *ebiten.Image) {
s.root.Draw(screen)
}
func (s *Scene) PrintNodeTree() {
printNodeTree(s.root, 0)
}
func printNodeTree(node Node, indent int) {
for _, child := range node.GetChildren() {
fmt.Println(strings.Repeat(" ", indent), child.Name())
printNodeTree(child, indent+2)
}
}