TechbloxModdingAPI/GamecraftModdingAPI/Block.cs

115 lines
4.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Utility;
using RobocraftX.Common;
using Svelto.ECS;
using Unity.Mathematics;
namespace GamecraftModdingAPI
{
public class Block
{
private static readonly PlacementEngine PlacementEngine = new PlacementEngine();
private static readonly MovementEngine MovementEngine = new MovementEngine();
private static readonly RotationEngine RotationEngine = new RotationEngine();
private static readonly RemovalEngine RemovalEngine = new RemovalEngine();
private static readonly BlockEngine BlockEngine = new BlockEngine();
/// <summary>
/// Place a new block at the given position. If scaled, position means the center of the block. The default block size is 0.2 in terms of position.
/// Place blocks next to each other to connect them.
/// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
/// </summary>
/// <param name="block">The block's type</param>
/// <param name="color">The block's color</param>
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
/// <param name="position">The block's position in the grid - default block size is 0.2</param>
/// <param name="rotation">The block's rotation in degrees</param>
/// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
/// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
/// <param name="player">The player who placed the block</param>
/// <returns>The placed block or null if failed</returns>
public static Block PlaceNew(BlockIDs block, float3 position,
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
int uscale = 1, float3 scale = default, Player player = null)
{
if (PlacementEngine.IsInGame && GameState.IsBuildMode())
{
try
{
return new Block(PlacementEngine.PlaceBlock(block, color, darkness,
position, uscale, scale, player, rotation));
}
catch (Exception e)
{
Logging.MetaDebugLog(e);
}
}
return null;
}
/// <summary>
/// Returns the most recently placed block.
/// </summary>
/// <returns>The block object</returns>
public static Block GetLastPlacedBlock()
{
return new Block(BlockIdentifiers.LatestBlockID);
}
public Block(EGID id)
{
Id = id;
}
public Block(uint id)
{
Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
}
public EGID Id { get; }
/// <summary>
/// The block's current position.
/// </summary>
public float3 Position
{
get => MovementEngine.GetPosition(Id.entityID);
set => MovementEngine.MoveBlock(Id.entityID, value);
}
/// <summary>
/// Returns an array of blocks that are connected to this one.
/// </summary>
public Block[] ConnectedCubes => BlockEngine.GetConnectedBlocks(Id.entityID);
/// <summary>
/// The block's current rotation in degrees.
/// </summary>
public float3 Rotation
{
get => RotationEngine.GetRotation(Id.entityID);
set => RotationEngine.RotateBlock(Id.entityID, value);
}
/// <summary>
/// Removes this block.
/// </summary>
/// <returns>True if the block exists and could be removed.</returns>
public bool Remove()
{
return RemovalEngine.RemoveBlock(Id);
}
public static void Init()
{
GameEngineManager.AddGameEngine(PlacementEngine);
GameEngineManager.AddGameEngine(MovementEngine);
GameEngineManager.AddGameEngine(RotationEngine);
GameEngineManager.AddGameEngine(RemovalEngine);
GameEngineManager.AddGameEngine(BlockEngine);
}
}
}