From 583658c34220db140479ccf612bd9f9f2c9b201a Mon Sep 17 00:00:00 2001 From: Bahil Botond <dxwbw9@inf.elte.hu> Date: Sat, 7 May 2022 18:05:22 +0200 Subject: [PATCH] Improving documentation --- Assets/Scripts/Logic/Data/World/Barrack.cs | 3 + Assets/Scripts/Logic/Data/World/Building.cs | 11 ++- Assets/Scripts/Logic/Data/World/Castle.cs | 3 + Assets/Scripts/Logic/Data/World/Dijkstra.cs | 18 ----- .../Scripts/Logic/Data/World/Dijkstra.cs.meta | 3 - Assets/Scripts/Logic/Data/World/GameWorld.cs | 3 + .../Logic/Data/World/ITowerTypeData.cs | 5 +- .../Scripts/Logic/Data/World/IUnitTypeData.cs | 3 + Assets/Scripts/Logic/Data/World/Obstacle.cs | 9 ++- Assets/Scripts/Logic/Data/World/TileObject.cs | 7 +- .../Scripts/Logic/Data/World/TilePosition.cs | 7 +- Assets/Scripts/Logic/Data/World/Tower.cs | 5 +- Assets/Scripts/Logic/Data/World/Unit.cs | 3 + Assets/Scripts/Logic/Data/World/Vector2.cs | 4 ++ .../Logic/Data/World/WorldGenerator.cs | 15 ++++ .../Logic/Data/World/WorldNavigation.cs | 68 +++++++++++++++++-- 16 files changed, 128 insertions(+), 39 deletions(-) delete mode 100644 Assets/Scripts/Logic/Data/World/Dijkstra.cs delete mode 100644 Assets/Scripts/Logic/Data/World/Dijkstra.cs.meta diff --git a/Assets/Scripts/Logic/Data/World/Barrack.cs b/Assets/Scripts/Logic/Data/World/Barrack.cs index 65b1c63..3dbd9d6 100644 --- a/Assets/Scripts/Logic/Data/World/Barrack.cs +++ b/Assets/Scripts/Logic/Data/World/Barrack.cs @@ -5,6 +5,9 @@ using Logic.Event.World.Barrack; using Logic.Event.World.Unit; namespace Logic.Data.World { +/// <summary> +/// Represents a barrack in the game. +/// </summary> public class Barrack : Building { #region Fields diff --git a/Assets/Scripts/Logic/Data/World/Building.cs b/Assets/Scripts/Logic/Data/World/Building.cs index 247648e..3ad85f6 100644 --- a/Assets/Scripts/Logic/Data/World/Building.cs +++ b/Assets/Scripts/Logic/Data/World/Building.cs @@ -1,10 +1,18 @@ ďťżnamespace Logic.Data.World { - +/// <summary> +/// Abstract class of every building of the world +/// </summary> public abstract class Building : TileObject { + /// <summary> + /// Color of the owner's team + /// </summary> public Color OwnerColor { get; } //Color is saved instead of GameTeam because in order to create a GameTeam // a Castle needed, so a Castle mustn't require a GameTeam instance. + /// <summary> + /// Owner of the building. + /// </summary> public GameTeam Owner => World.Overview.GetTeam(OwnerColor); private protected Building(GameWorld world, TilePosition position, Color owner) @@ -12,5 +20,4 @@ public abstract class Building : TileObject { OwnerColor = owner; } } - } diff --git a/Assets/Scripts/Logic/Data/World/Castle.cs b/Assets/Scripts/Logic/Data/World/Castle.cs index 3582098..6c256f6 100644 --- a/Assets/Scripts/Logic/Data/World/Castle.cs +++ b/Assets/Scripts/Logic/Data/World/Castle.cs @@ -2,6 +2,9 @@ using Logic.Event.World.Castle; namespace Logic.Data.World { +/// <summary> +/// represents a castle in the game. +/// </summary> public class Castle : Building { #region Properties diff --git a/Assets/Scripts/Logic/Data/World/Dijkstra.cs b/Assets/Scripts/Logic/Data/World/Dijkstra.cs deleted file mode 100644 index 2b77bbd..0000000 --- a/Assets/Scripts/Logic/Data/World/Dijkstra.cs +++ /dev/null @@ -1,18 +0,0 @@ -ďťżnamespace Logic.Data.World { -internal class Dijkstra { - public int D { get; set; } - public int Ox { get; } - public int Oy { get; } - public int Px { get; set; } - public int Py { get; set; } - public bool Queued { get; set; } - - public Dijkstra(int x, int y) { - D = int.MaxValue; - Ox = x; - Oy = y; - Px = -1; - Py = -1; - } -} -} diff --git a/Assets/Scripts/Logic/Data/World/Dijkstra.cs.meta b/Assets/Scripts/Logic/Data/World/Dijkstra.cs.meta deleted file mode 100644 index 7a413ca..0000000 --- a/Assets/Scripts/Logic/Data/World/Dijkstra.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -ďťżfileFormatVersion: 2 -guid: d6355afc4ea14c25b1aa45e6459ef6e6 -timeCreated: 1647607809 \ No newline at end of file diff --git a/Assets/Scripts/Logic/Data/World/GameWorld.cs b/Assets/Scripts/Logic/Data/World/GameWorld.cs index 5c83b46..b9ba569 100644 --- a/Assets/Scripts/Logic/Data/World/GameWorld.cs +++ b/Assets/Scripts/Logic/Data/World/GameWorld.cs @@ -5,6 +5,9 @@ using Logic.Event.World.Tower; using Logic.Event.World.Unit; namespace Logic.Data.World { +/// <summary> +/// Represents the world in which the game takes place. +/// </summary> public class GameWorld { #region Fields diff --git a/Assets/Scripts/Logic/Data/World/ITowerTypeData.cs b/Assets/Scripts/Logic/Data/World/ITowerTypeData.cs index 66f5ce6..ed0fb18 100644 --- a/Assets/Scripts/Logic/Data/World/ITowerTypeData.cs +++ b/Assets/Scripts/Logic/Data/World/ITowerTypeData.cs @@ -1,4 +1,7 @@ ďťżnamespace Logic.Data.World { +/// <summary> +/// Interface for the different types of <see cref="Tower"/>s +/// </summary> public interface ITowerTypeData { #region Properties @@ -33,7 +36,7 @@ public interface ITowerTypeData { public int DestroyRefund { get; } /// <summary> - /// Building cost of the tower type. + /// Upgrade cost of the tower type. /// </summary> public int UpgradeCost { get; } diff --git a/Assets/Scripts/Logic/Data/World/IUnitTypeData.cs b/Assets/Scripts/Logic/Data/World/IUnitTypeData.cs index ad997e8..33d0bf4 100644 --- a/Assets/Scripts/Logic/Data/World/IUnitTypeData.cs +++ b/Assets/Scripts/Logic/Data/World/IUnitTypeData.cs @@ -1,4 +1,7 @@ ďťżnamespace Logic.Data.World { +/// <summary> +/// Interface for the different types of <see cref="Unit"/>s +/// </summary> public interface IUnitTypeData { #region Properties diff --git a/Assets/Scripts/Logic/Data/World/Obstacle.cs b/Assets/Scripts/Logic/Data/World/Obstacle.cs index ea5e039..6b233f6 100644 --- a/Assets/Scripts/Logic/Data/World/Obstacle.cs +++ b/Assets/Scripts/Logic/Data/World/Obstacle.cs @@ -1,6 +1,13 @@ ďťżnamespace Logic.Data.World { - +/// <summary> +/// Represents an obstacle of the world. +/// </summary> public class Obstacle : TileObject { + /// <summary> + /// Creates an obstacle. + /// </summary> + /// <param name="world">The world in which it will be created.</param> + /// <param name="position">The position of the obstacle.</param> internal Obstacle(GameWorld world, TilePosition position) : base(world, position) {} } diff --git a/Assets/Scripts/Logic/Data/World/TileObject.cs b/Assets/Scripts/Logic/Data/World/TileObject.cs index ba3705e..60b8889 100644 --- a/Assets/Scripts/Logic/Data/World/TileObject.cs +++ b/Assets/Scripts/Logic/Data/World/TileObject.cs @@ -1,4 +1,7 @@ ďťżnamespace Logic.Data.World { +/// <summary> +/// Base class of every Building and obstacle of the world. +/// </summary> public abstract class TileObject { /// <summary> /// The world in which the TileObject is located. @@ -15,10 +18,6 @@ public abstract class TileObject { Position = position; } - /// <summary> - /// Formats the object for printing on screen. - /// </summary> - /// <returns>Printable format of the TileObject.</returns> public override string ToString() { return $"{GetType().Name}@{Position}"; } diff --git a/Assets/Scripts/Logic/Data/World/TilePosition.cs b/Assets/Scripts/Logic/Data/World/TilePosition.cs index 6e6077c..dc7ff13 100644 --- a/Assets/Scripts/Logic/Data/World/TilePosition.cs +++ b/Assets/Scripts/Logic/Data/World/TilePosition.cs @@ -1,6 +1,9 @@ ďťżusing System; namespace Logic.Data.World { +/// <summary> +/// Represents a tile's position on the grid. +/// </summary> public readonly struct TilePosition { /// <summary> /// First coordinate of the position. @@ -92,10 +95,6 @@ public readonly struct TilePosition { return Added(-x, -y); } - /// <summary> - /// Formats the TilePosition for printing on screen. - /// </summary> - /// <returns>Printable format of the TilePosition.</returns> public override string ToString() { return $"({X};{Y})"; } diff --git a/Assets/Scripts/Logic/Data/World/Tower.cs b/Assets/Scripts/Logic/Data/World/Tower.cs index 31fe3fb..6556d47 100644 --- a/Assets/Scripts/Logic/Data/World/Tower.cs +++ b/Assets/Scripts/Logic/Data/World/Tower.cs @@ -3,6 +3,9 @@ using System.Linq; using Logic.Event.World.Tower; namespace Logic.Data.World { +/// <summary> +/// Represents a tower of the world. +/// </summary> public class Tower : Building { #region Properties @@ -107,7 +110,7 @@ public class Tower : Building { } /// <summary> - /// The tower at the target. + /// The tower shoots at the target. /// </summary> /// <exception cref="InvalidOperationException">If the tower has no targets or is on cooldown.</exception> internal void Shoot() { diff --git a/Assets/Scripts/Logic/Data/World/Unit.cs b/Assets/Scripts/Logic/Data/World/Unit.cs index dc52fe7..788a839 100644 --- a/Assets/Scripts/Logic/Data/World/Unit.cs +++ b/Assets/Scripts/Logic/Data/World/Unit.cs @@ -3,6 +3,9 @@ using System.Linq; using Logic.Event.World.Unit; namespace Logic.Data.World { +/// <summary> +/// Represents a unit of the world. +/// </summary> public class Unit { #region Fields diff --git a/Assets/Scripts/Logic/Data/World/Vector2.cs b/Assets/Scripts/Logic/Data/World/Vector2.cs index 7b6680f..2a03742 100644 --- a/Assets/Scripts/Logic/Data/World/Vector2.cs +++ b/Assets/Scripts/Logic/Data/World/Vector2.cs @@ -1,6 +1,10 @@ ďťżusing System; namespace Logic.Data.World { + +/// <summary> +/// Represents an indiscreet position in the world. +/// </summary> public readonly struct Vector2 { /// <summary> /// First coordinate of the vector. diff --git a/Assets/Scripts/Logic/Data/World/WorldGenerator.cs b/Assets/Scripts/Logic/Data/World/WorldGenerator.cs index 7e65c8f..148a03a 100644 --- a/Assets/Scripts/Logic/Data/World/WorldGenerator.cs +++ b/Assets/Scripts/Logic/Data/World/WorldGenerator.cs @@ -3,7 +3,19 @@ using System.Collections.Generic; using System.Linq; namespace Logic.Data.World { +/// <summary> +/// Class for generating the world. +/// </summary> internal class WorldGenerator { + /// <summary> + /// Generates the world. + /// </summary> + /// <param name="seed">Seed for randomisation.</param> + /// <param name="width">The width of the world.</param> + /// <param name="height">The height of the world.</param> + /// <param name="generateObstacles">If false, obstacles won't be generated.</param> + /// <param name="constructors">The constructors for the TileObjects.</param> + /// <returns>The grid of the world.</returns> public static TileObject[,] GenerateGrid(int seed, int width, int height, bool generateObstacles, ITileObjectConstructors constructors) { WorldGenerator generator = new WorldGenerator(seed, width, height, generateObstacles, constructors); @@ -117,6 +129,9 @@ internal class WorldGenerator { } } + /// <summary> + /// Interface for constructing TileObjects during generation. + /// </summary> public interface ITileObjectConstructors { public Castle CreateCastle(TilePosition position, Color team); public Barrack CreateBarrack(TilePosition position, Color team); diff --git a/Assets/Scripts/Logic/Data/World/WorldNavigation.cs b/Assets/Scripts/Logic/Data/World/WorldNavigation.cs index f1aab0f..cf3a570 100644 --- a/Assets/Scripts/Logic/Data/World/WorldNavigation.cs +++ b/Assets/Scripts/Logic/Data/World/WorldNavigation.cs @@ -3,21 +3,49 @@ using System.Collections.Generic; using System.Linq; namespace Logic.Data.World { +/// <summary> +/// Represents the navigation system of the world. +/// </summary> internal class WorldNavigation { private readonly TileObject[,] _grid; + /// <summary> + /// Creates a navigation system. + /// </summary> + /// <param name="grid">The worldgrid.</param> public WorldNavigation(TileObject[,] grid) { _grid = grid; } + /// <summary> + /// Checks if there's a path between two tiles. + /// </summary> + /// <param name="from">First tile's position.</param> + /// <param name="to">Second tile's position.</param> + /// <param name="blockedTiles">The tiles that are blocked.</param> + /// <returns>True if a path exists.</returns> public bool IsPositionReachable(TilePosition from, TilePosition to, ISet<TilePosition> blockedTiles) { - return GetReachablePositionSubset(from, new HashSet<TilePosition> { to }, blockedTiles).Any(); + return GetReachablePositionSubset(from, new HashSet<TilePosition> {to}, blockedTiles).Any(); } + /// <summary> + /// Checks if there's a path between two tiles. + /// </summary> + /// <param name="from">First tile's position.</param> + /// <param name="to">Second tile's position.</param> + /// <returns>True if a path exists.</returns> public bool IsPositionReachable(TilePosition from, TilePosition to) { - return GetReachablePositionSubset(from, new HashSet<TilePosition> { to }).Any(); + return GetReachablePositionSubset(from, new HashSet<TilePosition> {to}).Any(); } + /// <summary> + /// Finds all the reachable TilePosition from a TilePosition. + /// </summary> + /// <param name="from">The start of the pathfinding.</param> + /// <param name="to">Set containing the destination for the pathfinding. </param> + /// <param name="blockedTiles">The tiles that are blocked. </param> + /// <returns>The set of reachable positions.</returns> + /// <exception cref="ArgumentException">If 'to' is a blocked position.</exception> public ISet<TilePosition> GetReachablePositionSubset(TilePosition from, ISet<TilePosition> to, ISet<TilePosition> blockedTiles) { if (blockedTiles.Overlaps(to)) @@ -41,6 +69,12 @@ internal class WorldNavigation { return result; } + /// <summary> + /// Finds all the reachable TilePosition from a TilePosition. + /// </summary> + /// <param name="from">The start of the pathfinding.</param> + /// <param name="to">Set containing the destination for the pathfinding. </param> + /// <returns>The set of reachable positions.</returns> public ISet<TilePosition> GetReachablePositionSubset(TilePosition from, ISet<TilePosition> to) { Dijkstra[,] dGrid = RunDijkstra(from.X, from.Y, to); HashSet<TilePosition> result = new HashSet<TilePosition>(to); @@ -66,8 +100,8 @@ internal class WorldNavigation { queue.Add(dGrid[fromX, fromY]); dGrid[fromX, fromY].Queued = true; - int[] diffX = { 0, 0, -1, 1 }; - int[] diffY = { 1, -1, 0, 0 }; + int[] diffX = {0, 0, -1, 1}; + int[] diffY = {1, -1, 0, 0}; //Instead of removing from the queue, advance an index: this is faster. //The first element always has the least weight due to all edges having the same cost (1). @@ -101,11 +135,18 @@ internal class WorldNavigation { return dGrid; } + /// <summary> + /// Calculates the vectors from tile to tile in the path between two positions. + /// </summary> + /// <param name="from">The start if the path.</param> + /// <param name="to">The end of the path.</param> + /// <param name="colliderRadius">The colliderRadius of the object that travels on the path.</param> + /// <returns>The pathDelta vectors</returns> public List<Vector2> TryGetPathDeltas(Vector2 from, Vector2 to, float colliderRadius) { if (from.ToTilePosition().Equals(to.ToTilePosition())) return new List<Vector2>(); Dijkstra[,] dGrid = RunDijkstra((int) from.X, (int) from.Y, - new HashSet<TilePosition> { to.ToTilePosition() }); + new HashSet<TilePosition> {to.ToTilePosition()}); if (dGrid[(int) to.X, (int) to.Y].D == int.MaxValue) return null; List<Vector2> pathDeltas = new List<Vector2>(); @@ -127,5 +168,22 @@ internal class WorldNavigation { private class FillerTileObject : TileObject { public FillerTileObject(TilePosition position) : base(null, position) {} } + + private class Dijkstra { + public int D { get; set; } + public int Ox { get; } + public int Oy { get; } + public int Px { get; set; } + public int Py { get; set; } + public bool Queued { get; set; } + + public Dijkstra(int x, int y) { + D = int.MaxValue; + Ox = x; + Oy = y; + Px = -1; + Py = -1; + } + } } } -- GitLab