func (is *IntroState) buildScene() { am := is.assetManager // asset manager rootEntity := scenegraph.NewEntity(nil) rootEntity.W = gamecontext.GContext.MainSurface.W rootEntity.H = gamecontext.GContext.MainSurface.H titleEntity := scenegraph.NewEntity(am.Surfaces["titleText"]) mColor := is.fontNormalColor mHiColor := is.fontHighlightColor menuItems := []menu.Item{ {AssetFontID: "menuFont", Text: "Play!", Color: mColor, HiColor: mHiColor}, {AssetFontID: "menuFont", Text: "Quit", Color: mColor, HiColor: mHiColor}, } is.menu = menu.New(am, "introMenu", menuItems, 60, menu.MenuJustifyCenter) scenegraph.CenterEntityInParent(is.menu.RootEntity, rootEntity) is.menu.RootEntity.Y = 200 rootEntity.AddChild(titleEntity, is.menu.RootEntity) is.rootEntity = rootEntity // position title scenegraph.CenterEntityInSurface(titleEntity, gamecontext.GContext.MainSurface) titleEntity.Y = 40 }
// buildScene constructs the necessary elements for the scene func (ps *PlayState) buildScene() { var err error am := ps.assetManager // asset manager ps.rootEntity, err = scenegraph.LoadJSON(am, "playgraph.json", nil) if err != nil { panic(fmt.Sprintf("playgraph.json: %v", err)) } ps.nestEntity = ps.rootEntity.SearchByID("nest") ps.chixEntity = ps.rootEntity.SearchByID("chicken") ps.chixLeftEntity = ps.rootEntity.SearchByID("chickenLeftContainer") ps.chixRightEntity = ps.rootEntity.SearchByID("chickenRightContainer") ps.chixLegEntity = []*scenegraph.Entity{ ps.rootEntity.SearchByID("chickenLeftLegs0"), ps.rootEntity.SearchByID("chickenLeftLegs1"), ps.rootEntity.SearchByID("chickenRightLegs0"), ps.rootEntity.SearchByID("chickenRightLegs1"), } ps.interludeTextEntity = ps.rootEntity.SearchByID("interludeText") ps.eggContainer = ps.rootEntity.SearchByID("eggContainer") // This is hackish, but we need to know the width of the chicken, and // the chicken parent node is sizeless. So we copy the size from one of // the children. This should probably be an option in the JSON reader. ps.chixEntity.W = ps.rootEntity.SearchByID("chickenLeftImage").W // Pause menu stuff ps.buildPauseMenu() scenegraph.CenterEntityInParent(ps.menu.RootEntity, ps.rootEntity) ps.rootEntity.AddChild(ps.pauseMenuEntity) }
// constructInterludeImage builds the "LEVEL X" image func (ps *PlayState) constructInterludeImage() { surface, err := util.RenderText(ps.assetManager.Fonts["interludeFont"], fmt.Sprintf("LEVEL %d", ps.level), sdl.Color{R: 255, G: 255, B: 0, A: 255}) if err != nil { panic(fmt.Sprintf("Error constructing interlude text: %v", err)) } ps.interludeTextEntity.Surface = surface ps.interludeTextEntity.W = surface.W // hackishly make centering work on the next line scenegraph.CenterEntityInParent(ps.interludeTextEntity, ps.rootEntity) }
// New constructs the menu func New(am *assetmanager.AssetManager, id string, items []Item, spacing int32, justification int) *Menu { // TODO: do these even need to be registered with any asset manager? just use util.RenderText? menu := &Menu{items: items, spacing: spacing, justification: justification} menuEntity := scenegraph.NewEntity(nil) maxH := int32(0) maxW := int32(0) idNum := 0 for i, item := range menu.items { var err error var surface, surfaceHi *sdl.Surface var entity, entityHi *scenegraph.Entity if surface, err = am.RenderText(fmt.Sprintf("%s-%d", id, idNum), item.AssetFontID, item.Text, item.Color); err != nil { panic(fmt.Sprintf("Menu render font: %v", err)) } idNum++ if surfaceHi, err = am.RenderText(fmt.Sprintf("%s-%d", id, idNum), item.AssetFontID, item.Text, item.HiColor); err != nil { panic(fmt.Sprintf("Intro render font: %v", err)) } idNum++ entity = scenegraph.NewEntity(surface) entity.Visible = i > 0 entityHi = scenegraph.NewEntity(surfaceHi) entityHi.Visible = i == 0 menuEntity.AddChild(entity, entityHi) if entity.W > maxW { maxW = entity.W } entity.Y = maxH entityHi.Y = maxH maxH += menu.spacing } menuEntity.W = maxW menuEntity.H = maxH // position everything now that we have sizes known for i := range menu.items { entity := menuEntity.GetChild(i * 2) entityHi := menuEntity.GetChild(i*2 + 1) switch menu.justification { case MenuJustifyLeft: entity.X = 0 entityHi.X = 0 case MenuJustifyCenter: scenegraph.CenterEntityInParent(entity, menuEntity) scenegraph.CenterEntityInParent(entityHi, menuEntity) case MenuJustifyRight: scenegraph.RightJustifyEntityInParent(entity, menuEntity) scenegraph.RightJustifyEntityInParent(entityHi, menuEntity) } } menu.RootEntity = menuEntity return menu }