Change to use pattern matching

This commit is contained in:
teinarss
2021-02-28 19:00:32 +01:00
committed by reaperrr
parent 7c0e4b25ae
commit d60c05eff3
35 changed files with 63 additions and 122 deletions

View File

@@ -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

View File

@@ -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)
{

View File

@@ -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;