UnitType.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 MobiusEditor.Render;
  16. using MobiusEditor.Utility;
  17. using System;
  18. using System.Drawing;
  19. namespace MobiusEditor.Model
  20. {
  21. public static class UnitTypeIDMask
  22. {
  23. public const sbyte Aircraft = 1 << 5;
  24. public const sbyte Vessel = 1 << 6;
  25. }
  26. public class UnitType : ICellOverlapper, ICellOccupier, ITechnoType, IBrowsableType
  27. {
  28. public sbyte ID { get; private set; }
  29. public string Name { get; private set; }
  30. public string DisplayName { get; private set; }
  31. public Rectangle OverlapBounds => new Rectangle(-1, -1, 3, 3);
  32. public bool[,] OccupyMask => new bool[1, 1] { { true } };
  33. public string OwnerHouse { get; private set; }
  34. public bool HasTurret { get; private set; }
  35. public bool IsFixedWing { get; private set; }
  36. public bool IsUnit => !IsAircraft && !IsVessel;
  37. public bool IsAircraft => (ID & UnitTypeIDMask.Aircraft) != 0;
  38. public bool IsVessel => (ID & UnitTypeIDMask.Vessel) != 0;
  39. public Size RenderSize { get; set; }
  40. public Image Thumbnail { get; set; }
  41. public UnitType(sbyte id, string name, string textId, string ownerHouse, bool hasTurret, bool isFixedWing)
  42. {
  43. ID = id;
  44. Name = name;
  45. DisplayName = Globals.TheGameTextManager[textId];
  46. OwnerHouse = ownerHouse;
  47. HasTurret = hasTurret;
  48. IsFixedWing = isFixedWing;
  49. }
  50. public UnitType(sbyte id, string name, string textId, string ownerHouse, bool hasTurret)
  51. : this(id, name, textId, ownerHouse, hasTurret, false)
  52. {
  53. }
  54. public UnitType(sbyte id, string name, string textId)
  55. : this(id, name, textId, null, false)
  56. {
  57. }
  58. public UnitType(sbyte id, string name, string textId, string ownerHouse)
  59. : this(id, name, textId, ownerHouse, false)
  60. {
  61. }
  62. public UnitType(sbyte id, string name, string textId, bool hasTurret)
  63. : this(id, name, textId, null, hasTurret)
  64. {
  65. }
  66. public override bool Equals(object obj)
  67. {
  68. if (obj is UnitType)
  69. {
  70. return this == obj;
  71. }
  72. else if (obj is sbyte)
  73. {
  74. return ID == (sbyte)obj;
  75. }
  76. else if (obj is string)
  77. {
  78. return string.Equals(Name, obj as string, StringComparison.OrdinalIgnoreCase);
  79. }
  80. return base.Equals(obj);
  81. }
  82. public override int GetHashCode()
  83. {
  84. return ID.GetHashCode();
  85. }
  86. public override string ToString()
  87. {
  88. return Name;
  89. }
  90. public void Init(GameType gameType, TheaterType theater, HouseType house, DirectionType direction)
  91. {
  92. if (Globals.TheTilesetManager.GetTileData(theater.Tilesets, Name, 0, out Tile tile))
  93. {
  94. RenderSize = new Size(tile.Image.Width / Globals.TileScale, tile.Image.Height / Globals.TileScale);
  95. }
  96. var mockUnit = new Unit()
  97. {
  98. Type = this,
  99. House = house,
  100. Strength = 256,
  101. Direction = direction
  102. };
  103. var unitThumbnail = new Bitmap(Globals.TileWidth * 3, Globals.TileHeight * 3);
  104. using (var g = Graphics.FromImage(unitThumbnail))
  105. {
  106. MapRenderer.Render(gameType, theater, new Point(1, 1), Globals.TileSize, mockUnit).Item2(g);
  107. }
  108. Thumbnail = unitThumbnail;
  109. }
  110. }
  111. }