Infantry.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using MobiusEditor.Interface;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.Drawing;
  19. using System.Linq;
  20. using System.Numerics;
  21. using System.Runtime.CompilerServices;
  22. namespace MobiusEditor.Model
  23. {
  24. public enum InfantryStoppingType
  25. {
  26. Center = 0,
  27. UpperLeft = 1,
  28. UpperRight = 2,
  29. LowerLeft = 3,
  30. LowerRight = 4
  31. }
  32. public class Infantry : INotifyPropertyChanged, ICloneable
  33. {
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. public InfantryGroup InfantryGroup { get; set; }
  36. private InfantryType type;
  37. public InfantryType Type { get => type; set => SetField(ref type, value); }
  38. private HouseType house;
  39. public HouseType House { get => house; set => SetField(ref house, value); }
  40. private int strength;
  41. public int Strength { get => strength; set => SetField(ref strength, value); }
  42. private DirectionType direction;
  43. public DirectionType Direction { get => direction; set => SetField(ref direction, value); }
  44. private string mission;
  45. public string Mission { get => mission; set => SetField(ref mission, value); }
  46. private string trigger = Model.Trigger.None;
  47. public string Trigger { get => trigger; set => SetField(ref trigger, value); }
  48. public Color Tint { get; set; } = Color.White;
  49. public Infantry(InfantryGroup infantryGroup)
  50. {
  51. InfantryGroup = infantryGroup;
  52. }
  53. public Infantry Clone()
  54. {
  55. return new Infantry(InfantryGroup)
  56. {
  57. Type = Type,
  58. House = House,
  59. Strength = Strength,
  60. Direction = Direction,
  61. Trigger = Trigger,
  62. Mission = Mission,
  63. };
  64. }
  65. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  66. {
  67. if (EqualityComparer<T>.Default.Equals(field, value))
  68. {
  69. return false;
  70. }
  71. field = value;
  72. OnPropertyChanged(propertyName);
  73. return true;
  74. }
  75. protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  76. object ICloneable.Clone()
  77. {
  78. return Clone();
  79. }
  80. }
  81. public class InfantryGroup : ICellOverlapper, ICellOccupier
  82. {
  83. private static readonly Point[] stoppingLocations = new Point[Globals.NumInfantryStops];
  84. public Rectangle OverlapBounds => new Rectangle(-1, -1, 3, 3);
  85. public bool[,] OccupyMask => new bool[1, 1] { { true } };
  86. public readonly Infantry[] Infantry = new Infantry[Globals.NumInfantryStops];
  87. static InfantryGroup()
  88. {
  89. stoppingLocations[(int)InfantryStoppingType.Center] = new Point(Globals.PixelWidth / 2, Globals.PixelHeight / 2);
  90. stoppingLocations[(int)InfantryStoppingType.UpperLeft] = new Point(Globals.PixelWidth / 4, Globals.PixelHeight / 4);
  91. stoppingLocations[(int)InfantryStoppingType.UpperRight] = new Point(3 * Globals.PixelWidth / 4, Globals.PixelHeight / 4);
  92. stoppingLocations[(int)InfantryStoppingType.LowerLeft] = new Point(Globals.PixelWidth / 4, 3 * Globals.PixelHeight / 4);
  93. stoppingLocations[(int)InfantryStoppingType.LowerRight] = new Point(3 * Globals.PixelWidth / 4, 3 * Globals.PixelHeight / 4);
  94. }
  95. public static IEnumerable<InfantryStoppingType> ClosestStoppingTypes(Point subPixel)
  96. {
  97. var stoppingDistances = new (InfantryStoppingType type, float dist)[stoppingLocations.Length];
  98. for (int i = 0; i < stoppingDistances.Length; ++i)
  99. {
  100. stoppingDistances[i] = ((InfantryStoppingType)i, new Vector2(subPixel.X - stoppingLocations[i].X, subPixel.Y - stoppingLocations[i].Y).LengthSquared());
  101. }
  102. return stoppingDistances.OrderBy(sd => sd.dist).Select(sd => sd.type);
  103. }
  104. }
  105. }