CodingLand/pkg/internal/land/root.go

136 lines
2.8 KiB
Go
Raw Normal View History

2024-07-18 05:59:06 +00:00
package land
import (
"github.com/veandco/go-sdl2/sdl"
2024-07-18 06:47:41 +00:00
"time"
2024-07-18 05:59:06 +00:00
)
type RootObject struct {
Children []Object
Overlays []Object
2024-07-18 06:47:41 +00:00
Analyzer *PerformanceAnalyzer
}
func NewRootObject() *RootObject {
in := &RootObject{
Analyzer: &PerformanceAnalyzer{},
}
2024-07-18 09:21:25 +00:00
go in.Analyzer.RunResetter(1 * time.Second)
2024-07-18 06:47:41 +00:00
return in
}
func (p *RootObject) AddChild(child Object) {
p.Children = append(p.Children, child)
}
func (p *RootObject) AddOverlay(overlay Object) {
p.Overlays = append(p.Overlays, overlay)
}
2024-07-18 06:47:41 +00:00
func (p *RootObject) RunEventLoop(tickDuration time.Duration) {
for {
startTime := time.Now()
p.Update()
elapsed := time.Since(startTime)
hangDuration := tickDuration - elapsed
if hangDuration > 0 {
time.Sleep(hangDuration)
}
}
}
2024-07-18 09:46:36 +00:00
func (p *RootObject) ForEachChildren(cb func(child Object)) {
var caller func(current Object)
caller = func(current Object) {
cb(current)
for _, child := range current.GetChildren() {
caller(child)
}
}
for _, child := range p.Children {
caller(child)
}
for _, child := range p.Overlays {
caller(child)
}
2024-07-18 09:46:36 +00:00
}
func (p *RootObject) HandleUserEvent(event sdl.Event) {
switch event := event.(type) {
case *sdl.MouseButtonEvent:
2024-07-18 14:59:03 +00:00
x, y := float64(event.X), float64(event.Y)
vec := Vector2D{x, y}
p.ForEachChildren(func(child Object) {
if interChild, ok := child.(InteractableObject); ok {
if IsOverlapWithPoint(child.(PositionedObject), vec) {
if event.Type == sdl.MOUSEBUTTONDOWN {
interChild.OnEvent(UserEventMouseDown, vec)
} else if event.Type == sdl.MOUSEBUTTONUP {
interChild.OnEvent(UserEventMouseUp, vec)
2024-07-18 09:46:36 +00:00
}
}
2024-07-18 14:59:03 +00:00
}
})
case *sdl.MouseMotionEvent:
x, y := float64(event.X), float64(event.Y)
vec := Vector2D{x, y}
p.ForEachChildren(func(child Object) {
if interChild, ok := child.(InteractableObject); ok {
isOverlap := IsOverlapWithPoint(child.(PositionedObject), vec)
interChild.OnEvent(UserEventMouseMove, vec, isOverlap)
}
})
2024-07-18 09:46:36 +00:00
}
}
2024-07-18 06:47:41 +00:00
func (p *RootObject) Update() {
p.ForEachChildren(func(child Object) {
child.Update()
})
2024-07-18 06:47:41 +00:00
2024-07-18 09:21:25 +00:00
// Check collision
2024-07-18 09:46:36 +00:00
p.ForEachChildren(func(child Object) {
p.ForEachChildren(func(other Object) {
if child == other {
return
}
2024-07-18 09:21:25 +00:00
collidableChild, ok := child.(CollidableObject)
if !ok {
2024-07-18 09:46:36 +00:00
return
2024-07-18 09:21:25 +00:00
}
collidableOther, ok := other.(CollidableObject)
if !ok {
2024-07-18 09:46:36 +00:00
return
2024-07-18 09:21:25 +00:00
}
2024-07-18 09:46:36 +00:00
if checkCollisionBetweenObject(collidableChild, collidableOther) {
2024-07-18 09:21:25 +00:00
collidableChild.OnCollide(collidableOther)
collidableOther.OnCollide(collidableChild)
}
2024-07-18 09:46:36 +00:00
})
})
2024-07-18 09:21:25 +00:00
2024-07-18 06:47:41 +00:00
p.Analyzer.Tick()
2024-07-18 05:59:06 +00:00
}
func (p *RootObject) Draw(pen *sdl.Renderer) {
2024-07-18 06:47:41 +00:00
// Render background and clear previous state
pen.SetDrawColor(77, 77, 77, 255)
pen.Clear()
// Render each child
2024-07-18 09:46:36 +00:00
p.ForEachChildren(func(child Object) {
2024-07-18 05:59:06 +00:00
if drawableChild, ok := child.(DrawableObject); ok {
drawableChild.Draw(pen)
}
2024-07-18 09:46:36 +00:00
})
2024-07-18 06:47:41 +00:00
p.Analyzer.Draw()
2024-07-18 05:59:06 +00:00
}