-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMap.cs
More file actions
371 lines (315 loc) · 11.3 KB
/
GameMap.cs
File metadata and controls
371 lines (315 loc) · 11.3 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace MapEditor
{
public partial class GameMap : Form
{
public GameMap()
{
InitializeComponent();
KeyPreview = true;
_Bitmap = new Bitmap(MapBox.Width, MapBox.Height, PixelFormat.Format32bppArgb);
_Gr = Graphics.FromImage(_Bitmap);
_Back_Color = Color.Black;
_Rect = new Rectangle { X = 0, Y = 0, Height = MapBox.Height, Width = MapBox.Width };
_Gr.FillRectangle(new SolidBrush(_Back_Color), _Rect);
_StartPos = new Point((int)Math.Round(MapBox.Width / 2.0), (int)Math.Round(MapBox.Height / 2.0));
_LocationList = new List<CellMap>();
_Nation = new List<Nation>();
_GridType = new Hexagon(32);
_Nation.Add(new Nation(new AxialCoord(0,0)));
_Nation[0].NameNation = "Гоблинвилль";
NationSelect.Items.Add(_Nation[0].NameNation);
NationSelect.SelectedIndex = 0;
_Location = _Nation[NationSelect.SelectedIndex].NationCoord;
LoadMap();
BackgroundSelect.Items.Add("Empty");
GexesList.ImageSize = new Size(64, 56);
string Path = Directory.GetCurrentDirectory() + _Path;
string[] Files = Directory.GetFiles(Path + @"Background\");
for (int i = 0; i < Files.Length; i++)
{
string Tag = Files[i].Replace(Path + @"Background\", "").Replace(".png", "");
GexesList.Images.Add(Tag, Image.FromFile(Files[i]));
BackgroundSelect.Items.Add(Tag);
}
Files = Directory.GetFiles(Path + @"Special\");
for (int i = 0; i < Files.Length; i++)
{
string Tag = Files[i].Replace(Path + @"Special\", "").Replace(".png", "");
GexesList.Images.Add(Tag, Image.FromFile(Files[i]));
}
Files = Directory.GetFiles(Path + @"Background\Road\");
for (int i = 0; i < Files.Length; i++)
{
string Tag = Files[i].Replace(Path + @"Background\Road\", "").Replace(".png", "");
GexesList.Images.Add(Tag, Image.FromFile(Files[i]));
}
Files = Directory.GetFiles(Path + @"Background\River\");
for (int i = 0; i < Files.Length; i++)
{
string Tag = Files[i].Replace(Path + @"Background\River\", "").Replace(".png", "");
GexesList.Images.Add(Tag, Image.FromFile(Files[i]));
}
BackgroundSelect.SelectedIndex = 0;
BackgroundBox.Visible = false;
FogOfWarCheck.Checked = true;
DrawField();
}
private void MapEditor_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void MapEditor_Resize(object sender, EventArgs e)
{
MapBox.Size = new Size { Width = Size.Width - 310, Height = Size.Height - 57 };
_Bitmap = new Bitmap(MapBox.Width, MapBox.Height);
_Gr = Graphics.FromImage(_Bitmap);
_Gr.FillRectangle(new SolidBrush(_Back_Color), 0, 0, MapBox.Width, MapBox.Height);
}
private void MapEditor_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q) _Location = _Location.ToCube().Neighbor((int)CubeCoord.Directions.UpLeft).ToAxial();
else if (e.KeyCode == Keys.W) _Location = _Location.ToCube().Neighbor((int)CubeCoord.Directions.Up).ToAxial();
else if (e.KeyCode == Keys.E) _Location = _Location.ToCube().Neighbor((int)CubeCoord.Directions.UpRight).ToAxial();
else if (e.KeyCode == Keys.A) _Location = _Location.ToCube().Neighbor((int)CubeCoord.Directions.DownLeft).ToAxial();
else if (e.KeyCode == Keys.S) _Location = _Location.ToCube().Neighbor((int)CubeCoord.Directions.Down).ToAxial();
else if (e.KeyCode == Keys.D) _Location = _Location.ToCube().Neighbor((int)CubeCoord.Directions.DownRight).ToAxial();
else if (e.KeyCode == Keys.R) CellMap.Find(_LocationList, _Location).RemoveProp(e.Shift);
else if (e.KeyCode == Keys.F) Edit();
else if (e.KeyCode == Keys.C)
{
_StartPos = new Point((int)Math.Round(MapBox.Width / 2.0), (int)Math.Round(MapBox.Height / 2.0));
_Location = _Nation[NationSelect.SelectedIndex].NationCoord;
}
if (FogOfWarCheck.Checked) FogOfWar();
DrawField();
CoordinatesText.Text = $"Координаты: {_Location.ToString()}";
}
private void MapBox_MouseMove(object sender, MouseEventArgs e)
{
CubeCoord Location_ = _GridType.PixelToHex(new PointF(e.X, e.Y), _StartPos).Add(_Location.ToCube());
int Distance = _Location.ToCube().Distance(Location_);
CoordinatesText.Text = $"Дистанция: {Distance}";
}
private void MapBox_MouseDown(object sender, MouseEventArgs e)
{
}
//== КНОПКИ И ПРОЧИЕ КОНТРОЛЫ ==//
private void ButtonSaveScreen_Click(object sender, EventArgs e)
{
DrawField();
SaveFileDialog savedialog = new SaveFileDialog
{
Title = "Сохранить картинку как...",
OverwritePrompt = true,
CheckPathExists = true,
Filter = "Image Files (*.png)|*.png",
FileName = "Map.png"
};
if (savedialog.ShowDialog() == DialogResult.OK)
{
try
{
_Bitmap.Save(savedialog.FileName, ImageFormat.Png);
}
catch
{
MessageBox.Show("Невозможно сохранить изображение", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonSaveMap_Click(object sender, EventArgs e)
{
ButtonMapSave.Enabled = false;
CellMap.Save(_LocationList, _Path);
}
private void ButtonLoadMap_Click(object sender, EventArgs e)
{
LoadMap();
}
private void LoadMap()
{
ButtonMapLoad.Enabled = false;
ButtonMapScreenShot.Enabled = true;
_Path = CellMap.Load(_LocationList, @"Resources/Map.txt");
}
private void BacksBox_SelectedIndexChanged(object sender, EventArgs e)
{
BackgroundSelect.Enabled = false;
BackgroundSelect.Enabled = true;
RefreshMap();
}
private void ModeGodCheck_CheckedChanged(object sender, EventArgs e)
{
if (ModeGodCheck.Checked)
{
BackgroundBox.Visible = true;
}
else
{
BackgroundBox.Visible = false;
FogOfWarCheck.Checked = true;
}
}
//== ==//
private void DrawField()
{
GC.Collect();
_Gr.Clear(_Back_Color);
AxialCoord[] Coord_ = _GridType.СubeSpiral(_Location, 5);
for (int Loc = 0; Loc < Coord_.Length; Loc++)
{
if (FogOfWarCheck.Checked && CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, Coord_[Loc]).State == CellMap.States.Hidden)
{
DrawImage(Coord_[Loc], "Hidden");
}
else if (FogOfWarCheck.Checked && CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, Coord_[Loc]).State == CellMap.States.Shadow)
{
for (int Prop = 0; Prop < 3; Prop++)
if (CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, Coord_[Loc]).Properties[Prop] != "")
DrawImage(Coord_[Loc], CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, Coord_[Loc]).Properties[Prop], (Bitmap)GexesList.Images[GexesList.Images.IndexOfKey("Shadow")]);
}
else
{
for (int Prop = 0; Prop < 3; Prop++)
if (CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, Coord_[Loc]).Properties[Prop] != "")
DrawImage(Coord_[Loc], CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, Coord_[Loc]).Properties[Prop]);
}
}
RefreshMap();
}
private string DrawPath(int Index)
{
string FinalPath = string.Empty;
CellMap Loc;
CubeCoord coord = _Location.ToCube();
for (int i = 0; i < 6; i++)
if ((Loc = CellMap.Find(_LocationList, coord.Neighbor(i).ToAxial())).ID != null)
if (Loc.Properties[Index] != "") FinalPath += i+1;
if (Index == 1) return "Road" + FinalPath;
else return "River" + FinalPath;
}
private void Edit()
{
ButtonMapSave.Enabled = true;
string[] Prop = new string[3] { "", "", "" };
if ((string)BackgroundSelect.SelectedItem != "Empty") Prop[0] = (string)BackgroundSelect.SelectedItem;
else Prop[0] = CellMap.Find(_LocationList, _Location).Properties[0];
if (RoadCheck.Checked) Prop[1] = DrawPath(1);
if (RiverCheck.Checked) Prop[2] = DrawPath(2);
if (CellMap.Find(_LocationList, _Location).ID == null)
{
_LocationList.Add(new CellMap(_Location, Prop));
CellMap.Sort(_LocationList);
}
else CellMap.Find(_LocationList, _Location).SetProp(Prop);
}
private void DrawImage(AxialCoord Pt, string Key, Bitmap SecondImg = null) => DrawImage(Pt, GexesList.Images.IndexOfKey(Key), SecondImg);
private void DrawImage(AxialCoord Pt, int Index, Bitmap SecondImg = null)
{
try
{
PointF coord = _GridType.Draw(Pt.Subtract(_Location), _StartPos);
Bitmap Img = (Bitmap)GexesList.Images[Index];
if (SecondImg != null)
{
Img = ImageAdditions(Img, SecondImg, 200);
}
_Gr.DrawImage(Img, (int)Math.Round(coord.X - 32), (int)Math.Round(coord.Y - 28));
}
catch
{
MessageBox.Show(CellMap.Find(_LocationList, Pt).ToString());
}
}
private void RefreshMap()
{
if (ModeGodCheck.Checked)
{
if ((string)BackgroundSelect.SelectedItem != "Empty") DrawImage(_Location, (string)BackgroundSelect.SelectedItem);
if (RoadCheck.Checked) DrawImage(_Location, "Road");
if (RiverCheck.Checked) DrawImage(_Location, "River");
}
DrawImage(_Location, "Point");
MapBox.Image = _Bitmap;
}
private Bitmap ImageAdditions(Bitmap x, Bitmap y, byte s)
{
if (x == null || y == null)
throw new NullReferenceException();
Bitmap bmp = new Bitmap(
Math.Min(x.Width, y.Width),
Math.Min(x.Height, y.Height),
PixelFormat.Format32bppArgb
);
Color clr0, clr1;
for (int _x = 0; _x < bmp.Width; _x++)
for (int _y = 0; _y < bmp.Height; _y++)
{
clr0 = x.GetPixel(_x, _y);
clr1 = y.GetPixel(_x, _y);
if (clr0.A == 0 || clr1.A == 0)
{
bmp.SetPixel(_x, _y, clr0);
}
else
{
bmp.SetPixel(_x, _y,
Color.FromArgb(
Math.Min(255, clr0.R * (255 - s) / 255 + clr1.R * s / 255),
Math.Min(255, clr0.G * (255 - s) / 255 + clr1.G * s / 255),
Math.Min(255, clr0.B * (255 - s) / 255 + clr1.B * s / 255)
));
}
}
return bmp;
}
private void FogOfWar()
{
if (CellMap.Find(_LocationList, _Location).ID != null)
{
if (CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, _Location).ID == null)
{
_Nation[NationSelect.SelectedIndex].Vision.Add(new CellMap(_Location, CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, _Location).Properties));
CellMap.Sort(_Nation[NationSelect.SelectedIndex].Vision);
}
else
{
CellMap.Find(_Nation[NationSelect.SelectedIndex].Vision, _Location).State = CellMap.States.Visible;
}
}
//for (int dir = 0; dir < _GridType.Directions; dir++)
//{
// CellMap ThisCell = CellMap.Find(_LocationList, _Location.Neighbor(dir).ToAxial());
// // CellMap PrevCell = CellMap.Find(_LocationList, _Location.ToAxial());
// if (CellMap.Find(_Nation.Vision, ThisCell.Coords).ID == null)
// {
// _Nation.Vision.Add(new CellMap(ThisCell.Coords, CellMap.Find(_LocationList, ThisCell.Coords).Properties));
// CellMap.Sort(_Nation.Vision);
// }
// else
// {
// CellMap.Find(_Nation.Vision, ThisCell.Coords).State = CellMap.States.Shadow;
// }
//}
CoordinatesText.Text = $"Координаты: {_Location.ToString()}";
}
private Color _Back_Color;
private Rectangle _Rect;
private Bitmap _Bitmap;
private Graphics _Gr;
private Point _StartPos;
private AxialCoord _Location;
private Hexagon _GridType;
private List<CellMap> _LocationList;
private List<Nation> _Nation;
private string _Path;
}
}