2018-11-28 19:28:41 -05:00
|
|
|
|
using System;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
2018-11-29 17:58:56 -05:00
|
|
|
|
using System.Threading;
|
2018-11-28 19:28:41 -05:00
|
|
|
|
using OpenDiablo2.Common;
|
2018-12-08 09:12:40 -05:00
|
|
|
|
using OpenDiablo2.Common.Enums;
|
2018-11-28 19:28:41 -05:00
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
|
|
|
|
using OpenDiablo2.Common.Models;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.Core
|
|
|
|
|
{
|
2018-11-22 20:23:00 -05:00
|
|
|
|
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 IMPQProvider mpqProvider;
|
2018-11-22 14:30:37 -05:00
|
|
|
|
private readonly Func<IRenderWindow> getRenderWindow;
|
2018-12-08 09:12:40 -05:00
|
|
|
|
private readonly Func<eSceneType, IScene> getScene;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
private readonly Func<IResourceManager> getResourceManager;
|
2018-11-28 19:28:41 -05:00
|
|
|
|
private readonly Func<IGameState> getGameState;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
private IScene currentScene;
|
2018-11-22 20:23:00 -05:00
|
|
|
|
private IScene nextScene = null;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
private readonly MPQ[] MPQs;
|
|
|
|
|
|
2018-12-08 09:32:09 -05:00
|
|
|
|
private readonly Dictionary<string, SoundEntry> soundTable = new Dictionary<string, SoundEntry>();
|
2018-11-22 00:18:42 -05:00
|
|
|
|
public Dictionary<string, Palette> PaletteTable { get; private set; } = new Dictionary<string, Palette>();
|
2018-12-05 22:29:45 -05:00
|
|
|
|
|
2018-11-22 16:40:13 -05:00
|
|
|
|
public GameEngine(
|
|
|
|
|
IMPQProvider mpqProvider,
|
|
|
|
|
Func<IRenderWindow> getRenderWindow,
|
2018-12-08 09:12:40 -05:00
|
|
|
|
Func<eSceneType, IScene> getScene,
|
2018-11-28 19:28:41 -05:00
|
|
|
|
Func<IResourceManager> getResourceManager,
|
|
|
|
|
Func<IGameState> getGameState
|
2018-11-22 16:40:13 -05:00
|
|
|
|
)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
{
|
|
|
|
|
this.mpqProvider = mpqProvider;
|
2018-11-22 14:30:37 -05:00
|
|
|
|
this.getRenderWindow = getRenderWindow;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
this.getScene = getScene;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
this.getResourceManager = getResourceManager;
|
2018-11-28 19:28:41 -05:00
|
|
|
|
this.getGameState = getGameState;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadPalettes()
|
|
|
|
|
{
|
|
|
|
|
log.Info("Loading palettes");
|
2018-12-08 19:21:52 -05:00
|
|
|
|
var paletteFiles = mpqProvider.SelectMany(x => x.Files).Where(x => x.StartsWith("data\\global\\palette\\") && x.EndsWith(".dat"));
|
2018-11-22 00:18:42 -05:00
|
|
|
|
foreach (var paletteFile in paletteFiles)
|
|
|
|
|
{
|
|
|
|
|
var paletteNameParts = paletteFile.Split('\\');
|
|
|
|
|
var paletteName = paletteNameParts[paletteNameParts.Count() - 2];
|
2018-11-22 22:53:05 -05:00
|
|
|
|
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()
|
|
|
|
|
{
|
2018-11-22 16:40:13 -05:00
|
|
|
|
var renderWindow = getRenderWindow();
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
LoadPalettes();
|
|
|
|
|
LoadSoundData();
|
|
|
|
|
|
2018-12-08 13:31:50 -05:00
|
|
|
|
var mouseSprite = renderWindow.LoadSprite(ResourcePaths.CursorDefault, Palettes.Units);
|
2018-11-29 23:26:51 -05:00
|
|
|
|
var cursor = renderWindow.LoadCursor(mouseSprite, 0, new Point(0, 3));
|
|
|
|
|
renderWindow.MouseCursor = cursor;
|
|
|
|
|
|
2018-12-08 09:12:40 -05:00
|
|
|
|
currentScene = getScene(eSceneType.MainMenu);
|
2018-11-29 17:58:56 -05:00
|
|
|
|
var lastTicks = renderWindow.GetTicks();
|
2018-11-22 14:30:37 -05:00
|
|
|
|
while (getRenderWindow().IsRunning)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
{
|
2018-11-29 17:58:56 -05:00
|
|
|
|
var curTicks = renderWindow.GetTicks();
|
|
|
|
|
var ms = curTicks - lastTicks;
|
|
|
|
|
|
|
|
|
|
if (ms < 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ms < 33)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(33 - (int)ms);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
// Prevent falco-punch updates
|
|
|
|
|
if (ms > 1000)
|
|
|
|
|
{
|
2018-11-29 17:58:56 -05:00
|
|
|
|
lastTicks = renderWindow.GetTicks();
|
2018-11-22 00:18:42 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-11-28 19:28:41 -05:00
|
|
|
|
|
2018-11-29 17:58:56 -05:00
|
|
|
|
lastTicks = curTicks;
|
|
|
|
|
|
2018-12-01 00:00:17 -05:00
|
|
|
|
lock (getGameState().ThreadLocker)
|
2018-11-22 20:23:00 -05:00
|
|
|
|
{
|
2018-12-01 00:00:17 -05:00
|
|
|
|
getGameState().Update(ms);
|
|
|
|
|
getRenderWindow().Update();
|
|
|
|
|
currentScene.Update(ms);
|
2018-11-22 16:40:13 -05:00
|
|
|
|
|
2018-12-01 00:00:17 -05:00
|
|
|
|
if (nextScene!= null)
|
|
|
|
|
{
|
|
|
|
|
currentScene = nextScene;
|
|
|
|
|
nextScene = null;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-11-22 16:40:13 -05:00
|
|
|
|
|
2018-12-01 00:00:17 -05:00
|
|
|
|
renderWindow.Clear();
|
|
|
|
|
currentScene.Render();
|
|
|
|
|
|
|
|
|
|
renderWindow.Sync();
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2018-11-22 20:23:00 -05:00
|
|
|
|
currentScene?.Dispose();
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
2018-11-22 20:23:00 -05:00
|
|
|
|
|
2018-12-08 09:12:40 -05:00
|
|
|
|
public void ChangeScene(eSceneType sceneType)
|
|
|
|
|
=> nextScene = getScene(sceneType);
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|