Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Simple velocity setup + general code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Dec 11, 2023
1 parent a58e47e commit 30685e8
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Client/Rendering/World/ChunkMeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private void WorkLoop() {
var checkBlock = chunkStorages[checkTuple.Item1][checkTuple.Item2];

//Mark if any side of this block is visible.
isVisible |= !checkBlock.IsSolidBlock;
isVisible |= !checkBlock.IsAir;
neighbors[n] = checkBlock;
}

Expand Down
16 changes: 7 additions & 9 deletions Client/World/Entity/ControlledClientPlayerEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ public override void Tick() {

var movement3d = new dvec3(movement.x, 0, movement.y);

movement3d += new dvec3(0, 1, 0) * Keybinds.Jump.strength;
movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength;
// movement3d += new dvec3(0, 1, 0) * Keybinds.Jump.strength;
// movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength;

rotation += new dvec2((float)(looking.y * Constants.SecondsPerTick) * 1, (float)(looking.x * Constants.SecondsPerTick) * 1);

movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d;

if (Keybinds.Jump.strength < 1)
movement3d += new dvec3(0, -1, 0);
movement3d *= Constants.SecondsPerTick * 4;

MoveAndSlide(movement3d);
//position += movement3d;

velocity = dvec3.Lerp(velocity, movement3d * 4, 0.5);
velocity += new dvec3(0, -1, 0);
if (Keybinds.Jump.justPressed)
velocity += new dvec3(0, 5, 0);

var transformUpdate = PacketPool.GetPacket<PlayerUpdated>();
transformUpdate.Position = position;
Expand Down
60 changes: 37 additions & 23 deletions Common/Collision/AABB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,64 @@ public AABB(dvec3 a, dvec3 b) {
max = dvec3.Max(a, b);
}

public AABB Encapsulate(dvec3 point) => new() {
min = dvec3.Min(min, point), max = dvec3.Max(max, point)
};

public AABB Encapsulate(AABB other) => new() {
min = dvec3.Min(min, other.min), max = dvec3.Max(max, other.max)
};

public AABB Translated(dvec3 vec) => new() {
min = min + vec, max = max + vec
};

public AABB Expanded(dvec3 size) => new() {
min = min - size * 0.5, max = max + size * 0.5,
};

public AABB Expanded(AABB box) => Expanded(box.size);
public AABB Encapsulate(dvec3 point)
=> new() {
min = dvec3.Min(min, point),
max = dvec3.Max(max, point)
};

public AABB Encapsulate(AABB other)
=> new() {
min = dvec3.Min(min, other.min),
max = dvec3.Max(max, other.max)
};

public AABB Translated(dvec3 vec)
=> new() {
min = min + vec,
max = max + vec
};

public AABB Expanded(dvec3 size)
=> new() {
min = min - size * 0.5,
max = max + size * 0.5,
};

public AABB Expanded(AABB box)
=> Expanded(box.size);

public AABB Expanded(double size) => new() {
min = min - size * 0.5, max = max + size * 0.5,
};
public AABB Expanded(double size)
=> new() {
min = min - size * 0.5,
max = max + size * 0.5,
};

/// <summary>
/// Checks if two AABBs intersect.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Intersects(AABB other) => (min < other.max & max > other.min).All;
public bool Intersects(AABB other)
=> (min < other.max & max > other.min).All;


/// <summary>
/// Checks if a point is inside the AABB.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public bool Contains(dvec3 point) => (point < min & point > max).All;
public bool Contains(dvec3 point)
=> (point < min & point > max).All;


/// <summary>
/// Returns the closest point inside the AABB.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public dvec3 ClosestPointInside(dvec3 point) => dvec3.Clamp(point, min, max);
public dvec3 ClosestPointInside(dvec3 point)
=> dvec3.Clamp(point, min, max);


/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Common/Collision/PhysicsSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static dvec3 MoveAndSlide(AABB boundingBox, dvec3 movement, ColliderProvi
}

if (!ColliderCache.TryDequeue(out var sortedList))
sortedList = new List<CollidedAABB>();
sortedList = new();

var moveLength = movement.Length;

Expand Down
6 changes: 4 additions & 2 deletions Common/Collision/Ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public Ray(dvec3 pos, dvec3 dir) {
inverseDirection = 1 / direction;
}

public dvec3 GetPoint(float t) => position + direction * t;
public dvec3 GetPoint(double t) => position + direction * t;
public dvec3 GetPoint(float t)
=> position + direction * t;
public dvec3 GetPoint(double t)
=> position + direction * t;
}
3 changes: 2 additions & 1 deletion Common/Network/Packets/PacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class PacketHandler<T> where T : Packet {

private readonly Dictionary<Type, Action<T>> Handlers = new();

public void RegisterHandler<T2>(Action<T2> handler) where T2 : T => Handlers[typeof(T2)] = packet => handler((T2)packet);
public void RegisterHandler<T2>(Action<T2> handler) where T2 : T
=> Handlers[typeof(T2)] = packet => handler((T2)packet);

public bool HandlePacket(T packet) {
var type = packet.GetType();
Expand Down
6 changes: 3 additions & 3 deletions Common/Network/Packets/Utils/PacketPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static T GetPacket<T>() where T : Packet {
var baseType = typeof(T);

if (!Pools.TryGetValue(baseType, out var pool))
Pools[baseType] = pool = new ConcurrentQueue<Packet>();
Pools[baseType] = pool = new();

if (!pool.TryDequeue(out var packet))
packet = Activator.CreateInstance<T>();
Expand All @@ -24,7 +24,7 @@ public static T GetPacket<T>(Type targetType) where T : Packet {
throw new InvalidOperationException($"Cannot cast type {targetType} to {baseType}");

if (!Pools.TryGetValue(targetType, out var pool))
Pools[targetType] = pool = new ConcurrentQueue<Packet>();
Pools[targetType] = pool = new();

if (!pool.TryDequeue(out var packet))
packet = (Packet)Activator.CreateInstance(targetType);

Check warning on line 30 in Common/Network/Packets/Utils/PacketPool.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
Expand All @@ -35,7 +35,7 @@ public static T GetPacket<T>(Type targetType) where T : Packet {
public static void Return(Packet toReturn) {
var type = toReturn.GetType();
if (!Pools.TryGetValue(type, out var pool))
Pools[type] = pool = new ConcurrentQueue<Packet>();
Pools[type] = pool = new();

toReturn.OnReturnToPool();
pool.Enqueue(toReturn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ private void OnPlayerUpdated(PlayerUpdated pkt) {
//Console.WriteLine(entity.position + "|" + entity.chunkPosition);
}

public void Close() => Connection.Close();
public void Close()
=> Connection.Close();


public void SendPacket(S2CPacket packet) {
Connection.DeliverPacket(packet);
PacketPool.Return(packet);
}

public void SetPlayerEntity(PlayerEntity e) {
this.entity = e;
}
public void SetPlayerEntity(PlayerEntity e)
=> entity = e;

public void SetupViewArea(int range) {
if (entity == null)
Expand Down
2 changes: 1 addition & 1 deletion Common/Server/Components/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void OnConnectionMade(ServerConnectionContext context) {

context.SetPlayerEntity(pEntity);

Server.WorldManager.DefaultWorld.AddEntity(pEntity, dvec3.Zero, dvec2.Zero);
Server.WorldManager.DefaultWorld.AddEntity(pEntity, new(0, 100, 0), dvec2.Zero);

Console.WriteLine("Server:Sending player to world...");
context.SendPacket(new SetupWorld());
Expand Down
16 changes: 8 additions & 8 deletions Common/Tile/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ namespace Voxel.Common.Tile;
public class Block {
public readonly string Name;
public readonly BlockSettings Settings;
public bool IsAir => Settings.IsAir;

public uint id;

public bool IsSolidBlock => Settings.IsSolidBlock;

public Block(string name, BlockSettings settings) {
Name = name;
Expand All @@ -24,23 +24,23 @@ public Block(string name) : this(name, BlockSettings.Default) {}
public class BlockSettings {
public static readonly BlockSettings Default = new Builder().Build();

public readonly bool IsSolidBlock;
public float GetSolidityFloat => IsSolidBlock ? 1 : 0;
public readonly bool IsAir;
public float GetSolidityFloat => IsAir ? 1 : 0;

private BlockSettings(bool isSolidBlock) {
IsSolidBlock = isSolidBlock;
private BlockSettings(bool isAir) {
IsAir = isAir;
}

public class Builder {
public bool IsSolidBlock = true;
public bool IsAir = true;
public Builder() {}

public Builder(BlockSettings settings) {
IsSolidBlock = settings.IsSolidBlock;
IsAir = settings.IsAir;
}

public Builder(Block block) : this(block.Settings) {}

public BlockSettings Build() => new(IsSolidBlock);
public BlockSettings Build() => new(IsAir);
}
}
2 changes: 1 addition & 1 deletion Common/Tile/Blocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class Blocks {
private static readonly Dictionary<string, Block> BlocksByName = new();

public static readonly Block Air = new("air", new BlockSettings.Builder {
IsSolidBlock = false
IsAir = false
});
public static readonly Block Stone = new("stone");
public static readonly Block Dirt = new("dirt");
Expand Down
66 changes: 42 additions & 24 deletions Common/Util/Serialization/VDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,34 @@ private Span<byte> GetBytes(int length) {
return span;
}

public byte ReadByte() => GetBytes(1)[0];

public ushort ReadUShort() => BitConverter.ToUInt16(GetBytes(sizeof(ushort)));
public short ReadShort() => BitConverter.ToInt16(GetBytes(sizeof(short)));

public uint ReadUint() => BitConverter.ToUInt32(GetBytes(sizeof(uint)));
public int ReadInt() => BitConverter.ToInt32(GetBytes(sizeof(int)));

public ulong ReadULong() => BitConverter.ToUInt64(GetBytes(sizeof(ulong)));
public long ReadLong() => BitConverter.ToInt64(GetBytes(sizeof(long)));

public float ReadFloat() => BitConverter.ToSingle(GetBytes(sizeof(float)));
public double ReadDouble() => BitConverter.ToDouble(GetBytes(sizeof(double)));

public Guid ReadGuid() => new Guid(GetBytes(16));

public string ReadString() => ReadString(Encoding.UTF8);
public byte ReadByte()
=> GetBytes(1)[0];

public ushort ReadUShort()
=> BitConverter.ToUInt16(GetBytes(sizeof(ushort)));
public short ReadShort()
=> BitConverter.ToInt16(GetBytes(sizeof(short)));

public uint ReadUint()
=> BitConverter.ToUInt32(GetBytes(sizeof(uint)));
public int ReadInt()
=> BitConverter.ToInt32(GetBytes(sizeof(int)));

public ulong ReadULong()
=> BitConverter.ToUInt64(GetBytes(sizeof(ulong)));
public long ReadLong()
=> BitConverter.ToInt64(GetBytes(sizeof(long)));

public float ReadFloat()
=> BitConverter.ToSingle(GetBytes(sizeof(float)));
public double ReadDouble()
=> BitConverter.ToDouble(GetBytes(sizeof(double)));

public Guid ReadGuid()
=> new(GetBytes(16));

public string ReadString()
=> ReadString(Encoding.UTF8);
public string ReadString(Encoding encoding) {
var byteCount = ReadInt();
return encoding.GetString(GetBytes(byteCount));
Expand All @@ -89,16 +100,23 @@ public byte[] ReadByteArray() {
return GetBytes(count).ToArray();
}

public void ReadBytes(Span<byte> bytes) => GetBytes(bytes.Length).CopyTo(bytes);
public void ReadBytes(Span<byte> bytes)
=> GetBytes(bytes.Length).CopyTo(bytes);

public vec3 ReadVec3() => new(ReadFloat(), ReadFloat(), ReadFloat());
public vec3 ReadVec3()
=> new(ReadFloat(), ReadFloat(), ReadFloat());

public ivec3 ReadIVec3() => new(ReadInt(), ReadInt(), ReadInt());
public ivec3 ReadIVec3()
=> new(ReadInt(), ReadInt(), ReadInt());

public dvec3 ReadDVec3() => new(ReadDouble(), ReadDouble(), ReadDouble());
public dvec2 ReadDVec2() => new(ReadDouble(), ReadDouble());
public dvec3 ReadDVec3()
=> new(ReadDouble(), ReadDouble(), ReadDouble());
public dvec2 ReadDVec2()
=> new(ReadDouble(), ReadDouble());

public lvec3 ReadLVec3() => new(ReadLong(), ReadLong(), ReadLong());
public lvec3 ReadLVec3()
=> new(ReadLong(), ReadLong(), ReadLong());

public void ReadSerializable(VSerializable serializable) => serializable.Read(this);
public void ReadSerializable(VSerializable serializable)
=> serializable.Read(this);
}
Loading

0 comments on commit 30685e8

Please sign in to comment.