Maze /MapEditor/Forms/Editor.cs

Language C# Lines 669
MD5 Hash 608e3f9cf5ecd715757729be8e7eaa74 Estimated Cost $30,203 (why?)
Repository git://github.com/Concliff/Maze.git View Raw File View Project SPDX
  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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Maze.Classes;

namespace MapEditor.Forms
{
    public partial class Editor : Form
    {
        /// <summary>
        /// Central position of the displayed map.
        /// </summary>
        private GPS centralGPS;
        private CellEdit cellEditForm;

        private Point capturedMousePoint;
        private bool isCapturingMove;

        private string[] mapNames;
        private int currentMapIndex;
        private int levelsCount;
        private bool isNewMap;

        private Dictionary<GridLocation, Cell> mapCells;
        private Dictionary<int, GridLocation> startLocations;
        private Dictionary<int, GridLocation> finishLocations;

        private int maxCellId;

        public Editor()
        {
            InitializeComponent();
            CustomInitialize();

            this.mapCells = new Dictionary<GridLocation, Cell>();
            this.startLocations = new Dictionary<int, GridLocation>();
            this.finishLocations = new Dictionary<int, GridLocation>();

            this.currentMapIndex = -1;

            this.pr_isMapSaved = true;

            LoadMapNames();
            if (this.mapNames.Length > 0)
                LoadMap(0);

            this.nudCurrentLevel.Value = 1;

            Invalidate();
            this.Focus();
        }

        private int pr_currentLevel;
        /// <summary>
        /// Gets or sets the current level of the map.
        /// </summary>
        private int currentLevel
        {
            get
            {
                return this.pr_currentLevel;
            }
            set
            {
                this.pr_currentLevel = value;
                // Return center to the Start point
                if (this.startLocations.ContainsKey(this.pr_currentLevel))
                    this.centralGPS = new GPS(this.startLocations[this.pr_currentLevel], 25, 25);
                else
                    this.centralGPS = new GPS(new GridLocation(0, 0, 0, this.pr_currentLevel), 25, 25);
                this.pbMap.Refresh();
            }
        }

        public int NewCellID
        {
            get
            {
                return this.maxCellId + 1;
            }
        }

        private bool pr_isMapSaved;
        private bool isMapSaved
        {
            get
            {
                return this.pr_isMapSaved;
            }
            set
            {
                if (this.pr_isMapSaved == value)
                    return;

                if (value)
                {
                    this.lblIsMapSaved.Text = "Saved";
                    this.btnSave.Enabled = false;
                }
                else
                {
                    this.lblIsMapSaved.Text = "";
                    this.btnSave.Enabled = true;
                }

                this.pr_isMapSaved = value;
            }

        }

        private void LoadMapNames()
        {
            DirectoryInfo mapDirectory = new DirectoryInfo(GlobalConstants.MAPS_PATH);
            FileInfo[] mapFiles = mapDirectory.GetFiles();
            List<string> mapNames = new List<string>();

            foreach (FileInfo fi in mapFiles)
            {
                if (fi.Extension == ".map")
                    mapNames.Add(Path.GetFileNameWithoutExtension(fi.FullName));
            }

            this.mapNames = mapNames.ToArray();
            this.cboCurrentMap.Items.AddRange(this.mapNames);
            this.cboCurrentMap.SelectedIndex = 0;
        }

        private void LoadMap(int mapIndex)
        {
            // An attempt to reload the current map.
            if (this.currentMapIndex == mapIndex)
                return;

            string mapName = null;
            if (mapIndex != -1)
            {
                try
                {
                    mapName = this.mapNames[mapIndex];
                }
                catch (IndexOutOfRangeException)
                {
                    MessageBox.Show("Map with index '" + mapIndex.ToString() + "' doesn't exist");
                    return;
                }
            }

            this.mapCells = new Dictionary<GridLocation, Cell>();
            this.startLocations = new Dictionary<int, GridLocation>();
            this.finishLocations = new Dictionary<int, GridLocation>();
            this.currentMapIndex = mapIndex;
            this.levelsCount = 1;
            this.maxCellId = 0;

            if (mapIndex != -1)
            {
                StreamReader reader = File.OpenText(GlobalConstants.MAPS_PATH + this.mapNames[this.currentMapIndex] + ".map");
                string currentString;
                while ((currentString = reader.ReadLine()) != null)
                {
                    string[] stringStruct = new string[10];
                    stringStruct = currentString.Split(' ');
                    Cell cell;

                    cell.ID = Convert.ToInt32(stringStruct[0]);
                    cell.Location.X = Convert.ToInt32(stringStruct[1]);
                    cell.Location.Y = Convert.ToInt32(stringStruct[2]);
                    cell.Location.Z = Convert.ToInt32(stringStruct[3]);
                    cell.Location.Level = Convert.ToInt32(stringStruct[4]);
                    cell.Type = Convert.ToUInt32(stringStruct[5]);
                    cell.Attribute = Convert.ToUInt32(stringStruct[6]);
                    cell.Option = Convert.ToUInt32(stringStruct[7]);
                    cell.OptionValue = Convert.ToInt32(stringStruct[8]);
                    cell.ND4 = Convert.ToInt32(stringStruct[9]);

                    AddCell(cell);
                }
                reader.Close();

                this.isNewMap = false;
                this.tbxMapName.Text = mapName;
                this.isMapSaved = true;
            }
            else
            {
                this.isNewMap = true;
                this.tbxMapName.Text = "Enter_name";
                this.tbxMapName.Focus();
            }

            // Set the current Level to 0
            // This also changes this.currentLevel field
            this.nudCurrentLevel.Value = 1;
            this.pbMap.Invalidate();
        }

