OpenDiablo2/d2game/d2gamescreen/credits.go

296 lines
6.6 KiB
Go
Raw Normal View History

package d2gamescreen
2019-10-26 04:26:48 +00:00
import (
"bufio"
"fmt"
"log"
"os"
"path"
2019-10-26 04:55:59 +00:00
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
2020-02-09 02:02:37 +00:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
2020-09-12 20:25:09 +00:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2screen"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
2019-10-26 04:26:48 +00:00
)
const (
creditsX, creditsY = 0, 0
charSelExitBtnX, charSelExitBtnY = 33, 543
)
const secondsPerCycle float64 = 0.02
type labelItem struct {
Label *d2ui.Label
IsHeading bool
Available bool
}
// Credits represents the credits screen
2019-10-26 04:26:48 +00:00
type Credits struct {
creditsBackground *d2ui.Sprite
exitButton *d2ui.Button
creditsText []string
labels []*labelItem
cycleTime float64
cyclesTillNextLine int
doneWithCredits bool
remove d2asset singleton (#726) * export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
2020-09-12 20:51:30 +00:00
asset *d2asset.AssetManager
renderer d2interface.Renderer
navigator d2interface.Navigator
uiManager *d2ui.UIManager
2019-10-26 04:26:48 +00:00
}
// CreateCredits creates an instance of the credits screen
func CreateCredits(navigator d2interface.Navigator, asset *d2asset.AssetManager, renderer d2interface.Renderer,
remove d2asset singleton (#726) * export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
2020-09-12 20:51:30 +00:00
ui *d2ui.UIManager) *Credits {
2019-10-26 04:26:48 +00:00
result := &Credits{
remove d2asset singleton (#726) * export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
2020-09-12 20:51:30 +00:00
asset: asset,
labels: make([]*labelItem, 0),
cycleTime: 0,
doneWithCredits: false,
cyclesTillNextLine: 0,
renderer: renderer,
navigator: navigator,
uiManager: ui,
2019-10-26 04:26:48 +00:00
}
2019-10-26 04:26:48 +00:00
return result
}
// LoadContributors loads the contributors data from file
func (v *Credits) LoadContributors() []string {
file, err := os.Open(path.Join("./", "CONTRIBUTORS"))
if err != nil || file == nil {
2019-11-15 04:55:51 +00:00
log.Print("CONTRIBUTORS file is missing")
2020-02-09 02:02:37 +00:00
return []string{"MISSING CONTRIBUTOR FILES!"}
}
defer func() {
if err = file.Close(); err != nil {
fmt.Printf("an error occurred while closing file: %s, err: %q\n", file.Name(), err)
}
}()
scanner := bufio.NewScanner(file)
var contributors []string
for scanner.Scan() {
contributors = append(contributors, strings.Trim(scanner.Text(), " "))
}
return contributors
}
// OnLoad is called to load the resources for the credits screen
func (v *Credits) OnLoad(loading d2screen.LoadingState) {
var err error
v.creditsBackground, err = v.uiManager.NewSprite(d2resource.CreditsBackground, d2resource.PaletteSky)
if err != nil {
log.Print(err)
}
v.creditsBackground.SetPosition(creditsX, creditsY)
loading.Progress(twentyPercent)
2020-02-09 02:02:37 +00:00
v.exitButton = v.uiManager.NewButton(d2ui.ButtonTypeMedium, "EXIT")
v.exitButton.SetPosition(charSelExitBtnX, charSelExitBtnY)
2020-02-09 02:02:37 +00:00
v.exitButton.OnActivated(func() { v.onExitButtonClicked() })
loading.Progress(fourtyPercent)
2020-02-09 02:02:37 +00:00
remove d2asset singleton (#726) * export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
2020-09-12 20:51:30 +00:00
fileData, err := v.asset.LoadFile(d2resource.CreditsText)
2020-02-09 02:02:37 +00:00
if err != nil {
loading.Error(err)
return
2019-10-26 04:26:48 +00:00
}
loading.Progress(sixtyPercent)
creditData, err := d2util.Utf16BytesToString(fileData[2:])
if err != nil {
log.Print(err)
}
2020-02-09 02:02:37 +00:00
v.creditsText = strings.Split(creditData, "\r\n")
2020-02-09 02:02:37 +00:00
for i := range v.creditsText {
v.creditsText[i] = strings.Trim(v.creditsText[i], " ")
}
loading.Progress(eightyPercent)
2020-02-09 02:02:37 +00:00
v.creditsText = append(v.LoadContributors(), v.creditsText...)
2019-10-26 04:26:48 +00:00
}
// Render renders the credits screen
func (v *Credits) Render(screen d2interface.Surface) {
err := v.creditsBackground.RenderSegmented(screen, 4, 3, 0)
if err != nil {
return
}
for _, label := range v.labels {
if label.Available {
continue
}
label.Label.Render(screen)
}
2019-10-26 04:26:48 +00:00
}
// Advance runs the update logic on the credits screen
2020-02-09 02:02:37 +00:00
func (v *Credits) Advance(tickTime float64) error {
2019-10-26 14:34:55 +00:00
v.cycleTime += tickTime
for v.cycleTime >= secondsPerCycle {
v.cycleTime -= secondsPerCycle
v.cyclesTillNextLine--
if !v.doneWithCredits && v.cyclesTillNextLine <= 0 {
v.addNextItem()
}
2019-10-26 04:26:48 +00:00
for _, label := range v.labels {
if label.Available {
continue
}
if label.Label.Y-1 < -15 {
label.Available = true
continue
}
label.Label.Y--
}
}
2020-02-09 02:02:37 +00:00
return nil
2019-10-26 04:26:48 +00:00
}
2019-10-26 04:55:59 +00:00
func (v *Credits) onExitButtonClicked() {
v.navigator.ToMainMenu()
2019-10-26 04:55:59 +00:00
}
func (v *Credits) addNextItem() {
if len(v.creditsText) == 0 {
v.doneWithCredits = true
return
}
2019-10-26 14:34:55 +00:00
text := v.creditsText[0]
v.creditsText = v.creditsText[1:]
if text == "" {
if v.creditsText[0][0] == '*' {
v.cyclesTillNextLine = 38
return
}
v.cyclesTillNextLine = 19
return
}
isHeading := text[0] == '*'
2019-10-26 14:34:55 +00:00
isNextHeading := len(v.creditsText) > 0 && len(v.creditsText[0]) > 0 && v.creditsText[0][0] == '*'
isNextSpace := len(v.creditsText) > 0 && v.creditsText[0] == ""
var label = v.getNewFontLabel(isHeading)
if isHeading {
label.SetText(text[1:])
} else {
label.SetText(text)
}
isDoubled, isNextHeading := v.setItemLabelPosition(label, isHeading, isNextHeading, isNextSpace)
switch {
case isHeading && isNextHeading:
v.cyclesTillNextLine = 38
case isNextHeading:
if isDoubled {
v.cyclesTillNextLine = 38
} else {
v.cyclesTillNextLine = 57
}
case isHeading:
v.cyclesTillNextLine = 38
default:
v.cyclesTillNextLine = 19
}
}
const (
itemLabelY = 605
itemLabelX = 400
itemLabel2offsetX = 10
halfItemLabel2offsetX = itemLabel2offsetX / 2
)
func (v *Credits) setItemLabelPosition(label *d2ui.Label, isHeading, isNextHeading, isNextSpace bool) (isDoubled, nextHeading bool) {
2019-10-26 14:34:55 +00:00
width, _ := label.GetSize()
half := 2
halfWidth := width / half
if !isHeading && !isNextHeading && !isNextSpace {
isDoubled = true
// Gotta go side by side
label.SetPosition(itemLabelX-width, itemLabelY)
2019-10-26 14:34:55 +00:00
text2 := v.creditsText[0]
v.creditsText = v.creditsText[1:]
nextHeading = len(v.creditsText) > 0 && len(v.creditsText[0]) > 0 && v.creditsText[0][0] == '*'
label2 := v.getNewFontLabel(isHeading)
label2.SetText(text2)
label2.SetPosition(itemLabelX+itemLabel2offsetX, itemLabelY)
return isDoubled, nextHeading
}
label.SetPosition(itemLabelX+halfItemLabel2offsetX-halfWidth, itemLabelY)
return isDoubled, isNextHeading
}
const (
lightRed = 0xff5852ff
beige = 0xc6b296ff
)
2019-11-10 13:51:02 +00:00
func (v *Credits) getNewFontLabel(isHeading bool) *d2ui.Label {
for _, label := range v.labels {
if label.Available {
label.Available = false
2019-10-26 14:34:55 +00:00
if isHeading {
label.Label.Color[0] = rgbaColor(lightRed)
2019-10-26 14:34:55 +00:00
} else {
label.Label.Color[0] = rgbaColor(beige)
2019-10-26 14:34:55 +00:00
}
return label.Label
}
}
newLabelItem := &labelItem{
Available: false,
IsHeading: isHeading,
Label: v.uiManager.NewLabel(d2resource.FontFormal10, d2resource.PaletteSky),
}
if isHeading {
newLabelItem.Label.Color[0] = rgbaColor(lightRed)
} else {
newLabelItem.Label.Color[0] = rgbaColor(beige)
}
v.labels = append(v.labels, newLabelItem)
return newLabelItem.Label
}