Skip to content

Commit

Permalink
fix analyzer warnings/suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ar1a committed Oct 5, 2024
1 parent 1d9b282 commit 4493fc6
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 60 deletions.
8 changes: 7 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = priva
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case

# Prefer braces
csharp_prefer_braces = true:silent
csharp_prefer_braces = when_multiline

# Prefer var
csharp_style_var_for_built_in_types = true:suggestion
Expand All @@ -22,3 +22,9 @@ dotnet_style_predefined_type_for_member_access = true:suggestion

# namespace file scope
csharp_style_namespace_declarations = file_scoped

# Remove redundant default switch section
dotnet_diagnostic.rcs1070.severity = none

# stop asking me to do `_ = EmitSignal`
dotnet_diagnostic.IDE0058.severity = none
2 changes: 1 addition & 1 deletion scenes/GameCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void _Process(double delta)
if (Input.IsActionPressed("move_map"))
{
var mousePosition = GetViewport().GetMousePosition();
GlobalPosition -= (mousePosition - fixedTogglePoint);
GlobalPosition -= mousePosition - fixedTogglePoint;
fixedTogglePoint = mousePosition;
}

Expand Down
2 changes: 1 addition & 1 deletion scenes/building/BuildingGhost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void AddSpriteNode(Node2D spriteNode)

