forked from quasilyte/ge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiled_background.go
135 lines (109 loc) · 3.42 KB
/
tiled_background.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ge
import (
"image"
"math"
"github.com/hajimehoshi/ebiten/v2"
resource "github.com/quasilyte/ebitengine-resource"
"github.com/quasilyte/ge/tiled"
"github.com/quasilyte/gmath"
)
type TiledBackground struct {
Pos Pos
Visible bool
ColorScale ColorScale
Hue gmath.Rad
disposed bool
imageCache *imageCache
combined *ebiten.Image
}
type tileInfo struct {
offset gmath.Vec
frame int
}
func NewTiledBackground(ctx *Context) *TiledBackground {
return &TiledBackground{
Visible: true,
ColorScale: defaultColorScale,
imageCache: &ctx.imageCache,
}
}
func (bg *TiledBackground) LoadTilesetWithRand(ctx *Context, rand *gmath.Rand, width, height float64, source resource.ImageID, tileset resource.RawID) {
ts, err := tiled.UnmarshalTileset(ctx.Loader.LoadRaw(tileset).Data)
if err != nil {
panic(err)
}
spriteSheet := ctx.Loader.LoadImage(source)
frames := make([]*ebiten.Image, 0, ts.NumTiles)
for i := 0; i < ts.NumTiles; i++ {
x := i * int(ts.TileWidth)
frameRect := image.Rect(x, 0, x+int(ts.TileWidth), int(ts.TileHeight))
frameImage := spriteSheet.Data.SubImage(frameRect).(*ebiten.Image)
frames = append(frames, frameImage)
}
framePicker := gmath.NewRandPicker[int](rand)
for i := 0; i < ts.NumTiles; i++ {
framePicker.AddOption(i, *ts.Tiles[i].Probability)
}
combined := ebiten.NewImage(int(width), int(height))
var op ebiten.DrawImageOptions
applyColorScale(bg.ColorScale, &op.ColorM)
if bg.Hue != 0 {
op.ColorM.RotateHue(float64(bg.Hue))
}
for y := float64(0); y < height; y += ts.TileHeight {
for x := float64(0); x < width; x += ts.TileWidth {
offset := gmath.Vec{X: x, Y: y}
frameIndex := framePicker.Pick()
img := frames[frameIndex]
op.GeoM.Reset()
op.GeoM.Translate(offset.X, offset.Y)
combined.DrawImage(img, &op)
}
}
bg.combined = combined
}
func (bg *TiledBackground) LoadTileset(ctx *Context, width, height float64, source resource.ImageID, tileset resource.RawID) {
bg.LoadTilesetWithRand(ctx, &ctx.Rand, width, height, source, tileset)
}
func (bg *TiledBackground) IsDisposed() bool {
return bg.disposed
}
func (bg *TiledBackground) Dispose() {
bg.disposed = true
}
func (bg *TiledBackground) DrawPartial(screen *ebiten.Image, section gmath.Rect) {
bg.DrawPartialWithOffset(screen, section, gmath.Vec{})
}
func (bg *TiledBackground) DrawPartialWithOffset(screen *ebiten.Image, section gmath.Rect, offset gmath.Vec) {
if !bg.Visible {
return
}
// TODO: handle pos too?
min := gmath.Vec{X: math.Round(section.Min.X), Y: math.Round(section.Min.Y)}
max := gmath.Vec{X: math.Round(section.Max.X), Y: math.Round(section.Max.Y)}
unsafeSrc := toUnsafeImage(bg.combined)
unsafeSubImage := bg.imageCache.UnsafeImageForSubImage()
unsafeSubImage.original = unsafeSrc
unsafeSubImage.bounds = image.Rectangle{
Min: image.Point{X: int(min.X), Y: int(min.Y)},
Max: image.Point{X: int(max.X), Y: int(max.Y)},
}
unsafeSubImage.image = unsafeSrc.image
srcImage := toEbitenImage(unsafeSubImage)
var op ebiten.DrawImageOptions
op.GeoM.Translate(min.X, min.Y)
op.GeoM.Translate(offset.X, offset.Y)
screen.DrawImage(srcImage, &op)
}
func (bg *TiledBackground) Draw(screen *ebiten.Image) {
if !bg.Visible {
return
}
pos := bg.Pos.Resolve()
var op ebiten.DrawImageOptions
op.GeoM.Translate(pos.X, pos.Y)
screen.DrawImage(bg.combined, &op)
}
func (bg *TiledBackground) DrawImage(img *ebiten.Image, options *ebiten.DrawImageOptions) {
bg.combined.DrawImage(img, options)
}