From fa13e4c97bb3c7ec92e38701276b90cc78371726 Mon Sep 17 00:00:00 2001 From: stevenhowes <38082088+stevenhowes@users.noreply.github.com> Date: Tue, 4 Jan 2022 22:22:00 +0000 Subject: [PATCH] Merge the two screen bounders to use callbacks --- component_bounder_screen.go | 18 +++++------ component_bounder_screen_resetting.go | 46 --------------------------- 2 files changed, 9 insertions(+), 55 deletions(-) delete mode 100644 component_bounder_screen_resetting.go diff --git a/component_bounder_screen.go b/component_bounder_screen.go index 751d380..30ed7d3 100644 --- a/component_bounder_screen.go +++ b/component_bounder_screen.go @@ -4,16 +4,19 @@ package GoRetro * -------------------- * BounderScreen * -------------------- - * A bounder which sets active = false on anything which - * has left the screen + * A bounder which triggers if the element isn't on the screen */ type bounderScreen struct { - container *Element + container *Element + callbackFunc func(element *Element) } -func NewBounderScreen(container *Element) *bounderScreen { - return &bounderScreen{container: container} +func NewBounderScreen(container *Element, callback func(element *Element)) *bounderScreen { + return &bounderScreen{ + container: container, + callbackFunc: callback, + } } func (bounder *bounderScreen) onDraw() error { @@ -23,12 +26,9 @@ func (bounder *bounderScreen) onDraw() error { func (bounder *bounderScreen) onUpdate() error { b := bounder.container - // If the position is outside the screen bounds then set it as inactive - // and mark for deletion if b.Position.X > float64(Config.ScreenWidth) || b.Position.X < 0 || b.Position.Y > float64(Config.ScreenHeight) || b.Position.Y < 0 { - b.Active = false - b.Delete = true + bounder.callbackFunc(bounder.container) } return nil diff --git a/component_bounder_screen_resetting.go b/component_bounder_screen_resetting.go deleted file mode 100644 index 2288e29..0000000 --- a/component_bounder_screen_resetting.go +++ /dev/null @@ -1,46 +0,0 @@ -package GoRetro - -/* - * -------------------- - * BounderScreen - * -------------------- - * A bounder which resets the position to the opposite of - * whichever bound was hit - */ - -type bounderScreenResetting struct { - container *Element -} - -func NewBounderScreenResetting(container *Element) *bounderScreenResetting { - return &bounderScreenResetting{container: container} -} - -func (bounder *bounderScreenResetting) onDraw() error { - return nil -} - -func (bounder *bounderScreenResetting) onUpdate() error { - b := bounder.container - - // If any position exceeds the screen dimensions, wrap it to the - // opposite side - if b.Position.X > float64(Config.ScreenWidth) { - b.Position.X = 0 - } - if b.Position.X < 0 { - b.Position.X = float64(Config.ScreenWidth) - } - if b.Position.Y > float64(Config.ScreenHeight) { - b.Position.Y = 0 - } - if b.Position.Y < 0 { - b.Position.Y = float64(Config.ScreenWidth) - } - - return nil -} - -func (bounder *bounderScreenResetting) onCollision(other *Element) error { - return nil -}