736 lines
29 KiB
C#
736 lines
29 KiB
C#
using Microsoft.AspNetCore.SignalR.Client;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using Myra;
|
|
using Myra.Graphics2D.Brushes;
|
|
using Myra.Graphics2D.TextureAtlases;
|
|
using Myra.Graphics2D.UI;
|
|
using Myra.Graphics2D.UI.File;
|
|
using Newtonsoft.Json;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System;
|
|
using Sledgemapper.Shared.Entities;
|
|
|
|
namespace Sledgemapper
|
|
{
|
|
public class Sledgemapper : Game
|
|
{
|
|
private CommunicationManager _communicationManager;
|
|
private State _state;
|
|
private GraphicsDeviceManager _graphics;
|
|
private SpriteBatch _spriteBatch;
|
|
private readonly Desktop _desktop;
|
|
private KeyboardState oldState;
|
|
private MouseState oldMouseState;
|
|
private Vector3 _viewportCenter = new Vector3(0, 0, 0);
|
|
private SpriteFont font;
|
|
private Dictionary<string, SpriteFont> _fonts;
|
|
private Session _sessionData;
|
|
|
|
public Sledgemapper()
|
|
{
|
|
_graphics = new GraphicsDeviceManager(this);
|
|
Content.RootDirectory = "Content";
|
|
_desktop = new Desktop();
|
|
MyraEnvironment.Game = this;
|
|
_sessionData = new Session();
|
|
IsFixedTimeStep = false;
|
|
_communicationManager = new CommunicationManager(_sessionData);
|
|
_state = new State();
|
|
}
|
|
|
|
protected override void Initialize()
|
|
{
|
|
IsMouseVisible = true;
|
|
Window.AllowUserResizing = true;
|
|
base.Initialize();
|
|
}
|
|
|
|
private HorizontalMenu BuildMenu()
|
|
{
|
|
var menu = new HorizontalMenu();
|
|
var menuFile = new MenuItem("_file", "File");
|
|
var menuFileLoad = new MenuItem("_file_load", "Load");
|
|
var menuFileSave = new MenuItem("_file_save", "Save");
|
|
var menuConnect = new MenuItem("_connect", "Connect");
|
|
var menuConnectNew = new MenuItem("_connect_new", "New");
|
|
var menuConnectJoin = new MenuItem("_connect_join", "Join");
|
|
var menuConnectSync = new MenuItem("_connect_sync", "Sync");
|
|
|
|
menuConnectSync.Selected += OnMenuConnectSyncSelected;
|
|
menuFileLoad.Selected += OnMenuFileLoadSelected;
|
|
menuFileSave.Selected += OnMenuFileSaveSelected;
|
|
menuConnectNew.Selected += OnMenuConnectNewSelected;
|
|
menuConnectJoin.Selected += OnMenuConnectJoinSelected;
|
|
|
|
menuConnect.Items.Add(menuConnectNew);
|
|
menuConnect.Items.Add(menuConnectJoin);
|
|
menuConnect.Items.Add(menuConnectSync);
|
|
menu.Items.Add(menuConnect);
|
|
menuFile.Items.Add(menuFileLoad);
|
|
menuFile.Items.Add(menuFileSave);
|
|
menu.Items.Add(menuFile);
|
|
menu.Items.Add(menuConnect);
|
|
|
|
return menu;
|
|
}
|
|
|
|
private void OnMenuConnectJoinSelected(object sender, EventArgs e)
|
|
{
|
|
Window window = new Window
|
|
{
|
|
Title = "Join mapping session"
|
|
};
|
|
var content = new VerticalStackPanel();
|
|
var grid = new Grid
|
|
{
|
|
Width = 200,
|
|
ShowGridLines = false,
|
|
ColumnSpacing = 8,
|
|
RowSpacing = 3,
|
|
};
|
|
|
|
// Set partitioning configuration
|
|
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
|
|
grid.ColumnsProportions.Add(new Proportion(ProportionType.Fill));
|
|
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
|
|
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
|
|
var sessionNameLabel = new Label { Text = "Session Name:" };
|
|
var initialsLabel = new Label { Text = "Initials:", GridRow = 1 };
|
|
var textbox = new TextBox { GridColumn = 1 };
|
|
var initialsTextbox = new TextBox { GridColumn = 1, GridRow = 1 };
|
|
TextButton button = new TextButton
|
|
{
|
|
Text = "Start",
|
|
HorizontalAlignment = HorizontalAlignment.Center
|
|
};
|
|
grid.Widgets.Add(textbox);
|
|
grid.Widgets.Add(initialsTextbox);
|
|
grid.Widgets.Add(sessionNameLabel);
|
|
grid.Widgets.Add(initialsLabel);
|
|
|
|
content.Widgets.Add(grid);
|
|
|
|
button.Click += async (s, e) =>
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textbox.Text))
|
|
{
|
|
return;
|
|
}
|
|
if (_communicationManager.Connection.State != HubConnectionState.Connected)
|
|
{ await _communicationManager.Connection.StartAsync(); }
|
|
var successful = false;
|
|
try
|
|
{
|
|
var result = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", textbox.Text, initialsTextbox.Text);
|
|
if (result != null)
|
|
{
|
|
_sessionData.Map = result.Map;
|
|
_sessionData.Walls = result.Walls;
|
|
_sessionData.Overlays = result.Overlays;
|
|
_sessionData.Players = result.Players;
|
|
_sessionData.MapEntityAdded += OnMapEntityAdded;
|
|
|
|
}
|
|
successful = result != null; ;
|
|
}
|
|
catch { }
|
|
if (successful)
|
|
{
|
|
_sessionData.SessionName = textbox.Text;
|
|
window.Close();
|
|
}
|
|
};
|
|
|
|
content.Widgets.Add(button);
|
|
window.Content = content;
|
|
|
|
window.ShowModal(_desktop);
|
|
}
|
|
|
|
private void OnMenuConnectNewSelected(object sender, EventArgs e)
|
|
{
|
|
Window window = new Window
|
|
{
|
|
Title = "New mapping session"
|
|
};
|
|
var content = new VerticalStackPanel();
|
|
var grid = new Grid
|
|
{
|
|
Width = 200,
|
|
ShowGridLines = false,
|
|
ColumnSpacing = 8,
|
|
RowSpacing = 3,
|
|
};
|
|
|
|
// Set partitioning configuration
|
|
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
|
|
grid.ColumnsProportions.Add(new Proportion(ProportionType.Fill));
|
|
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
|
|
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
|
|
var sessionNameLabel = new Label { Text = "Session Name:" };
|
|
var initialsLabel = new Label { Text = "Initials:", GridRow = 1 };
|
|
var textbox = new TextBox { GridColumn = 1 };
|
|
var initialsTextbox = new TextBox { GridColumn = 1, GridRow = 1 };
|
|
TextButton button = new TextButton
|
|
{
|
|
Text = "Start",
|
|
HorizontalAlignment = HorizontalAlignment.Center
|
|
};
|
|
grid.Widgets.Add(textbox);
|
|
grid.Widgets.Add(initialsTextbox);
|
|
grid.Widgets.Add(sessionNameLabel);
|
|
grid.Widgets.Add(initialsLabel);
|
|
|
|
content.Widgets.Add(grid);
|
|
|
|
button.Click += async (s, e) =>
|
|
{
|
|
if (string.IsNullOrWhiteSpace(textbox.Text))
|
|
{
|
|
return;
|
|
}
|
|
if (_communicationManager.Connection.State != HubConnectionState.Connected)
|
|
{ await _communicationManager.Connection.StartAsync(); }
|
|
var successful = false;
|
|
try
|
|
{
|
|
var session = await _communicationManager.Connection?.InvokeAsync<Session>("NewSession", textbox.Text, initialsTextbox.Text);
|
|
if (session != null)
|
|
{
|
|
_sessionData = session;
|
|
_sessionData.SessionName = textbox.Text;
|
|
session.MapEntityAdded += OnMapEntityAdded;
|
|
session.Players = session.Players;
|
|
|
|
}
|
|
successful = session != null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
if (successful)
|
|
{
|
|
_sessionData.SessionName = textbox.Text;
|
|
_communicationManager.SessionData = _sessionData;
|
|
window.Close();
|
|
}
|
|
};
|
|
|
|
|
|
content.Widgets.Add(button);
|
|
window.Content = content;
|
|
|
|
window.Closed += (s, a) =>
|
|
{
|
|
// Called when window is closed
|
|
};
|
|
|
|
window.ShowModal(_desktop);
|
|
}
|
|
|
|
private void OnMenuFileSaveSelected(object sender, EventArgs e)
|
|
{
|
|
FileDialog dialog = new FileDialog(FileDialogMode.SaveFile)
|
|
{
|
|
Filter = "*.map"
|
|
};
|
|
|
|
dialog.Closed += (s, a) =>
|
|
{
|
|
if (!dialog.Result)
|
|
{
|
|
return;
|
|
}
|
|
|
|
using (StreamWriter file = File.CreateText(dialog.FilePath))
|
|
{
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
serializer.Serialize(file, _sessionData);
|
|
}
|
|
};
|
|
|
|
dialog.ShowModal(_desktop);
|
|
}
|
|
|
|
private void OnMenuFileLoadSelected(object sender, EventArgs e)
|
|
{
|
|
FileDialog dialog = new FileDialog(FileDialogMode.OpenFile)
|
|
{
|
|
Filter = "*.map"
|
|
};
|
|
|
|
dialog.Closed += (s, a) =>
|
|
{
|
|
if (!dialog.Result)
|
|
{
|
|
return;
|
|
}
|
|
using (StreamReader file = File.OpenText(dialog.FilePath))
|
|
{
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
_sessionData = (Session)serializer.Deserialize(file, typeof(Session));
|
|
}
|
|
};
|
|
|
|
dialog.ShowModal(_desktop);
|
|
}
|
|
|
|
private async void OnMenuConnectSyncSelected(object sender, EventArgs e)
|
|
{
|
|
await _communicationManager.Connection?.InvokeAsync("Sync", _sessionData.SessionName, _sessionData);
|
|
}
|
|
|
|
protected override void LoadContent()
|
|
{
|
|
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
MyraEnvironment.Game = this;
|
|
|
|
var mainPanel = new VerticalStackPanel();
|
|
|
|
var menu = BuildMenu();
|
|
mainPanel.Widgets.Add(menu);
|
|
|
|
var sidePanel = new VerticalStackPanel { Layout2d = new Myra.Graphics2D.UI.Properties.Layout2D("this.w=200;this.h=W.h"), Background = new SolidBrush(Color.DarkGray) };
|
|
|
|
var tileScrollView = new ScrollViewer { Layout2d = new Myra.Graphics2D.UI.Properties.Layout2D("this.w=200;this.h=W.h/3") };
|
|
var tileGrid = new Grid { ColumnSpacing = 3, RowSpacing = 3, Layout2d = new Myra.Graphics2D.UI.Properties.Layout2D("this.w=200"), Background = new SolidBrush(Color.DarkGray) };
|
|
tileScrollView.Content = tileGrid;
|
|
sidePanel.Widgets.Add(tileScrollView);
|
|
|
|
var wallScrollView = new ScrollViewer { Layout2d = new Myra.Graphics2D.UI.Properties.Layout2D("this.w=200;this.h=W.h/3") };
|
|
var wallGrid = new Grid { ColumnSpacing = 3, RowSpacing = 3, Layout2d = new Myra.Graphics2D.UI.Properties.Layout2D("this.w=200"), Background = new SolidBrush(Color.DarkGray) };
|
|
wallScrollView.Content = wallGrid;
|
|
sidePanel.Widgets.Add(wallScrollView);
|
|
|
|
var overlayScrollView = new ScrollViewer { Layout2d = new Myra.Graphics2D.UI.Properties.Layout2D("this.w=200;this.h=W.h/3") };
|
|
var overlayGrid = new Grid { ColumnSpacing = 3, RowSpacing = 3, Layout2d = new Myra.Graphics2D.UI.Properties.Layout2D("this.w=200"), Background = new SolidBrush(Color.DarkGray) };
|
|
overlayScrollView.Content = overlayGrid;
|
|
sidePanel.Widgets.Add(overlayScrollView);
|
|
|
|
mainPanel.Widgets.Add(sidePanel);
|
|
|
|
var tilesFolderContent = Content.LoadContentFolder<Texture2D>("tiles");
|
|
var indexX = 0;
|
|
var indexY = 0;
|
|
|
|
foreach (var item in tilesFolderContent)
|
|
{
|
|
var tileButton = new ImageButton { Image = new TextureRegion(item.Value), GridColumn = indexY, GridRow = indexX, Id = item.Key, Width = 40, Height = 40 };
|
|
tileButton.Click += (s, e) =>
|
|
{
|
|
_state._currentTileId = ((ImageButton)s).Id;
|
|
|
|
ClearSelection(wallGrid);
|
|
ClearSelection(tileGrid);
|
|
ClearSelection(overlayGrid);
|
|
|
|
((ImageButton)s).Border = new SolidBrush(Color.Red);
|
|
((ImageButton)s).BorderThickness = new Myra.Graphics2D.Thickness(2);
|
|
_state._insertMode = InsertMode.Tile;
|
|
};
|
|
tileGrid.Widgets.Add(tileButton);
|
|
indexY++;
|
|
if (indexY == 4)
|
|
{
|
|
indexY = 0;
|
|
indexX++;
|
|
}
|
|
}
|
|
|
|
var wallsFolderContent = Content.LoadContentFolder<Texture2D>("walls");
|
|
indexX = 0;
|
|
indexY = 0;
|
|
|
|
foreach (var item in wallsFolderContent)
|
|
{
|
|
var wallButton = new ImageButton { Image = new TextureRegion(item.Value), GridColumn = indexY, GridRow = indexX, Id = item.Key, Width = 40, Height = 40 };
|
|
wallButton.Click += (s, e) =>
|
|
{
|
|
_state._currentWallId = ((ImageButton)s).Id;
|
|
ClearSelection(wallGrid);
|
|
ClearSelection(tileGrid);
|
|
ClearSelection(overlayGrid);
|
|
|
|
|
|
((ImageButton)s).Border = new SolidBrush(Color.Red);
|
|
((ImageButton)s).BorderThickness = new Myra.Graphics2D.Thickness(2);
|
|
_state._insertMode = InsertMode.Wall;
|
|
|
|
};
|
|
wallGrid.Widgets.Add(wallButton);
|
|
indexY++;
|
|
if (indexY == 4)
|
|
{
|
|
indexY = 0;
|
|
indexX++;
|
|
}
|
|
}
|
|
|
|
var overlayFolderContent = Content.LoadContentFolder<Texture2D>("overlays");
|
|
indexX = 0;
|
|
indexY = 0;
|
|
|
|
foreach (var item in overlayFolderContent)
|
|
{
|
|
var overlayButton = new ImageButton { Image = new TextureRegion(item.Value), GridColumn = indexY, GridRow = indexX, Id = item.Key, Width = 40, Height = 40 };
|
|
overlayButton.Click += (s, e) =>
|
|
{
|
|
_state._currentOverlayId = ((ImageButton)s).Id;
|
|
ClearSelection(wallGrid);
|
|
ClearSelection(tileGrid);
|
|
ClearSelection(overlayGrid);
|
|
|
|
((ImageButton)s).Border = new SolidBrush(Color.Red);
|
|
((ImageButton)s).BorderThickness = new Myra.Graphics2D.Thickness(2);
|
|
_state._insertMode = InsertMode.Overlay;
|
|
};
|
|
overlayGrid.Widgets.Add(overlayButton);
|
|
indexY++;
|
|
if (indexY == 4)
|
|
{
|
|
indexY = 0;
|
|
indexX++;
|
|
}
|
|
}
|
|
|
|
_fonts = Content.LoadContentFolder<SpriteFont>("fonts");
|
|
|
|
// Add it to the desktop
|
|
// _desktop = new Desktop();
|
|
_desktop.Root = mainPanel;
|
|
// TODO: use this.Content to load your game content here
|
|
}
|
|
|
|
protected override void Update(GameTime gameTime)
|
|
{
|
|
// if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
|
// Exit();
|
|
KeyboardState newState = Keyboard.GetState();
|
|
// TODO: Add your update logic here
|
|
if (IsActive && GraphicsDevice.Viewport.Bounds.Contains(Mouse.GetState().Position) && !_desktop.IsMouseOverGUI && !_desktop.HasModalWidget)
|
|
{
|
|
var mouseState = Mouse.GetState();
|
|
|
|
var screenPosition = new Point((mouseState.Position.X - (int)_viewportCenter.X), (mouseState.Position.Y - (int)_viewportCenter.Y));
|
|
|
|
_state._hoveredTile.X = screenPosition.X / _state._tileSize;
|
|
_state._hoveredTile.Y = screenPosition.Y / _state._tileSize;
|
|
if (screenPosition.X < 0)
|
|
{
|
|
_state._hoveredTile.X--;
|
|
}
|
|
|
|
if (screenPosition.Y < 0)
|
|
{
|
|
_state._hoveredTile.Y--;
|
|
}
|
|
|
|
if (_state._insertMode == InsertMode.Wall)
|
|
{
|
|
_state.SelectClosestWall(screenPosition);
|
|
}
|
|
|
|
if (_state._insertMode == InsertMode.Overlay)
|
|
{
|
|
_state.SelectOverlay(screenPosition);
|
|
}
|
|
|
|
if (mouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton != oldMouseState.LeftButton)
|
|
{
|
|
_state._selectedTile.X = _state._hoveredTile.X;
|
|
_state._selectedTile.Y = _state._hoveredTile.Y;
|
|
_communicationManager.Connection?.SendAsync("UpdatePosition", _sessionData.SessionName, _state._selectedTile);
|
|
}
|
|
|
|
if (newState.IsKeyDown(Keys.LeftControl)
|
|
&& mouseState.LeftButton == ButtonState.Pressed
|
|
&& ((mouseState.LeftButton != oldMouseState.LeftButton) || (_state._selectedTile.X != _state._hoveredTile.X && _state._selectedTile.Y != _state._hoveredTile.Y)))
|
|
{
|
|
switch (_state._insertMode)
|
|
{
|
|
case InsertMode.Tile:
|
|
_state._selectedTile.X = _state._hoveredTile.X;
|
|
_state._selectedTile.Y = _state._hoveredTile.Y;
|
|
_communicationManager.Connection?.SendAsync("UpdatePosition", _sessionData.SessionName, _state._selectedTile);
|
|
|
|
_sessionData.NewTile(_state._selectedTile, _state._currentTileId);
|
|
break;
|
|
|
|
case InsertMode.Wall:
|
|
_sessionData.NewWall(_state._selectedWall, _state._currentWallId);
|
|
|
|
break;
|
|
case InsertMode.Overlay:
|
|
_sessionData.NewOverlay(_state._selectedOverlay, _state._currentOverlayId);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!newState.IsKeyDown(Keys.LeftControl) && mouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == oldMouseState.LeftButton)
|
|
{
|
|
_viewportCenter = new Vector3(_viewportCenter.X + mouseState.Position.X - oldMouseState.Position.X, _viewportCenter.Y + mouseState.Position.Y - oldMouseState.Position.Y, 0);
|
|
}
|
|
|
|
if (newState.IsKeyDown(Keys.LeftControl) && mouseState.ScrollWheelValue != oldMouseState.ScrollWheelValue)
|
|
{
|
|
if (mouseState.ScrollWheelValue > oldMouseState.ScrollWheelValue)
|
|
{
|
|
_state._tileSize = Math.Min(120, _state._tileSize + 10);
|
|
|
|
}
|
|
else if (mouseState.ScrollWheelValue < oldMouseState.ScrollWheelValue)
|
|
{
|
|
_state._tileSize = Math.Max(10, _state._tileSize - 10);
|
|
}
|
|
}
|
|
|
|
oldMouseState = mouseState;
|
|
}
|
|
|
|
|
|
if (newState.IsKeyDown(Keys.Delete))
|
|
{
|
|
switch (_state._insertMode)
|
|
{
|
|
case InsertMode.Tile:
|
|
_state._selectedTile.X = _state._hoveredTile.X;
|
|
_state._selectedTile.Y = _state._hoveredTile.Y;
|
|
_sessionData.DeleteTile(_state._selectedTile);
|
|
break;
|
|
case InsertMode.Wall:
|
|
_sessionData.DeleteWall(_state._selectedWall);
|
|
break;
|
|
case InsertMode.Overlay:
|
|
_sessionData.DeleteOverlay(_state._selectedOverlay);
|
|
break;
|
|
}
|
|
}
|
|
|
|
foreach (var key in newState.GetPressedKeys())
|
|
{
|
|
switch (key)
|
|
{
|
|
case Keys.Left:
|
|
if (oldState.IsKeyUp(Keys.Left) && newState.IsKeyDown(Keys.Left))
|
|
{ _state._selectedTile.X--; }
|
|
break;
|
|
case Keys.Right:
|
|
if (oldState.IsKeyUp(Keys.Right) && newState.IsKeyDown(Keys.Right))
|
|
{ _state._selectedTile.X++; }
|
|
break;
|
|
case Keys.Up:
|
|
if (oldState.IsKeyUp(Keys.Up) && newState.IsKeyDown(Keys.Up))
|
|
{ _state._selectedTile.Y--; }
|
|
break;
|
|
case Keys.Down:
|
|
if (oldState.IsKeyUp(Keys.Down) && newState.IsKeyDown(Keys.Down))
|
|
{ _state._selectedTile.Y++; }
|
|
break;
|
|
}
|
|
_communicationManager.Connection?.SendAsync("UpdatePosition", _sessionData.SessionName, _state._selectedTile);
|
|
|
|
}
|
|
|
|
oldState = newState;
|
|
|
|
base.Update(gameTime);
|
|
}
|
|
|
|
protected override void Draw(GameTime gameTime)
|
|
{
|
|
if (_spriteBatch is null)
|
|
{
|
|
return;
|
|
}
|
|
GraphicsDevice.Clear(Color.DarkGray);
|
|
|
|
// TODO: Add your drawing code here
|
|
var visibleTilesX = GraphicsDevice.Viewport.Width / _state._tileSize + 1;
|
|
var visibleTilesY = GraphicsDevice.Viewport.Height / _state._tileSize + 1;
|
|
|
|
_spriteBatch.Begin(transformMatrix: Matrix.CreateTranslation(_viewportCenter));
|
|
|
|
DrawGrid(visibleTilesX, visibleTilesY);
|
|
DrawTiles();
|
|
DrawWalls();
|
|
DrawOverlays();
|
|
|
|
if (string.IsNullOrWhiteSpace(_sessionData.SessionName))
|
|
{
|
|
_spriteBatch.DrawRectangle(new Rectangle(_state._selectedTile.X * _state._tileSize, _state._selectedTile.Y * _state._tileSize, _state._tileSize - 1, _state._tileSize - 1), Color.Red, 2);
|
|
}
|
|
|
|
DrawPlayers();
|
|
|
|
var startWall = new Vector2(_state._selectedWall.X * _state._tileSize, _state._selectedWall.Y * _state._tileSize);
|
|
if (_state._insertMode == InsertMode.Wall)
|
|
{
|
|
_spriteBatch.DrawLine(startWall, _state._tileSize, MathHelper.ToRadians(90 * _state._selectedWall.Rotation), Color.Red, 2);
|
|
}
|
|
var overlay = new Vector2(_state._selectedOverlay.X * _state._tileSize, _state._selectedOverlay.Y * _state._tileSize);
|
|
if (_state._insertMode == InsertMode.Overlay)
|
|
{
|
|
if (_state._selectedOverlay.Intersection)
|
|
{
|
|
_spriteBatch.DrawCircle(overlay, _state._tileSize / 3, 100, Color.Red, 2);
|
|
}
|
|
}
|
|
|
|
_spriteBatch.End();
|
|
|
|
_desktop?.Render();
|
|
base.Draw(gameTime);
|
|
}
|
|
|
|
private void DrawGrid(int visibleTilesX, int visibleTilesY)
|
|
{
|
|
for (var i = -1; i < visibleTilesX + 2; i++)
|
|
{
|
|
var posX1 = i * _state._tileSize - _viewportCenter.X;
|
|
var posY1 = -_viewportCenter.Y;
|
|
posX1 = posX1 - posX1 % _state._tileSize;
|
|
posY1 = posY1 - posY1 % _state._tileSize;
|
|
var posX2 = i * _state._tileSize - _viewportCenter.X;
|
|
var posY2 = GraphicsDevice.Viewport.Height - _viewportCenter.Y;
|
|
posX2 = posX2 - posX2 % _state._tileSize;
|
|
posY2 = posY2 - posY2 % _state._tileSize;
|
|
|
|
_spriteBatch.DrawLine(
|
|
posX1, posY1,
|
|
posX2,
|
|
posY2,
|
|
Color.Black);
|
|
}
|
|
|
|
for (var i = -1; i < visibleTilesY + 2; i++)
|
|
{
|
|
var posX1 = -_viewportCenter.X;
|
|
var posY1 = i * _state._tileSize - _viewportCenter.Y;
|
|
posX1 = posX1 - posX1 % _state._tileSize;
|
|
posY1 = posY1 - posY1 % _state._tileSize;
|
|
var posX2 = GraphicsDevice.Viewport.Width - _viewportCenter.X;
|
|
var posY2 = i * _state._tileSize - _viewportCenter.Y;
|
|
posX2 = posX2 - posX2 % _state._tileSize;
|
|
posY2 = posY2 - posY2 % _state._tileSize;
|
|
|
|
_spriteBatch.DrawLine(posX1, posY1,
|
|
posX2,
|
|
posY2,
|
|
Color.Black);
|
|
}
|
|
}
|
|
|
|
private void DrawTiles()
|
|
{
|
|
foreach (var tile in _sessionData.Map.Values)
|
|
{
|
|
var content = Content.Load<Texture2D>($"tiles/{tile.ID}");
|
|
|
|
var posX = tile.X * _state._tileSize + _state._tileSize / 2f;
|
|
var posY = tile.Y * _state._tileSize + _state._tileSize / 2f;
|
|
|
|
_spriteBatch.Draw(content, new Vector2(posX, posY),
|
|
null, Color.White, MathHelper.ToRadians(90 * tile.Rotation), new Vector2(content.Width / 2, content.Height / 2), ((float)_state._tileSize - 1) / content.Width, SpriteEffects.None, 0);
|
|
}
|
|
}
|
|
|
|
private void DrawPlayers()
|
|
{
|
|
foreach (var player in _sessionData.Players.Copy<List<Player>>())
|
|
{
|
|
var hexs = player.Color.Split(2).ToArray();
|
|
var color = new Color(int.Parse(hexs[0], System.Globalization.NumberStyles.HexNumber),
|
|
int.Parse(hexs[1], System.Globalization.NumberStyles.HexNumber),
|
|
int.Parse(hexs[2], System.Globalization.NumberStyles.HexNumber));
|
|
_spriteBatch.DrawRectangle(new Rectangle(player.Position.X * _state._tileSize, player.Position.Y * _state._tileSize, _state._tileSize - 1, _state._tileSize - 1), color, 2);
|
|
|
|
var ffont = _fonts.FirstOrDefault(m => int.Parse(m.Key.Replace("font", "")) > _state._tileSize).Value ?? _fonts.Last().Value;
|
|
|
|
var fscale = (float)_state._tileSize / ((float)ffont.LineSpacing * 2);
|
|
_spriteBatch.DrawString(ffont,
|
|
player.Initials,
|
|
new Vector2(player.Position.X * _state._tileSize + 2, player.Position.Y * _state._tileSize + _state._tileSize - 2 - ffont.LineSpacing * fscale),
|
|
color,
|
|
0,
|
|
Vector2.Zero,
|
|
fscale,
|
|
SpriteEffects.None,
|
|
0);
|
|
}
|
|
}
|
|
|
|
private void DrawOverlays()
|
|
{
|
|
foreach (var tile in _sessionData.Overlays.Values)
|
|
{
|
|
var content = Content.Load<Texture2D>($"overlays/{tile.ID}");
|
|
if (tile.Intersection)
|
|
{
|
|
var posX = tile.X * _state._tileSize;
|
|
var posY = tile.Y * _state._tileSize;
|
|
|
|
_spriteBatch.Draw(content, new Vector2(posX, posY),
|
|
null, new Color(24, 118, 157), MathHelper.ToRadians(90 * tile.Rotation), new Vector2(content.Width / 2, content.Height / 2), ((float)_state._tileSize - 10) / content.Width, SpriteEffects.None, 0);
|
|
}
|
|
else
|
|
{
|
|
var posX = tile.X * _state._tileSize + _state._tileSize / 2f;
|
|
var posY = tile.Y * _state._tileSize + _state._tileSize / 2f;
|
|
|
|
_spriteBatch.Draw(content, new Vector2(posX, posY),
|
|
null, new Color(24, 118, 157), MathHelper.ToRadians(90 * tile.Rotation), new Vector2(content.Width / 2, content.Height / 2), ((float)_state._tileSize - 10) / content.Width, SpriteEffects.None, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DrawWalls()
|
|
{
|
|
foreach (var wall in _sessionData.Walls.Values)
|
|
{
|
|
var content = Content.Load<Texture2D>($"walls/{wall.ID}");
|
|
var scale = _state._tileSize / (float)content.Height;
|
|
var offset = scale * content.Width / 2f;
|
|
var posX = wall.X * _state._tileSize;
|
|
var posY = wall.Y * _state._tileSize;
|
|
if (wall.Rotation == 1)
|
|
{
|
|
posX -= (int)offset;
|
|
}
|
|
else if (wall.Rotation == 0)
|
|
{
|
|
posY += (int)offset;
|
|
}
|
|
_spriteBatch.Draw(content, new Vector2(posX, posY), null, Color.White, MathHelper.ToRadians(90 * (wall.Rotation - 1)), new Vector2(0, 0), scale, SpriteEffects.None, 0);
|
|
}
|
|
}
|
|
|
|
private void ClearSelection(Grid grid)
|
|
{
|
|
foreach (var widget in grid.Widgets)
|
|
{
|
|
widget.Border = null;
|
|
}
|
|
}
|
|
|
|
private void OnMapEntityAdded(object sender, MapEntityAddedEventArgs e)
|
|
{
|
|
_communicationManager.Enqueue(e.MapEntity, TileAction.Add);
|
|
}
|
|
|
|
private void OnMapEntityDeleted(object sender, MapEntityDeletedEventArgs e)
|
|
{
|
|
_communicationManager.Enqueue(e.MapEntity, TileAction.Delete);
|
|
}
|
|
}
|
|
|
|
public enum TileAction
|
|
{
|
|
Add,
|
|
Delete
|
|
}
|
|
}
|