/
ScenePainter.cs
137 lines (121 loc) · 4.1 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
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;
public ScenePainter(Map[] maps)
{
paths = maps.ToDictionary(x => x, x => TransformPath(x, My_DungeonTask.FindShortestPath(x)).ToArray());
currentMap = maps[0];
mainIteration = 0;
CreateMap();
}
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 = My_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 chest in currentMap.Chests)
graphics.DrawImage(Properties.Resources.Chest, new Rectangle(chest.X, chest.Y, 1, 1));
graphics.DrawImage(Properties.Resources.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(Properties.Resources.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 = Properties.Resources.Grass.Width;
var cellHeight = Properties.Resources.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 ? Properties.Resources.Grass : Properties.Resources.Path;
graphics.DrawImage(image, new Rectangle(x * cellWidth, y * cellHeight, cellWidth, cellHeight));
}
}
}
}
}
}