riversong code showcase
This commit is contained in:
50
Source/Riversong/Game/EditTools/RoadTool.cs
Normal file
50
Source/Riversong/Game/EditTools/RoadTool.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class RoadTool : DragTool
|
||||
{
|
||||
private IEditToolValidatorService _validator;
|
||||
|
||||
private IRoadFactory _roadFactory;
|
||||
|
||||
public RoadTool(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
protected override DeletedGameObjectsFilter DeletedGameObjectsFilter => DeletedGameObjectsFilter.RawResources;
|
||||
|
||||
public override async UniTask InitializeAsync()
|
||||
{
|
||||
await base.InitializeAsync();
|
||||
|
||||
_validator = ServiceLocator.GetService<IEditToolValidatorService>();
|
||||
_roadFactory = ServiceLocator.GetService<IRoadFactory>();
|
||||
}
|
||||
|
||||
protected override bool Validate(ref int2 startTile, ref int2 endTile)
|
||||
{
|
||||
var dx = endTile.x - startTile.x;
|
||||
var dy = endTile.y - startTile.y;
|
||||
|
||||
if (math.abs(dx) >= math.abs(dy))
|
||||
endTile = new int2(endTile.x, startTile.y);
|
||||
else
|
||||
endTile = new int2(startTile.x, endTile.y);
|
||||
|
||||
return _validator.DoCommonValidation(new TileRect(startTile, endTile), BlockReason.CannotBuildRoad) == EditToolValidationResult.Success;
|
||||
}
|
||||
|
||||
protected override void UpdateAffectedTiles(bool isValid, int2 startTile, int2 endTile)
|
||||
{
|
||||
var type = isValid ? TileHighlightType.ValidTile : TileHighlightType.InvalidTile;
|
||||
foreach (var tile in TileRange.From(startTile, endTile)) AffectedTiles.Add((tile, type));
|
||||
}
|
||||
|
||||
protected override void DoTool(int2 startTile, int2 endTile)
|
||||
{
|
||||
_roadFactory.CreateRoad(startTile, endTile);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user