Skip to content
This repository has been archived by the owner on Oct 22, 2022. It is now read-only.

Commit

Permalink
General cleanup
Browse files Browse the repository at this point in the history
- Add .editorconfig
- Remove `private` accessor
- Use `new(...)` shortcut where possible
- Simplify using statements
- Replace Entity with EcsId
  • Loading branch information
copygirl committed Nov 14, 2021
1 parent 99e230e commit f7374df
Show file tree
Hide file tree
Showing 46 changed files with 650 additions and 614 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.cs]
indent_style = tab
indent_size = 4
# IDE0005: Using directive is unnecessary
dotnet_diagnostic.IDE0005.severity = suggestion

[*.md]
# Allows placing double-space at end of lines.
trim_trailing_whitespace = false
15 changes: 0 additions & 15 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
{
"editor.rulers": [ 80 ],
"editor.tabSize": 2,
"editor.insertSpaces": true,
"files.trimTrailingWhitespace": true,

"[csharp]": {
"editor.tabSize": 3,
"editor.insertSpaces": false
},

"[markdown]": {
// Allows to put double-space at end of lines.
"files.trimTrailingWhitespace": false,
},

"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
Expand Down
19 changes: 9 additions & 10 deletions src/Immersion/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ namespace Immersion
{
public class Program : Game
{
private static void Main(string[] args)
=> new Program().Run();
static void Main() => new Program().Run();


public Random RND { get; } = new Random();
public Random RND { get; } = new();

public Program()
{
Expand Down Expand Up @@ -63,15 +62,15 @@ protected override void OnLoad()
void CreateChunk(ChunkPos pos)
{
var chunk = Entities.New();
var storage = new ChunkPaletteStorage<Prototype>(default(Prototype));
var storage = new ChunkPaletteStorage<Prototype>(default);
for (var x = 0; x < 16; x++)
for (var y = 0; y < 16; y++)
for (var z = 0; z < 16; z++) {
var yy = (pos.Y << 4) | y;
if (RND.NextBool(0.5 - yy / 48.0))
storage[x, y, z] = new Prototype((yy > 16) ? grass
: (yy > -16) ? dirt
: stone);
storage[x, y, z] = new((yy > 16) ? grass
: (yy > -16) ? dirt
: stone);
}

Set(chunk, new Chunk(pos));
Expand All @@ -83,7 +82,7 @@ void CreateChunk(ChunkPos pos)
var chunkStore = (LookupDictionaryStore<ChunkPos, Chunk>)Components.GetStore<Chunk>();
void GenerateChunkMesh(ChunkPos pos)
{
var chunk = Entities.GetByID(chunkStore.GetEntityID(pos))!.Value;
var chunk = Entities.Lookup(chunkStore.GetEntityID(pos))!.Value;
var chunkMesh = chunkMeshGenerator.Generate(pos);
if (chunkMesh == null) return;
Set(chunk, chunkMesh.Value);
Expand All @@ -93,12 +92,12 @@ void GenerateChunkMesh(ChunkPos pos)
for (var x = -6; x < 6; x++)
for (var y = -2; y < 2; y++)
for (var z = -6; z < 6; z++)
CreateChunk(new ChunkPos(x, y, z));
CreateChunk(new(x, y, z));

for (var x = -6; x < 6; x++)
for (var y = -2; y < 2; y++)
for (var z = -6; z < 6; z++)
GenerateChunkMesh(new ChunkPos(x, y, z));
GenerateChunkMesh(new(x, y, z));
}


Expand Down
83 changes: 26 additions & 57 deletions src/gaemstone.Bloxel/BlockPos.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
using System;
using System.Numerics;
using gaemstone.Common.Utility;

namespace gaemstone.Bloxel
{
public readonly struct BlockPos
: IEquatable<BlockPos>
{
public static readonly BlockPos ORIGIN
= new BlockPos(0, 0, 0);

public static readonly BlockPos ORIGIN = new(0, 0, 0);

public int X { get; }
public int Y { get; }
public int Z { get; }

public BlockPos(int x, int y, int z)
=> (X, Y, Z) = (x, y, z);

public void Deconstruct(out int x, out int y, out int z)
=> (x, y, z) = (X, Y, Z);
public BlockPos(int x, int y, int z) => (X, Y, Z) = (x, y, z);
public void Deconstruct(out int x, out int y, out int z) => (x, y, z) = (X, Y, Z);

public Vector3 GetOrigin() => new(X, Y, Z);
public Vector3 GetCenter() => new(X + 0.5F, Y + 0.5F, Z + 0.5F);

public Vector3 GetOrigin()
=> new Vector3(X, Y, Z);
public Vector3 GetCenter()
=> new Vector3(X + 0.5F, Y + 0.5F, Z + 0.5F);


public BlockPos Add(int x, int y, int z)
=> new BlockPos(X + x, Y + y, Z + z);
public BlockPos Add(in BlockPos other)
=> new BlockPos(X + other.X, Y + other.Y, Z + other.Z);
public BlockPos Add(int x, int y, int z) => new(X + x, Y + y, Z + z);
public BlockPos Add(in BlockPos other) => new(X + other.X, Y + other.Y, Z + other.Z);

public BlockPos Add(BlockFacing facing)
{ var (x, y, z) = facing; return Add(x, y, z); }
Expand All @@ -44,10 +33,8 @@ public BlockPos Add(Neighbor neighor, int factor)
{ var (x, y, z) = neighor; return Add(x * factor, y * factor, z * factor); }


public BlockPos Subtract(int x, int y, int z)
=> new BlockPos(X - x, Y - y, Z - z);
public BlockPos Subtract(in BlockPos other)
=> new BlockPos(X - other.X, Y - other.Y, Z - other.Z);
public BlockPos Subtract(int x, int y, int z) => new(X - x, Y - y, Z - z);
public BlockPos Subtract(in BlockPos other) => new(X - other.X, Y - other.Y, Z - other.Z);

public BlockPos Subtract(BlockFacing facing)
{ var (x, y, z) = facing; return Subtract(x, y, z); }
Expand All @@ -63,45 +50,27 @@ public BlockPos Subtract(Neighbor neighor, int factor)
public bool Equals(BlockPos other)
=> (X == other.X) && (Y == other.Y) && (Z == other.Z);
public override bool Equals(object? obj)
=> (obj is BlockPos) && Equals((BlockPos)obj);

public override int GetHashCode()
=> HashCode.Combine(X, Y, Z);
public override string ToString()
=> $"BlockPos ({X}:{Y}:{Z})";
public string ToShortString()
=> $"{X}:{Y}:{Z}";


public static implicit operator BlockPos((int x, int y, int z) t)
=> new BlockPos(t.x, t.y, t.z);
public static implicit operator (int, int, int)(BlockPos pos)
=> (pos.X, pos.Y, pos.Z);

public static BlockPos operator +(BlockPos left, BlockPos right)
=> left.Add(right);
public static BlockPos operator -(BlockPos left, BlockPos right)
=> left.Subtract(right);
public static BlockPos operator +(BlockPos left, BlockFacing right)
=> left.Add(right);
public static BlockPos operator -(BlockPos left, BlockFacing right)
=> left.Subtract(right);
public static BlockPos operator +(BlockPos left, Neighbor right)
=> left.Add(right);
public static BlockPos operator -(BlockPos left, Neighbor right)
=> left.Subtract(right);

public static bool operator ==(BlockPos left, BlockPos right)
=> left.Equals(right);
public static bool operator !=(BlockPos left, BlockPos right)
=> !left.Equals(right);
=> (obj is BlockPos pos) && Equals(pos);

public override int GetHashCode() => HashCode.Combine(X, Y, Z);
public override string ToString() => $"BlockPos({X}:{Y}:{Z})";
public string ToShortString() => $"{X}:{Y}:{Z}";


public static BlockPos operator +(BlockPos left, BlockPos right) => left.Add(right);
public static BlockPos operator -(BlockPos left, BlockPos right) => left.Subtract(right);
public static BlockPos operator +(BlockPos left, BlockFacing right) => left.Add(right);
public static BlockPos operator -(BlockPos left, BlockFacing right) => left.Subtract(right);
public static BlockPos operator +(BlockPos left, Neighbor right) => left.Add(right);
public static BlockPos operator -(BlockPos left, Neighbor right) => left.Subtract(right);

public static bool operator ==(BlockPos left, BlockPos right) => left.Equals(right);
public static bool operator !=(BlockPos left, BlockPos right) => !left.Equals(right);
}

public static class BlockPosExtensions
{
public static BlockPos ToBlockPos(this Vector3 self)
=> new BlockPos((int)MathF.Floor(self.X),
(int)MathF.Floor(self.Y),
(int)MathF.Floor(self.Z));
=> new((int)MathF.Floor(self.X), (int)MathF.Floor(self.Y), (int)MathF.Floor(self.Z));
}
}
2 changes: 0 additions & 2 deletions src/gaemstone.Bloxel/Chunks/Chunk.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

namespace gaemstone.Bloxel.Chunks
{
public readonly struct Chunk
{
public ChunkPos Position { get; }

public Chunk(ChunkPos pos) => Position = pos;
}
}
38 changes: 19 additions & 19 deletions src/gaemstone.Bloxel/Chunks/ChunkPaletteStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace gaemstone.Bloxel.Chunks
// https://www.reddit.com/r/VoxelGameDev/comments/9yu8qy/palettebased_compression_for_chunked_discrete/
public class ChunkPaletteStorage<T>
{
private const int SIZE = 16 * 16 * 16;
private static readonly EqualityComparer<T> COMPARER
const int SIZE = 16 * 16 * 16;
static readonly EqualityComparer<T> COMPARER
= EqualityComparer<T>.Default;

private BitArray? _data;
private PaletteEntry[]? _palette;
private int _usedPalettes;
private int _indicesLength;
BitArray? _data;
PaletteEntry[]? _palette;
int _usedPalettes;
int _indicesLength;


public T Default { get; }
Expand All @@ -27,7 +27,7 @@ private static readonly EqualityComparer<T> COMPARER
}

public IEnumerable<T> Blocks
=> _palette?.Where(entry => !COMPARER.Equals(entry.Value, default(T)!))
=> _palette?.Where(entry => !COMPARER.Equals(entry.Value, default!))
.Select(entry => entry.Value!)
?? Enumerable.Empty<T>();

Expand All @@ -36,14 +36,14 @@ public ChunkPaletteStorage(T @default)
=> Default = @default;


private T Get(int x, int y, int z)
T Get(int x, int y, int z)
{
if (_palette == null) return Default;
var entry = _palette[GetPaletteIndex(x, y, z)];
return !COMPARER.Equals(entry.Value, default(T)!) ? entry.Value : Default;
return !COMPARER.Equals(entry.Value, default!) ? entry.Value : Default;
}

private void Set(int x, int y, int z, T value)
void Set(int x, int y, int z, T value)
{
if (_palette == null) {
if (COMPARER.Equals(value, Default)) return;
Expand Down Expand Up @@ -76,7 +76,7 @@ private void Set(int x, int y, int z, T value)
_usedPalettes++;
}

private int NewPaletteEntry()
int NewPaletteEntry()
{
if (_palette != null) {
int firstFree = Array.FindIndex(_palette, entry =>
Expand All @@ -88,9 +88,9 @@ private int NewPaletteEntry()
return NewPaletteEntry();
}

private void GrowPalette() {
void GrowPalette() {
if (_palette == null) {
_data = new BitArray(SIZE);
_data = new(SIZE);
_palette = new PaletteEntry[2];
_usedPalettes = 1;
_indicesLength = 1;
Expand Down Expand Up @@ -147,29 +147,29 @@ private void GrowPalette() {
// }


private int GetPaletteIndex(int x, int y, int z)
int GetPaletteIndex(int x, int y, int z)
=> GetPaletteIndex(GetIndex(x, y, z));
private int GetPaletteIndex(int index)
int GetPaletteIndex(int index)
{
var paletteIndex = 0;
for (var i = 0; i < _indicesLength; i++)
paletteIndex |= (_data!.Get(index + i) ? 1 : 0) << i;
return paletteIndex;
}

private void SetPaletteIndex(int x, int y, int z, int paletteIndex)
void SetPaletteIndex(int x, int y, int z, int paletteIndex)
=> SetPaletteIndex(GetIndex(x, y, z), paletteIndex);
private void SetPaletteIndex(int index, int paletteIndex)
void SetPaletteIndex(int index, int paletteIndex)
{
for (var i = 0; i < _indicesLength; i++)
_data!.Set(index + i, ((paletteIndex >> i) & 0b1) == 0b1);
}

private int GetIndex(int x, int y, int z)
int GetIndex(int x, int y, int z)
=> (x | (y << 4) | (z << 8)) * _indicesLength;


private struct PaletteEntry
struct PaletteEntry
{
public T Value { get; set; }
public int RefCount { get; set; }
Expand Down
Loading

0 comments on commit f7374df

Please sign in to comment.