2021-07-30 14:17:40 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-07-30 17:26:23 +10:00
|
|
|
"encoding/gob"
|
2021-07-30 16:31:08 +10:00
|
|
|
"math"
|
2021-07-30 14:17:40 +10:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
2021-08-20 15:01:31 +10:00
|
|
|
// Ensure Scene satisfies Scener.
|
2021-08-20 16:23:10 +10:00
|
|
|
var _ Scener = &Scene{}
|
2021-08-18 16:34:51 +10:00
|
|
|
|
2021-07-30 17:26:23 +10:00
|
|
|
func init() {
|
2021-08-25 15:04:38 +10:00
|
|
|
gob.Register(&Scene{})
|
2021-07-30 17:26:23 +10:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:25:32 +10:00
|
|
|
// Scene manages drawing and updating a bunch of components.
|
|
|
|
type Scene struct {
|
2021-08-23 10:34:56 +10:00
|
|
|
ID
|
2021-08-28 18:07:53 +10:00
|
|
|
Bounds // world coordinates
|
|
|
|
Camera *Camera // optional; applies a bunch of transforms to draw calls
|
2021-07-30 14:17:40 +10:00
|
|
|
Components []interface{}
|
2021-08-23 11:10:46 +10:00
|
|
|
Disabled
|
2021-08-23 10:34:56 +10:00
|
|
|
Hidden
|
2021-08-20 13:45:01 +10:00
|
|
|
ZOrder
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:25:32 +10:00
|
|
|
// Draw draws all components in order.
|
2021-08-12 14:06:01 +10:00
|
|
|
func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
2021-08-01 17:08:26 +10:00
|
|
|
if s.Hidden {
|
|
|
|
return
|
|
|
|
}
|
2021-08-28 18:07:53 +10:00
|
|
|
if s.Camera == nil {
|
|
|
|
// Draw everything, no camera transforms.
|
|
|
|
for _, i := range s.Components {
|
|
|
|
if d, ok := i.(Drawer); ok {
|
|
|
|
d.Draw(screen, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is a camera; apply camera transforms.
|
|
|
|
br := s.BoundingRect()
|
|
|
|
|
|
|
|
// The lower bound on zoom is the larger of
|
|
|
|
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
|
|
|
|
zoom := s.Camera.Zoom
|
|
|
|
sz := br.Size()
|
|
|
|
if z := float64(s.Camera.game.ScreenWidth) / float64(sz.X); zoom < z {
|
|
|
|
zoom = z
|
|
|
|
}
|
|
|
|
if z := float64(s.Camera.game.ScreenHeight) / float64(sz.Y); zoom < z {
|
|
|
|
zoom = z
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the configured centre puts the camera out of bounds, move it.
|
|
|
|
centre := s.Camera.Centre
|
|
|
|
// Camera frame currently Rectangle{ centre ± (screen/(2*zoom)) }.
|
|
|
|
sw2, sh2 := float64(s.Camera.game.ScreenWidth/2), float64(s.Camera.game.ScreenHeight/2)
|
|
|
|
swz, shz := int(sw2/zoom), int(sh2/zoom)
|
|
|
|
if centre.X-swz < br.Min.X {
|
|
|
|
centre.X = br.Min.X + swz
|
|
|
|
}
|
|
|
|
if centre.Y-shz < br.Min.Y {
|
|
|
|
centre.Y = br.Min.Y + shz
|
|
|
|
}
|
|
|
|
if centre.X+swz > br.Max.X {
|
|
|
|
centre.X = br.Max.X - swz
|
|
|
|
}
|
|
|
|
if centre.Y+shz > br.Max.Y {
|
|
|
|
centre.Y = br.Max.Y - shz
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply other options
|
|
|
|
opts.Filter = s.Camera.Filter
|
|
|
|
|
|
|
|
// Compute common matrix (parts independent of parallax, which is step 1).
|
|
|
|
// Moving centre to the origin happens per component.
|
|
|
|
var comm ebiten.GeoM
|
|
|
|
// 2. Zoom (this is also where rotation would be)
|
|
|
|
comm.Scale(zoom, zoom)
|
|
|
|
// 3. Move the origin to the centre of screen space.
|
|
|
|
comm.Translate(sw2, sh2)
|
|
|
|
// 4. Apply transforms from the caller.
|
|
|
|
comm.Concat(opts.GeoM)
|
|
|
|
|
2021-08-18 15:23:02 +10:00
|
|
|
// Draw everything.
|
2021-07-30 14:36:11 +10:00
|
|
|
for _, i := range s.Components {
|
2021-08-28 18:07:53 +10:00
|
|
|
d, ok := i.(Drawer)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pf := 1.0
|
|
|
|
if s, ok := i.(ParallaxScaler); ok {
|
|
|
|
pf = s.ParallaxFactor()
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
2021-08-28 18:07:53 +10:00
|
|
|
var geom ebiten.GeoM
|
|
|
|
// 1. Move centre to the origin, subject to parallax factor
|
|
|
|
geom.Translate(-float64(centre.X)*pf, -float64(centre.Y)*pf)
|
|
|
|
geom.Concat(comm)
|
|
|
|
opts.GeoM = geom
|
|
|
|
d.Draw(screen, opts)
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:14:56 +10:00
|
|
|
// Prepare does an initial Z-order sort.
|
2021-08-27 14:52:24 +10:00
|
|
|
func (s *Scene) Prepare(game *Game) error {
|
|
|
|
s.sortByDrawOrder()
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-05 15:14:56 +10:00
|
|
|
|
2021-08-18 14:02:15 +10:00
|
|
|
// sortByDrawOrder sorts the components by Z position.
|
2021-08-05 15:14:56 +10:00
|
|
|
// Everything without a Z sorts first. Stable sort is used to avoid Z-fighting
|
|
|
|
// (among layers without a Z, or those with equal Z).
|
2021-08-18 14:02:15 +10:00
|
|
|
func (s *Scene) sortByDrawOrder() {
|
2021-07-30 14:36:11 +10:00
|
|
|
sort.SliceStable(s.Components, func(i, j int) bool {
|
2021-08-28 17:50:54 +10:00
|
|
|
a, aok := s.Components[i].(Drawer)
|
|
|
|
b, bok := s.Components[j].(Drawer)
|
2021-07-30 14:17:40 +10:00
|
|
|
if aok && bok {
|
2021-08-18 14:02:15 +10:00
|
|
|
return a.DrawOrder() < b.DrawOrder()
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
|
|
|
return !aok && bok
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-28 18:07:53 +10:00
|
|
|
// Scan returns all immediate subcomponents (including the camera, if not nil).
|
|
|
|
func (s *Scene) Scan() []interface{} {
|
|
|
|
if s.Camera != nil {
|
|
|
|
return append(s.Components, s.Camera)
|
|
|
|
}
|
|
|
|
return s.Components
|
|
|
|
}
|
2021-08-01 16:10:30 +10:00
|
|
|
|
2021-08-20 15:52:01 +10:00
|
|
|
// Scene returns itself.
|
|
|
|
func (s *Scene) Scene() *Scene { return s }
|
|
|
|
|
2021-07-30 14:17:40 +10:00
|
|
|
// Update calls Update on all Updater components.
|
2021-07-30 14:36:11 +10:00
|
|
|
func (s *Scene) Update() error {
|
2021-08-02 14:38:48 +10:00
|
|
|
if s.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-05 15:14:56 +10:00
|
|
|
|
2021-07-30 14:36:11 +10:00
|
|
|
for _, c := range s.Components {
|
2021-07-30 16:31:08 +10:00
|
|
|
// Update each updater in turn
|
2021-07-30 14:17:40 +10:00
|
|
|
if u, ok := c.(Updater); ok {
|
|
|
|
if err := u.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 15:14:56 +10:00
|
|
|
// Check if the updates put the components out of order; if so, sort
|
2021-08-18 14:02:15 +10:00
|
|
|
cz := -math.MaxFloat64 // fun fact: this is min float64
|
2021-08-05 15:14:56 +10:00
|
|
|
for _, c := range s.Components {
|
2021-08-28 17:50:54 +10:00
|
|
|
z, ok := c.(Drawer)
|
2021-08-05 15:14:56 +10:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-28 16:26:24 +10:00
|
|
|
if t := z.DrawOrder(); t >= cz {
|
2021-08-18 14:02:15 +10:00
|
|
|
cz = t
|
|
|
|
continue
|
2021-08-05 15:14:56 +10:00
|
|
|
}
|
2021-08-18 14:02:15 +10:00
|
|
|
s.sortByDrawOrder()
|
|
|
|
return nil
|
2021-07-30 14:17:40 +10:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|