riversong code showcase
This commit is contained in:
12
Source/Riversong/Game/World/FertilityMap/FertilityMap.cs
Normal file
12
Source/Riversong/Game/World/FertilityMap/FertilityMap.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Unity.Collections;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class FertilityMap : NativeGrid<FertilityMapValue>
|
||||
{
|
||||
public FertilityMap(int2 size) : base(size, Allocator.Persistent)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public struct FertilityMapValue
|
||||
{
|
||||
public float CurrentFertility;
|
||||
|
||||
public float MaxFertility;
|
||||
|
||||
public int LockId;
|
||||
|
||||
public void Deconstruct(out float currentFertility, out float maxFertility)
|
||||
{
|
||||
currentFertility = CurrentFertility;
|
||||
maxFertility = MaxFertility;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[RequiresWorldReadyForUpdate]
|
||||
[GameSystemGroup(typeof(EarlyGameSystemGroup))]
|
||||
public class UpdateFertilityMapSystem : GameSystem, IInitializable, IDisposable, IUpdatable
|
||||
{
|
||||
[InjectService]
|
||||
private GameConfig _config;
|
||||
|
||||
[InjectService]
|
||||
private World _world;
|
||||
|
||||
[InjectService]
|
||||
private ISignalBus _signalBus;
|
||||
|
||||
public UpdateFertilityMapSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public UniTask InitializeAsync()
|
||||
{
|
||||
_signalBus.Subscribe<BuildingPlacementAnimationStartedSignal>(OnBuildingPlacementAnimationStarted);
|
||||
_signalBus.Subscribe<RoadTileUpdatedSignal>(OnRoadTileUpdated);
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_signalBus.Unsubscribe<BuildingPlacementAnimationStartedSignal>(OnBuildingPlacementAnimationStarted);
|
||||
_signalBus.Unsubscribe<RoadTileUpdatedSignal>(OnRoadTileUpdated);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var blockMap = _world.BlockMap.GetNativeArray();
|
||||
var fertility = _world.Fertility.GetNativeArray();
|
||||
var delta = _config.Terrain.FertilityRegenarationRate * Time.deltaTime;
|
||||
|
||||
new UpdateFertilityMapJob
|
||||
{
|
||||
BlockMap = blockMap,
|
||||
Fertility = fertility,
|
||||
Delta = delta
|
||||
}.Schedule(fertility.Length, _world.Size.x)
|
||||
.Complete();
|
||||
}
|
||||
|
||||
private void OnBuildingPlacementAnimationStarted(BuildingPlacementAnimationStartedSignal signal)
|
||||
{
|
||||
foreach (var tile in TileRange.From(signal.Rect)) _world.Fertility.GetValueRW(tile).CurrentFertility = 0;
|
||||
}
|
||||
|
||||
private void OnRoadTileUpdated(RoadTileUpdatedSignal signal)
|
||||
{
|
||||
if (!signal.RoadTileAdded) return;
|
||||
|
||||
_world.Fertility.GetValueRW(signal.Tile).CurrentFertility = 0;
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
private struct UpdateFertilityMapJob : IJobParallelFor
|
||||
{
|
||||
[ReadOnly]
|
||||
public NativeArray<BlockReason> BlockMap;
|
||||
|
||||
public NativeArray<FertilityMapValue> Fertility;
|
||||
|
||||
public float Delta;
|
||||
|
||||
[BurstCompile]
|
||||
public void Execute(int index)
|
||||
{
|
||||
var value = Fertility[index];
|
||||
|
||||
if ((BlockMap[index] & BlockReason.ClearGrassMask) != 0 || value.CurrentFertility >= value.MaxFertility) return;
|
||||
|
||||
value.CurrentFertility = math.min(value.CurrentFertility + Delta, value.MaxFertility);
|
||||
|
||||
Fertility[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user