Refactored code
This commit is contained in:
parent
bb75f29fdb
commit
3963f5c735
|
@ -33,11 +33,6 @@ const (
|
|||
foregroundSpeed = 120
|
||||
)
|
||||
|
||||
type scrollingBackground struct {
|
||||
Pic pixel.Picture
|
||||
speed int
|
||||
}
|
||||
|
||||
func run() {
|
||||
cfg := pixelgl.WindowConfig{
|
||||
Title: "Scrolling background demo",
|
||||
|
@ -59,45 +54,15 @@ func run() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
// Backgrounds are made taking the left and right halves of the image
|
||||
background1 := pixel.NewSprite(picBackground, pixel.R(0, 0, windowWidth, windowHeight))
|
||||
background2 := pixel.NewSprite(picBackground, pixel.R(windowWidth, 0, windowWidth*2, windowHeight))
|
||||
foreground1 := pixel.NewSprite(picForeground, pixel.R(0, 0, windowWidth, foregroundHeight))
|
||||
foreground2 := pixel.NewSprite(picForeground, pixel.R(windowWidth, 0, windowWidth*2, foregroundHeight))
|
||||
background := newScrollingBackground(picBackground, windowWidth, windowHeight, windowWidth)
|
||||
foreground := newScrollingBackground(picForeground, windowWidth, foregroundHeight, windowWidth)
|
||||
|
||||
// In the beginning, vector1 will put background1 filling the whole window, while vector2 will
|
||||
// put background2 just at the right side of the window, out of view
|
||||
backgroundVector1 := pixel.V(windowWidth/2, (windowHeight/2)+1)
|
||||
backgroundVector2 := pixel.V(windowWidth+(windowWidth/2), (windowHeight/2)+1)
|
||||
|
||||
foregroundVector1 := pixel.V(windowWidth/2, (foregroundHeight/2)+1)
|
||||
foregroundVector2 := pixel.V(windowWidth+(windowWidth/2), (foregroundHeight/2)+1)
|
||||
|
||||
bi, fi := 0., 0.
|
||||
last := time.Now()
|
||||
for !win.Closed() {
|
||||
dt := time.Since(last).Seconds()
|
||||
last = time.Now()
|
||||
// When one of the backgrounds has completely scrolled, we swap displacement vectors,
|
||||
// so the backgrounds will swap positions too regarding the previous iteration,
|
||||
// thus making the background endless.
|
||||
if bi <= -windowWidth {
|
||||
bi = 0
|
||||
backgroundVector1, backgroundVector2 = backgroundVector2, backgroundVector1
|
||||
}
|
||||
if fi <= -windowWidth {
|
||||
fi = 0
|
||||
foregroundVector1, foregroundVector2 = foregroundVector2, foregroundVector1
|
||||
}
|
||||
// This delta vector will move the backgrounds to the left
|
||||
db := pixel.V(-bi, 0)
|
||||
df := pixel.V(-fi, 0)
|
||||
background1.Draw(win, pixel.IM.Moved(backgroundVector1.Sub(db)))
|
||||
background2.Draw(win, pixel.IM.Moved(backgroundVector2.Sub(db)))
|
||||
foreground1.Draw(win, pixel.IM.Moved(foregroundVector1.Sub(df)))
|
||||
foreground2.Draw(win, pixel.IM.Moved(foregroundVector2.Sub(df)))
|
||||
bi -= backgroundSpeed * dt
|
||||
fi -= foregroundSpeed * dt
|
||||
background.update(win, backgroundSpeed, dt)
|
||||
foreground.update(win, foregroundSpeed, dt)
|
||||
win.Update()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"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
|
||||
}
|
||||
|
||||
func newScrollingBackground(pic pixel.Picture, width, height, windowWidth float64) *scrollingBackground {
|
||||
return &scrollingBackground{
|
||||
width: width,
|
||||
height: height,
|
||||
windowWidth: windowWidth,
|
||||
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),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (sb *scrollingBackground) update(win *pixelgl.Window, speed, dt float64) {
|
||||
if sb.displacementCounter <= -sb.windowWidth {
|
||||
sb.displacementCounter = 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
|
||||
}
|
Loading…
Reference in New Issue