Block fixes, add mass and complexity properties, make Player.LocalPlayer return null if not found

This commit is contained in:
Norbi Peti 2022-02-19 02:25:58 +01:00
parent c6dae688fe
commit 7f63944a6e
5 changed files with 53 additions and 6 deletions

View file

@ -67,7 +67,7 @@ namespace TechbloxModdingAPI
/// <returns>The block object or null if doesn't exist</returns> /// <returns>The block object or null if doesn't exist</returns>
public static Block GetLastPlacedBlock() public static Block GetLastPlacedBlock()
{ {
uint lastBlockID = (uint) AccessTools.Field(typeof(CommonExclusiveGroups), "_nextBlockEntityID").GetValue(null) - 1; uint lastBlockID = CommonExclusiveGroups.blockIDGeneratorClient.Peek() - 1;
EGID? egid = BlockEngine.FindBlockEGID(lastBlockID); EGID? egid = BlockEngine.FindBlockEGID(lastBlockID);
return egid.HasValue ? New(egid.Value) : null; return egid.HasValue ? New(egid.Value) : null;
} }
@ -366,8 +366,12 @@ namespace TechbloxModdingAPI
get get
{ {
if (blockGroup != null) return blockGroup; if (blockGroup != null) return blockGroup;
if (!GameState.IsBuildMode()) return null; // Breaks in simulation
var bgec = BlockEngine.GetBlockInfo<BlockGroupEntityComponent>(this); var bgec = BlockEngine.GetBlockInfo<BlockGroupEntityComponent>(this);
return blockGroup = bgec.currentBlockGroup == -1 ? null : new BlockGroup(bgec.currentBlockGroup, this); return blockGroup = bgec.currentBlockGroup == -1
? null
: GetInstance(new EGID((uint)bgec.currentBlockGroup, BlockGroupExclusiveGroups.BlockGroupEntityGroup),
egid => new BlockGroup((int)egid.entityID, this));
} }
set set
{ {
@ -395,6 +399,23 @@ namespace TechbloxModdingAPI
set => BlockEngine.GetBlockInfo<BlockStaticComponent>(this).isStatic = value; set => BlockEngine.GetBlockInfo<BlockStaticComponent>(this).isStatic = value;
} }
/// <summary>
/// The mass of the block.
/// </summary>
public float Mass
{
get => BlockEngine.GetBlockInfo<MassStruct>(this).mass;
}
/// <summary>
/// Block complexity used for build rules. Determines the 'cost' of the block.
/// </summary>
public BlockComplexity Complexity
{
get => new(BlockEngine.GetBlockInfo<BlockComplexityComponent>(this));
set => BlockEngine.GetBlockInfo<BlockComplexityComponent>(this) = value;
}
/// <summary> /// <summary>
/// Whether the block exists. The other properties will return a default value if the block doesn't exist. /// Whether the block exists. The other properties will return a default value if the block doesn't exist.
/// If the block was just placed, then this will also return false but the properties will work correctly. /// If the block was just placed, then this will also return false but the properties will work correctly.

View file

@ -0,0 +1,17 @@
using RobocraftX.Blocks;
namespace TechbloxModdingAPI.Blocks
{
public record BlockComplexity(int Cpu, int Power)
{
public BlockComplexity(BlockComplexityComponent component) : this(component.cpu, component.power)
{
}
public int Cpu { get; } = Cpu;
public int Power { get; } = Power;
public static implicit operator BlockComplexityComponent(BlockComplexity complexity) =>
new() { cpu = complexity.Cpu, power = complexity.Power };
}
}

View file

@ -1,6 +1,7 @@
using Gamecraft.Damage; using Gamecraft.Damage;
using RobocraftX.Common; using RobocraftX.Common;
using Svelto.ECS; using Svelto.ECS;
using Techblox.Physics;
namespace TechbloxModdingAPI namespace TechbloxModdingAPI
{ {
@ -36,6 +37,11 @@ namespace TechbloxModdingAPI
set => Block.BlockEngine.GetBlockInfo<HealthEntityComponent>(this).healthMultiplier = value; set => Block.BlockEngine.GetBlockInfo<HealthEntityComponent>(this).healthMultiplier = value;
} }
/// <summary>
/// The mass of the cluster.
/// </summary>
public float Mass => Block.BlockEngine.GetBlockInfo<ClusterMassComponent>(this).mass;
/// <summary> /// <summary>
/// Returns the simulation-time rigid bodies for the chunks in this cluster. /// Returns the simulation-time rigid bodies for the chunks in this cluster.
/// </summary> /// </summary>

View file

@ -66,13 +66,16 @@ namespace TechbloxModdingAPI
/// <summary> /// <summary>
/// Returns the current player belonging to this client. It will be different after entering/leaving simulation. /// Returns the current player belonging to this client. It will be different after entering/leaving simulation.
/// May return null if the local player doesn't exist.
/// </summary> /// </summary>
public static Player LocalPlayer public static Player LocalPlayer
{ {
get get
{ {
if (localPlayer == null || localPlayer.Id != playerEngine.GetLocalPlayer()) var playerId = playerEngine.GetLocalPlayer();
localPlayer = GetInstance(playerEngine.GetLocalPlayer()); if (playerId == uint.MaxValue) return null;
if (localPlayer == null || localPlayer.Id != playerId)
localPlayer = GetInstance(playerId);
return localPlayer; return localPlayer;
} }
} }

View file

@ -75,13 +75,13 @@ namespace TechbloxModdingAPI
public float Mass public float Mass
{ {
get => math.rcp(GetStruct().physicsMass.InverseMass); get => 0f; //TODO: Get mass from UECS entity
//set => GetStruct().physicsMass.InverseMass = math.rcp(value); //set => GetStruct().physicsMass.InverseMass = math.rcp(value);
} }
public float3 CenterOfMass public float3 CenterOfMass
{ {
get => GetStruct().physicsMass.CenterOfMass; get => Block.BlockEngine.GetBlockInfo<MassEntityStruct>(this).centreOfMass;
//set => GetStruct().physicsMass.CenterOfMass = value; //set => GetStruct().physicsMass.CenterOfMass = value;
} }