UnitType.cs (4528B)
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 9 // The Command & Conquer Map Editor and corresponding source code is distributed 10 // in the hope that it will be useful, but with permitted additional restrictions 11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 12 // distributed with this program. You should have received a copy of the 13 // GNU General Public License along with permitted additional restrictions 14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection 15 using MobiusEditor.Interface; 16 using MobiusEditor.Render; 17 using MobiusEditor.Utility; 18 using System; 19 using System.Drawing; 20 21 namespace MobiusEditor.Model 22 { 23 public static class UnitTypeIDMask 24 { 25 public const sbyte Aircraft = 1 << 5; 26 public const sbyte Vessel = 1 << 6; 27 } 28 29 public class UnitType : ICellOverlapper, ICellOccupier, ITechnoType, IBrowsableType 30 { 31 public sbyte ID { get; private set; } 32 33 public string Name { get; private set; } 34 35 public string DisplayName { get; private set; } 36 37 public Rectangle OverlapBounds => new Rectangle(-1, -1, 3, 3); 38 39 public bool[,] OccupyMask => new bool[1, 1] { { true } }; 40 41 public string OwnerHouse { get; private set; } 42 43 public bool HasTurret { get; private set; } 44 45 public bool IsFixedWing { get; private set; } 46 47 public bool IsUnit => !IsAircraft && !IsVessel; 48 49 public bool IsAircraft => (ID & UnitTypeIDMask.Aircraft) != 0; 50 51 public bool IsVessel => (ID & UnitTypeIDMask.Vessel) != 0; 52 53 public Size RenderSize { get; set; } 54 55 public Image Thumbnail { get; set; } 56 57 public UnitType(sbyte id, string name, string textId, string ownerHouse, bool hasTurret, bool isFixedWing) 58 { 59 ID = id; 60 Name = name; 61 DisplayName = Globals.TheGameTextManager[textId]; 62 OwnerHouse = ownerHouse; 63 HasTurret = hasTurret; 64 IsFixedWing = isFixedWing; 65 } 66 67 public UnitType(sbyte id, string name, string textId, string ownerHouse, bool hasTurret) 68 : this(id, name, textId, ownerHouse, hasTurret, false) 69 { 70 } 71 72 public UnitType(sbyte id, string name, string textId) 73 : this(id, name, textId, null, false) 74 { 75 } 76 77 public UnitType(sbyte id, string name, string textId, string ownerHouse) 78 : this(id, name, textId, ownerHouse, false) 79 { 80 } 81 82 public UnitType(sbyte id, string name, string textId, bool hasTurret) 83 : this(id, name, textId, null, hasTurret) 84 { 85 } 86 87 public override bool Equals(object obj) 88 { 89 if (obj is UnitType) 90 { 91 return this == obj; 92 } 93 else if (obj is sbyte) 94 { 95 return ID == (sbyte)obj; 96 } 97 else if (obj is string) 98 { 99 return string.Equals(Name, obj as string, StringComparison.OrdinalIgnoreCase); 100 } 101 102 return base.Equals(obj); 103 } 104 105 public override int GetHashCode() 106 { 107 return ID.GetHashCode(); 108 } 109 110 public override string ToString() 111 { 112 return Name; 113 } 114 115 public void Init(GameType gameType, TheaterType theater, HouseType house, DirectionType direction) 116 { 117 if (Globals.TheTilesetManager.GetTileData(theater.Tilesets, Name, 0, out Tile tile)) 118 { 119 RenderSize = new Size(tile.Image.Width / Globals.TileScale, tile.Image.Height / Globals.TileScale); 120 } 121 122 var mockUnit = new Unit() 123 { 124 Type = this, 125 House = house, 126 Strength = 256, 127 Direction = direction 128 }; 129 var unitThumbnail = new Bitmap(Globals.TileWidth * 3, Globals.TileHeight * 3); 130 using (var g = Graphics.FromImage(unitThumbnail)) 131 { 132 MapRenderer.Render(gameType, theater, new Point(1, 1), Globals.TileSize, mockUnit).Item2(g); 133 } 134 Thumbnail = unitThumbnail; 135 } 136 } 137 }