-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Giving more control to what types can be specified for shapes and add…
…ing in a basic idea of a generator for the grid
- Loading branch information
Showing
11 changed files
with
225 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MonoGame.Grid.Extensions | ||
{ | ||
public static class CreateGeneratorGridExtensions | ||
{ | ||
/// <summary> | ||
/// Will create a genertor to define how the grid will be generated with | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of object that will be assigned in the grid</typeparam> | ||
/// <param name="grid">The current grid we need to generate values in</param> | ||
/// <returns>Will return a generator to fill in a grid</returns> | ||
public static IGridGenerator<TValue> CreateGenerator<TValue>(this IGrid<TValue> grid) | ||
{ | ||
return new GridGenerator<TValue>(grid); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
namespace MonoGame.Grid.Extensions | ||
{ | ||
/// <summary> | ||
/// Implmentation of <see cref="IGridGenerator{TValue}" /> | ||
/// </summary> | ||
/// <typeparam name="TValue">The current value that is stored in the grid</typeparam> | ||
internal class GridGenerator<TValue> : IGridGenerator<TValue> | ||
{ | ||
/// <summary> | ||
/// The current grid we are filling in | ||
/// </summary> | ||
private readonly IGrid<TValue> _grid; | ||
|
||
/// <summary> | ||
/// The current shapes we are currently trying to avoid making | ||
/// </summary> | ||
private readonly List<IBaseShape<TValue>> _shapesToAvoid; | ||
|
||
/// <summary> | ||
/// The getter function to get a random value to be put in the grid | ||
/// </summary> | ||
private Func<TValue>? _randomGetter; | ||
|
||
/// <summary> | ||
/// Builds the generator constructor to fill a grid in | ||
/// </summary> | ||
/// <param name="grid">The current grid we are filling in</param> | ||
public GridGenerator(IGrid<TValue> grid) | ||
{ | ||
_grid = grid; | ||
_shapesToAvoid = new List<IBaseShape<TValue>>(); | ||
_randomGetter = null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Generate() | ||
{ | ||
if (_randomGetter == null) | ||
throw new ArgumentNullException("randomGetter", "Cannot fill a grid if values are not configured"); | ||
|
||
var span = _grid.AsSpan(); | ||
for (var i = span.Length - 1; i >=0; --i) | ||
{ | ||
if (span[i] == null) | ||
{ | ||
var point = i.ToPoint(_grid.Width); | ||
|
||
do | ||
{ // TODO: Figure out a better way to calculate the new item that should be put on the grid | ||
// Right now this is very inefficient | ||
_grid[point] = _randomGetter(); | ||
} while (_shapesToAvoid.Count > 0 && _shapesToAvoid.Any(x => x.HasShapeOnPoint(point, _grid) != null)); | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IGridGenerator<TValue> WithoutShapes<TType>(params IShape<TValue, TType>[] shapes) | ||
{ | ||
_shapesToAvoid.AddRange(shapes); | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IGridGenerator<TValue> OverwriteNoneNulls() | ||
{ | ||
_grid.Clear(); | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IGridGenerator<TValue> WithRandomValue(Func<TValue> randomGetter) | ||
{ | ||
_randomGetter = randomGetter; | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace MonoGame.Grid.Extensions | ||
{ | ||
/// <summary> | ||
/// Grid generator to fill in a grid with values | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of value that will fill the grid with</typeparam> | ||
public interface IGridGenerator<TValue> | ||
{ | ||
/// <summary> | ||
/// Adds a set of shapes that we want avoid adding to the grid when filling it in | ||
/// </summary> | ||
/// <typeparam name="TType">The type of shape we are working with</typeparam> | ||
/// <param name="shapes">The shapes we are avoiding when creating the grid</param> | ||
/// <returns>Will return the generator for chaining</returns> | ||
public IGridGenerator<TValue> WithoutShapes<TType>(params IShape<TValue, TType>[] shapes); | ||
|
||
/// <summary> | ||
/// Adds a getter to randomly set a value on the grid based on the value type | ||
/// </summary> | ||
/// <param name="randomGetter">The value getter to call when creating a new object</param> | ||
/// <returns>Will return the generator for chaining</returns> | ||
public IGridGenerator<TValue> WithRandomValue(Func<TValue> randomGetter); | ||
|
||
/// <summary> | ||
/// Will tell the generator to overwrite the whole grid and ignore nulls completely | ||
/// </summary> | ||
/// <returns>Will return the generator for chaining</returns> | ||
public IGridGenerator<TValue> OverwriteNoneNulls(); | ||
|
||
/// <summary> | ||
/// Will generate the grid with values | ||
/// </summary> | ||
public void Generate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.