Merge the two screen bounders to use callbacks

This commit is contained in:
stevenhowes
2022-01-04 22:22:00 +00:00
parent bc5cd16447
commit fa13e4c97b
2 changed files with 9 additions and 55 deletions
+8 -8
View File
@@ -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
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
-46
View File
@@ -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
}