riversong code showcase
This commit is contained in:
47
Source/Riversong/Game/World/EntityIdMap/EntityIdMap.cs
Normal file
47
Source/Riversong/Game/World/EntityIdMap/EntityIdMap.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using Unity.Collections;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class EntityIdMap : IDisposable
|
||||
{
|
||||
private NativeGrid<EntityIdMapValue> _data;
|
||||
|
||||
public EntityIdMap(int2 size)
|
||||
{
|
||||
_data = new NativeGrid<EntityIdMapValue>(size, Allocator.Persistent);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_data?.Dispose();
|
||||
_data = null;
|
||||
}
|
||||
|
||||
public void SetBuildingId(TileRect rect, int id)
|
||||
{
|
||||
foreach (var tile in TileRange.From(rect))
|
||||
{
|
||||
ref var value = ref _data.GetValueRW(tile);
|
||||
value.BuildingId = id;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetResourceNodeId(int2 tile, int id)
|
||||
{
|
||||
ref var value = ref _data.GetValueRW(tile);
|
||||
value.ResourceNodeId = id;
|
||||
}
|
||||
|
||||
public ref EntityIdMapValue GetValueRW(int2 tile)
|
||||
{
|
||||
return ref _data.GetValueRW(tile);
|
||||
}
|
||||
|
||||
public NativeArray<EntityIdMapValue> GetNativeArray()
|
||||
{
|
||||
return _data.GetNativeArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.Pool;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class EntityIdMapUpdateSystem : GameSystem, IInitializable, IDisposable, IOnWorldGenerationCompletedCallback
|
||||
{
|
||||
[InjectService]
|
||||
private IEntityCollection _entityCollection;
|
||||
|
||||
[InjectService]
|
||||
private ISignalBus _signalBus;
|
||||
|
||||
[InjectService]
|
||||
private World _world;
|
||||
|
||||
[InjectService]
|
||||
private GameConfig _config;
|
||||
|
||||
public EntityIdMapUpdateSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public UniTask InitializeAsync()
|
||||
{
|
||||
_entityCollection.On<ConstructionSite>().Added += OnConstructionSiteAdded;
|
||||
_entityCollection.On<ConstructionSite>().Removed += OnConstructionSiteRemoved;
|
||||
_entityCollection.On<Building>().Added += OnBuildingAdded;
|
||||
_entityCollection.On<Building>().Removed += OnBuildingRemoved;
|
||||
_signalBus.Subscribe<WorldGenerationCompletedSignal>(OnWorldGenerationCompleted);
|
||||
_signalBus.Subscribe<RawResourcesRemovedSignal>(OnRawResourcesRemoved);
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_entityCollection.On<ConstructionSite>().Added -= OnConstructionSiteAdded;
|
||||
_entityCollection.On<ConstructionSite>().Removed -= OnConstructionSiteRemoved;
|
||||
_entityCollection.On<Building>().Added -= OnBuildingAdded;
|
||||
_entityCollection.On<Building>().Removed -= OnBuildingRemoved;
|
||||
_signalBus.Unsubscribe<WorldGenerationCompletedSignal>(OnWorldGenerationCompleted);
|
||||
_signalBus.Unsubscribe<RawResourcesRemovedSignal>(OnRawResourcesRemoved);
|
||||
}
|
||||
|
||||
private void OnConstructionSiteAdded(ConstructionSite constructionSite)
|
||||
{
|
||||
_world.EntityIdMap.SetBuildingId(constructionSite.Rect, constructionSite.Id);
|
||||
}
|
||||
|
||||
private void OnConstructionSiteRemoved(ConstructionSite constructionSite)
|
||||
{
|
||||
_world.EntityIdMap.SetBuildingId(constructionSite.Rect, Entity.InvalidId);
|
||||
}
|
||||
|
||||
private void OnBuildingAdded(Building building)
|
||||
{
|
||||
_world.EntityIdMap.SetBuildingId(building.Rect, building.Id);
|
||||
}
|
||||
|
||||
private void OnBuildingRemoved(Building building)
|
||||
{
|
||||
_world.EntityIdMap.SetBuildingId(building.Rect, Entity.InvalidId);
|
||||
}
|
||||
|
||||
private void OnWorldGenerationCompleted(WorldGenerationCompletedSignal signal)
|
||||
{
|
||||
signal.Callbacks.Add(this);
|
||||
}
|
||||
|
||||
public UniTask OnWorldGenerationCompletedAsync(World world)
|
||||
{
|
||||
foreach (var resourceNode in _world.RawResources) _world.EntityIdMap.SetResourceNodeId(resourceNode.Tile, resourceNode.DefinitionId);
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
private void OnRawResourcesRemoved(RawResourcesRemovedSignal signal)
|
||||
{
|
||||
using var resourceNodesScope = ListPool<(int, bool)>.Get(out var resourceNodes);
|
||||
|
||||
foreach (var removedResourceNode in signal.ResourceNodes)
|
||||
{
|
||||
resourceNodes.Clear();
|
||||
_world.RawResources.GetResourceNodes(TileRect.OneTile(removedResourceNode.Tile), _config.GeneralSettings.BaseElevation, resourceNodes);
|
||||
|
||||
var definitionId = Entity.InvalidId;
|
||||
if (resourceNodes.Count > 0)
|
||||
{
|
||||
var (resourceId, _) = resourceNodes[0];
|
||||
_world.RawResources.TryGetResourceNode(resourceId, out var resourceNode);
|
||||
definitionId = resourceNode.DefinitionId;
|
||||
}
|
||||
_world.EntityIdMap.SetResourceNodeId(removedResourceNode.Tile, definitionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Source/Riversong/Game/World/EntityIdMap/EntityIdMapValue.cs
Normal file
11
Source/Riversong/Game/World/EntityIdMap/EntityIdMapValue.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public struct EntityIdMapValue
|
||||
{
|
||||
public int BuildingId;
|
||||
|
||||
public int ResourceNodeId;
|
||||
|
||||
public int CritterHerdId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user