feat: add map drawing
This commit is contained in:
56
Client/Domain/Service/MapImageSelector.cs
Normal file
56
Client/Domain/Service/MapImageSelector.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Client.Domain.ValueObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace Client.Domain.Service
|
||||
{
|
||||
public class MapImageSelector
|
||||
{
|
||||
private static readonly uint BLOCK_SIZE = 32768;
|
||||
private static readonly uint DELTA_X = 20;
|
||||
private static readonly uint DELTA_Y = 18;
|
||||
|
||||
public List<MapBlock> SelectImages(float viewportWidth, float viewportHeight, Vector3 heroPosition, float scale)
|
||||
{
|
||||
var viewportCenter = new Tuple<float, float>(viewportWidth / 2, viewportHeight / 2);
|
||||
|
||||
var topLeft = new Tuple<float, float>(
|
||||
heroPosition.X - viewportCenter.Item1 * scale,
|
||||
heroPosition.Y - viewportCenter.Item2 * scale
|
||||
);
|
||||
var bottomRight = new Tuple<float, float>(
|
||||
heroPosition.X + viewportCenter.Item1 * scale,
|
||||
heroPosition.Y + viewportCenter.Item2 * scale
|
||||
);
|
||||
|
||||
uint top = (uint)(MathF.Floor(topLeft.Item2 / BLOCK_SIZE) + DELTA_Y);
|
||||
uint bottom = (uint)(MathF.Floor(bottomRight.Item2 / BLOCK_SIZE) + DELTA_Y);
|
||||
uint left = (uint)(MathF.Floor(topLeft.Item1 / BLOCK_SIZE) + DELTA_X);
|
||||
uint right = (uint)(MathF.Floor(bottomRight.Item1 / BLOCK_SIZE) + DELTA_X);
|
||||
|
||||
List<MapBlock> result = new List<MapBlock>();
|
||||
for (uint x = left; x <= right; x++)
|
||||
{
|
||||
for (uint y = top; y <= bottom; y++)
|
||||
{
|
||||
Tuple<int, int> blockTopLeft = new Tuple<int, int>(
|
||||
(int)((x - DELTA_X) * BLOCK_SIZE),
|
||||
(int)((y - DELTA_Y) * BLOCK_SIZE)
|
||||
);
|
||||
Tuple<float, float> delta = new Tuple<float, float>(
|
||||
(blockTopLeft.Item1 - topLeft.Item1) / scale,
|
||||
(blockTopLeft.Item2 - topLeft.Item2) / scale
|
||||
);
|
||||
|
||||
result.Add(new MapBlock(x, y, delta.Item1, delta.Item2, BLOCK_SIZE / scale));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
32
Client/Domain/ValueObjects/MapBlock.cs
Normal file
32
Client/Domain/ValueObjects/MapBlock.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Client.Domain.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.ValueObjects
|
||||
{
|
||||
public class MapBlock : ObservableObject
|
||||
{
|
||||
private float deltaX;
|
||||
private float deltaY;
|
||||
private float size;
|
||||
|
||||
public uint Id => (BlockX + BlockY) * (BlockX + BlockY + 1) / 2 + BlockX;
|
||||
public uint BlockX { get; set; }
|
||||
public uint BlockY { get; set; }
|
||||
public float DeltaX { get => deltaX; set { if (value != deltaX) { deltaX = value; OnPropertyChanged(); } } }
|
||||
public float DeltaY { get => deltaY; set { if (value != deltaY) { deltaY = value; OnPropertyChanged(); } } }
|
||||
public float Size { get => size; set { if (value != size) { size = value; OnPropertyChanged(); } } }
|
||||
|
||||
public MapBlock(uint blockX, uint blockY, float deltaX, float deltaY, float size)
|
||||
{
|
||||
BlockX = blockX;
|
||||
BlockY = blockY;
|
||||
DeltaX = deltaX;
|
||||
DeltaY = deltaY;
|
||||
Size = size;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user