Skip to content

Commit d8a417d

Browse files
[Physics] Add Physics.TryGetBody3D;
[Tooling] Add FloatConverter;
1 parent 6e95f53 commit d8a417d

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

Engine/Core/Physics/Physics.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,25 @@ public static bool RayCast3D(Ray ray, out IBody3D body, out float fraction, Laye
4242
}
4343

4444
/// <summary>
45-
/// Gets the 3D body that belongs to an entity, if any
45+
/// Gets the 3D body that belongs to an entity
4646
/// </summary>
4747
/// <param name="entity">The entity to check</param>
4848
/// <returns>The body, or null</returns>
4949
public static IBody3D GetBody3D(Entity entity)
5050
{
5151
return Physics3D.Instance?.GetBody(entity);
5252
}
53+
54+
/// <summary>
55+
/// Attempts to get a 3D body that belong to an entity
56+
/// </summary>
57+
/// <param name="entity">The entity to check</param>
58+
/// <param name="body">The body</param>
59+
/// <returns>Whether the body was found</returns>
60+
public static bool TryGetBody3D(Entity entity, out IBody3D body)
61+
{
62+
body = Physics3D.Instance?.GetBody(entity);
63+
64+
return body != null;
65+
}
5366
}

Engine/Tooling/FloatConverter.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Globalization;
4+
5+
namespace Staple.Tooling;
6+
7+
internal class FloatConverter : JsonConverter
8+
{
9+
public override bool CanConvert(Type objectType)
10+
{
11+
return objectType == typeof(float) ||
12+
objectType == typeof(double);
13+
}
14+
15+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
16+
{
17+
if (objectType == typeof(float))
18+
{
19+
return reader.ReadAsDecimal();
20+
}
21+
else if (objectType == typeof(double))
22+
{
23+
return reader.ReadAsDouble();
24+
}
25+
26+
return null;
27+
}
28+
29+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
30+
{
31+
if(value is float f)
32+
{
33+
writer.WriteRawValue(f.ToString("0.0###", CultureInfo.InvariantCulture));
34+
}
35+
else if(value is double d)
36+
{
37+
writer.WriteRawValue(d.ToString("0.0###", CultureInfo.InvariantCulture));
38+
}
39+
}
40+
}

Engine/Tooling/StapleTooling.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<WarningLevel>4</WarningLevel>
2626
</PropertyGroup>
2727
<ItemGroup>
28+
<Compile Include="FloatConverter.cs" />
2829
<Compile Include="IgnorableSerializerContractResolver.cs" />
2930
<Compile Include="ShaderParser.cs" />
3031
<Compile Include="Utilities.cs" />

Engine/Tooling/Utilities.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static bool TryGetShaderUniformType(string name, out ShaderUniformType ty
101101
Converters =
102102
{
103103
new StringEnumConverter(),
104+
new FloatConverter(),
104105
},
105106
ContractResolver = JsonIgnorableResolver.Value,
106107
};

0 commit comments

Comments
 (0)