sledgemapper/MyGame/Game1.cs
Michele Scandura c8e96425cd Walls!
2020-11-02 13:57:07 +00:00

696 lines
26 KiB
C#

using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
// using MonoGame.Extended;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Myra.Graphics2D.UI;
using Myra.Graphics2D.Brushes;
using Myra.Graphics2D.TextureAtlases;
using Myra.Graphics2D.UI.File;
using Myra;
using System.IO;
using Microsoft.Xna.Framework.Content;
using Newtonsoft.Json;
namespace MyGame
{
public enum InsertMode
{
Tile,
Wall,
Overlay
}
public struct Tile
{
public int X { get; set; }
public int Y { get; set; }
public int ID { get; set; }
public int Rotation { get; set; }
}
public struct Wall
{
public int X { get; set; }
public int Y { get; set; }
public int ID { get; set; }
public int Rotation { get; set; }
}
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Vector2 _topLeft = new Vector2(-20, 20);
private List<Tile> _map = new List<Tile>();
private List<Wall> _mapWalls = new List<Wall>();
private Tile _selectedTile = new Tile { X = 1, Y = 1 };
private Tile _hoveredTile = new Tile { X = 1, Y = 1 };
private Wall _selectedWall = new Wall { X = 1, Y = 1 };
private int _tileSize = 30;
HubConnection connection;
private Desktop _desktop;
private int _currentTileId = 1;
private int _currentWallId = 1;
private InsertMode _insertMode;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
_desktop = new Desktop();
MyraEnvironment.Game = this;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
IsMouseVisible = true;
Window.AllowUserResizing = true;
connection = new HubConnectionBuilder()
.WithUrl("http://hub.michelescandura.com:5000/ChatHub")
.Build();
connection.On<string, string>("ReceiveMessage", (user, message) =>
{
var newMessage = $"{user}: {message}";
_map.Add(new Tile { X = int.Parse(user.Split(':')[0]), Y = int.Parse(user.Split(':')[1]), ID = int.Parse(message) });
});
connection.On<List<Tile>>("UpdateMap", (map) =>
{
_map = map;
});
base.Initialize();
}
private string _session;
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 += async (s, e) =>
{
await connection?.InvokeAsync("Sync", _session, _map);
};
menuFileLoad.Selected += (s, e) =>
{
FileDialog dialog = new FileDialog(FileDialogMode.OpenFile)
{
Filter = "*.map"
//Folder = @"D:\Temp"
};
dialog.Closed += (s, a) =>
{
if (!dialog.Result)
{
// "Cancel" or Escape
return;
}
using (StreamReader file = File.OpenText(dialog.FilePath))
{
JsonSerializer serializer = new JsonSerializer();
_map = (List<Tile>)serializer.Deserialize(file, typeof(List<Tile>));
}
// "Ok" or Enter
// ...
};
dialog.ShowModal(_desktop);
};
menuFileSave.Selected += (s, e) =>
{
FileDialog dialog = new FileDialog(FileDialogMode.SaveFile)
{
Filter = "*.map"
//Folder = @"D:\Temp"
};
dialog.Closed += (s, a) =>
{
if (!dialog.Result)
{
// "Cancel" or Escape
return;
}
using (StreamWriter file = File.CreateText(dialog.FilePath))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, _map);
}
// "Ok" or Enter
// ...
};
dialog.ShowModal(_desktop);
};
menuConnectNew.Selected += (s, e) =>
{
Window window = new Window
{
Title = "New mapping session"
};
var content = new VerticalStackPanel();
var textbox = new TextBox();
TextButton button = new TextButton
{
Text = "Start",
HorizontalAlignment = HorizontalAlignment.Center
};
button.Click += async (s, e) =>
{
if (string.IsNullOrWhiteSpace(textbox.Text))
{
return;
}
if (connection.State != HubConnectionState.Connected)
{ await connection.StartAsync(); }
var successful = false;
try
{
await connection?.InvokeAsync("NewSession", textbox.Text);
successful = true;
}
catch { }
if (successful)
{
_session = textbox.Text;
window.Close();
}
};
content.Widgets.Add(textbox);
content.Widgets.Add(button);
window.Content = content;
window.Closed += (s, a) =>
{
// Called when window is closed
};
window.ShowModal(_desktop);
};
menuConnectJoin.Selected += (s, e) =>
{
Window window = new Window
{
Title = "Join mapping session"
};
var content = new VerticalStackPanel();
var textbox = new TextBox();
TextButton button = new TextButton
{
Text = "Start",
HorizontalAlignment = HorizontalAlignment.Center
};
button.Click += async (s, e) =>
{
if (string.IsNullOrWhiteSpace(textbox.Text))
{
return;
}
if (connection.State != HubConnectionState.Connected)
{ await connection.StartAsync(); }
var successful = false;
try
{
var result = await connection?.InvokeAsync<bool>("JoinSession", textbox.Text);
if (result)
{
_map = await connection.InvokeAsync<List<Tile>>("Refresh", textbox.Text);
}
successful = result;
}
catch { }
if (successful)
{
_session = textbox.Text;
window.Close();
}
};
content.Widgets.Add(textbox);
content.Widgets.Add(button);
window.Content = content;
window.Closed += (s, a) =>
{
// Called when window is closed
};
window.ShowModal(_desktop);
};
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;
}
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);
// _desktop.MenuBar = menu;
// _desktop.MenuBar.Visible = true;
// _desktop.MenuBar.Enabled = true;
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) =>
{
_currentTileId = int.Parse(((ImageButton)s).Id.Replace("tile", ""));
ClearSelection(wallGrid);
ClearSelection(tileGrid);
((ImageButton)s).Border = new SolidBrush(Color.Red);
((ImageButton)s).BorderThickness = new Myra.Graphics2D.Thickness(2);
_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) =>
{
_currentWallId = int.Parse(((ImageButton)s).Id.Replace("wall", ""));
ClearSelection(wallGrid);
ClearSelection(tileGrid);
((ImageButton)s).Border = new SolidBrush(Color.Red);
((ImageButton)s).BorderThickness = new Myra.Graphics2D.Thickness(2);
_insertMode = InsertMode.Wall;
};
wallGrid.Widgets.Add(wallButton);
indexY++;
if (indexY == 4)
{
indexY = 0;
indexX++;
}
}
// Add it to the desktop
// _desktop = new Desktop();
_desktop.Root = mainPanel;
// TODO: use this.Content to load your game content here
}
private void ClearSelection(Grid grid)
{
foreach (var widget in grid.Widgets)
{
widget.Border = null;
}
}
private KeyboardState oldState;
private MouseState oldMouseState;
private Vector3 _viewportCenter = new Vector3(0, 0, 0);
float Sign2(Point p1, Point p2, Point p3) { return (p1.X - p3.X) * (p2.Y - p3.Y) - (p2.X - p3.X) * (p1.Y - p3.Y); }
bool PointInTri(Point pt, Point v1, Point v2, Point v3)
{
bool b1, b2, b3;
b1 = Sign2(pt, v1, v2) < 0.0f;
b2 = Sign2(pt, v2, v3) < 0.0f;
b3 = Sign2(pt, v3, v1) < 0.0f;
return ((b1 == b2) && (b2 == b3));
}
private void SelectClosestWall(Point mousePosition)
{
var topLeft = new Point(_hoveredTile.X * _tileSize, _hoveredTile.Y * _tileSize);
var bottomLeft = new Point(_hoveredTile.X * _tileSize, _hoveredTile.Y * _tileSize + _tileSize);
var topRight = new Point(_hoveredTile.X * _tileSize + _tileSize, _hoveredTile.Y * _tileSize);
var bottomRight = new Point(_hoveredTile.X * _tileSize + _tileSize, _hoveredTile.Y * _tileSize + _tileSize);
var center = new Point(_hoveredTile.X * _tileSize + _tileSize / 2, _hoveredTile.Y * _tileSize + _tileSize / 2);
System.Console.WriteLine($"{mousePosition.X} - {mousePosition.Y}");
System.Console.WriteLine($"{_hoveredTile.X} - {_hoveredTile.Y}");
var leftWall = PointInTri(mousePosition, topLeft, center, bottomLeft);
var rightWall = PointInTri(mousePosition, topRight, bottomRight, center);
var topWall = PointInTri(mousePosition, topLeft, topRight, center);
var bottomtWall = PointInTri(mousePosition, bottomLeft, center, bottomRight);
if (leftWall)
{
_selectedWall.X = _hoveredTile.X;
_selectedWall.Y = _hoveredTile.Y;
_selectedWall.Rotation = 1;
}
else if (rightWall)
{
_selectedWall.X = _hoveredTile.X + 1;
_selectedWall.Y = _hoveredTile.Y;
_selectedWall.Rotation = 1;
}
else if (topWall)
{
_selectedWall.X = _hoveredTile.X;
_selectedWall.Y = _hoveredTile.Y;
_selectedWall.Rotation = 0;
}
else if (bottomtWall)
{
_selectedWall.X = _hoveredTile.X;
_selectedWall.Y = _hoveredTile.Y + 1;
_selectedWall.Rotation = 0;
}
}
private int scrollvalue=0;
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
var mouseState = Mouse.GetState();
var screenPosition = new Point((mouseState.Position.X - (int)_viewportCenter.X), (mouseState.Position.Y - (int)_viewportCenter.Y));
_hoveredTile.X = screenPosition.X / _tileSize;
_hoveredTile.Y = screenPosition.Y / _tileSize;
if (screenPosition.X < 0)
{
_hoveredTile.X--;
}
if (screenPosition.Y < 0)
{
_hoveredTile.Y--;
}
SelectClosestWall(screenPosition);
if (mouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton != oldMouseState.LeftButton)
{
_selectedTile.X = _hoveredTile.X;
_selectedTile.Y = _hoveredTile.Y;
}
if (newState.IsKeyDown(Keys.LeftControl) && mouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton != oldMouseState.LeftButton)
{
switch (_insertMode)
{
case InsertMode.Tile:
_selectedTile.X = _hoveredTile.X;
_selectedTile.Y = _hoveredTile.Y;
SetTile(_currentTileId);
break;
case InsertMode.Wall:
SetWall(_currentWallId);
break;
}
}
if (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)
{
_tileSize=System.Math.Min(120, _tileSize+10);
}
else if (mouseState.ScrollWheelValue<oldMouseState.ScrollWheelValue)
{
_tileSize=System.Math.Max(10, _tileSize-10);
}
}
foreach (var key in newState.GetPressedKeys())
{
switch (key)
{
case Keys.Left:
if (oldState.IsKeyUp(Keys.Left) && newState.IsKeyDown(Keys.Left))
{ _selectedTile.X--; }
break;
case Keys.Right:
if (oldState.IsKeyUp(Keys.Right) && newState.IsKeyDown(Keys.Right))
{ _selectedTile.X++; }
break;
case Keys.Up:
if (oldState.IsKeyUp(Keys.Up) && newState.IsKeyDown(Keys.Up))
{ _selectedTile.Y--; }
break;
case Keys.Down:
if (oldState.IsKeyUp(Keys.Down) && newState.IsKeyDown(Keys.Down))
{ _selectedTile.Y++; }
break;
}
if (oldState.IsKeyUp(key) && newState.IsKeyDown(key))
{
var tileId = ((int)key) - 48;
if (tileId >= 1 && tileId <= 9)
{
SetTile(tileId);
}
}
}
oldState = newState;
oldMouseState = mouseState;
base.Update(gameTime);
}
private void SetTile(int tileId)
{
var tileExist = _map.Any(m => m.X == _selectedTile.X && m.Y == _selectedTile.Y);
if (tileExist)
{
var tile = _map.First(m => m.X == _selectedTile.X && m.Y == _selectedTile.Y);
var index = _map.IndexOf(tile);
_map.RemoveAt(index);
if (tile.ID == tileId)
{
var newTile = new Tile { X = _selectedTile.X, Y = _selectedTile.Y, ID = tileId, Rotation = (tile.Rotation + 1) % 4 };
_map.Add(newTile);
}
else
{
_map.Add(new Tile { X = _selectedTile.X, Y = _selectedTile.Y, ID = tileId });
}
}
else
{
_map.Add(new Tile { X = _selectedTile.X, Y = _selectedTile.Y, ID = tileId });
connection?.InvokeAsync("SendMessage", $"{_selectedTile.X}:{_selectedTile.Y}", tileId.ToString(), _session);
}
}
private void SetWall(int wallId)
{
var tileExist = _mapWalls.Any(m => m.X == _selectedWall.X && m.Y == _selectedWall.Y && m.Rotation == _selectedWall.Rotation);
if (tileExist)
{
var wall = _mapWalls.First(m => m.X == _selectedWall.X && m.Y == _selectedWall.Y && m.Rotation == _selectedWall.Rotation);
var index = _mapWalls.IndexOf(wall);
_mapWalls.RemoveAt(index);
_mapWalls.Add(new Wall { X = _selectedWall.X, Y = _selectedWall.Y, ID = wallId, Rotation = _selectedWall.Rotation });
}
else
{
_mapWalls.Add(new Wall { X = _selectedWall.X, Y = _selectedWall.Y, ID = wallId, Rotation = _selectedWall.Rotation });
//connection?.InvokeAsync("SendMessage", $"{_selectedTile.X}:{_selectedTile.Y}", tileId.ToString(), _session);
}
}
protected override void Draw(GameTime gameTime)
{
if (_spriteBatch is null)
{
return;
}
GraphicsDevice.Clear(Color.DarkGray);
// TODO: Add your drawing code here
var visibleTilesX = (int)GraphicsDevice.Viewport.Width / _tileSize + 1;
var visibleTilesY = (int)GraphicsDevice.Viewport.Height / _tileSize + 1;
_spriteBatch.Begin(transformMatrix: Matrix.CreateTranslation(_viewportCenter));
for (var i = 0; i < visibleTilesX; i++)
{
var posX1 = i * _tileSize - _viewportCenter.X;
var posY1 = -_viewportCenter.Y;
posX1 = posX1 - posX1 % _tileSize;
posY1 = posY1 - posY1 % _tileSize;
var posX2 = i * _tileSize - _viewportCenter.X;
var posY2 = GraphicsDevice.Viewport.Height - _viewportCenter.Y;
posX2 = posX2 - posX2 % _tileSize;
posY2 = posY2 - posY2 % _tileSize;
_spriteBatch.DrawLine(
posX1, posY1,
posX2,
posY2,
Color.Black);
}
for (var i = 0; i < visibleTilesY; i++)
{
var posX1 = -_viewportCenter.X;
var posY1 = i * _tileSize - _viewportCenter.Y;
posX1 = posX1 - posX1 % _tileSize;
posY1 = posY1 - posY1 % _tileSize;
var posX2 = GraphicsDevice.Viewport.Width - _viewportCenter.X;
var posY2 = i * _tileSize - _viewportCenter.Y;
posX2 = posX2 - posX2 % _tileSize;
posY2 = posY2 - posY2 % _tileSize;
_spriteBatch.DrawLine(posX1, posY1,
posX2,
posY2,
Color.Black);
}
foreach (var tile in _map)
{
var content = Content.Load<Texture2D>($"tiles/tile{tile.ID.ToString().PadLeft(2, '0')}");
var destinationRectangle = new Rectangle(tile.X * _tileSize, tile.Y * _tileSize, _tileSize, _tileSize);
var posX = tile.X * _tileSize + _tileSize / 2f;
var posY = tile.Y * _tileSize + _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)_tileSize - 1) / (float)content.Width, SpriteEffects.None, 0);
}
foreach (var wall in _mapWalls)
{
var content = Content.Load<Texture2D>($"walls/wall{wall.ID.ToString().PadLeft(2, '0')}");
var scale = (float)_tileSize / (float)content.Height;
var offset = scale * (float)content.Width / 2f;
var posX = wall.X * _tileSize;
var posY = wall.Y * _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(offset, 0), scale, SpriteEffects.None, 0);
_spriteBatch.Draw(content, new Vector2(posX, posY),null, Color.White, MathHelper.ToRadians(90 * (wall.Rotation - 1)), new Vector2(0, 0), scale, SpriteEffects.None, 0);
}
// _spriteBatch.DrawRectangle(new Rectangle(_selectedTile.X * _tileSize, _selectedTile.Y * _tileSize, _tileSize, _tileSize), Color.Blue, 3);
var startWall = new Vector2(_selectedWall.X * _tileSize, _selectedWall.Y * _tileSize);
if (_insertMode == InsertMode.Wall)
{
_spriteBatch.DrawLine(startWall, _tileSize, MathHelper.ToRadians(90 * _selectedWall.Rotation), Color.Red, 2);
}
_spriteBatch.End();
_desktop?.Render();
base.Draw(gameTime);
}
private float Sign(Point p1, Point p2, Point p3)
{
return (p1.X - p3.X) * (p2.Y - p3.Y) - (p2.X - p3.X) * (p1.Y - p3.Y);
}
private bool PointInTriangle(Point pt, Point v1, Point v2, Point v3)
{
float d1, d2, d3;
bool has_neg, has_pos;
d1 = Sign(pt, v1, v2);
d2 = Sign(pt, v2, v3);
d3 = Sign(pt, v3, v1);
has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
return !(has_neg && has_pos);
}
}
}