public void DoHoverAnimation()
{
if (spriteTween != null && spriteTween.IsValid())
if (spriteTween?.IsValid() == true)
spriteTween.Kill();
spriteTween = CreateTween();
spriteTween
Expand Down
7 changes: 3 additions & 4 deletions scenes/component/BuildingAnimatorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void PlayInAnimation()
{
if (animationRootNode == null)
return;
if (activeTween != null && activeTween.IsValid())
if (activeTween?.IsValid() == true)
activeTween.Kill();

activeTween = CreateTween();
Expand Down Expand Up @@ -62,7 +62,7 @@ public void PlayDestroyAnimation()
{
if (animationRootNode == null)
return;
if (activeTween != null && activeTween.IsValid())
if (activeTween?.IsValid() == true)
activeTween.Kill();

// snap to zero if destroyed during placing animation
Expand Down Expand Up @@ -92,8 +92,7 @@ public void PlayDestroyAnimation()

private void SetupNodes()
{
var spriteNode = GetChildren().FirstOrDefault() as Node2D;
if (spriteNode == null)
if (GetChildren().FirstOrDefault() is not Node2D spriteNode)
return;
RemoveChild(spriteNode);
Position = spriteNode.Position;
Expand Down
14 changes: 8 additions & 6 deletions scenes/component/BuildingComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ public partial class BuildingComponent : Node2D
public bool IsDestroying { get; private set; }
public bool IsDisabled { get; private set; }

private HashSet<Vector2I> occupiedTiles = new();
private readonly HashSet<Vector2I> occupiedTiles = new();

public static IEnumerable<BuildingComponent> GetValidBuildingComponents(Node node)
{
return node.GetTree()
.GetNodesInGroup(nameof(BuildingComponent))
.Cast<BuildingComponent>()
.Where(x => !x.IsDestroying);
.Where(static x => !x.IsDestroying);
}

public static IEnumerable<BuildingComponent> GetDangerBuildingComponents(Node node)
{
return GetValidBuildingComponents(node).Where(x => x.BuildingResource.IsDangerBuilding());
return GetValidBuildingComponents(node)
.Where(static x => x.BuildingResource.IsDangerBuilding());
}

public static IEnumerable<BuildingComponent> GetNonDangerBuildingComponents(Node node)
{
return GetValidBuildingComponents(node).Where(x => !x.BuildingResource.IsDangerBuilding());
return GetValidBuildingComponents(node)
.Where(static x => !x.BuildingResource.IsDangerBuilding());
}

public override void _Ready()
Expand Down Expand Up @@ -103,9 +105,9 @@ public void Destroy()
private void CalculateOccupiedCellPositions()
{
var gridPosition = GetGridCellPosition();
for (int x = gridPosition.X; x < gridPosition.X + BuildingResource.Dimensions.X; x++)
for (var x = gridPosition.X; x < gridPosition.X + BuildingResource.Dimensions.X; x++)
{
for (int y = gridPosition.Y; y < gridPosition.Y + BuildingResource.Dimensions.Y; y++)
for (var y = gridPosition.Y; y < gridPosition.Y + BuildingResource.Dimensions.Y; y++)
{
occupiedTiles.Add(new Vector2I(x, y));
}
Expand Down
2 changes: 1 addition & 1 deletion scenes/level/BaseLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override void _Ready()

private void OnGridStateUpdated()
{
var goldMineTilePosition = gridManager.ConvertWorldPositionToTilePosition(
var goldMineTilePosition = GridManager.ConvertWorldPositionToTilePosition(
goldMine.GlobalPosition
);
if (gridManager.IsTilePositionInAnyBuildingRadius(goldMineTilePosition))
Expand Down
21 changes: 12 additions & 9 deletions scenes/manager/BuildingManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Linq;
using Game.Building;
using Game.Component;
Expand Down Expand Up @@ -31,8 +30,8 @@ public partial class BuildingManager : Node

private enum State
{
Normal,
PlacingBuilding,
Normal = 0,
PlacingBuilding = 1,
}

private int currentResourceCount;
Expand All @@ -42,7 +41,7 @@ private enum State
private BuildingGhost buildingGhost;
private Vector2 buildingGhostDimensions;
private State currentState = State.Normal;
private int startingResourceCount = 0;
private int startingResourceCount;

private int AvailableResourceCount =>
startingResourceCount + currentResourceCount - currentlyUsedResourceCount;
Expand All @@ -58,24 +57,24 @@ public override void _Ready()
.CallDeferred();
}

public override void _UnhandledInput(InputEvent evt)
public override void _UnhandledInput(InputEvent @event)
{
switch (currentState)
{
case State.Normal:
if (evt.IsActionPressed(ACTION_RIGHT_CLICK))
if (@event.IsActionPressed(ACTION_RIGHT_CLICK))
{
DestroyBuildAtHoveredCellPosition();
}
break;
case State.PlacingBuilding:
if (evt.IsActionPressed(ACTION_CANCEL))
if (@event.IsActionPressed(ACTION_CANCEL))
{
ChangeState(State.Normal);
}
else if (
toPlaceBuildingResource != null
&& evt.IsActionPressed(ACTION_LEFT_CLICK)
&& @event.IsActionPressed(ACTION_LEFT_CLICK)
&& IsBuildingPlaceableAtArea(hoveredGridArea)
)
{
Expand All @@ -101,7 +100,7 @@ public void SetStartingResourceCount(int count)

public override void _Process(double delta)
{
Vector2I mouseGridPosition = Vector2I.Zero;
var mouseGridPosition = Vector2I.Zero;
var rootCell = hoveredGridArea.Position;

switch (currentState)
Expand All @@ -115,6 +114,8 @@ public override void _Process(double delta)
);
buildingGhost.GlobalPosition = mouseGridPosition * 64;
break;
default:
break;
}

if (rootCell != mouseGridPosition)
Expand Down Expand Up @@ -216,6 +217,8 @@ private void UpdateHoveredGridArea()
case State.PlacingBuilding:
UpdateGridDisplay();
break;
default:
break;
}
}

Expand Down
67 changes: 34 additions & 33 deletions scenes/manager/GridManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public partial class GridManager : Node
[Signal]
public delegate void GridStateUpdatedEventHandler();

private HashSet<Vector2I> validBuildableTiles = new();
private HashSet<Vector2I> validBuildableAttackTiles = new();
private HashSet<Vector2I> allTilesInBuildingRadius = new();
private HashSet<Vector2I> collectedResourceTiles = new();
private HashSet<Vector2I> occupiedTiles = new();
private HashSet<Vector2I> dangerOccupiedTiles = new();
private HashSet<Vector2I> attackTiles = new();
private readonly HashSet<Vector2I> validBuildableTiles = new();
private readonly HashSet<Vector2I> validBuildableAttackTiles = new();
private readonly HashSet<Vector2I> allTilesInBuildingRadius = new();
private readonly HashSet<Vector2I> collectedResourceTiles = new();
private readonly HashSet<Vector2I> occupiedTiles = new();
private readonly HashSet<Vector2I> dangerOccupiedTiles = new();
private readonly HashSet<Vector2I> attackTiles = new();

[Export]
private TileMapLayer highlightTilemapLayer;
Expand All @@ -35,10 +35,11 @@ public partial class GridManager : Node
private TileMapLayer baseTerrainTilemapLayer;

private List<TileMapLayer> allTilemapLayers = new();
private Dictionary<TileMapLayer, ElevationLayer> tileMapLayerToElevationLayer = new();
private Dictionary<BuildingComponent, HashSet<Vector2I>> buildingToBuildableTiles = new();
private Dictionary<BuildingComponent, HashSet<Vector2I>> dangerBuildingToTiles = new();
private Dictionary<BuildingComponent, HashSet<Vector2I>> attackBuildingToTiles = new();
private readonly Dictionary<TileMapLayer, ElevationLayer> tileMapLayerToElevationLayer = new();
private readonly Dictionary<BuildingComponent, HashSet<Vector2I>> buildingToBuildableTiles =
new();
private readonly Dictionary<BuildingComponent, HashSet<Vector2I>> dangerBuildingToTiles = new();
private readonly Dictionary<BuildingComponent, HashSet<Vector2I>> attackBuildingToTiles = new();

public override void _Ready()
{
Expand Down Expand Up @@ -85,7 +86,7 @@ public bool IsTileAreaBuildable(Rect2I tileArea, bool isAttackTiles = false)
if (tiles.Count == 0)
return false;

(TileMapLayer firstTileMapLayer, _) = GetTileCustomData(tiles[0], IS_BUILDABLE);
(var firstTileMapLayer, _) = GetTileCustomData(tiles[0], IS_BUILDABLE);
var targetElevationLayer =
firstTileMapLayer != null ? tileMapLayerToElevationLayer[firstTileMapLayer] : null;

Expand All @@ -98,10 +99,7 @@ public bool IsTileAreaBuildable(Rect2I tileArea, bool isAttackTiles = false)
return tiles.All(
(tilePosition) =>
{
(TileMapLayer tileMapLayer, bool isBuildable) = GetTileCustomData(
tilePosition,
IS_BUILDABLE
);
(var tileMapLayer, var isBuildable) = GetTileCustomData(tilePosition, IS_BUILDABLE);
var elevationLayer =
tileMapLayer != null ? tileMapLayerToElevationLayer[tileMapLayer] : null;

Expand Down Expand Up @@ -182,7 +180,7 @@ public Vector2I GetMouseGridCellPosition()
return ConvertWorldPositionToTilePosition(highlightTilemapLayer.GetGlobalMousePosition());
}

public Vector2I ConvertWorldPositionToTilePosition(Vector2 worldPosition)
public static Vector2I ConvertWorldPositionToTilePosition(Vector2 worldPosition)
{
return (Vector2I)(worldPosition / 64).Floor();
}
Expand Down Expand Up @@ -253,11 +251,10 @@ private bool CanDestroyBarracks(BuildingComponent toDestroyBuildingComponent)
dangerBuilding
.GetOccupiedCellPositions()
.Any(tilePosition =>
attackBuildingToTiles
.Keys.Where(attackBuilding => attackBuilding != toDestroyBuildingComponent)
.Any(attackBuilding =>
attackBuildingToTiles[attackBuilding].Contains(tilePosition)
)
attackBuildingToTiles.Keys.Any(attackBuilding =>
attackBuilding != toDestroyBuildingComponent
&& attackBuildingToTiles[attackBuilding].Contains(tilePosition)
)
)
);

Expand Down Expand Up @@ -304,12 +301,9 @@ HashSet<BuildingComponent> visitedBuildings
.GetNonDangerBuildingComponents(this)
.Where(x =>
{
if (x.BuildingResource.BuildableRadius == 0)
return false;
if (visitedBuildings.Contains(x))
return false;

return x.GetOccupiedCellPositions()
return x.BuildingResource.BuildableRadius != 0
&& !visitedBuildings.Contains(x)
&& x.GetOccupiedCellPositions()
.All(
(tilePosition) =>
buildingToBuildableTiles[rootBuilding].Contains(tilePosition)
Expand All @@ -330,7 +324,7 @@ private HashSet<Vector2I> GetBuildableTileSet(bool isAttackTiles = false)
return isAttackTiles ? validBuildableAttackTiles : validBuildableTiles;
}

private List<TileMapLayer> GetAllTilemapLayers(Node2D rootNode)
private static List<TileMapLayer> GetAllTilemapLayers(Node2D rootNode)
{
var result = new List<TileMapLayer>();
var children = rootNode.GetChildren();
Expand Down Expand Up @@ -399,7 +393,7 @@ private void UpdateValidBuildableTiles(BuildingComponent buildingComponent)
var allTiles = GetTilesInRadius(
tileArea,
buildingComponent.BuildingResource.BuildableRadius,
(_) => true
static (_) => true
);
allTilesInBuildingRadius.UnionWith(allTiles);

Expand Down Expand Up @@ -443,7 +437,7 @@ private void UpdateAttackTiles(BuildingComponent buildingComponent)
var newAttackTiles = GetTilesInRadius(
tileArea,
buildingComponent.BuildingResource.AttackRadius,
(_) => true
static (_) => true
)
.ToHashSet();
attackBuildingToTiles[buildingComponent] = newAttackTiles;
Expand Down Expand Up @@ -501,15 +495,19 @@ private void RecalculateDangerOccupiedTiles()
}
}

private bool IsTileInsideCircle(Vector2 centerPosition, Vector2 tilePosiiton, float radius)
private static bool IsTileInsideCircle(
Vector2 centerPosition,
Vector2 tilePosiiton,
float radius
)
{
var dx = centerPosition.X - (tilePosiiton.X + .5);
var dy = centerPosition.Y - (tilePosiiton.Y + .5);
var distance = (dx * dx) + (dy * dy);
return distance <= radius * radius;
}

private List<Vector2I> GetTilesInRadius(
private static List<Vector2I> GetTilesInRadius(
Rect2I tileArea,
int radius,
Func<Vector2I, bool> filterFn
Expand All @@ -527,7 +525,10 @@ Func<Vector2I, bool> filterFn
!IsTileInsideCircle(tileAreaCenter, tilePosition, radius + radiusMod)
|| !filterFn(tilePosition)
)
{
continue;
}

result.Add(tilePosition);
}
}
Expand Down
3 changes: 2 additions & 1 deletion scenes/ui/LevelSelectScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ private void ShowPage()

levelSelectSection.SetLevelDefinition(levelDefinitions[i]);
levelSelectSection.SetLevelIndex(i);
levelSelectSection.LevelSelected += (index) => LevelManager.Instance.ChangeLevel(index);
levelSelectSection.LevelSelected += static (index) =>
LevelManager.Instance.ChangeLevel(index);
}
}
}
3 changes: 2 additions & 1 deletion scenes/ui/MainMenu.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Game.UI;
using Godot;

namespace Game.UI;

public partial class MainMenu : Node
{
private Button playButton;
Expand Down
4 changes: 2 additions & 2 deletions scripts/Rect2IExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static class Rect2IExtensions
public static List<Vector2I> ToTiles(this Rect2I rect)
{
var tiles = new List<Vector2I>();
for (int x = rect.Position.X; x < rect.End.X; x++)
for (var x = rect.Position.X; x < rect.End.X; x++)
{
for (int y = rect.Position.Y; y < rect.End.Y; y++)
for (var y = rect.Position.Y; y < rect.End.Y; y++)
{
tiles.Add(new Vector2I(x, y));
}
Expand Down

0 comments on commit 4493fc6

Please sign in to comment.