using System; using Svelto.ECS; using RobocraftX.Common; using Unity.Mathematics; using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Utility; 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(); /// /// 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. /// /// The block's type /// The block's color /// The block color's darkness (0-9) - 0 is default color /// The block's position in the grid - default block size is 0.2 /// The block's rotation in degrees /// The block's uniform scale - default scale is 1 (with 0.2 width) /// The block's non-uniform scale - 0 means is used /// The player who placed the block /// The placed block or null if failed 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; } /// /// Returns the most recently placed block. /// /// The block object 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; } /// /// The block's current position. /// public float3 Position { get => MovementEngine.GetPosition(Id.entityID); set => MovementEngine.MoveBlock(Id.entityID, value); } /// /// The block's current rotation in degrees. /// public float3 Rotation { get => RotationEngine.GetRotation(Id.entityID); set => RotationEngine.RotateBlock(Id.entityID, value); } /// /// The block's type (ID). Changing from or to a functional part may crash the game. /// public BlockIDs Type { get => (BlockIDs) BlockEngine.GetBlockInfo(Id).DBID; set { BlockEngine.GetBlockInfo(Id).DBID = (uint) value; uint prefabId = PrefabsID.GetPrefabId((uint) value, 0); BlockEngine.GetBlockInfo(Id).prefabID = prefabId; BlockEngine.GetBlockInfo(Id) = new PhysicsPrefabEntityStruct(prefabId); } } public BlockColors Color { get => (BlockColors) (BlockEngine.GetBlockInfo(Id).indexInPalette % 10); set { ref var color = ref BlockEngine.GetBlockInfo(Id); color.indexInPalette = (byte) (color.indexInPalette / 10 * 10 + value); color.needsUpdate = true; } } public byte ColorDarkness { get => (byte) (BlockEngine.GetBlockInfo(Id).indexInPalette / 10); set { ref var color = ref BlockEngine.GetBlockInfo(Id); color.indexInPalette = (byte) (10 * (byte) value + color.indexInPalette % 10); color.needsUpdate = true; } } /// /// Returns an array of blocks that are connected to this one. /// public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id.entityID); /// /// Removes this block. /// /// True if the block exists and could be removed. public bool Remove() => RemovalEngine.RemoveBlock(Id); public override string ToString() { return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}"; } public static void Init() { GameEngineManager.AddGameEngine(PlacementEngine); GameEngineManager.AddGameEngine(MovementEngine); GameEngineManager.AddGameEngine(RotationEngine); GameEngineManager.AddGameEngine(RemovalEngine); GameEngineManager.AddGameEngine(BlockEngine); } } }