1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-17 01:51:14 -05:00
OpenDiablo2/OpenDiablo2.Core/GameEngine.cs

140 lines
4.8 KiB
C#
Raw Normal View History

using System;
2018-11-22 00:18:42 -05:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using OpenDiablo2.Common;
using OpenDiablo2.Common.Interfaces;
using OpenDiablo2.Common.Models;
2018-11-22 00:18:42 -05:00
namespace OpenDiablo2.Core
{
public sealed class GameEngine : IGameEngine, IPaletteProvider, ISceneManager
2018-11-22 00:18:42 -05:00
{
static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private readonly GlobalConfiguration globalConfig;
2018-11-22 00:18:42 -05:00
private readonly IMPQProvider mpqProvider;
private readonly Func<IRenderWindow> getRenderWindow;
private readonly Func<IMouseInfoProvider> getMouseInfoProvider;
2018-11-22 00:18:42 -05:00
private readonly Func<string, IScene> getScene;
private readonly Func<IResourceManager> getResourceManager;
private readonly Func<IGameState> getGameState;
2018-11-22 00:18:42 -05:00
private IScene currentScene;
private IScene nextScene = null;
private ISprite mouseSprite;
2018-11-22 00:18:42 -05:00
private readonly MPQ[] MPQs;
private Dictionary<string, SoundEntry> soundTable = new Dictionary<string, SoundEntry>();
public Dictionary<string, Palette> PaletteTable { get; private set; } = new Dictionary<string, Palette>();
private Stopwatch sw = new Stopwatch();
public GameEngine(
GlobalConfiguration globalConfig,
IMPQProvider mpqProvider,
Func<IRenderWindow> getRenderWindow,
Func<IMouseInfoProvider> getMouseInfoProvider,
Func<string, IScene> getScene,
Func<IResourceManager> getResourceManager,
Func<IGameState> getGameState
)
2018-11-22 00:18:42 -05:00
{
this.globalConfig = globalConfig;
2018-11-22 00:18:42 -05:00
this.mpqProvider = mpqProvider;
this.getRenderWindow = getRenderWindow;
this.getMouseInfoProvider = getMouseInfoProvider;
2018-11-22 00:18:42 -05:00
this.getScene = getScene;
this.getResourceManager = getResourceManager;
this.getGameState = getGameState;
2018-11-22 00:18:42 -05:00
MPQs = mpqProvider.GetMPQs().ToArray();
}
private void LoadPalettes()
{
log.Info("Loading palettes");
var paletteFiles = MPQs.SelectMany(x => x.Files).Where(x => x.StartsWith("data\\global\\palette\\") && x.EndsWith(".dat"));
foreach (var paletteFile in paletteFiles)
{
var paletteNameParts = paletteFile.Split('\\');
var paletteName = paletteNameParts[paletteNameParts.Count() - 2];
PaletteTable[paletteName] = getResourceManager().GetPalette(paletteFile);
2018-11-22 00:18:42 -05:00
}
}
private void LoadSoundData()
{
log.Info("Loading sound configuration data");
2018-11-24 17:54:15 -05:00
var soundDescFile = mpqProvider.GetTextFile("data\\global\\excel\\Sounds.txt");
foreach (var row in soundDescFile.Skip(1).Where(x => !String.IsNullOrWhiteSpace(x)))
2018-11-22 00:18:42 -05:00
{
2018-11-24 17:54:15 -05:00
var soundEntry = row.ToSoundEntry();
soundTable[soundEntry.Handle] = soundEntry;
2018-11-22 00:18:42 -05:00
}
2018-11-24 17:54:15 -05:00
2018-11-22 00:18:42 -05:00
}
public void Run()
{
var renderWindow = getRenderWindow();
var mouseInfoProvider = getMouseInfoProvider();
2018-11-22 00:18:42 -05:00
LoadPalettes();
LoadSoundData();
mouseSprite = renderWindow.LoadSprite(ResourcePaths.CursorDefault, Palettes.Units);
IMouseCursor cursor;
if (globalConfig.MouseMode == eMouseMode.Hardware)
{
cursor = renderWindow.LoadCursor(mouseSprite, 0, new Point(0, 0));
renderWindow.SetCursor(cursor);
}
2018-11-22 00:18:42 -05:00
currentScene = getScene("Main Menu");
sw.Start();
while (getRenderWindow().IsRunning)
2018-11-22 00:18:42 -05:00
{
var ms = sw.ElapsedMilliseconds;
sw.Restart();
2018-11-22 00:18:42 -05:00
// Prevent falco-punch updates
if (ms > 1000)
{
sw.Restart();
continue;
}
getGameState().Update(ms);
getRenderWindow().Update();
2018-11-22 00:18:42 -05:00
currentScene.Update(ms);
if (nextScene!= null)
{
currentScene = nextScene;
nextScene = null;
continue;
}
renderWindow.Clear();
2018-11-22 00:18:42 -05:00
currentScene.Render();
if (globalConfig.MouseMode == eMouseMode.Software)
renderWindow.Draw(mouseSprite, new Point(mouseInfoProvider.MouseX, mouseInfoProvider.MouseY + 3));
renderWindow.Sync();
2018-11-22 00:18:42 -05:00
}
}
public void Dispose()
{
currentScene?.Dispose();
2018-11-22 00:18:42 -05:00
}
public void ChangeScene(string sceneName)
=> nextScene = getScene(sceneName);
2018-11-22 00:18:42 -05:00
}
}