-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawscreen.go
75 lines (60 loc) · 2.56 KB
/
drawscreen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"image"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/jcgraybill/it-costs-money/sys"
)
func (g *Game) Draw(screen *ebiten.Image) {
// TODO objects should draw themselves - add a draw.go to coin, level, player
// Pass framebuffer for them to decorate
frameBuffer.Clear()
parallax(g.level.BgImage1, -g.player.X, 4)
parallax(g.level.BgImage2, -g.player.X, 3)
parallax(g.level.BgImage3, -g.player.X, 2)
viewPortOffset := g.player.X - (sys.ScreenWidth/2 + sys.FrameWidth/2)
levelViewFinder := image.Rect(viewPortOffset, 0, viewPortOffset+sys.ScreenWidth, sys.ScreenHeight)
frameBuffer.DrawImage(g.level.LevelBackgroundImage.SubImage(levelViewFinder).(*ebiten.Image), nil)
frameBuffer.DrawImage(g.level.LevelImage.SubImage(levelViewFinder).(*ebiten.Image), nil)
runnerOP := &ebiten.DrawImageOptions{}
if g.player.FacingLeft {
runnerOP.GeoM.Scale(-1, 1)
runnerOP.GeoM.Translate(sys.FrameWidth, 0)
}
runnerOP.GeoM.Translate(-float64(sys.FrameWidth)/2, 0)
runnerOP.GeoM.Translate(sys.ScreenWidth/2, float64(g.player.Y))
i := (g.count / 5) % len(*g.player.Slides)
for _, coin := range g.level.Coin.Coins {
if coin.Uncollected {
if coin.X > viewPortOffset || coin.X < viewPortOffset+sys.ScreenWidth/2-sys.FrameWidth {
coinOp := &ebiten.DrawImageOptions{}
coinOp.GeoM.Translate(-float64(viewPortOffset-coin.X), float64(coin.Y))
frameBuffer.DrawImage(sys.CoinTiles[(g.count/sys.CoinAnimationSpeed)%sys.CoinSlides], coinOp)
}
}
}
frameBuffer.DrawImage((*g.player.Slides)[i], runnerOP)
frameBuffer.DrawImage(g.level.LevelForegroundImage.SubImage(levelViewFinder).(*ebiten.Image), nil)
if message != "" {
ebitenutil.DebugPrint(frameBuffer, message)
}
screen.DrawImage(frameBuffer, nil)
coins := fmt.Sprintf("Coins: %d", g.player.Coins)
textBounds := text.BoundString(sys.Ttf, coins)
text.Draw(screen, coins, sys.Ttf, sys.ScreenWidth-textBounds.Dx()-18, textBounds.Dy()+2, color.RGBA{0x00, 0x00, 0x00, 0xff})
text.Draw(screen, coins, sys.Ttf, sys.ScreenWidth-textBounds.Dx()-20, textBounds.Dy(), color.RGBA{0xd4, 0xaf, 0x47, 0xff})
}
func parallax(image *ebiten.Image, offset int, speed int) {
panelWidth := image.Bounds().Dx()
position := (offset / speed) % panelWidth
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(position), 0)
frameBuffer.DrawImage(image, op)
for i := 0; position+i*panelWidth+panelWidth < sys.ScreenWidth; i++ {
op.GeoM.Translate(float64(panelWidth), 0)
frameBuffer.DrawImage(image, op)
}
}