-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScenePainter.cs
180 lines (157 loc) · 5.06 KB
/
ScenePainter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Dungeon
{
public class ScenePainter
{
public SizeF Size => new SizeF(currentMap.Dungeon.GetLength(0), currentMap.Dungeon.GetLength(1));
public Size LevelSize => new Size(currentMap.Dungeon.GetLength(0), currentMap.Dungeon.GetLength(1));
private readonly Dictionary<Map, Point[]> paths;
private Map currentMap;
private int mainIteration;
private Bitmap mapImage;
private Point? lastMouseClick;
private IEnumerable<List<Point>> pathsToChests;
private Image grass;
private Image path;
private Image peasant;
private Image castle;
private Image chest;
public ScenePainter(Map[] maps)
{
paths = maps.ToDictionary(x => x, x => TransformPath(x, DungeonTask.FindShortestPath(x)).ToArray());
currentMap = maps[0];
mainIteration = 0;
LoadResources();
CreateMap();
}
// Loading non-string resources via ResourcesManages causes error with BuildTools2022
private void LoadResources()
{
var assembly = Assembly.GetExecutingAssembly();
const string resourcesPrefix = "Dungeon.Images.";
Stream stream;
using (stream = assembly.GetManifestResourceStream($"{resourcesPrefix}Grass.png"))
{
grass = Image.FromStream(stream);
}
using (stream = assembly.GetManifestResourceStream($"{resourcesPrefix}Path.png"))
{
path = Image.FromStream(stream);
}
using (stream = assembly.GetManifestResourceStream($"{resourcesPrefix}Peasant.png"))
{
peasant = Image.FromStream(stream);
}
using (stream = assembly.GetManifestResourceStream($"{resourcesPrefix}Castle.png"))
{
castle = Image.FromStream(stream);
}
using (stream = assembly.GetManifestResourceStream($"{resourcesPrefix}Chest.png"))
{
chest = Image.FromStream(stream);
}
}
public void ChangeLevel(Map newMap)
{
currentMap = newMap;
CreateMap();
mainIteration = 0;
lastMouseClick = null;
pathsToChests = null;
}
public void Update()
{
mainIteration = Math.Min(mainIteration + 1, paths[currentMap].Length - 1);
}
public void OnMouseDown(Point location)
{
lastMouseClick = new Point(location.X, location.Y);
pathsToChests = null;
if (currentMap.InBounds(location) &&
currentMap.Dungeon[lastMouseClick.Value.X, lastMouseClick.Value.Y] == MapCell.Empty)
{
pathsToChests = BfsTask.FindPaths(currentMap, lastMouseClick.Value, currentMap.Chests)
.Select(x => x.ToList()).ToList();
foreach (var pathsToChest in pathsToChests)
pathsToChest.Reverse();
}
}
public void OnMouseUp()
{
pathsToChests = null;
}
public void Paint(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
DrawLevel(g);
DrawMainPath(g, mainIteration);
if (pathsToChests != null && lastMouseClick.HasValue)
DrawAdditionalPaths(g, lastMouseClick.Value);
}
private void DrawLevel(Graphics graphics)
{
graphics.DrawImage(mapImage, new Rectangle(0, 0, LevelSize.Width, LevelSize.Height));
foreach (var chestPoint in currentMap.Chests)
graphics.DrawImage(chest, new Rectangle(chestPoint.X, chestPoint.Y, 1, 1));
graphics.DrawImage(castle, new Rectangle(currentMap.Exit.X, currentMap.Exit.Y, 1, 1));
}
private void DrawPath(Graphics graphics, Color color, IEnumerable<Point> path)
{
var points = path.Select(x => new PointF(x.X + 0.5f, x.Y + 0.5f)).ToArray();
var pen = new Pen(color, 0.15f)
{
DashStyle = DashStyle.Dash,
};
for (var i = 0; i < points.Length - 1; i++)
graphics.DrawLine(pen, points[i], points[i + 1]);
}
private void DrawMainPath(Graphics graphics, int interation)
{
var path = paths[currentMap].Take(interation + 1).ToArray();
DrawPath(graphics, Color.Green, path);
var position = path[path.Length - 1];
graphics.DrawImage(peasant, new Rectangle(position.X, position.Y, 1, 1));
}
private void DrawAdditionalPaths(Graphics graphics, Point lastClick)
{
graphics.FillRectangle(Brushes.Red, new Rectangle(lastClick.X, lastClick.Y, 1, 1));
foreach (var pathToChest in pathsToChests)
DrawPath(graphics, Color.Red, pathToChest);
}
private IEnumerable<Point> TransformPath(Map map, MoveDirection[] path)
{
var walker = new Walker(map.InitialPosition);
yield return map.InitialPosition;
foreach (var direction in path)
{
walker = walker.WalkInDirection(map, direction);
yield return walker.Position;
if (walker.PointOfCollision.HasValue)
break;
}
}
private void CreateMap()
{
var cellWidth = grass.Width;
var cellHeight = grass.Height;
mapImage = new Bitmap(LevelSize.Width * cellWidth, LevelSize.Height * cellHeight);
using (var graphics = Graphics.FromImage(mapImage))
{
for (var x = 0; x < currentMap.Dungeon.GetLength(0); x++)
{
for (var y = 0; y < currentMap.Dungeon.GetLength(1); y++)
{
var image = currentMap.Dungeon[x, y] == MapCell.Wall ? grass : path;
graphics.DrawImage(image, new Rectangle(x * cellWidth, y * cellHeight, cellWidth, cellHeight));
}
}
}
}
}
}