From ff0040bbb1ccca58a210e160650fef3aa4de87f2 Mon Sep 17 00:00:00 2001
From: Sergio Vera Castellano <severa@magento.com>
Date: Fri, 12 Jan 2018 12:19:58 +0100
Subject: [PATCH] Small adjustments

---
 .../parallax-scrolling-background/README.md   |  4 +-
 .../parallax-scrolling-background/main.go     | 14 ++--
 .../scrolling_background.go                   | 68 ++++++++++++-------
 3 files changed, 55 insertions(+), 31 deletions(-)

diff --git a/examples/community/parallax-scrolling-background/README.md b/examples/community/parallax-scrolling-background/README.md
index 4c3b117..03ecfc6 100644
--- a/examples/community/parallax-scrolling-background/README.md
+++ b/examples/community/parallax-scrolling-background/README.md
@@ -2,6 +2,6 @@
 
 Created by [Sergio Vera](https://github.com/svera)
 
-This example shows how to implement an infinite side scrolling background with a depth effect, using [parallax scrolling](https://en.wikipedia.org/wiki/Parallax_scrolling).
+This example shows how to implement an infinite side scrolling background with a depth effect, using [parallax scrolling](https://en.wikipedia.org/wiki/Parallax_scrolling). Code is based in the [infinite scrolling background](https://github.com/faiface/pixel/tree/master/examples/community/scrolling-background) demo.
 
-Credits to [Peter Hellberg](https://github.com/peterhellberg) for the reworked background image.
+Credits to [Peter Hellberg](https://github.com/peterhellberg) for the improved background images.
diff --git a/examples/community/parallax-scrolling-background/main.go b/examples/community/parallax-scrolling-background/main.go
index d74f367..a60b21c 100644
--- a/examples/community/parallax-scrolling-background/main.go
+++ b/examples/community/parallax-scrolling-background/main.go
@@ -29,8 +29,10 @@ const (
 	windowHeight     = 450
 	foregroundHeight = 149
 	// This is the scrolling speed (pixels per second)
-	backgroundSpeed = 60
-	foregroundSpeed = 120
+	// Negative values will make background to scroll to the left,
+	// positive to the right.
+	backgroundSpeed = -60
+	foregroundSpeed = -120
 )
 
 func run() {
@@ -54,15 +56,15 @@ func run() {
 		panic(err)
 	}
 
-	background := newScrollingBackground(picBackground, windowWidth, windowHeight, windowWidth)
-	foreground := newScrollingBackground(picForeground, windowWidth, foregroundHeight, windowWidth)
+	background := NewScrollingBackground(picBackground, windowWidth, windowHeight, backgroundSpeed)
+	foreground := NewScrollingBackground(picForeground, windowWidth, foregroundHeight, foregroundSpeed)
 
 	last := time.Now()
 	for !win.Closed() {
 		dt := time.Since(last).Seconds()
 		last = time.Now()
-		background.update(win, backgroundSpeed, dt)
-		foreground.update(win, foregroundSpeed, dt)
+		background.Update(win, dt)
+		foreground.Update(win, dt)
 		win.Update()
 	}
 }
diff --git a/examples/community/parallax-scrolling-background/scrolling_background.go b/examples/community/parallax-scrolling-background/scrolling_background.go
index 993c430..87ba83f 100644
--- a/examples/community/parallax-scrolling-background/scrolling_background.go
+++ b/examples/community/parallax-scrolling-background/scrolling_background.go
@@ -1,42 +1,64 @@
 package main
 
 import (
+	"math"
+
 	"github.com/faiface/pixel"
 	"github.com/faiface/pixel/pixelgl"
 )
 
-type scrollingBackground struct {
-	width               float64
-	height              float64
-	windowWidth         float64
-	displacementCounter float64
-	backgrounds         [2]*pixel.Sprite
-	positions           [2]pixel.Vec
+// ScrollingBackground stores all needed information to scroll a background
+// to the left or right
+type ScrollingBackground struct {
+	width        float64
+	height       float64
+	displacement float64
+	speed        float64
+	backgrounds  [2]*pixel.Sprite
+	positions    [2]pixel.Vec
 }
 
-func newScrollingBackground(pic pixel.Picture, width, height, windowWidth float64) *scrollingBackground {
-	return &scrollingBackground{
-		width:       width,
-		height:      height,
-		windowWidth: windowWidth,
+// NewScrollingBackground construct and returns a new instance of scrollingBackground,
+// positioning the background images according to the speed value
+func NewScrollingBackground(pic pixel.Picture, width, height, speed float64) *ScrollingBackground {
+	sb := &ScrollingBackground{
+		width:  width,
+		height: height,
+		speed:  speed,
 		backgrounds: [2]*pixel.Sprite{
 			pixel.NewSprite(pic, pixel.R(0, 0, width, height)),
 			pixel.NewSprite(pic, pixel.R(width, 0, width*2, height)),
 		},
-		positions: [2]pixel.Vec{
-			pixel.V(width/2, (height/2)+1),
-			pixel.V(width+(width/2), (height/2)+1),
-		},
+	}
+
+	sb.positionImages()
+	return sb
+}
+
+// If scrolling speed > 0, put second background image ouside the screen,
+// at the left side, otherwise put it at the right side.
+func (sb *ScrollingBackground) positionImages() {
+	if sb.speed > 0 {
+		sb.positions = [2]pixel.Vec{
+			pixel.V(sb.width/2, (sb.height/2)+1),
+			pixel.V((sb.width/2)-sb.width, (sb.height/2)+1),
+		}
+	} else {
+		sb.positions = [2]pixel.Vec{
+			pixel.V(sb.width/2, (sb.height/2)+1),
+			pixel.V(sb.width+(sb.width/2), (sb.height/2)+1),
+		}
 	}
 }
 
-func (sb *scrollingBackground) update(win *pixelgl.Window, speed, dt float64) {
-	if sb.displacementCounter <= -sb.windowWidth {
-		sb.displacementCounter = 0
+// Update will move backgrounds certain pixels, depending of the amount of time passed
+func (sb *ScrollingBackground) Update(win *pixelgl.Window, dt float64) {
+	if math.Abs(sb.displacement) >= sb.width {
+		sb.displacement = 0
 		sb.positions[0], sb.positions[1] = sb.positions[1], sb.positions[0]
 	}
-	d := pixel.V(-sb.displacementCounter, 0)
-	sb.backgrounds[0].Draw(win, pixel.IM.Moved(sb.positions[0].Sub(d)))
-	sb.backgrounds[1].Draw(win, pixel.IM.Moved(sb.positions[1].Sub(d)))
-	sb.displacementCounter -= speed * dt
+	d := pixel.V(sb.displacement, 0)
+	sb.backgrounds[0].Draw(win, pixel.IM.Moved(sb.positions[0].Add(d)))
+	sb.backgrounds[1].Draw(win, pixel.IM.Moved(sb.positions[1].Add(d)))
+	sb.displacement += sb.speed * dt
 }
-- 
GitLab