using System; using System.Reflection; using Svelto.ECS; using Svelto.ECS.EntityStructs; using RobocraftX.Common; using RobocraftX.Blocks; using Unity.Mathematics; using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI { /// /// A single (perhaps scaled) block. Properties may return default values if the block is removed and then setting them is ignored. /// For specific block type operations, use the specialised block classes in the GamecraftModdingAPI.Blocks namespace. /// public class Block { protected static readonly PlacementEngine PlacementEngine = new PlacementEngine(); protected static readonly MovementEngine MovementEngine = new MovementEngine(); protected static readonly RotationEngine RotationEngine = new RotationEngine(); protected static readonly RemovalEngine RemovalEngine = new RemovalEngine(); protected static readonly BlockEngine BlockEngine = new BlockEngine(); protected static readonly SignalEngine SignalEngine = new SignalEngine(); /// /// 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; if (!BlockEngine.BlockExists(Id)) { Sync(); if (!BlockEngine.BlockExists(Id)) { throw new BlockDoesNotExistException($"Block {Id.entityID} must be placed using PlaceNew(...) since it does not exist yet"); } } } public Block(uint id) { Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP); if (!BlockEngine.BlockExists(Id)) { Sync(); if (!BlockEngine.BlockExists(Id)) { throw new BlockDoesNotExistException($"Block {Id.entityID} must be placed using PlaceNew(...) since it does not exist yet"); } } } /// /// Synchronize newly created entity components with entities DB. /// This forces a partial game tick, so it may be slow. /// This also has the potential to make Gamecraft unstable. /// Use this sparingly. /// protected static void Sync() { DeterministicStepCompositionRootPatch.SubmitEntitiesNow(); } public EGID Id { get; protected set; } /// /// The block's current position or zero if the block no longer exists. /// A block is 0.2 wide by default in terms of position. /// public float3 Position { get => Exists ? MovementEngine.GetPosition(Id.entityID) : float3.zero; set { if (Exists) MovementEngine.MoveBlock(Id.entityID, value); } } /// /// The block's current rotation in degrees or zero if the block doesn't exist. /// public float3 Rotation { get => Exists ? RotationEngine.GetRotation(Id.entityID) : float3.zero; set { if (Exists) RotationEngine.RotateBlock(Id.entityID, value); } } /// /// The block's non-uniform scale or zero if the block's invalid. Independent of the uniform scaling. /// public float3 Scale { get => BlockEngine.GetBlockInfo(Id).scale; set { BlockEngine.GetBlockInfo(Id).scale = value; } } /// /// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale. /// public int UniformScale { get => BlockEngine.GetBlockInfo(Id).scaleFactor; set { ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id); scaleStruct.scaleFactor = value; Scale = new float3(value, value, value); } } /// /// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore. /// public BlockIDs Type { get { var id = (BlockIDs) BlockEngine.GetBlockInfo(Id, out var exists).DBID; return exists ? id : BlockIDs.Invalid; } } /// /// The block's color. Returns BlockColors.Default if the block no longer exists. /// public BlockColor Color { get { byte index = BlockEngine.GetBlockInfo(Id, out var exists).indexInPalette; if (!exists) index = byte.MaxValue; if (index == byte.MaxValue) return new BlockColor { Color = BlockColors.Default }; return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) }; } set { ref var color = ref BlockEngine.GetBlockInfo(Id); color.indexInPalette = (byte)(value.Color + value.Darkness * 10); color.overridePaletteColour = false; color.needsUpdate = true; BlockEngine.SetBlockColorFromPalette(ref color); } } /// /// The block's exact color. Gets reset to the palette color (Color property) after reentering the game. /// public float4 CustomColor { get => BlockEngine.GetBlockInfo(Id).overriddenColour; set { ref var color = ref BlockEngine.GetBlockInfo(Id); color.overriddenColour = value; color.overridePaletteColour = true; color.needsUpdate = true; } } /// /// Whether the block exists. The other properties will return a default value if the block doesn't exist. /// public bool Exists => BlockEngine.BlockExists(Id); /// /// Returns an array of blocks that are connected to this one. Returns an empty array if the block doesn't exist. /// public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id); /// /// 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); } /// /// Convert the block to a specialised block class. /// /// The block. /// The specialised block type. public T Specialise() where T : Block { // What have I gotten myself into? // C# can't cast to a child of Block unless the object was originally that child type // And C# doesn't let me make implicit cast operators for child types // So thanks to Microsoft, we've got this horrible implementation using reflection ConstructorInfo ctor = typeof(T).GetConstructor(types: new System.Type[] { typeof(EGID) }); if (ctor == null) { throw new BlockSpecializationException("Specialized block constructor does not accept an EGID"); } return (T)ctor.Invoke(new object[] { Id }); } #if DEBUG public static EntitiesDB entitiesDB { get { return BlockEngine.GetEntitiesDB(); } } #endif } }