Change to use pattern matching
This commit is contained in:
@@ -197,10 +197,9 @@ namespace OpenRA.Mods.Common.Activities
|
||||
maxRange = armaments.Min(a => a.MaxRange());
|
||||
|
||||
var pos = self.CenterPosition;
|
||||
var mobile = move as Mobile;
|
||||
if (!target.IsInRange(pos, maxRange)
|
||||
|| (minRange.Length != 0 && target.IsInRange(pos, minRange))
|
||||
|| (mobile != null && !mobile.CanInteractWithGroundLayer(self)))
|
||||
|| (move is Mobile mobile && !mobile.CanInteractWithGroundLayer(self)))
|
||||
{
|
||||
// Try to move within range, drop the target otherwise
|
||||
if (move == null)
|
||||
|
||||
@@ -102,12 +102,8 @@ namespace OpenRA.Mods.Common
|
||||
|
||||
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
|
||||
{
|
||||
if (destinationType == typeof(string))
|
||||
{
|
||||
var reference = value as ActorInitActorReference;
|
||||
if (reference != null)
|
||||
return reference.InternalName;
|
||||
}
|
||||
if (destinationType == typeof(string) && value is ActorInitActorReference reference)
|
||||
return reference.InternalName;
|
||||
|
||||
return base.ConvertTo(context, culture, value, destinationType);
|
||||
}
|
||||
|
||||
@@ -34,8 +34,7 @@ namespace OpenRA.Mods.Common.Effects
|
||||
|
||||
target.World.RemoveAll(effect =>
|
||||
{
|
||||
var flashTarget = effect as FlashTarget;
|
||||
return flashTarget != null && flashTarget.target == target;
|
||||
return effect is FlashTarget flashTarget && flashTarget.target == target;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,7 @@ namespace OpenRA.Mods.Common.Lint
|
||||
{
|
||||
foreach (var weaponInfo in rules.Weapons)
|
||||
{
|
||||
var missile = weaponInfo.Value.Projectile as MissileInfo;
|
||||
if (missile != null)
|
||||
if (weaponInfo.Value.Projectile is MissileInfo missile)
|
||||
{
|
||||
var minAngle = missile.MinimumLaunchAngle.Angle;
|
||||
var maxAngle = missile.MaximumLaunchAngle.Angle;
|
||||
@@ -31,8 +30,7 @@ namespace OpenRA.Mods.Common.Lint
|
||||
CheckLaunchAngles(weaponInfo.Key, minAngle, testMaxAngle, maxAngle, emitError);
|
||||
}
|
||||
|
||||
var bullet = weaponInfo.Value.Projectile as BulletInfo;
|
||||
if (bullet != null)
|
||||
if (weaponInfo.Value.Projectile is BulletInfo bullet)
|
||||
{
|
||||
var minAngle = bullet.LaunchAngle[0].Angle;
|
||||
var maxAngle = bullet.LaunchAngle.Length > 1 ? bullet.LaunchAngle[1].Angle : minAngle;
|
||||
|
||||
@@ -21,9 +21,8 @@ namespace OpenRA.Mods.Common.Lint
|
||||
foreach (var weaponInfo in rules.Weapons)
|
||||
{
|
||||
var range = weaponInfo.Value.Range;
|
||||
var missile = weaponInfo.Value.Projectile as MissileInfo;
|
||||
|
||||
if (missile != null && missile.RangeLimit > WDist.Zero && missile.RangeLimit < range)
|
||||
if (weaponInfo.Value.Projectile is MissileInfo missile && missile.RangeLimit > WDist.Zero && missile.RangeLimit < range)
|
||||
emitError("Weapon `{0}`: projectile RangeLimit lower than weapon range!"
|
||||
.F(weaponInfo.Key));
|
||||
}
|
||||
|
||||
@@ -325,8 +325,7 @@ namespace OpenRA.Mods.Common.Orders
|
||||
foreach (var blocker in blockers)
|
||||
{
|
||||
CPos moveCell;
|
||||
var mobile = blocker.Trait as Mobile;
|
||||
if (mobile != null)
|
||||
if (blocker.Trait is Mobile mobile)
|
||||
{
|
||||
var availableCells = adjacentTiles.Where(t => mobile.CanEnterCell(t)).ToList();
|
||||
if (availableCells.Count == 0)
|
||||
|
||||
@@ -40,9 +40,7 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
if (initInstance.Length > 1)
|
||||
initType.GetField("InstanceName").SetValue(init, initInstance[1]);
|
||||
|
||||
var compositeInit = init as CompositeActorInit;
|
||||
var tableValue = value as LuaTable;
|
||||
if (tableValue != null && compositeInit != null)
|
||||
if (value is LuaTable tableValue && init is CompositeActorInit compositeInit)
|
||||
{
|
||||
var args = compositeInit.InitializeArgs();
|
||||
var initValues = new Dictionary<string, object>();
|
||||
@@ -74,8 +72,7 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
}
|
||||
|
||||
// HACK: Backward compatibility for legacy int facings
|
||||
var facingInit = init as FacingInit;
|
||||
if (facingInit != null)
|
||||
if (init is FacingInit facingInit)
|
||||
{
|
||||
if (value.TryGetClrValue(out int facing))
|
||||
{
|
||||
|
||||
@@ -117,10 +117,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
if (!h.Key.IsIdle)
|
||||
{
|
||||
var act = h.Key.CurrentActivity as FindAndDeliverResources;
|
||||
|
||||
// Ignore this actor if FindAndDeliverResources is working fine or it is performing a different activity
|
||||
if (act == null || !act.LastSearchFailed)
|
||||
if (!(h.Key.CurrentActivity is FindAndDeliverResources act) || !act.LastSearchFailed)
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,8 +36,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
// The DeployTransform order does not have a position associated with it,
|
||||
// so we can only queue a new transformation if something else has
|
||||
// already triggered the undeploy.
|
||||
var currentTransform = self.CurrentActivity as Transform;
|
||||
if (!order.Queued || currentTransform == null)
|
||||
if (!order.Queued || !(self.CurrentActivity is Transform currentTransform))
|
||||
return;
|
||||
|
||||
if (!order.Queued)
|
||||
|
||||
@@ -91,8 +91,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
IEnumerable<IRenderable> RenderArmaments(Actor self, AttackBase attack)
|
||||
{
|
||||
// Fire ports on garrisonable structures
|
||||
var garrison = attack as AttackGarrisoned;
|
||||
if (garrison != null)
|
||||
if (attack is AttackGarrisoned garrison)
|
||||
{
|
||||
var bodyOrientation = coords.Value.QuantizeOrientation(self, self.Orientation);
|
||||
foreach (var p in garrison.Info.Ports)
|
||||
|
||||
@@ -209,12 +209,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
Func<object, bool> saveInit = init =>
|
||||
{
|
||||
var factionInit = init as FactionInit;
|
||||
if (factionInit != null && factionInit.Value == Owner.Faction)
|
||||
if (init is FactionInit factionInit && factionInit.Value == Owner.Faction)
|
||||
return false;
|
||||
|
||||
var healthInit = init as HealthInit;
|
||||
if (healthInit != null && healthInit.Value == 100)
|
||||
if (init is HealthInit healthInit && healthInit.Value == 100)
|
||||
return false;
|
||||
|
||||
// TODO: Other default values will need to be filtered
|
||||
|
||||
@@ -303,8 +303,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (check <= BlockedByActor.Immovable && cellFlag.HasCellFlag(CellFlag.HasMovableActor) &&
|
||||
actor.Owner.RelationshipWith(otherActor.Owner) == PlayerRelationship.Ally)
|
||||
{
|
||||
var mobile = otherActor.OccupiesSpace as Mobile;
|
||||
if (mobile != null && !mobile.IsTraitDisabled && !mobile.IsTraitPaused && !mobile.IsImmovable)
|
||||
if (otherActor.OccupiesSpace is Mobile mobile && !mobile.IsTraitDisabled && !mobile.IsTraitPaused && !mobile.IsImmovable)
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -324,8 +323,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (cellFlag.HasCellFlag(CellFlag.HasTransitOnlyActor))
|
||||
{
|
||||
// Transit only tiles should not block movement
|
||||
var building = otherActor.OccupiesSpace as Building;
|
||||
if (building != null && building.TransitOnlyCells().Contains(cell))
|
||||
if (otherActor.OccupiesSpace is Building building && building.TransitOnlyCells().Contains(cell))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -346,16 +344,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
static bool IsMoving(Actor self, Actor other)
|
||||
{
|
||||
// PERF: Because we can be sure that OccupiesSpace is Mobile here we can save some performance by avoiding querying for the trait.
|
||||
var otherMobile = other.OccupiesSpace as Mobile;
|
||||
if (otherMobile == null || !otherMobile.CurrentMovementTypes.HasMovementType(MovementType.Horizontal))
|
||||
if (!(other.OccupiesSpace is Mobile otherMobile) || !otherMobile.CurrentMovementTypes.HasMovementType(MovementType.Horizontal))
|
||||
return false;
|
||||
|
||||
// PERF: Same here.
|
||||
var selfMobile = self.OccupiesSpace as Mobile;
|
||||
if (selfMobile == null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return self.OccupiesSpace is Mobile;
|
||||
}
|
||||
|
||||
public void WorldLoaded(World w, WorldRenderer wr)
|
||||
@@ -466,8 +459,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var isMovable = mobile != null && !mobile.IsTraitDisabled && !mobile.IsTraitPaused && !mobile.IsImmovable;
|
||||
var isMoving = isMovable && mobile.CurrentMovementTypes.HasMovementType(MovementType.Horizontal);
|
||||
|
||||
var building = actor.OccupiesSpace as Building;
|
||||
var isTransitOnly = building != null && building.TransitOnlyCells().Contains(cell);
|
||||
var isTransitOnly = actor.OccupiesSpace is Building building && building.TransitOnlyCells().Contains(cell);
|
||||
|
||||
if (isTransitOnly)
|
||||
{
|
||||
|
||||
@@ -158,8 +158,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
IEnumerable<IRenderable> ITiledTerrainRenderer.RenderUIPreview(WorldRenderer wr, TerrainTemplateInfo t, int2 origin, float scale)
|
||||
{
|
||||
var template = t as DefaultTerrainTemplateInfo;
|
||||
if (template == null)
|
||||
if (!(t is DefaultTerrainTemplateInfo template))
|
||||
yield break;
|
||||
|
||||
var ts = map.Grid.TileSize;
|
||||
|
||||
@@ -36,8 +36,8 @@ namespace OpenRA.Mods.Common.UtilityCommands
|
||||
var modData = Game.ModData = utility.ModData;
|
||||
|
||||
// HACK: We know that maps can only be oramap or folders, which are ReadWrite
|
||||
var package = new Folder(Platform.EngineDir).OpenPackage(args[1], modData.ModFiles) as IReadWritePackage;
|
||||
if (package == null)
|
||||
var folder = new Folder(Platform.EngineDir);
|
||||
if (!(folder.OpenPackage(args[1], modData.ModFiles) is IReadWritePackage package))
|
||||
throw new FileNotFoundException(args[1]);
|
||||
|
||||
IEnumerable<UpdateRule> rules = null;
|
||||
|
||||
@@ -371,9 +371,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
currentPackage = package;
|
||||
currentFilename = filename;
|
||||
var prefix = "";
|
||||
var fs = modData.DefaultFileSystem as OpenRA.FileSystem.FileSystem;
|
||||
|
||||
if (fs != null)
|
||||
if (modData.DefaultFileSystem is OpenRA.FileSystem.FileSystem fs)
|
||||
{
|
||||
prefix = fs.GetPrefix(package);
|
||||
if (prefix != null)
|
||||
|
||||
@@ -180,8 +180,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
|
||||
try
|
||||
{
|
||||
var package = map.Package as IReadWritePackage;
|
||||
if (package == null || package.Name != combinedPath)
|
||||
if (!(map.Package is IReadWritePackage package) || package.Name != combinedPath)
|
||||
{
|
||||
selectedDirectory.Folder.Delete(combinedPath);
|
||||
if (fileType == MapFileType.OraMap)
|
||||
|
||||
@@ -273,12 +273,10 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
bool IsForceModifiersActive(Modifiers modifiers)
|
||||
{
|
||||
var fmog = world.OrderGenerator as ForceModifiersOrderGenerator;
|
||||
if (fmog != null && fmog.Modifiers.HasFlag(modifiers))
|
||||
if (world.OrderGenerator is ForceModifiersOrderGenerator fmog && fmog.Modifiers.HasFlag(modifiers))
|
||||
return true;
|
||||
|
||||
var uog = world.OrderGenerator as UnitOrderGenerator;
|
||||
if (uog != null && Game.GetModifierKeys().HasFlag(modifiers))
|
||||
if (world.OrderGenerator is UnitOrderGenerator uog && Game.GetModifierKeys().HasFlag(modifiers))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
@@ -65,8 +65,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
status.GetText = statusText;
|
||||
}
|
||||
|
||||
var timerTooltip = timer as LabelWithTooltipWidget;
|
||||
if (timerTooltip != null)
|
||||
if (timer is LabelWithTooltipWidget timerTooltip)
|
||||
{
|
||||
var connection = orderManager.Connection as ReplayConnection;
|
||||
if (connection != null && connection.FinalGameTick != 0)
|
||||
|
||||
@@ -19,8 +19,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
[ObjectCreator.UseCtor]
|
||||
public SellOrderButtonLogic(Widget widget, World world)
|
||||
{
|
||||
var sell = widget as ButtonWidget;
|
||||
if (sell != null)
|
||||
if (widget is ButtonWidget sell)
|
||||
OrderButtonsChromeUtils.BindOrderButton<SellOrderGenerator>(world, sell, "sell");
|
||||
}
|
||||
}
|
||||
@@ -30,8 +29,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
[ObjectCreator.UseCtor]
|
||||
public RepairOrderButtonLogic(Widget widget, World world)
|
||||
{
|
||||
var repair = widget as ButtonWidget;
|
||||
if (repair != null)
|
||||
if (widget is ButtonWidget repair)
|
||||
OrderButtonsChromeUtils.BindOrderButton<RepairOrderGenerator>(world, repair, "repair");
|
||||
}
|
||||
}
|
||||
@@ -41,8 +39,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
[ObjectCreator.UseCtor]
|
||||
public PowerdownOrderButtonLogic(Widget widget, World world)
|
||||
{
|
||||
var power = widget as ButtonWidget;
|
||||
if (power != null)
|
||||
if (widget is ButtonWidget power)
|
||||
OrderButtonsChromeUtils.BindOrderButton<PowerDownOrderGenerator>(world, power, "power");
|
||||
}
|
||||
}
|
||||
@@ -52,8 +49,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
[ObjectCreator.UseCtor]
|
||||
public BeaconOrderButtonLogic(Widget widget, World world)
|
||||
{
|
||||
var beacon = widget as ButtonWidget;
|
||||
if (beacon != null)
|
||||
if (widget is ButtonWidget beacon)
|
||||
OrderButtonsChromeUtils.BindOrderButton<BeaconOrderGenerator>(world, beacon, "beacon");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,8 +282,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
var item = Children.FirstOrDefault(c =>
|
||||
{
|
||||
var si = c as ScrollItemWidget;
|
||||
return si != null && si.ItemKey == itemKey;
|
||||
return c is ScrollItemWidget si && si.ItemKey == itemKey;
|
||||
});
|
||||
|
||||
if (item != null)
|
||||
@@ -294,8 +293,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
var item = Children.FirstOrDefault(c =>
|
||||
{
|
||||
var si = c as ScrollItemWidget;
|
||||
return si != null && si.IsSelected();
|
||||
return c is ScrollItemWidget si && si.IsSelected();
|
||||
});
|
||||
|
||||
if (item != null)
|
||||
|
||||
@@ -88,9 +88,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
var useClassicMouseStyle = Game.Settings.Game.UseClassicMouseStyle;
|
||||
|
||||
var multiClick = mi.MultiTapCount >= 2;
|
||||
var uog = World.OrderGenerator as UnitOrderGenerator;
|
||||
|
||||
if (uog == null)
|
||||
if (!(World.OrderGenerator is UnitOrderGenerator uog))
|
||||
{
|
||||
ApplyOrders(World, mi);
|
||||
isDragging = false;
|
||||
|
||||
Reference in New Issue
Block a user