1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-18 10:26:02 -05:00
OpenDiablo2/OpenDiablo2.Scenes/Game.cs

227 lines
7.7 KiB
C#
Raw Normal View History

2018-11-24 23:52:23 -05:00
using System;
using System.Drawing;
using OpenDiablo2.Common;
using OpenDiablo2.Common.Attributes;
using OpenDiablo2.Common.Enums;
using OpenDiablo2.Common.Interfaces;
namespace OpenDiablo2.Scenes
{
[Scene("Game")]
public sealed class Game : IScene
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private readonly IRenderWindow renderWindow;
private readonly IResourceManager resourceManager;
private readonly IMapEngine mapEngine;
private readonly IMouseInfoProvider mouseInfoProvider;
2018-11-24 23:52:23 -05:00
private readonly IGameState gameState;
private readonly ISessionManager sessionManager;
2018-11-24 23:52:23 -05:00
private readonly IKeyboardInfoProvider keyboardInfoProvider;
//private ISprite[] testSprite;
private ISprite panelSprite, healthManaSprite, gameGlobeOverlapSprite;
2018-11-26 18:18:23 -05:00
private IMiniPanel minipanel;
private ICharacterPanel characterpanel;
private IInventoryPanel inventorypanel;
private bool showMinipanel = false;
2018-11-26 18:18:23 -05:00
private IButton runButton, menuButton;
private eMovementType lastMovementType = eMovementType.Stopped;
private int lastDirection = -1;
const double Rad2Deg = 180.0 / Math.PI;
const double Deg2Rad = Math.PI / 180.0;
2018-11-24 23:52:23 -05:00
public Game(
IRenderWindow renderWindow,
IResourceManager resourceManager,
IMapEngine mapEngine,
2018-11-24 23:52:23 -05:00
IGameState gameState,
IMouseInfoProvider mouseInfoProvider,
2018-11-24 23:52:23 -05:00
IKeyboardInfoProvider keyboardInfoProvider,
ISessionManager sessionManager,
2018-11-26 18:18:23 -05:00
Func<eButtonType, IButton> createButton,
Func<IMiniPanel> createMiniPanel,
Func<ICharacterPanel> createCharacterPanel,
Func<IInventoryPanel> createInventoryPanel
2018-11-24 23:52:23 -05:00
)
{
this.renderWindow = renderWindow;
this.resourceManager = resourceManager;
this.mapEngine = mapEngine;
this.gameState = gameState;
this.mouseInfoProvider = mouseInfoProvider;
2018-11-24 23:52:23 -05:00
this.keyboardInfoProvider = keyboardInfoProvider;
this.sessionManager = sessionManager;
2018-11-24 23:52:23 -05:00
panelSprite = renderWindow.LoadSprite(ResourcePaths.GamePanels, Palettes.Act1);
healthManaSprite = renderWindow.LoadSprite(ResourcePaths.HealthMana, Palettes.Act1);
gameGlobeOverlapSprite = renderWindow.LoadSprite(ResourcePaths.GameGlobeOverlap, Palettes.Act1);
2018-11-26 18:18:23 -05:00
minipanel = createMiniPanel();
// Maybe? Not sure.
// miniPanel.OnMenuActivate();
characterpanel = createCharacterPanel();
inventorypanel = createInventoryPanel();
2018-11-24 23:52:23 -05:00
runButton = createButton(eButtonType.Run);
runButton.Location = new Point(256, 570);
runButton.OnToggle = OnRunToggle;
2018-11-24 23:52:23 -05:00
menuButton = createButton(eButtonType.Menu);
menuButton.Location = new Point(393, 561);
menuButton.OnToggle = OnMenuToggle;
}
private void OnMenuToggle(bool isToggled)
{
this.showMinipanel = isToggled;
2018-11-24 23:52:23 -05:00
}
private void OnRunToggle(bool isToggled)
{
log.Debug("Run Toggle: " + isToggled);
}
public void Render()
{
// TODO: Maybe show some sort of connecting/loading message?
if (mapEngine.FocusedPlayerId == 0)
return;
2018-11-24 23:52:23 -05:00
mapEngine.Render();
DrawPanel();
}
private void DrawPanel()
{
if(gameState.ShowInventoryPanel)
{
inventorypanel.Render();
}
if (gameState.ShowCharacterPanel)
{
characterpanel.Render();
}
2018-11-24 23:52:23 -05:00
// Render the background bottom bar
renderWindow.Draw(panelSprite, 0, new Point(0, 600));
renderWindow.Draw(panelSprite, 1, new Point(166, 600));
renderWindow.Draw(panelSprite, 2, new Point(294, 600));
renderWindow.Draw(panelSprite, 3, new Point(422, 600));
renderWindow.Draw(panelSprite, 4, new Point(550, 600));
renderWindow.Draw(panelSprite, 5, new Point(685, 600));
// Render the health bar
renderWindow.Draw(healthManaSprite, 0, new Point(30, 587));
renderWindow.Draw(gameGlobeOverlapSprite, 0, new Point(28, 595));
// Render the mana bar
renderWindow.Draw(healthManaSprite, 1, new Point(692, 588));
renderWindow.Draw(gameGlobeOverlapSprite, 1, new Point(693, 591));
if(showMinipanel)
{
minipanel.Render();
}
2018-11-24 23:52:23 -05:00
runButton.Render();
menuButton.Render();
}
public void Update(long ms)
{
var seconds = ms / 1000f;
if(showMinipanel)
{
minipanel.Update();
}
characterpanel.Update();
2018-11-24 23:52:23 -05:00
runButton.Update();
menuButton.Update();
HandleMovement();
//var xMod = 0f;
//var yMod = 0f;
2018-11-24 23:52:23 -05:00
//if (keyboardInfoProvider.KeyIsPressed(80 /*left*/))
//{
// xMod = -8f * seconds;
//}
2018-11-24 23:52:23 -05:00
//if (keyboardInfoProvider.KeyIsPressed(79 /*right*/))
//{
// xMod = 8f * seconds;
//}
//if (keyboardInfoProvider.KeyIsPressed(81 /*down*/))
//{
// yMod = 10f * seconds;
//}
//if (keyboardInfoProvider.KeyIsPressed(82 /*up*/))
//{
// yMod = -10f * seconds;
//}
//if (xMod != 0f || yMod != 0f)
//{
// xMod *= .5f;
// yMod *= .5f;
// mapEngine.CameraLocation = new PointF(mapEngine.CameraLocation.X + xMod, mapEngine.CameraLocation.Y + yMod);
//}
mapEngine.Update(ms);
}
2018-11-24 23:52:23 -05:00
private void HandleMovement()
{
if (mouseInfoProvider.MouseY > 530) // 550 is what it should be, but the minipanel check needs to happent oo
return;
if (gameState.ShowInventoryPanel && mouseInfoProvider.MouseX >= 400)
return;
if (gameState.ShowCharacterPanel && mouseInfoProvider.MouseX < 400)
return;
// TODO: Filter movement for inventory panel
var xOffset = (gameState.ShowInventoryPanel ? -200 : 0) + (gameState.ShowCharacterPanel ? 200 : 0);
var cursorDirection = (int)Math.Round(((Math.Atan2(300 - mouseInfoProvider.MouseY, mouseInfoProvider.MouseX - (400 + xOffset)) * Rad2Deg) + 180) / 32);
if (mouseInfoProvider.LeftMouseDown && (lastMovementType == eMovementType.Stopped || lastDirection != cursorDirection))
2018-11-24 23:52:23 -05:00
{
lastDirection = cursorDirection;
lastMovementType = runButton.Toggled ? eMovementType.Running : eMovementType.Walking;
sessionManager.MoveRequest(cursorDirection, lastMovementType);
2018-11-24 23:52:23 -05:00
}
else if (!mouseInfoProvider.LeftMouseDown && lastMovementType != eMovementType.Stopped)
{
lastDirection = cursorDirection;
lastMovementType = eMovementType.Stopped;
sessionManager.MoveRequest(cursorDirection, lastMovementType);
}
2018-11-24 23:52:23 -05:00
}
public void Dispose()
{
}
}
}