2018-12-08 16:08:59 -05:00
|
|
|
|
/* OpenDiablo 2 - An open source re-implementation of Diablo 2 in C#
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-12-08 18:02:54 -05:00
|
|
|
|
using OpenDiablo2.Common;
|
2018-11-24 23:52:23 -05:00
|
|
|
|
using OpenDiablo2.Common.Attributes;
|
|
|
|
|
using OpenDiablo2.Common.Enums;
|
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
2018-12-08 16:08:59 -05:00
|
|
|
|
using System;
|
2018-11-24 23:52:23 -05:00
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.Scenes
|
|
|
|
|
{
|
2018-12-08 09:12:40 -05:00
|
|
|
|
[Scene(eSceneType.Game)]
|
2018-11-24 23:52:23 -05:00
|
|
|
|
public sealed class Game : IScene
|
|
|
|
|
{
|
|
|
|
|
private readonly IRenderWindow renderWindow;
|
|
|
|
|
private readonly IMapEngine mapEngine;
|
2018-12-01 15:22:55 -05:00
|
|
|
|
private readonly IMouseInfoProvider mouseInfoProvider;
|
2018-11-24 23:52:23 -05:00
|
|
|
|
private readonly IGameState gameState;
|
2018-12-01 15:22:55 -05:00
|
|
|
|
private readonly ISessionManager sessionManager;
|
2018-12-08 11:09:56 -05:00
|
|
|
|
private readonly IGameHUD gameHUD;
|
2018-11-24 23:52:23 -05:00
|
|
|
|
|
2018-12-01 15:22:55 -05:00
|
|
|
|
private eMovementType lastMovementType = eMovementType.Stopped;
|
2018-12-01 20:17:51 -05:00
|
|
|
|
private byte lastDirection = 255;
|
2018-12-01 15:22:55 -05:00
|
|
|
|
|
|
|
|
|
const double Rad2Deg = 180.0 / Math.PI;
|
2018-11-24 23:52:23 -05:00
|
|
|
|
|
|
|
|
|
public Game(
|
2018-11-24 23:33:04 -05:00
|
|
|
|
IRenderWindow renderWindow,
|
|
|
|
|
IMapEngine mapEngine,
|
2018-11-24 23:52:23 -05:00
|
|
|
|
IGameState gameState,
|
2018-12-01 15:22:55 -05:00
|
|
|
|
IMouseInfoProvider mouseInfoProvider,
|
2018-12-02 16:34:00 -05:00
|
|
|
|
IItemManager itemManager,
|
2018-12-01 15:22:55 -05:00
|
|
|
|
ISessionManager sessionManager,
|
2018-12-08 18:02:54 -05:00
|
|
|
|
ISoundProvider soundProvider,
|
|
|
|
|
IMPQProvider mpqProvider,
|
2018-12-08 11:09:56 -05:00
|
|
|
|
IGameHUD gameHUD
|
2018-11-24 23:52:23 -05:00
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
this.renderWindow = renderWindow;
|
|
|
|
|
this.mapEngine = mapEngine;
|
|
|
|
|
this.gameState = gameState;
|
2018-12-01 15:22:55 -05:00
|
|
|
|
this.mouseInfoProvider = mouseInfoProvider;
|
|
|
|
|
this.sessionManager = sessionManager;
|
2018-12-08 11:09:56 -05:00
|
|
|
|
this.gameHUD = gameHUD;
|
2018-11-24 23:52:23 -05:00
|
|
|
|
|
2018-12-08 18:02:54 -05:00
|
|
|
|
// TODO: Dynamic based on actual location
|
|
|
|
|
soundProvider.StopSong();
|
|
|
|
|
soundProvider.LoadSong(mpqProvider.GetStream(ResourcePaths.BGMTown1));
|
|
|
|
|
soundProvider.PlaySong();
|
2018-12-08 09:32:09 -05:00
|
|
|
|
//var item = itemManager.getItem("hdm");
|
2018-11-24 23:52:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Render()
|
|
|
|
|
{
|
2018-12-01 12:10:16 -05:00
|
|
|
|
// TODO: Maybe show some sort of connecting/loading message?
|
|
|
|
|
if (mapEngine.FocusedPlayerId == 0)
|
|
|
|
|
return;
|
2018-11-24 23:52:23 -05:00
|
|
|
|
|
|
|
|
|
mapEngine.Render();
|
2018-12-08 11:09:56 -05:00
|
|
|
|
gameHUD.Render();
|
2018-11-24 23:52:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(long ms)
|
|
|
|
|
{
|
2018-12-01 15:22:55 -05:00
|
|
|
|
HandleMovement();
|
|
|
|
|
|
|
|
|
|
mapEngine.Update(ms);
|
2018-12-08 11:09:56 -05:00
|
|
|
|
gameHUD.Update();
|
2018-12-01 15:22:55 -05:00
|
|
|
|
}
|
2018-11-24 23:52:23 -05:00
|
|
|
|
|
2018-12-01 15:22:55 -05:00
|
|
|
|
private void HandleMovement()
|
|
|
|
|
{
|
2018-12-08 11:09:56 -05:00
|
|
|
|
// todo; if clicked on hud, then we don't move. But when clicked on map and move cursor over hud, then it's fine
|
|
|
|
|
if (gameHUD.IsMouseOver())
|
2018-12-01 15:22:55 -05:00
|
|
|
|
return;
|
|
|
|
|
|
2018-12-08 13:11:52 -05:00
|
|
|
|
var mx = mouseInfoProvider.MouseX - 400 - gameState.CameraOffset;
|
|
|
|
|
var my = mouseInfoProvider.MouseY - 300;
|
2018-12-01 20:17:51 -05:00
|
|
|
|
|
|
|
|
|
var tx = (mx / 60f + my / 40f) / 2f;
|
|
|
|
|
var ty = (my / 40f - (mx / 60f)) / 2f;
|
|
|
|
|
var cursorDirection = (int)Math.Round(Math.Atan2(ty, tx) * Rad2Deg);
|
|
|
|
|
if (cursorDirection < 0)
|
|
|
|
|
cursorDirection += 360;
|
|
|
|
|
var actualDirection = (byte)(cursorDirection / 22);
|
2018-12-08 01:01:30 -05:00
|
|
|
|
if (actualDirection >= 16)
|
|
|
|
|
actualDirection -= 16;
|
2018-12-01 15:22:55 -05:00
|
|
|
|
|
2018-12-01 20:17:51 -05:00
|
|
|
|
if (mouseInfoProvider.LeftMouseDown && (lastMovementType == eMovementType.Stopped || lastDirection != actualDirection))
|
2018-11-24 23:52:23 -05:00
|
|
|
|
{
|
2018-12-01 20:17:51 -05:00
|
|
|
|
lastDirection = actualDirection;
|
2018-12-08 11:09:56 -05:00
|
|
|
|
lastMovementType = gameHUD.IsRunningEnabled ? eMovementType.Running : eMovementType.Walking;
|
2018-12-01 20:17:51 -05:00
|
|
|
|
sessionManager.MoveRequest(actualDirection, lastMovementType);
|
2018-11-24 23:52:23 -05:00
|
|
|
|
}
|
2018-12-01 15:22:55 -05:00
|
|
|
|
else if (!mouseInfoProvider.LeftMouseDown && lastMovementType != eMovementType.Stopped)
|
2018-11-28 19:28:41 -05:00
|
|
|
|
{
|
2018-12-01 20:17:51 -05:00
|
|
|
|
lastDirection = actualDirection;
|
2018-12-01 15:22:55 -05:00
|
|
|
|
lastMovementType = eMovementType.Stopped;
|
2018-12-01 20:17:51 -05:00
|
|
|
|
sessionManager.MoveRequest(actualDirection, lastMovementType);
|
2018-11-28 19:28:41 -05:00
|
|
|
|
}
|
2018-11-24 23:52:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-07 18:45:54 -05:00
|
|
|
|
}
|