        private void SaveMap()
        {
            string mapName = this.tbxMapName.Text;
            if (string.IsNullOrEmpty(mapName))
            {
                MessageBox.Show("Enter Map Name", "Saving Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbxMapName.Focus();
                return;
            }
            for (int i = 0; i < this.mapNames.Length; ++i)
            {
                if (i == this.currentMapIndex)
                    continue;
                if (this.mapNames[i] == mapName)
                {
                    MessageBox.Show("Map \"" + mapName + "\" already exists.", "Saving Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.tbxMapName.Focus();
                    return;
                }
            }

            StreamWriter writer = new StreamWriter(GlobalConstants.MAPS_PATH + mapName + ".map", false);
            string cellString;

            foreach (KeyValuePair<GridLocation, Cell> cell in this.mapCells)
            {
                cellString = cell.Value.ID.ToString() + " "
                    + cell.Value.Location.X.ToString() + " "
                    + cell.Value.Location.Y.ToString() + " "
                    + cell.Value.Location.Z.ToString() + " "
                    + cell.Value.Location.Level.ToString() + " "
                    + cell.Value.Type.ToString() + " "
                    + cell.Value.Attribute.ToString() + " "
                    + cell.Value.Option.ToString() + " "
                    + cell.Value.OptionValue.ToString() + " "
                    + cell.Value.ND4.ToString();
                writer.WriteLine(cellString);
            }
            writer.Close();

            if (this.isNewMap)
            {
                Array.Resize<string>(ref this.mapNames, this.mapNames.Length + 1);
                this.currentMapIndex = this.mapNames.Length - 1;
                this.mapNames[this.currentMapIndex] = mapName;
                this.cboCurrentMap.Items.Clear();
                this.cboCurrentMap.Items.AddRange(this.mapNames);
                this.cboCurrentMap.SelectedIndex = this.currentMapIndex;
                this.isNewMap = false;
            }
            else
            {
                // Change displaying Map Name if it has been changed
                if (mapName != this.mapNames[this.currentMapIndex])
                {
                    // Drop the previous map file
                    File.Delete(GlobalConstants.MAPS_PATH + this.mapNames[this.currentMapIndex] + ".map");
                    this.cboCurrentMap.Items[this.currentMapIndex] = mapName;
                    this.mapNames[this.currentMapIndex] = mapName;
                }
            }

            // Mark that map is at newer verstion
            this.isMapSaved = true;
        }

        public void AddCell(Cell cell)
        {
            if (this.mapCells.ContainsKey(cell.Location))
            {
                ReplaceCell(cell);
                return;
            }

            this.mapCells.Add(cell.Location, cell);

            if (Convert.ToInt32(cell.Location.Level) >= this.levelsCount)
                ++this.levelsCount;

            if (cell.HasAttribute(CellAttributes.IsStart))
                this.startLocations[cell.Location.Level] = cell.Location;
            if (cell.HasAttribute(CellAttributes.IsFinish))
                this.finishLocations[cell.Location.Level] = cell.Location;

            if (cell.ID > this.maxCellId)
                this.maxCellId = cell.ID;

            this.isMapSaved = false;
        }

        public void RemoveCell(Cell cell)
        {
            this.mapCells.Remove(cell.Location);

            if (this.maxCellId == cell.ID)
                --this.maxCellId;

            if (cell.HasAttribute(CellAttributes.IsStart))
                this.startLocations.Remove(cell.Location.Level);
            if (cell.HasAttribute(CellAttributes.IsFinish))
                this.startLocations.Remove(cell.Location.Level);

            this.isMapSaved = false;
        }

        public void ReplaceCell(Cell cell)
        {
            RemoveCell(cell);
            AddCell(cell);
        }

        public Cell GetCell(GridLocation location)
        {
            Cell cell;
            if (!this.mapCells.TryGetValue(location, out cell))
            {
                // default cell
                cell.Initialize();
                // but specified location
                cell.Location = location;
            }

            return cell;
        }

        void pbMap_MouseClick(object sender, MouseEventArgs e)
        {
            // Only Left Mouse Button
            if (e.Button != System.Windows.Forms.MouseButtons.Right)
                return;

            GridLocation cursorLocation = new GridLocation();

            // Calculate Mouse absolute position
            // i.e How far it is from the cetral position
            cursorLocation.X = this.centralGPS.Absolute.X - (this.pbMap.Size.Width / 2 - e.Location.X);
            cursorLocation.Y = this.centralGPS.Absolute.Y - (this.pbMap.Size.Height / 2 - e.Location.Y);
            cursorLocation.Z = this.centralGPS.Absolute.Z;
            cursorLocation.Level = this.centralGPS.Location.Level;

            GPS cursorGPS = new GPS();
            cursorGPS.Absolute = cursorLocation;

            Cell cell = GetCell(cursorGPS.Location);

            // Prevent double BlockEdit window openning
            if (this.cellEditForm == null)
                this.cellEditForm = new CellEdit(cell);
            else
            {
                this.cellEditForm.Close();
                this.cellEditForm = new CellEdit(cell);
            }
            this.cellEditForm.FormClosing += (sender_, e_) => { this.pbMap.Refresh(); };
            this.cellEditForm.ShowDialog();
        }

        void pbMap_Paint(object sender, PaintEventArgs e)
        {
            Graphics gGraphic = e.Graphics;

            GridLocation cellLocation = new GridLocation();
            Cell cell = new Cell();
            // CellGraph
            int cellsCountWidth = (int)Math.Ceiling(this.pbMap.Size.Width / 2d / GlobalConstants.CELL_WIDTH) * 2 + 1;
            int cellsCountHeight = (int)Math.Ceiling(this.pbMap.Size.Height / 2d / GlobalConstants.CELL_HEIGHT) * 2 + 1;

            // HACK: Correction values because the width and height of drawing region are not a multiple of CELL_WIDTH and CELL_HEIGHT
            int xCorrection = ((int)Math.Ceiling(this.pbMap.Size.Height * 1d / GlobalConstants.CELL_HEIGHT) * GlobalConstants.CELL_HEIGHT - this.pbMap.Size.Height) / 2;
            int yCorrection = ((int)Math.Ceiling(this.pbMap.Size.Width * 1d / GlobalConstants.CELL_WIDTH) * GlobalConstants.CELL_WIDTH - this.pbMap.Size.Width) / 2;

            for (int i = 0; i < cellsCountWidth; ++i)
                for (int j = 0; j < cellsCountHeight; ++j)
                {
                    int x, y;
                    x = (i - 1) * GlobalConstants.CELL_WIDTH - this.centralGPS.X + xCorrection + 25;
                    y = (j - 1) * GlobalConstants.CELL_HEIGHT - this.centralGPS.Y + yCorrection + 25;
                    cellLocation.X = centralGPS.Location.X + i - cellsCountWidth / 2;
                    cellLocation.Y = centralGPS.Location.Y + j - cellsCountHeight / 2;
                    cellLocation.Z = centralGPS.Location.Z;
                    cellLocation.Level = centralGPS.Location.Level;
                    cell = GetCell(cellLocation);

                    gGraphic.DrawImage(PictureManager.GetPictureByType(cell.Type), x, y, GlobalConstants.CELL_WIDTH, GlobalConstants.CELL_HEIGHT);

                    // Draw Start Block
                    if (cell.HasAttribute(CellAttributes.IsStart))
                    {
                        gGraphic.DrawImage(PictureManager.StartImage, x + 5, y + 5, 40, 40);
                    }

                    // Draw Finish Block
                    if (cell.HasAttribute(CellAttributes.IsFinish))
                    {
                        gGraphic.DrawImage(PictureManager.FinishImage, x + 5, y + 5, PictureManager.FinishImage.Width, PictureManager.FinishImage.Height);
                    }

                    // Draw Ooze Drop
                    if (cell.HasAttribute(CellAttributes.HasDrop))
                    {
                        gGraphic.DrawImage(PictureManager.DropImage, x + 15, y + 10, 20, 30);
                    }

                    // Portal
                    if (cell.HasOption(CellOptions.Portal))
                    {
                        Image image = PictureManager.PortalImage;
                        gGraphic.DrawImage(image,
                            x + (GlobalConstants.CELL_WIDTH - image.Width) / 2,
                            y + (GlobalConstants.CELL_HEIGHT - image.Height) / 2,
                            PictureManager.PortalImage.Width, PictureManager.PortalImage.Height);
                    }
                }
        }

        void nudCurrentLevel_ValueChanged(object sender, System.EventArgs e)
        {
            if (this.nudCurrentLevel.Value < 1)
                this.nudCurrentLevel.Value = 1;
            else if (this.nudCurrentLevel.Value > this.levelsCount)
                this.nudCurrentLevel.Value = this.levelsCount;

            this.currentLevel = (int)this.nudCurrentLevel.Value - 1;
        }

        void cboCurrentMap_SelectedValueChanged(object sender, System.EventArgs e)
        {
            if (!this.isMapSaved && this.currentMapIndex != this.cboCurrentMap.SelectedIndex)
            {
                System.Windows.Forms.DialogResult result = MessageBox.Show("Would you like to save the current map?", "Map Saving", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == System.Windows.Forms.DialogResult.Yes)
                    SaveMap();
                else if (result == System.Windows.Forms.DialogResult.Cancel)
                    this.cboCurrentMap.SelectedIndex = this.currentMapIndex;

            }
            LoadMap(this.cboCurrentMap.SelectedIndex);
        }

        private void btnAddMap_Click(object sender, System.EventArgs e)
        {
            this.cboCurrentMap.SelectedIndex = -1;
        }

        private void btnRemoveMap_Click(object sender, System.EventArgs e)
        {
            // TODO: Remove current map
        }

        private void btnAddLevel_Click(object sender, System.EventArgs e)
        {
            ++this.levelsCount;
            this.nudCurrentLevel.Value = (int)this.levelsCount;
        }

        private void btnRemoveLevel_Click(object sender, System.EventArgs e)
        {
            // TODO: remove current level
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            SaveMap();
        }

        void tbxMapName_TextChanged(object sender, System.EventArgs e)
        {
            this.isMapSaved = false;
        }

        private void pbMap_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left)
                return;

            this.capturedMousePoint = Cursor.Position;
            this.isCapturingMove = true;
            Cursor = Cursors.Hand;
        }

