riversong code showcase
This commit is contained in:
65
Source/Riversong/Game/Collections/NativeGrid.cs
Normal file
65
Source/Riversong/Game/Collections/NativeGrid.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class NativeGrid<T> : IDisposable where T : struct
|
||||
{
|
||||
private NativeArray<T> _data;
|
||||
|
||||
public NativeGrid(int2 size, Allocator allocator)
|
||||
{
|
||||
Size = size;
|
||||
_data = new NativeArray<T>(Size.x * Size.y, allocator);
|
||||
}
|
||||
|
||||
public int2 Size { get; }
|
||||
|
||||
protected int GetIndex(int2 point)
|
||||
{
|
||||
return math.mad(point.y, Size.x, point.x);
|
||||
}
|
||||
|
||||
public T GetValue(int index)
|
||||
{
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
public T GetValue(int2 point)
|
||||
{
|
||||
return GetValue(GetIndex(point));
|
||||
}
|
||||
|
||||
public unsafe ref T GetValueRW(int index)
|
||||
{
|
||||
return ref UnsafeUtility.ArrayElementAsRef<T>(_data.GetUnsafePtr(), index);
|
||||
}
|
||||
|
||||
public ref T GetValueRW(int2 point)
|
||||
{
|
||||
return ref GetValueRW(GetIndex(point));
|
||||
}
|
||||
|
||||
public void SetValue(int index, T value)
|
||||
{
|
||||
_data[index] = value;
|
||||
}
|
||||
|
||||
public void SetValue(int2 point, T value)
|
||||
{
|
||||
SetValue(GetIndex(point), value);
|
||||
}
|
||||
|
||||
public NativeArray<T> GetNativeArray()
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_data.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user