Last active
May 26, 2024 14:14
-
-
Save Sythelux/423e4cea0341102d4515f0bb9ffd539e to your computer and use it in GitHub Desktop.
Godot 4 C# Array2D
This file contains hidden or 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
using System.Linq; | |
using Godot; | |
using Godot.Collections; | |
public class Array2D<[MustBeVariant] T> | |
{ | |
public readonly Array<T> Array; | |
public readonly int Width; | |
public readonly int Height; | |
public readonly int Length; // Width * Height | |
public T this[uint index] | |
{ | |
get => Array[(int)index]; | |
set => Array[(int)index] = value; | |
} | |
public T this[int index] | |
{ | |
get => Array[index]; | |
set => Array[index] = value; | |
} | |
public T this[uint y, uint x] | |
{ | |
get => Array[(int)(y * Width + x)]; | |
set => Array[(int)(y * Width + x)] = value; | |
} | |
public T this[int y, int x] | |
{ | |
get => Array[y * Width + x]; | |
set => Array[y * Width + x] = value; | |
} | |
public Array2D(int height, int width) | |
{ | |
Height = height; | |
Width = width; | |
Length = Width * Height; | |
Array = new Array<T>(new T[Length]); | |
} | |
public Array2D(T[,] source) | |
{ | |
Array = new Array<T>(source.Cast<T>()); | |
Height = source.GetLength(0); | |
Width = source.GetLength(1); | |
Length = Width * Height; | |
} | |
public Array2D(int height, int width, out Array<T> outputNativeArray) | |
{ | |
Height = height; | |
Width = width; | |
Length = Width * Height; | |
Array = new Array<T>(new T[Length]); | |
outputNativeArray = Array; //TODO check, but this should be a reference | |
} | |
public void Clear() | |
{ | |
Array.Clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment