add loading switch and dummy load for testing
This commit is contained in:
parent
93113ca7d6
commit
f83bc28f50
7 changed files with 82 additions and 8 deletions
|
@ -40,6 +40,8 @@ func init() {
|
||||||
type Camera struct {
|
type Camera struct {
|
||||||
ID
|
ID
|
||||||
Child interface{}
|
Child interface{}
|
||||||
|
Disables
|
||||||
|
Hides
|
||||||
|
|
||||||
// Camera controls
|
// Camera controls
|
||||||
// These directly manipulate the camera. If you want to restrict the camera
|
// These directly manipulate the camera. If you want to restrict the camera
|
||||||
|
|
18
engine/dummy.go
Normal file
18
engine/dummy.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package engine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DummyLoad is a loader that just takes up time and doesn't actually load
|
||||||
|
// anything.
|
||||||
|
type DummyLoad struct {
|
||||||
|
time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load sleeps for d.Duration, then returns nil.
|
||||||
|
func (d DummyLoad) Load(fs.FS) error {
|
||||||
|
time.Sleep(d.Duration)
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -17,12 +17,56 @@ limitations under the License.
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"io/fs"
|
||||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
"log"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoadingScreen struct{}
|
type LoadingSwitch struct {
|
||||||
|
During Hider
|
||||||
|
After Hider
|
||||||
|
|
||||||
func (LoadingScreen) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
|
assets fs.FS
|
||||||
ebitenutil.DebugPrint(screen, "Loading...")
|
}
|
||||||
|
|
||||||
|
// Scan only scans s.During, thus, only s.During is loaded by Game directly.
|
||||||
|
func (s *LoadingSwitch) Scan(visit VisitFunc) error {
|
||||||
|
return visit(s.During)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load stores a copy of assets to use later.
|
||||||
|
func (s *LoadingSwitch) Load(assets fs.FS) error {
|
||||||
|
s.assets = assets
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare loads, registers, and prepares.After in a separate goroutine. Once
|
||||||
|
// ready, LoadingSwitch hides s.During and shows s.After.
|
||||||
|
func (s *LoadingSwitch) Prepare(game *Game) error {
|
||||||
|
go func() {
|
||||||
|
startLoad := time.Now()
|
||||||
|
if err := game.Load(s.After, s.assets); err != nil {
|
||||||
|
log.Printf("Couldn't load: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("LoadingSwitch: finished loading in %v", time.Since(startLoad))
|
||||||
|
|
||||||
|
startBuild := time.Now()
|
||||||
|
if err := game.Register(s.After, s); err != nil {
|
||||||
|
log.Printf("Couldn't register: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("LoadingSwitch: finished registering in %v", time.Since(startBuild))
|
||||||
|
startPrep := time.Now()
|
||||||
|
if err := game.Prepare(s.After); err != nil {
|
||||||
|
log.Printf("Couldn't prepare: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("LoadingSwitch: finished preparing in %v", time.Since(startPrep))
|
||||||
|
|
||||||
|
// TODO: better scene transitions
|
||||||
|
s.During.Hide()
|
||||||
|
s.After.Show()
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
12
example.go
12
example.go
|
@ -91,9 +91,15 @@ func main() {
|
||||||
ID: "bg_fill",
|
ID: "bg_fill",
|
||||||
Color: color.Gray{100},
|
Color: color.Gray{100},
|
||||||
},
|
},
|
||||||
&engine.Camera{
|
&engine.LoadingSwitch{
|
||||||
ID: "game_camera",
|
During: &engine.Billboard{
|
||||||
Child: lev1,
|
ID: "loading_screen",
|
||||||
|
Src: engine.ImageRef{Path: "assets/loading.png"},
|
||||||
|
},
|
||||||
|
After: &engine.Camera{
|
||||||
|
ID: "game_camera",
|
||||||
|
Child: lev1,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
&engine.DebugToast{ID: "toast", Pos: image.Pt(0, 15)},
|
&engine.DebugToast{ID: "toast", Pos: image.Pt(0, 15)},
|
||||||
engine.PerfDisplay{},
|
engine.PerfDisplay{},
|
||||||
|
|
BIN
example/asset_src/loading.aseprite
Normal file
BIN
example/asset_src/loading.aseprite
Normal file
Binary file not shown.
BIN
example/assets/loading.png
Normal file
BIN
example/assets/loading.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
|
@ -18,6 +18,7 @@ package example
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/DrJosh9000/ichigo/engine"
|
"github.com/DrJosh9000/ichigo/engine"
|
||||||
"github.com/DrJosh9000/ichigo/geom"
|
"github.com/DrJosh9000/ichigo/geom"
|
||||||
|
@ -29,6 +30,9 @@ func Level1() *engine.Scene {
|
||||||
ID: "level_1",
|
ID: "level_1",
|
||||||
Bounds: engine.Bounds(image.Rect(-32, -32, 320+32, 240+32)),
|
Bounds: engine.Bounds(image.Rect(-32, -32, 320+32, 240+32)),
|
||||||
Child: engine.MakeContainer(
|
Child: engine.MakeContainer(
|
||||||
|
engine.DummyLoad{
|
||||||
|
Duration: 2 * time.Second,
|
||||||
|
},
|
||||||
&engine.Parallax{
|
&engine.Parallax{
|
||||||
CameraID: "game_camera",
|
CameraID: "game_camera",
|
||||||
Child: &engine.Billboard{
|
Child: &engine.Billboard{
|
||||||
|
|
Loading…
Reference in a new issue