2019-11-06 22:12:15 -05:00
|
|
|
package core
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2019-10-25 20:28:14 -04:00
|
|
|
"math"
|
2019-10-24 09:31:59 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/mpq"
|
2019-11-06 18:25:19 -05:00
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/palettedefs"
|
2019-11-01 14:12:23 -04:00
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/sound"
|
2019-10-25 20:28:14 -04:00
|
|
|
|
2019-11-06 22:12:15 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/common"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/resourcepaths"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/scenes"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/ui"
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
)
|
|
|
|
|
2019-10-25 18:40:27 -04:00
|
|
|
// Engine is the core OpenDiablo2 engine
|
2019-10-24 09:31:59 -04:00
|
|
|
type Engine struct {
|
2019-11-08 01:37:21 -05:00
|
|
|
Settings *common.Configuration // Engine configuration settings from json file
|
|
|
|
Files map[string]string // Map that defines which files are in which MPQs
|
|
|
|
CheckedPatch map[string]bool // First time we check a file, we'll check if it's in the patch. This notes that we've already checked that.
|
|
|
|
LoadingSprite *common.Sprite // The sprite shown when loading stuff
|
|
|
|
loadingProgress float64 // LoadingProcess is a range between 0.0 and 1.0. If set, loading screen displays.
|
2019-11-08 11:05:51 -05:00
|
|
|
loadingIndex int // Determines which load function is currently being called
|
|
|
|
thingsToLoad []func() // The load functions for the next scene
|
2019-11-08 01:37:21 -05:00
|
|
|
stepLoadingSize float64 // The size for each loading step
|
|
|
|
CurrentScene scenes.Scene // The current scene being rendered
|
|
|
|
UIManager *ui.Manager // The UI manager
|
|
|
|
SoundManager *sound.Manager // The sound manager
|
|
|
|
nextScene scenes.Scene // The next scene to be loaded at the end of the game loop
|
|
|
|
fullscreenKey bool // When true, the fullscreen toggle is still being pressed
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEngine creates and instance of the OpenDiablo2 engine
|
|
|
|
func CreateEngine() *Engine {
|
|
|
|
result := &Engine{
|
2019-10-25 20:28:14 -04:00
|
|
|
CurrentScene: nil,
|
|
|
|
nextScene: nil,
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
result.loadConfigurationFile()
|
2019-11-06 22:12:15 -05:00
|
|
|
resourcepaths.LanguageCode = result.Settings.Language
|
2019-10-24 09:31:59 -04:00
|
|
|
result.mapMpqFiles()
|
2019-11-06 22:12:15 -05:00
|
|
|
common.LoadPalettes(result.Files, result)
|
|
|
|
common.LoadTextDictionary(result)
|
|
|
|
common.LoadLevelTypes(result)
|
|
|
|
common.LoadLevelPresets(result)
|
|
|
|
common.LoadLevelWarps(result)
|
|
|
|
common.LoadObjectTypes(result)
|
|
|
|
common.LoadObjects(result)
|
|
|
|
common.LoadWeapons(result)
|
2019-11-07 20:51:15 -05:00
|
|
|
common.LoadArmors(result)
|
2019-11-06 23:32:02 -05:00
|
|
|
common.LoadUniqueItems(result)
|
2019-11-06 22:12:15 -05:00
|
|
|
common.LoadMissiles(result)
|
|
|
|
common.LoadSounds(result)
|
|
|
|
common.LoadObjectLookups()
|
|
|
|
common.LoadAnimationData(result)
|
2019-11-07 23:44:03 -05:00
|
|
|
common.LoadMonStats(result)
|
2019-11-06 22:12:15 -05:00
|
|
|
result.SoundManager = sound.CreateManager(result)
|
2019-10-26 20:09:33 -04:00
|
|
|
result.SoundManager.SetVolumes(result.Settings.BgmVolume, result.Settings.SfxVolume)
|
2019-11-06 22:12:15 -05:00
|
|
|
result.UIManager = ui.CreateManager(result, *result.SoundManager)
|
|
|
|
result.LoadingSprite = result.LoadSprite(resourcepaths.LoadingScreen, palettedefs.Loading)
|
2019-10-24 09:31:59 -04:00
|
|
|
loadingSpriteSizeX, loadingSpriteSizeY := result.LoadingSprite.GetSize()
|
|
|
|
result.LoadingSprite.MoveTo(int(400-(loadingSpriteSizeX/2)), int(300+(loadingSpriteSizeY/2)))
|
2019-11-06 22:12:15 -05:00
|
|
|
result.SetNextScene(scenes.CreateMainMenu(result, result, result.UIManager, result.SoundManager))
|
2019-10-31 21:17:13 -04:00
|
|
|
//result.SetNextScene(Scenes.CreateBlizzardIntro(result, result))
|
2019-10-24 09:31:59 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) loadConfigurationFile() {
|
2019-10-30 01:09:39 -04:00
|
|
|
log.Println("Loading configuration file")
|
2019-11-08 01:37:21 -05:00
|
|
|
v.Settings = common.LoadConfiguration()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2019-11-06 18:25:19 -05:00
|
|
|
func (v *Engine) mapMpqFiles() {
|
|
|
|
v.Files = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
var mutex sync.Mutex
|
|
|
|
|
|
|
|
func (v *Engine) LoadFile(fileName string) []byte {
|
2019-11-06 22:12:15 -05:00
|
|
|
fileName = strings.ReplaceAll(fileName, "{LANG}", resourcepaths.LanguageCode)
|
2019-11-06 18:25:19 -05:00
|
|
|
fileName = strings.ToLower(fileName)
|
|
|
|
fileName = strings.ReplaceAll(fileName, `/`, "\\")
|
|
|
|
if fileName[0] == '\\' {
|
|
|
|
fileName = fileName[1:]
|
|
|
|
}
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
// TODO: May want to cache some things if performance becomes an issue
|
|
|
|
cachedMpqFile, cacheExists := v.Files[fileName]
|
|
|
|
if cacheExists {
|
2019-11-07 23:36:56 -05:00
|
|
|
archive, _ := mpq.Load(cachedMpqFile)
|
|
|
|
result, _ := archive.ReadFile(fileName)
|
2019-11-06 18:25:19 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
for _, mpqFile := range v.Settings.MpqLoadOrder {
|
2019-11-07 23:36:56 -05:00
|
|
|
archive, _ := mpq.Load(path.Join(v.Settings.MpqPath, mpqFile))
|
|
|
|
if !archive.FileExists(fileName) {
|
2019-11-06 18:25:19 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-11-07 23:36:56 -05:00
|
|
|
result, _ := archive.ReadFile(fileName)
|
2019-11-07 17:26:36 -05:00
|
|
|
if len(result) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.Files[fileName] = path.Join(v.Settings.MpqPath, mpqFile)
|
2019-11-06 18:25:19 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
log.Fatalf("Could not load %s from MPQs", fileName)
|
|
|
|
return []byte{}
|
|
|
|
}
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
// IsLoading returns true if the engine is currently in a loading state
|
|
|
|
func (v *Engine) IsLoading() bool {
|
2019-10-25 20:28:14 -04:00
|
|
|
return v.loadingProgress < 1.0
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadSprite loads a sprite from the game's data files
|
2019-11-06 22:12:15 -05:00
|
|
|
func (v *Engine) LoadSprite(fileName string, palette palettedefs.PaletteType) *common.Sprite {
|
2019-10-25 19:37:04 -04:00
|
|
|
data := v.LoadFile(fileName)
|
2019-11-06 22:12:15 -05:00
|
|
|
sprite := common.CreateSprite(data, common.Palettes[palette])
|
2019-10-24 09:31:59 -04:00
|
|
|
return sprite
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateScene handles the scene maintenance for the engine
|
|
|
|
func (v *Engine) updateScene() {
|
|
|
|
if v.nextScene == nil {
|
2019-11-08 11:05:51 -05:00
|
|
|
if v.thingsToLoad != nil {
|
|
|
|
if v.loadingIndex < len(v.thingsToLoad) {
|
|
|
|
v.thingsToLoad[v.loadingIndex]()
|
|
|
|
v.loadingIndex++
|
|
|
|
if v.loadingIndex < len(v.thingsToLoad) {
|
|
|
|
v.StepLoading()
|
|
|
|
} else {
|
|
|
|
v.FinishLoading()
|
|
|
|
v.thingsToLoad = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 09:31:59 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if v.CurrentScene != nil {
|
|
|
|
v.CurrentScene.Unload()
|
|
|
|
}
|
|
|
|
v.CurrentScene = v.nextScene
|
|
|
|
v.nextScene = nil
|
2019-10-25 19:12:42 -04:00
|
|
|
v.UIManager.Reset()
|
2019-11-08 11:05:51 -05:00
|
|
|
v.thingsToLoad = v.CurrentScene.Load()
|
|
|
|
v.loadingIndex = 0
|
|
|
|
v.SetLoadingStepSize(1.0 / float64(len(v.thingsToLoad)))
|
2019-10-25 20:28:14 -04:00
|
|
|
v.ResetLoading()
|
2019-10-25 15:06:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
// Update updates the internal state of the engine
|
|
|
|
func (v *Engine) Update() {
|
2019-10-25 17:15:44 -04:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyAlt) && ebiten.IsKeyPressed(ebiten.KeyEnter) {
|
|
|
|
if !v.fullscreenKey {
|
|
|
|
ebiten.SetFullscreen(!ebiten.IsFullscreen())
|
|
|
|
}
|
|
|
|
v.fullscreenKey = true
|
|
|
|
} else {
|
|
|
|
v.fullscreenKey = false
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
v.updateScene()
|
|
|
|
if v.CurrentScene == nil {
|
2019-10-25 15:06:47 -04:00
|
|
|
log.Fatal("no scene loaded")
|
|
|
|
}
|
2019-10-25 19:12:42 -04:00
|
|
|
|
|
|
|
if v.IsLoading() {
|
|
|
|
return
|
|
|
|
}
|
2019-10-25 20:28:14 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
v.CurrentScene.Update(float64(1) / ebiten.CurrentTPS())
|
2019-10-25 19:12:42 -04:00
|
|
|
v.UIManager.Update()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws the game
|
|
|
|
func (v *Engine) Draw(screen *ebiten.Image) {
|
2019-10-25 20:28:14 -04:00
|
|
|
if v.loadingProgress < 1.0 {
|
2019-11-06 22:12:15 -05:00
|
|
|
v.LoadingSprite.Frame = uint8(common.Max(0, common.Min(uint32(len(v.LoadingSprite.Frames)-1), uint32(float64(len(v.LoadingSprite.Frames)-1)*v.loadingProgress))))
|
2019-10-24 09:31:59 -04:00
|
|
|
v.LoadingSprite.Draw(screen)
|
|
|
|
} else {
|
|
|
|
if v.CurrentScene == nil {
|
2019-10-25 15:06:47 -04:00
|
|
|
log.Fatal("no scene loaded")
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
v.CurrentScene.Render(screen)
|
2019-10-25 19:12:42 -04:00
|
|
|
v.UIManager.Draw(screen)
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNextScene tells the engine what scene to load on the next update cycle
|
2019-11-06 22:12:15 -05:00
|
|
|
func (v *Engine) SetNextScene(nextScene scenes.Scene) {
|
2019-10-24 09:31:59 -04:00
|
|
|
v.nextScene = nextScene
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:28:14 -04:00
|
|
|
// SetLoadingStepSize sets the size of the loading step
|
|
|
|
func (v *Engine) SetLoadingStepSize(size float64) {
|
|
|
|
v.stepLoadingSize = size
|
|
|
|
}
|
2019-10-25 15:06:47 -04:00
|
|
|
|
2019-10-25 20:28:14 -04:00
|
|
|
// ResetLoading resets the loading progress
|
|
|
|
func (v *Engine) ResetLoading() {
|
|
|
|
v.loadingProgress = 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
// StepLoading increments the loading progress
|
|
|
|
func (v *Engine) StepLoading() {
|
|
|
|
v.loadingProgress = math.Min(1.0, v.loadingProgress+v.stepLoadingSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinishLoading terminates the loading phase
|
|
|
|
func (v *Engine) FinishLoading() {
|
|
|
|
v.loadingProgress = 1.0
|
2019-10-25 15:06:47 -04:00
|
|
|
}
|