Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#builder #588 mutil scenes #291

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ func loadJson(ret interface{}, fs spxfs.Dir, file string) (err error) {
return json.NewDecoder(f).Decode(ret)
}

func loadProjConfig(proj *projConfig, fs spxfs.Dir, index interface{}) (err error) {
func loadsceneConfig(p *Game, scene *sceneConfig, fs spxfs.Dir, index interface{}) (err error) {
switch v := index.(type) {
case io.Reader:
err = json.NewDecoder(v).Decode(proj)
err = json.NewDecoder(v).Decode(scene)
case string:
err = loadJson(&proj, fs, v)
err = loadJson(&scene, fs, v)
case int:
err = loadJson(&scene, fs, p.setting.GetScene(v))
case nil:
err = loadJson(&proj, fs, "index.json")
err = loadJson(&scene, fs, "index.json")
default:
return syscall.EINVAL
}
Expand Down Expand Up @@ -99,6 +101,22 @@ func toMapMode(mode string) int {
}

type projConfig struct {
Scenes []string `json:"scenes"`
DefaultSceneIndex int `json:"defaultSceneIndex"`
}

func (p *projConfig) GetDefaultScene() string {
return p.GetScene(p.DefaultSceneIndex)
}

func (p *projConfig) GetScene(sceneIndex int) string {
if sceneIndex < 0 || sceneIndex >= len(p.Scenes) {
panic("invalid scene index")
}
return p.Scenes[sceneIndex]
}

type sceneConfig struct {
Zorder []interface{} `json:"zorder"`
Backdrops []*backdropConfig `json:"backdrops"`
BackdropIndex *int `json:"backdropIndex"`
Expand All @@ -113,7 +131,7 @@ type projConfig struct {
SceneIndex int `json:"sceneIndex"` //this property is deprecated, use BackdropIndex instead
}

func (p *projConfig) getBackdrops() []*backdropConfig {
func (p *sceneConfig) getBackdrops() []*backdropConfig {
if p.Backdrops != nil {
return p.Backdrops
}
Expand All @@ -123,7 +141,7 @@ func (p *projConfig) getBackdrops() []*backdropConfig {
return p.Costumes
}

func (p *projConfig) getBackdropIndex() int {
func (p *sceneConfig) getBackdropIndex() int {
if p.BackdropIndex != nil {
return *p.BackdropIndex
}
Expand Down
70 changes: 43 additions & 27 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ type Game struct {
eventSinks
Camera

fs spxfs.Dir
shared *sharedImages
fs spxfs.Dir
shared *sharedImages
setting *projConfig

sounds soundMgr
turtle turtleCanvas
Expand Down Expand Up @@ -163,6 +164,7 @@ func (p *Game) reset() {
p.items = nil
p.isLoaded = false
p.sprs = make(map[string]Spriter)
p.worldWidth_, p.worldHeight_ = 0, 0
}

func (p *Game) initGame(sprites []Spriter) *Game {
Expand Down Expand Up @@ -196,15 +198,31 @@ func Gopt_Game_Run(game Gamer, resource interface{}, gameConf ...*Config) {
panic(err)
}

var conf Config
var proj projConfig
{
projPath := "setting.json"
_, e := fs.Open(projPath)
if e == nil {
err = loadJson(&proj, fs, projPath)
} else {
proj = projConfig{}
proj.Scenes = []string{"index.json"}
proj.DefaultSceneIndex = 0
}
}
v := reflect.ValueOf(game).Elem()
g := instance(v)
g.setting = &proj

var conf Config
var scene sceneConfig
if gameConf != nil {
conf = *gameConf[0]
err = loadProjConfig(&proj, fs, conf.Index)
err = loadsceneConfig(g, &scene, fs, conf.Index)
} else {
err = loadProjConfig(&proj, fs, nil)
if proj.Run != nil { // load Config from index.json
conf = *proj.Run
err = loadsceneConfig(g, &scene, fs, proj.GetDefaultScene())
if scene.Run != nil { // load Config from index.json
conf = *scene.Run
}
}
if err != nil {
Expand Down Expand Up @@ -244,8 +262,6 @@ func Gopt_Game_Run(game Gamer, resource interface{}, gameConf ...*Config) {
}
}

v := reflect.ValueOf(game).Elem()
g := instance(v)
if debugLoad {
log.Println("==> StartLoad", resource)
}
Expand All @@ -266,7 +282,7 @@ func Gopt_Game_Run(game Gamer, resource interface{}, gameConf ...*Config) {
// p.sprs[name] = fld (has been set by loadSprite)
}
}
if err := g.endLoad(v, &proj); err != nil {
if err := g.endLoad(v, &scene); err != nil {
panic(err)
}

Expand Down Expand Up @@ -379,25 +395,25 @@ func spriteOf(sprite Spriter) *Sprite {
return vSpr.Field(0).Addr().Interface().(*Sprite)
}

func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
if backdrops := proj.getBackdrops(); len(backdrops) > 0 {
p.baseObj.initBackdrops("", backdrops, proj.getBackdropIndex())
p.worldWidth_ = proj.Map.Width
p.worldHeight_ = proj.Map.Height
func (p *Game) loadIndex(g reflect.Value, scene *sceneConfig) (err error) {
if backdrops := scene.getBackdrops(); len(backdrops) > 0 {
p.baseObj.initBackdrops("", backdrops, scene.getBackdropIndex())
p.worldWidth_ = scene.Map.Width
p.worldHeight_ = scene.Map.Height
p.doWorldSize() // set world size
} else {
p.worldWidth_ = proj.Map.Width
p.worldHeight_ = proj.Map.Height
p.worldWidth_ = scene.Map.Width
p.worldHeight_ = scene.Map.Height
p.baseObj.initWithSize(p.worldWidth_, p.worldHeight_)
}
if debugLoad {
log.Println("==> SetWorldSize", p.worldWidth_, p.worldHeight_)
}
p.world = ebiten.NewImage(p.worldWidth_, p.worldHeight_)
p.mapMode = toMapMode(proj.Map.Mode)
p.mapMode = toMapMode(scene.Map.Mode)

inits := make([]Spriter, 0, len(proj.Zorder))
for _, v := range proj.Zorder {
inits := make([]Spriter, 0, len(scene.Zorder))
for _, v := range scene.Zorder {
if name, ok := v.(string); ok {
sp := p.getSpriteProtoByName(name, g)
p.addShape(spriteOf(sp))
Expand Down Expand Up @@ -425,8 +441,8 @@ func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
p.Camera.init(p, float64(p.windowWidth_), float64(p.windowHeight_), float64(p.worldWidth_), float64(p.worldHeight_))

ebiten.SetWindowResizingMode(ebiten.WindowResizingModeOnlyFullscreenEnabled)
if proj.Camera != nil && proj.Camera.On != "" {
p.Camera.On(proj.Camera.On)
if scene.Camera != nil && scene.Camera.On != "" {
p.Camera.On(scene.Camera.On)
}
if loader, ok := g.Addr().Interface().(interface{ OnLoaded() }); ok {
loader.OnLoaded()
Expand All @@ -436,11 +452,11 @@ func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
return
}

func (p *Game) endLoad(g reflect.Value, proj *projConfig) (err error) {
func (p *Game) endLoad(g reflect.Value, scene *sceneConfig) (err error) {
if debugLoad {
log.Println("==> EndLoad")
}
return p.loadIndex(g, proj)
return p.loadIndex(g, scene)
}

func Gopt_Game_Reload(game Gamer, index interface{}) (err error) {
Expand All @@ -455,11 +471,11 @@ func Gopt_Game_Reload(game Gamer, index interface{}) (err error) {
}
}
}
var proj projConfig
if err = loadProjConfig(&proj, g.fs, index); err != nil {
var scene sceneConfig
if err = loadsceneConfig(g, &scene, g.fs, index); err != nil {
return
}
return g.loadIndex(v, &proj)
return g.loadIndex(v, &scene)
}

// -----------------------------------------------------------------------------
Expand Down
Loading