-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScenePainter.cs
114 lines (101 loc) · 3.28 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Rivals
{
public class ScenePainter
{
public SizeF Size => new SizeF(currentMap.Maze.GetLength(0), currentMap.Maze.GetLength(1));
public Size LevelSize => new Size(currentMap.Maze.GetLength(0), currentMap.Maze.GetLength(1));
private Map currentMap;
private int currentIteration;
private Bitmap mapImage;
private Dictionary<Map, List<OwnedLocation>> mapStates;
private Image grass;
private Image path;
private static readonly Color[] colourValues = new []
{
"#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF", "#000000",
"#800000", "#008000", "#000080", "#808000", "#800080", "#008080", "#808080"
}.Select(ColorTranslator.FromHtml).ToArray();
public ScenePainter(Map[] maps)
{
currentMap = maps[0];
PlayLevels(maps);
currentIteration = 0;
LoadResources();
CreateMap();
}
// Loading non-string resources via ResourcesManages causes error with BuildTools2022
private void LoadResources()
{
var assembly = Assembly.GetExecutingAssembly();
const string resourcesPrefix = "Rivals.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);
}
}
private void PlayLevels(Map[] maps)
{
mapStates = new Dictionary<Map, List<OwnedLocation>>();
foreach (var map in maps)
mapStates[map] = RivalsTask.AssignOwners(map).ToList();
}
public void ChangeLevel(Map newMap)
{
currentMap = newMap;
CreateMap();
currentIteration = 0;
}
public void Update()
{
currentIteration = Math.Min(currentIteration + 1, mapStates[currentMap].Count);
}
public void Paint(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawImage(mapImage, new Rectangle(0, 0, LevelSize.Width, LevelSize.Height));
DrawPath(g);
}
private void DrawPath(Graphics graphics)
{
var mapState = mapStates[currentMap];
foreach (var cell in mapState.Take(currentIteration))
{
var rect = new Rectangle(cell.Location.X, cell.Location.Y, 1, 1);
graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, colourValues[cell.Owner % colourValues.Length])), rect);
graphics.DrawString(cell.Distance.ToString(), new Font(SystemFonts.DefaultFont.FontFamily, 0.3f), Brushes.Beige, rect,
new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center});
}
}
private void CreateMap()
{
var dungeonWidth = currentMap.Maze.GetLength(0);
var dungeonHeight = currentMap.Maze.GetLength(1);
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 < dungeonWidth; x++)
{
for (var y = 0; y < dungeonHeight; y++)
{
var image = currentMap.Maze[x, y] == MapCell.Empty ? path : grass;
graphics.DrawImage(image, new Rectangle(x*cellWidth, y*cellHeight, cellWidth, cellHeight));
}
}
}
}
}
}