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 {
|
|
|
|
BaseObject
|
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) RunEventLoop(tickDuration time.Duration) {
|
|
|
|
for {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
p.Update()
|
|
|
|
|
|
|
|
elapsed := time.Since(startTime)
|
|
|
|
hangDuration := tickDuration - elapsed
|
|
|
|
if hangDuration > 0 {
|
|
|
|
time.Sleep(hangDuration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RootObject) Update() {
|
|
|
|
p.BaseObject.Update()
|
|
|
|
|
2024-07-18 09:21:25 +00:00
|
|
|
// Check collision
|
|
|
|
for _, child := range p.Children {
|
|
|
|
for _, other := range p.Children {
|
|
|
|
collidableChild, ok := child.(CollidableObject)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
collidableOther, ok := other.(CollidableObject)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if child != other && checkCollisionBetweenObject(collidableChild, collidableOther) {
|
|
|
|
collidableChild.OnCollide(collidableOther)
|
|
|
|
collidableOther.OnCollide(collidableChild)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 05:59:06 +00:00
|
|
|
for _, child := range p.Children {
|
|
|
|
if drawableChild, ok := child.(DrawableObject); ok {
|
|
|
|
drawableChild.Draw(pen)
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 06:47:41 +00:00
|
|
|
|
|
|
|
p.Analyzer.Draw()
|
2024-07-18 05:59:06 +00:00
|
|
|
}
|