        private void pbMap_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left)
                return;

            this.isCapturingMove = false;
            Cursor = DefaultCursor;
        }

        private void pbMap_MouseMove(object sender, MouseEventArgs e)
        {
            if (!this.isCapturingMove)
                return;
            // Find out where the mouse were moved
            // and these changes apply to the new Map center
            GridLocation prevLocation = this.centralGPS.Absolute;
            prevLocation.X += this.capturedMousePoint.X - Cursor.Position.X;
            prevLocation.Y += this.capturedMousePoint.Y - Cursor.Position.Y;
            this.centralGPS.Absolute = prevLocation;

            //Cursor.Position = this.capturedMousePoint;
            this.capturedMousePoint = Cursor.Position;
            this.pbMap.Refresh();
        }

        private void Editor_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!this.isMapSaved)
            {
                System.Windows.Forms.DialogResult result = MessageBox.Show("Would you like to save the current map?", "Map Saving", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == System.Windows.Forms.DialogResult.Yes)
                    SaveMap();
                else if (result == System.Windows.Forms.DialogResult.Cancel)
                    e.Cancel = true;
            }
        }


        private void pbMap_MouseLeave(object sender, System.EventArgs e)
        {
            this.tbxMapName.Focus();
        }

        private void pbMap_MouseEnter(object sender, System.EventArgs e)
        {
            this.pbMap.Focus();
        }

        private void pbMap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.W && e.KeyCode != Keys.A && e.KeyCode != Keys.S && e.KeyCode != Keys.D && e.KeyCode != Keys.Z && e.KeyCode != Keys.R)
                return;

            // Compute the cursor GPS
            Point cursorPoint = this.pbMap.PointToClient(Cursor.Position);
            //Point cursorLocation = new Point(Cursor.Position.X - pbMapPoint.X, Cursor.Position.Y - pbMapPoint.Y);

            GridLocation cursorLocation = new GridLocation();

            // Calculate Mouse absolute position
            // i.e How far it is from the cetral position
            cursorLocation.X = this.centralGPS.Absolute.X - (this.pbMap.Size.Width / 2 - cursorPoint.X);
            cursorLocation.Y = this.centralGPS.Absolute.Y - (this.pbMap.Size.Height / 2 - cursorPoint.Y);
            cursorLocation.Z = this.centralGPS.Absolute.Z;
            cursorLocation.Level = this.centralGPS.Location.Level;

            GPS cursorGPS = new GPS();
            cursorGPS.Absolute = cursorLocation;

            Cell cell = GetCell(cursorGPS.Location);

            if (cell.Type == (uint)Directions.None)
                cell.Type = 0;

            byte modifiedDirection = 0;

            switch (e.KeyCode)
            {
                case Keys.W:
                    modifiedDirection += (byte)Directions.Up;
                    break;
                case Keys.A:
                    modifiedDirection += (byte)Directions.Left;
                    break;
                case Keys.S:
                    modifiedDirection += (byte)Directions.Down;
                    break;
                case Keys.D:
                    modifiedDirection += (byte)Directions.Right;
                    break;
                case Keys.Z:
                    modifiedDirection += (byte)Directions.Up + (byte)Directions.Left + (byte)Directions.Down + (byte)Directions.Right;
                    break;
                case Keys.R:
                    // Mark the cell with every direction to change
                    // then change the current cell Type to these direction
                    // that allows to change all the neighbours and
                    // remove the cell because it will be marked as 'no way to go'.
                    modifiedDirection += (byte)Directions.Up + (byte)Directions.Left + (byte)Directions.Down + (byte)Directions.Right;
                    cell.Type = modifiedDirection;
                    break;
            }

            if ((modifiedDirection & (byte)Directions.Up) != 0)
            {
                cell.Type ^= (uint)Directions.Up;
                // Neighbour Cell
                Cell neighbourCell = GetCell(new GridLocation(cell.Location.X, cell.Location.Y - 1, cell.Location.Z, cell.Location.Level));
                if (neighbourCell.ID == -1)
                {
                    neighbourCell.ID = NewCellID;
                    neighbourCell.Type = 0;
                }
                if ((cell.Type & (uint)Directions.Up) != (neighbourCell.Type & (uint)Directions.Down))
                    neighbourCell.Type ^= (uint)Directions.Down;
                if (neighbourCell.Type == 0)
                    RemoveCell(neighbourCell);
                else
                    AddCell(neighbourCell);
            }

            if ((modifiedDirection & (byte)Directions.Down) != 0)
            {
                cell.Type ^= (uint)Directions.Down;
                // Neighbour Cell
                Cell neighbourCell = GetCell(new GridLocation(cell.Location.X, cell.Location.Y + 1, cell.Location.Z, cell.Location.Level));
                if (neighbourCell.ID == -1)
                {
                    neighbourCell.ID = NewCellID;
                    neighbourCell.Type = 0;
                }
                if ((cell.Type & (uint)Directions.Down) != (neighbourCell.Type & (uint)Directions.Up))
                    neighbourCell.Type ^= (uint)Directions.Up;
                if (neighbourCell.Type == 0)
                    RemoveCell(neighbourCell);
                else
                    AddCell(neighbourCell);
            }

            if ((modifiedDirection & (byte)Directions.Left) != 0)
            {
                cell.Type ^= (uint)Directions.Left;
                // Neighbour Cell
                Cell neighbourCell = GetCell(new GridLocation(cell.Location.X - 1, cell.Location.Y, cell.Location.Z, cell.Location.Level));
                if (neighbourCell.ID == -1)
                {
                    neighbourCell.ID = NewCellID;
                    neighbourCell.Type = 0;
                }
                if ((cell.Type & (uint)Directions.Left) != (neighbourCell.Type & (uint)Directions.Right))
                    neighbourCell.Type ^= (uint)Directions.Right;
                if (neighbourCell.Type == 0)
                    RemoveCell(neighbourCell);
                else
                    AddCell(neighbourCell);
            }

            if ((modifiedDirection & (byte)Directions.Right) != 0)
            {
                cell.Type ^= (uint)Directions.Right;
                // Neighbour Cell
                Cell neighbourCell = GetCell(new GridLocation(cell.Location.X + 1, cell.Location.Y, cell.Location.Z, cell.Location.Level));
                if (neighbourCell.ID == -1)
                {
                    neighbourCell.ID = NewCellID;
                    neighbourCell.Type = 0;
                }
                if ((cell.Type & (uint)Directions.Right) != (neighbourCell.Type & (uint)Directions.Left))
                    neighbourCell.Type ^= (uint)Directions.Left;
                if (neighbourCell.Type == 0)
                    RemoveCell(neighbourCell);
                else
                    AddCell(neighbourCell);
            }


            if (cell.ID == -1)
                cell.ID = NewCellID;
            if (cell.Type == 0)
                RemoveCell(cell);
            else
                AddCell(cell);
            this.pbMap.Refresh();
        }
    }
}
Back to Top