-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
599 lines (512 loc) · 22.4 KB
/
MainWindow.xaml.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BattleMap
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public class Monster_Token
{
int last_Monster_Position_X;
int last_Monster_Position_Y;
int monster_Speed;
Rectangle last_Monster_Rect;
public Monster_Token(Rectangle last_Monster_Rect = null)
{
last_Monster_Position_X = 0;
last_Monster_Position_Y = 0;
monster_Speed = 0;
this.Last_Monster_Rect = last_Monster_Rect;
}
public Monster_Token(Monster_Token m, Rectangle last_Monster_Rect = null)
{
last_Monster_Position_X = m.last_Monster_Position_X;
last_Monster_Position_Y = m.last_Monster_Position_Y;
monster_Speed = m.monster_Speed;
this.last_Monster_Rect = last_Monster_Rect;
}
public Monster_Token(int last_Monster_Position_X, int last_Monster_Position_Y, int monster_Speed, Rectangle last_Monster_Rect)
{
this.Last_Monster_Position_X = last_Monster_Position_X;
this.Last_Monster_Position_Y = last_Monster_Position_Y;
this.Monster_Speed = monster_Speed;
this.Last_Monster_Rect = last_Monster_Rect;
}
public int Last_Monster_Position_X { get => last_Monster_Position_X; set => last_Monster_Position_X = value; }
public int Last_Monster_Position_Y { get => last_Monster_Position_Y; set => last_Monster_Position_Y = value; }
public int Monster_Speed { get => monster_Speed; set => monster_Speed = value; }
public Rectangle Last_Monster_Rect { get => last_Monster_Rect; set => last_Monster_Rect = value; }
public override bool Equals(object obj)
{
if (((Monster_Token)obj).last_Monster_Position_X == last_Monster_Position_X &&
((Monster_Token)obj).last_Monster_Position_Y == last_Monster_Position_Y &&
((Monster_Token)obj).monster_Speed == monster_Speed &&
((Monster_Token)obj).last_Monster_Rect == last_Monster_Rect)
return true;
return false;
}
}
public partial class MainWindow : Window
{
static List<Monster_Token> Monster_Positions = new List<Monster_Token>();
//List_Last_Positions -> max to 7 records
static List<Monster_Token> List_Last_Positions = new List<Monster_Token>();
//
static List<string> Monster_Actions_List = new List<string>();
static Random rnd = new Random();
static int x = rnd.Next(0, 950);
static int y = rnd.Next(0, 500);
//get last player position
static int last_Player_Position_X = 0;
static int last_Player_Position_Y = 0;
static Rectangle last_player_rect;
public static List<Monster_Token> Monster_Positions1 { get => Monster_Positions; set => Monster_Positions = value; }
public static List<Monster_Token> List_Last_Positions1 { get => List_Last_Positions; set => List_Last_Positions = value; }
public static List<string> Monster_Actions_List1 { get => Monster_Actions_List; set => Monster_Actions_List = value; }
public MainWindow()
{
InitializeComponent();
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
setPlayerPosition();
}
private void setPlayerPosition()
{
x -= (x % 50);
y -= (y % 50);
if (x > 500)
{
x = 500;
}
else if (x < 0)
{
x = 0;
}
if (y > 950)
{
y = 950;
}
else if (y < 50)
{
y = 50;
}
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/player.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
canvas.Children.Add(rect);
last_Player_Position_X = x;
last_Player_Position_Y = y;
last_player_rect = rect;
}
private void canvas_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up)
{
if (y - 50 >= 0)
{
y -= 50;
canvas.Children.Remove(last_player_rect);
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/player.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
canvas.Children.Add(rect);
last_Player_Position_X = x;
last_Player_Position_Y = y;
last_player_rect = rect;
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
}
else if (e.Key == Key.Down)
{
if (y + 50 <= 500)
{
y += 50;
canvas.Children.Remove(last_player_rect);
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/player.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
canvas.Children.Add(rect);
last_Player_Position_X = x;
last_Player_Position_Y = y;
last_player_rect = rect;
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
}
else if (e.Key == Key.Left)
{
if (x - 50 >= 0)
{
x -= 50;
canvas.Children.Remove(last_player_rect);
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/player.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
canvas.Children.Add(rect);
last_Player_Position_X = x;
last_Player_Position_Y = y;
last_player_rect = rect;
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
}
else if (e.Key == Key.Right)
{
if (x + 50 <= 950)
{
x += 50;
canvas.Children.Remove(last_player_rect);
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/player.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
canvas.Children.Add(rect);
last_Player_Position_X = x;
last_Player_Position_Y = y;
last_player_rect = rect;
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
}
}
private void End_turn_Button_Click(object sender, RoutedEventArgs e)
{
Monster_Actions_List1.Clear();
Random rd = new Random();
int pX = 0;
int pY = 0;
int clicks = 0;
ComboBox_Monster_List.Items.Clear();
for (int i = 0; i < Monster_Positions1.Count; i++)
{
int X = rd.Next(0, Monster_Positions1[i].Monster_Speed);
int Y = rd.Next(0, Monster_Positions1[i].Monster_Speed);
int Plus_Minus = rd.Next(0, 2);
canvas.Children.Remove(Monster_Positions1[i].Last_Monster_Rect);
// 0 = Plus | 1 = Minus
if (Plus_Minus == 0)
{
if (((Monster_Positions1[i].Last_Monster_Position_X + X) >= 0) && ((Monster_Positions1[i].Last_Monster_Position_X + X) <= 950))
{
for (int j = 0; j < List_Last_Positions1.Count(); j++)
{
if ((Monster_Positions1[i].Last_Monster_Position_X + X) != List_Last_Positions1[j].Last_Monster_Position_X)
{
pX = Monster_Positions1[i].Last_Monster_Position_X + X;
}
else
{
pX = Monster_Positions1[i].Last_Monster_Position_X;
}
}
}
if (((Monster_Positions1[i].Last_Monster_Position_Y + Y) >= 0) && ((Monster_Positions1[i].Last_Monster_Position_Y + Y) <= 500))
{
for (int j = 0; j < List_Last_Positions1.Count(); j++)
{
if ((Monster_Positions1[i].Last_Monster_Position_Y + Y) != List_Last_Positions1[j].Last_Monster_Position_Y)
{
pY = Monster_Positions1[i].Last_Monster_Position_Y + Y;
}
else
{
pY = Monster_Positions1[i].Last_Monster_Position_Y;
}
}
}
}
else if (Plus_Minus == 1)
{
if (((Monster_Positions1[i].Last_Monster_Position_X - X) >= 0) && ((Monster_Positions1[i].Last_Monster_Position_X - X) <= 950))
{
for (int j = 0; j < List_Last_Positions1.Count(); j++)
{
if ((Monster_Positions1[i].Last_Monster_Position_X - X) != List_Last_Positions1[j].Last_Monster_Position_X)
{
pX = Monster_Positions1[i].Last_Monster_Position_X - X;
}
else
{
pX = Monster_Positions1[i].Last_Monster_Position_X;
}
}
}
if (((Monster_Positions1[i].Last_Monster_Position_Y - Y) >= 0) && ((Monster_Positions1[i].Last_Monster_Position_Y - Y) <= 500))
{
for (int j = 0; j < List_Last_Positions1.Count(); j++)
{
if ((Monster_Positions1[i].Last_Monster_Position_Y - Y) != List_Last_Positions1[i].Last_Monster_Position_Y)
{
pY = Monster_Positions1[i].Last_Monster_Position_Y - Y;
}
else
{
pY = Monster_Positions1[i].Last_Monster_Position_Y;
}
}
}
}
if (i > 0)
{
if (Plus_Minus == 0)
{
if (pX == (Monster_Positions1[i - 1].Last_Monster_Position_X))
{
if ((pX += 50) <= 950)
{
pX += 50;
}
else
{
pX -= 50;
}
}
if (pY == (Monster_Positions1[i - 1].Last_Monster_Position_Y))
{
if ((pY += 50) <= 500)
{
pY += 50;
}
else
{
pY -= 50;
}
}
}
else if (Plus_Minus == 1)
{
if (pX == (Monster_Positions1[i - 1].Last_Monster_Position_X))
{
if ((pX -= 50) >= 0)
{
pX -= 50;
}
else
{
pX += 50;
}
}
if (pY == (Monster_Positions1[i - 1].Last_Monster_Position_Y))
{
if ((pY -= 50) >= 0)
{
pY -= 50;
}
else
{
pY += 50;
}
}
}
}
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/Monster.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, pX);
Canvas.SetTop(rect, pY);
canvas.Children.Add(rect);
//Generate monster action
int Determine_Action = rd.Next(1, 5);
if (Determine_Action == 1 || Determine_Action == 3)
{
int Monster_D20 = rd.Next(1, 21);
Monster_Actions_List1.Add("Monster " + (i + 1) + " rolled " + Monster_D20 + " on its d20");
}
else
{
Monster_Actions_List1.Add("Monster " + (i + 1) + " just moved arround...");
}
Monster_Positions1[i].Last_Monster_Position_X = pX;
Monster_Positions1[i].Last_Monster_Position_Y = pY;
Monster_Positions1[i].Last_Monster_Rect = rect;
clicks++;
if (clicks == 10)
{
List_Last_Positions1.RemoveAt(0);
clicks = 0;
}
}
for (int i = 0; i < Monster_Positions1.Count(); i++)
{
ComboBox_Monster_List.Items.Add("Monster " + (i + 1) + ": X-> " + Monster_Positions1[i].Last_Monster_Position_X + " | Y-> " + Monster_Positions1[i].Last_Monster_Position_Y);
}
string Actions = string.Join("\r\n", Monster_Actions_List1.ToArray());
textbox.Text = Actions;
}
private void Add_Monster_Click(object sender, RoutedEventArgs e)
{
Add_Monster_Func();
}
private void Monster_Speed_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
private void Monster_Speed_MouseDown(object sender, MouseButtonEventArgs e)
{
Monster_Speed.Focusable = true;
}
private void Monster_Speed_MouseLeave(object sender, MouseEventArgs e)
{
canvas.Focus();
Monster_Speed.Focusable = false;
}
private void Monster_Speed_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Add_Monster_Func();
}
}
private void Add_Monster_Func()
{
try
{
Random r = new Random();
int Nx = r.Next(0, 950);
int Ny = r.Next(0, 500);
int Speed = Int32.Parse(Monster_Speed.Text);
if ((Speed < 50) || Speed.Equals("") || Speed.Equals(" "))
{
Speed = 50;
}
else if (Speed > 150)
{
Speed = 150;
}
if ((Nx != last_Player_Position_X) && (Ny != last_Player_Position_Y))
{
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/Monster.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, Nx);
Canvas.SetTop(rect, Ny);
canvas.Children.Add(rect);
int last_Monster_Position_X = Nx;
int last_Monster_Position_Y = Ny;
Monster_Positions1.Add(new Monster_Token(last_Monster_Position_X, last_Monster_Position_Y, Speed, rect));
List_Last_Positions1.Add(new Monster_Token(last_Monster_Position_X, last_Monster_Position_Y, Speed, rect));
int pos = Monster_Positions1.Count() - 1;
ComboBox_Monster_List.Items.Add("Monster " + (pos + 1) + ": X-> " + Monster_Positions1.ElementAt(Monster_Positions1.Count() - 1).Last_Monster_Position_X + " | Y-> " + Monster_Positions1.ElementAt(Monster_Positions1.Count() - 1).Last_Monster_Position_Y);
Monster_Speed.Focusable = false;
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
else
{
int Second_P_X = r.Next(0, 950);
int Second_P_Y = r.Next(0, 500);
Rectangle rect;
rect = new Rectangle();
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(@"img/Monster.png", UriKind.Relative));
rect.Fill = imgBrush;
rect.Width = 50;
rect.Height = 50;
Canvas.SetLeft(rect, Second_P_X);
Canvas.SetTop(rect, Second_P_Y);
canvas.Children.Add(rect);
int Second_last_Monster_Position_X = Second_P_X;
int Second_last_Monster_Position_Y = Second_P_Y;
Monster_Positions1.Add(new Monster_Token(Second_last_Monster_Position_X, Second_last_Monster_Position_Y, Speed, rect));
List_Last_Positions1.Add(new Monster_Token(Second_last_Monster_Position_X, Second_last_Monster_Position_Y, Speed, rect));
int pos = Monster_Positions1.Count() - 1;
ComboBox_Monster_List.Items.Add("Monster " + (pos + 1) + ": X-> " + Monster_Positions1.ElementAt(Monster_Positions1.Count() - 1).Last_Monster_Position_X + " | Y-> " + Monster_Positions1.ElementAt(Monster_Positions1.Count() - 1).Last_Monster_Position_Y);
Monster_Speed.Focusable = false;
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
}
catch
{
MessageBox.Show("Something went wrong :(\r\n\r\nPlease try again and if the problem persists, feel free to contact me.\r\n\r\nSomething that might be causing the problem is that you didn't add a value to the monster speed.", "Something went wrong");
}
}
private void Remove_Monster_Click(object sender, RoutedEventArgs e)
{
try
{
canvas.Children.Remove(Monster_Positions1[ComboBox_Monster_List.SelectedIndex].Last_Monster_Rect);
Monster_Positions1.RemoveAt(ComboBox_Monster_List.SelectedIndex);
ComboBox_Monster_List.Items.Remove(ComboBox_Monster_List.SelectedIndex);
ComboBox_Monster_List.Items.Clear();
for (int i = 0; i < Monster_Positions1.Count(); i++)
{
ComboBox_Monster_List.Items.Add("Monster " + (i + 1) + ": X-> " + Monster_Positions1[i].Last_Monster_Position_X + " | Y-> " + Monster_Positions1[i].Last_Monster_Position_Y);
}
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
catch
{
MessageBox.Show("You must select a monster to remove.");
}
}
private void ComboBox_Monster_List_MouseLeave(object sender, MouseEventArgs e)
{
canvas.Focus();
ComboBox_Monster_List.Focusable = false;
Remove_Monster.Focusable = false;
}
}
}