2020-05-13 12:02:36 +00:00
using System ;
2020-07-13 19:55:48 +00:00
using System.Collections.Generic ;
using System.Linq ;
using System.Reflection.Emit ;
2020-05-13 14:52:21 +00:00
2020-11-10 15:37:20 +00:00
using Gamecraft.Blocks.BlockGroups ;
2020-05-13 12:02:36 +00:00
using Svelto.ECS ;
2020-05-17 21:23:06 +00:00
using Svelto.ECS.EntityStructs ;
2020-05-13 14:52:21 +00:00
using RobocraftX.Common ;
2020-05-19 22:08:02 +00:00
using RobocraftX.Blocks ;
2020-05-13 12:02:36 +00:00
using Unity.Mathematics ;
2020-05-24 17:29:02 +00:00
using Gamecraft.Blocks.GUI ;
2020-05-13 12:02:36 +00:00
2020-05-13 14:52:21 +00:00
using GamecraftModdingAPI.Blocks ;
using GamecraftModdingAPI.Utility ;
2021-04-19 01:13:00 +00:00
using RobocraftX.Rendering.GPUI ;
2020-05-13 14:52:21 +00:00
2020-05-13 12:02:36 +00:00
namespace GamecraftModdingAPI
{
2020-05-18 03:19:16 +00:00
/// <summary>
/// A single (perhaps scaled) block. Properties may return default values if the block is removed and then setting them is ignored.
2020-05-21 00:42:02 +00:00
/// For specific block type operations, use the specialised block classes in the GamecraftModdingAPI.Blocks namespace.
2020-05-18 03:19:16 +00:00
/// </summary>
2020-06-04 22:20:35 +00:00
public class Block : IEquatable < Block > , IEquatable < EGID >
2020-05-18 03:19:16 +00:00
{
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 ( ) ;
2020-05-21 19:04:55 +00:00
protected static readonly SignalEngine SignalEngine = new SignalEngine ( ) ;
2020-06-02 01:12:40 +00:00
protected static readonly BlockEventsEngine BlockEventsEngine = new BlockEventsEngine ( ) ;
2020-06-03 23:42:13 +00:00
protected static readonly ScalingEngine ScalingEngine = new ScalingEngine ( ) ;
2020-11-13 20:35:53 +00:00
protected static readonly BlockCloneEngine BlockCloneEngine = new BlockCloneEngine ( ) ;
2020-07-21 00:36:11 +00:00
2020-05-22 22:06:49 +00:00
protected internal static readonly BlockEngine BlockEngine = new BlockEngine ( ) ;
2020-05-18 03:19:16 +00:00
/// <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>
2021-04-15 23:40:30 +00:00
/// <param name="material">The block's material</param>
2020-09-28 01:10:59 +00:00
/// <param name="position">The block's position - default block size is 0.2</param>
2020-05-18 03:19:16 +00:00
/// <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>
2021-04-19 01:13:00 +00:00
/// <param name="isFlipped">Whether the block should be flipped</param>
/// <param name="autoWire">Whether the block should be auto-wired (if functional)</param>
2020-05-18 03:19:16 +00:00
/// <param name="player">The player who placed the block</param>
/// <returns>The placed block or null if failed</returns>
2021-04-19 17:32:14 +00:00
public static Block PlaceNew ( BlockIDs block , float3 position , bool autoWire = false , Player player = null )
2020-05-18 03:19:16 +00:00
{
2021-04-19 17:32:14 +00:00
return PlaceNew < Block > ( block , position , autoWire , player ) ;
2020-05-18 03:19:16 +00:00
}
2020-05-22 01:00:33 +00:00
/// <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>
2021-04-15 23:40:30 +00:00
/// <param name="material">The block's materialr</param>
2020-09-28 01:10:59 +00:00
/// <param name="position">The block's position - default block size is 0.2</param>
2020-05-22 01:00:33 +00:00
/// <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>
2021-04-19 01:13:00 +00:00
/// <param name="isFlipped">Whether the block should be flipped</param>
/// <param name="autoWire">Whether the block should be auto-wired (if functional)</param>
2020-05-22 01:00:33 +00:00
/// <param name="player">The player who placed the block</param>
/// <returns>The placed block or null if failed</returns>
2021-04-19 17:32:14 +00:00
public static T PlaceNew < T > ( BlockIDs block , float3 position , bool autoWire = false , Player player = null )
2021-04-19 01:13:00 +00:00
where T : Block
2020-05-22 01:00:33 +00:00
{
if ( PlacementEngine . IsInGame & & GameState . IsBuildMode ( ) )
{
2021-04-19 17:32:14 +00:00
var initializer = PlacementEngine . PlaceBlock ( block , position , player , autoWire ) ;
var egid = initializer . EGID ;
2020-07-15 19:58:24 +00:00
var bl = New < T > ( egid . entityID , egid . groupID ) ;
bl . InitData . Group = BlockEngine . InitGroup ( initializer ) ;
2021-04-30 20:36:54 +00:00
bl . InitData . Reference = initializer . reference ;
2020-07-18 23:42:32 +00:00
Placed + = bl . OnPlacedInit ;
2020-07-15 19:58:24 +00:00
return bl ;
2020-05-22 01:00:33 +00:00
}
return null ;
}
2020-05-18 03:19:16 +00:00
/// <summary>
/// Returns the most recently placed block.
/// </summary>
/// <returns>The block object</returns>
public static Block GetLastPlacedBlock ( )
{
2020-07-13 19:55:48 +00:00
return New < Block > ( BlockIdentifiers . LatestBlockID ) ;
2020-05-18 03:19:16 +00:00
}
2020-06-02 01:12:40 +00:00
/// <summary>
/// An event that fires each time a block is placed.
/// </summary>
public static event EventHandler < BlockPlacedRemovedEventArgs > Placed
{
add = > BlockEventsEngine . Placed + = value ;
remove = > BlockEventsEngine . Placed - = value ;
}
/// <summary>
/// An event that fires each time a block is removed.
/// </summary>
public static event EventHandler < BlockPlacedRemovedEventArgs > Removed
{
add = > BlockEventsEngine . Removed + = value ;
remove = > BlockEventsEngine . Removed - = value ;
}
2020-07-13 19:55:48 +00:00
private static Dictionary < Type , Func < EGID , Block > > initializers = new Dictionary < Type , Func < EGID , Block > > ( ) ;
2020-12-11 16:11:24 +00:00
private static Dictionary < Type , ExclusiveBuildGroup [ ] > typeToGroup =
new Dictionary < Type , ExclusiveBuildGroup [ ] >
2020-07-13 19:55:48 +00:00
{
2020-11-09 21:18:25 +00:00
{ typeof ( LogicGate ) , new [ ] { CommonExclusiveGroups . LOGIC_BLOCK_GROUP } } ,
{ typeof ( Motor ) , new [ ] { CommonExclusiveGroups . MOTOR_BLOCK_GROUP } } ,
{ typeof ( MusicBlock ) , new [ ] { CommonExclusiveGroups . MUSIC_BLOCK_GROUP } } ,
{ typeof ( ObjectIdentifier ) , new [ ] { CommonExclusiveGroups . OBJID_BLOCK_GROUP } } ,
{ typeof ( Piston ) , new [ ] { CommonExclusiveGroups . PISTON_BLOCK_GROUP } } ,
{ typeof ( Servo ) , new [ ] { CommonExclusiveGroups . SERVO_BLOCK_GROUP } } ,
2020-07-13 19:55:48 +00:00
{
typeof ( SpawnPoint ) ,
new [ ]
{
2020-11-09 21:18:25 +00:00
CommonExclusiveGroups . SPAWNPOINT_BLOCK_GROUP ,
CommonExclusiveGroups . BUILDINGSPAWN_BLOCK_GROUP
2020-07-13 19:55:48 +00:00
}
} ,
2020-09-23 19:31:54 +00:00
{
typeof ( SfxBlock ) ,
new [ ]
{
2020-11-09 21:18:25 +00:00
CommonExclusiveGroups . SIMPLESFX_BLOCK_GROUP ,
CommonExclusiveGroups . LOOPEDSFX_BLOCK_GROUP
2020-09-23 19:31:54 +00:00
}
} ,
2020-11-09 21:18:25 +00:00
{ typeof ( DampedSpring ) , new [ ] { CommonExclusiveGroups . DAMPEDSPRING_BLOCK_GROUP } } ,
{ typeof ( TextBlock ) , new [ ] { CommonExclusiveGroups . TEXT_BLOCK_GROUP } } ,
{ typeof ( Timer ) , new [ ] { CommonExclusiveGroups . TIMER_BLOCK_GROUP } }
2020-07-13 19:55:48 +00:00
} ;
2020-07-20 22:19:30 +00:00
/// <summary>
/// Constructs a new instance of T with the given ID and group using dynamically created delegates.
/// It's equivalent to new T(EGID) with a minimal overhead thanks to caching the created delegates.
/// </summary>
/// <param name="id">The block ID</param>
/// <param name="group">The block group</param>
/// <typeparam name="T">The block's type or Block itself</typeparam>
/// <returns>An instance of the provided type</returns>
/// <exception cref="BlockTypeException">The block group doesn't match or cannot be found</exception>
/// <exception cref="MissingMethodException">The block class doesn't have the needed constructor</exception>
2020-07-13 19:55:48 +00:00
private static T New < T > ( uint id , ExclusiveGroupStruct ? group = null ) where T : Block
{
var type = typeof ( T ) ;
EGID egid ;
if ( ! group . HasValue )
{
if ( typeToGroup . TryGetValue ( type , out var gr ) & & gr . Length = = 1 )
egid = new EGID ( id , gr [ 0 ] ) ;
else
egid = BlockEngine . FindBlockEGID ( id ) ? ? throw new BlockTypeException ( "Could not find block group!" ) ;
}
else
{
egid = new EGID ( id , group . Value ) ;
if ( typeToGroup . TryGetValue ( type , out var gr )
& & gr . All ( egs = > egs ! = group . Value ) ) //If this subclass has a specific group, then use that - so Block should still work
2020-12-19 23:05:02 +00:00
throw new BlockTypeException ( $"Incompatible block type! Type {type.Name} belongs to group {gr.Select(g => ((uint)g).ToString()).Aggregate((a, b) => a + " , " + b)} instead of {(uint)group.Value}" ) ;
2020-07-13 19:55:48 +00:00
}
if ( initializers . TryGetValue ( type , out var func ) )
{
var bl = ( T ) func ( egid ) ;
return bl ;
}
//https://stackoverflow.com/a/10593806/2703239
var ctor = type . GetConstructor ( new [ ] { typeof ( EGID ) } ) ;
if ( ctor = = null )
throw new MissingMethodException ( "There is no constructor with an EGID parameter for this object" ) ;
DynamicMethod dynamic = new DynamicMethod ( string . Empty ,
type ,
new [ ] { typeof ( EGID ) } ,
type ) ;
ILGenerator il = dynamic . GetILGenerator ( ) ;
2020-09-28 01:10:59 +00:00
//il.DeclareLocal(type);
2020-07-13 19:55:48 +00:00
il . Emit ( OpCodes . Ldarg_0 ) ; //Load EGID and pass to constructor
il . Emit ( OpCodes . Newobj , ctor ) ; //Call constructor
2020-07-20 22:19:30 +00:00
//il.Emit(OpCodes.Stloc_0); - doesn't seem like we need these
//il.Emit(OpCodes.Ldloc_0);
2020-07-13 19:55:48 +00:00
il . Emit ( OpCodes . Ret ) ;
func = ( Func < EGID , T > ) dynamic . CreateDelegate ( typeof ( Func < EGID , T > ) ) ;
initializers . Add ( type , func ) ;
var block = ( T ) func ( egid ) ;
return block ;
}
2020-05-18 03:19:16 +00:00
public Block ( EGID id )
{
Id = id ;
2020-07-21 00:36:11 +00:00
var type = GetType ( ) ;
if ( typeToGroup . TryGetValue ( type , out var groups ) )
{
if ( groups . All ( gr = > gr ! = id . groupID ) )
throw new BlockTypeException ( "The block has the wrong group! The type is " + GetType ( ) +
" while the group is " + id . groupID ) ;
}
2020-12-17 19:20:46 +00:00
else if ( type ! = typeof ( Block ) & & ! typeof ( CustomBlock ) . IsAssignableFrom ( type ) )
2020-07-21 00:36:11 +00:00
Logging . LogWarning ( $"Unknown block type! Add {type} to the dictionary." ) ;
2020-05-18 03:19:16 +00:00
}
2020-07-11 00:26:36 +00:00
/// <summary>
/// This overload searches for the correct group the block is in.
/// It will throw an exception if the block doesn't exist.
/// Use the EGID constructor where possible or subclasses of Block as those specify the group.
/// </summary>
public Block ( uint id )
{
Id = BlockEngine . FindBlockEGID ( id ) ? ? throw new BlockTypeException ( "Could not find the appropriate group for the block. The block probably doesn't exist or hasn't been submitted." ) ;
2020-05-18 03:19:16 +00:00
}
2021-04-19 17:32:14 +00:00
/// <summary>
/// Places a new block in the world.
/// </summary>
/// <param name="type">The block's type</param>
/// <param name="position">The block's position (a block is 0.2 wide in terms of position)</param>
/// <param name="autoWire">Whether the block should be auto-wired (if functional)</param>
/// <param name="player">The player who placed the block</param>
public Block ( BlockIDs type , float3 position , bool autoWire = false , Player player = null )
{
if ( ! PlacementEngine . IsInGame | | ! GameState . IsBuildMode ( ) )
throw new BlockException ( "Blocks can only be placed in build mode." ) ;
var initializer = PlacementEngine . PlaceBlock ( type , position , player , autoWire ) ;
Id = initializer . EGID ;
InitData . Group = BlockEngine . InitGroup ( initializer ) ;
Placed + = OnPlacedInit ;
}
2020-06-04 22:20:35 +00:00
public EGID Id { get ; }
2020-05-18 03:19:16 +00:00
2020-07-15 19:58:24 +00:00
internal BlockEngine . BlockInitData InitData ;
2020-11-13 20:35:53 +00:00
private EGID copiedFrom ;
2020-07-15 19:58:24 +00:00
2020-05-18 03:19:16 +00:00
/// <summary>
/// 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.
/// </summary>
public float3 Position
{
2020-07-15 20:46:48 +00:00
get = > MovementEngine . GetPosition ( Id , InitData ) ;
2020-05-18 03:19:16 +00:00
set
{
2020-07-15 20:46:48 +00:00
MovementEngine . MoveBlock ( Id , InitData , value ) ;
2020-11-12 01:39:58 +00:00
if ( blockGroup ! = null )
blockGroup . PosAndRotCalculated = false ;
2020-11-13 22:59:37 +00:00
BlockEngine . UpdateDisplayedBlock ( Id ) ;
2020-05-18 03:19:16 +00:00
}
}
/// <summary>
/// The block's current rotation in degrees or zero if the block doesn't exist.
/// </summary>
public float3 Rotation
{
2020-07-15 20:46:48 +00:00
get = > RotationEngine . GetRotation ( Id , InitData ) ;
2020-05-18 03:19:16 +00:00
set
{
2020-07-15 20:46:48 +00:00
RotationEngine . RotateBlock ( Id , InitData , value ) ;
2020-11-12 01:39:58 +00:00
if ( blockGroup ! = null )
blockGroup . PosAndRotCalculated = false ;
2020-11-13 22:59:37 +00:00
BlockEngine . UpdateDisplayedBlock ( Id ) ;
2020-05-18 03:19:16 +00:00
}
}
/// <summary>
/// The block's non-uniform scale or zero if the block's invalid. Independent of the uniform scaling.
2020-05-27 15:20:53 +00:00
/// The default scale of 1 means 0.2 in terms of position.
2020-05-18 03:19:16 +00:00
/// </summary>
public float3 Scale
{
2020-07-15 19:58:24 +00:00
get = > BlockEngine . GetBlockInfo ( this , ( ScalingEntityStruct st ) = > st . scale ) ;
2020-05-18 03:19:16 +00:00
set
{
2021-04-19 17:32:14 +00:00
int uscale = UniformScale ;
if ( value . x < 4e-5 ) value . x = uscale ;
if ( value . y < 4e-5 ) value . y = uscale ;
if ( value . z < 4e-5 ) value . z = uscale ;
2020-07-15 19:58:24 +00:00
BlockEngine . SetBlockInfo ( this , ( ref ScalingEntityStruct st , float3 val ) = > st . scale = val , value ) ;
2020-06-03 23:42:13 +00:00
if ( ! Exists ) return ; //UpdateCollision needs the block to exist
ScalingEngine . UpdateCollision ( Id ) ;
2020-11-13 22:59:37 +00:00
BlockEngine . UpdateDisplayedBlock ( Id ) ;
2020-05-18 03:19:16 +00:00
}
}
/// <summary>
/// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale.
2020-05-27 15:20:53 +00:00
/// The default scale of 1 means 0.2 in terms of position.
2020-05-18 03:19:16 +00:00
/// </summary>
public int UniformScale
{
2020-07-15 19:58:24 +00:00
get = > BlockEngine . GetBlockInfo ( this , ( UniformBlockScaleEntityStruct st ) = > st . scaleFactor ) ;
2020-05-18 03:19:16 +00:00
set
{
2021-04-19 17:32:14 +00:00
if ( value < 1 ) value = 1 ;
2020-07-15 19:58:24 +00:00
BlockEngine . SetBlockInfo ( this , ( ref UniformBlockScaleEntityStruct st , int val ) = > st . scaleFactor = val ,
value ) ;
2020-05-18 03:19:16 +00:00
Scale = new float3 ( value , value , value ) ;
}
}
2021-04-19 01:13:00 +00:00
/ * *
2021-04-19 17:32:14 +00:00
* Whether the block is flipped .
2021-04-19 01:13:00 +00:00
* /
public bool Flipped
{
get = > BlockEngine . GetBlockInfo ( this , ( ScalingEntityStruct st ) = > st . scale . x < 0 ) ;
set
{
BlockEngine . SetBlockInfo ( this , ( ref ScalingEntityStruct st , bool val ) = >
st . scale . x = math . abs ( st . scale . x ) * ( val ? - 1 : 1 ) , value ) ;
BlockEngine . SetBlockInfo ( this , ( ref GFXPrefabEntityStructGPUI st , bool val ) = >
{
uint prefabId = PrefabsID . GetOrCreatePrefabID ( ( ushort ) Type , ( byte ) Material , 0 , value ) ;
st . prefabID = prefabId ;
} , value ) ;
}
}
2020-05-18 03:19:16 +00:00
/// <summary>
/// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
/// </summary>
2020-05-19 22:08:02 +00:00
public BlockIDs Type
{
get
{
2020-07-15 19:58:24 +00:00
return BlockEngine . GetBlockInfo ( this , ( DBEntityStruct st ) = > ( BlockIDs ) st . DBID , BlockIDs . Invalid ) ;
2020-05-19 22:08:02 +00:00
}
}
2020-05-18 03:19:16 +00:00
/// <summary>
/// The block's color. Returns BlockColors.Default if the block no longer exists.
/// </summary>
public BlockColor Color
{
get
{
2020-07-15 19:58:24 +00:00
byte index = BlockEngine . GetBlockInfo ( this , ( ColourParameterEntityStruct st ) = > st . indexInPalette ,
byte . MaxValue ) ;
2020-06-04 22:20:35 +00:00
return new BlockColor ( index ) ;
2020-05-18 03:19:16 +00:00
}
set
{
2020-07-15 19:58:24 +00:00
BlockEngine . SetBlockInfo ( this , ( ref ColourParameterEntityStruct color , BlockColor val ) = >
2021-04-19 17:32:14 +00:00
{ //TODO: Check if setting to 255 works
2021-04-15 23:40:30 +00:00
color . indexInPalette = val . Index ;
2020-11-09 21:18:25 +00:00
color . hasNetworkChange = true ;
2020-09-28 01:10:59 +00:00
color . paletteColour = BlockEngine . ConvertBlockColor ( color . indexInPalette ) ;
2020-07-15 19:58:24 +00:00
} , value ) ;
2020-05-19 22:08:02 +00:00
}
}
/// <summary>
/// The block's exact color. Gets reset to the palette color (Color property) after reentering the game.
/// </summary>
public float4 CustomColor
{
2020-11-09 21:18:25 +00:00
get = > BlockEngine . GetBlockInfo ( this , ( ColourParameterEntityStruct st ) = > st . paletteColour ) ;
2020-05-19 22:08:02 +00:00
set
{
2020-07-15 19:58:24 +00:00
BlockEngine . SetBlockInfo ( this , ( ref ColourParameterEntityStruct color , float4 val ) = >
{
2020-11-09 21:18:25 +00:00
color . paletteColour = val ;
color . hasNetworkChange = true ;
2020-07-15 19:58:24 +00:00
} , value ) ;
2020-05-18 03:19:16 +00:00
}
}
2021-04-19 17:32:14 +00:00
/ * *
* The block ' s material .
* /
2021-04-15 23:40:30 +00:00
public BlockMaterial Material
{
2021-04-19 17:32:14 +00:00
get = > BlockEngine . GetBlockInfo ( this , ( CubeMaterialStruct cmst ) = > ( BlockMaterial ) cmst . materialId , BlockMaterial . Default ) ;
2021-04-15 23:40:30 +00:00
set = > BlockEngine . SetBlockInfo ( this ,
( ref CubeMaterialStruct cmst , BlockMaterial val ) = > cmst . materialId = ( byte ) val , value ) ;
}
2020-05-24 17:29:02 +00:00
/// <summary>
2020-07-15 19:58:24 +00:00
/// The text displayed on the block if applicable, or null.
2020-05-24 17:29:02 +00:00
/// Setting it is temporary to the session, it won't be saved.
/// </summary>
public string Label
{
2020-08-07 17:55:00 +00:00
get = > BlockEngine . GetBlockInfoViewStruct ( this , ( TextLabelEntityViewStruct st ) = > st . textLabelComponent ? . text ) ;
2020-05-24 17:29:02 +00:00
set
{
2020-08-07 17:55:00 +00:00
BlockEngine . SetBlockInfoViewStruct ( this , ( ref TextLabelEntityViewStruct text , string val ) = >
2020-07-15 19:58:24 +00:00
{
if ( text . textLabelComponent ! = null ) text . textLabelComponent . text = val ;
} , value ) ;
2020-05-24 17:29:02 +00:00
}
}
2020-11-12 01:39:58 +00:00
private BlockGroup blockGroup ;
2020-11-10 15:37:20 +00:00
/// <summary>
2020-11-14 21:43:45 +00:00
/// Returns the block group this block is a part of. Block groups can also be placed using blueprints.
2020-11-12 01:39:58 +00:00
/// Returns null if not part of a group.<br />
2020-11-13 22:59:37 +00:00
/// Setting the group after the block has been initialized will not update everything properly,
/// so you can only set this property on blocks newly placed by your code.<br />
/// To set it for existing blocks, you can use the Copy() method and set the property on the resulting block
/// (and remove this block).
2020-11-10 15:37:20 +00:00
/// </summary>
public BlockGroup BlockGroup
{
2020-11-12 01:39:58 +00:00
get
{
if ( blockGroup ! = null ) return blockGroup ;
return blockGroup = BlockEngine . GetBlockInfo ( this ,
( BlockGroupEntityComponent bgec ) = >
bgec . currentBlockGroup = = - 1 ? null : new BlockGroup ( bgec . currentBlockGroup , this ) ) ;
}
set
{
2020-11-13 22:59:37 +00:00
if ( Exists )
{
Logging . LogWarning ( "Attempted to set group of existing block. This is not supported."
+ " Copy the block and set the group of the resulting block." ) ;
return ;
}
2020-11-12 01:39:58 +00:00
blockGroup ? . RemoveInternal ( this ) ;
BlockEngine . SetBlockInfo ( this ,
( ref BlockGroupEntityComponent bgec , BlockGroup val ) = > bgec . currentBlockGroup = val ? . Id ? ? - 1 ,
value ) ;
value ? . AddInternal ( this ) ;
blockGroup = value ;
}
2020-11-10 15:37:20 +00:00
}
2020-05-18 03:19:16 +00:00
/// <summary>
/// Whether the block exists. The other properties will return a default value if the block doesn't exist.
2020-07-20 22:19:30 +00:00
/// If the block was just placed, then this will also return false but the properties will work correctly.
2020-05-18 03:19:16 +00:00
/// </summary>
public bool Exists = > BlockEngine . BlockExists ( Id ) ;
/// <summary>
/// Returns an array of blocks that are connected to this one. Returns an empty array if the block doesn't exist.
/// </summary>
public Block [ ] GetConnectedCubes ( ) = > BlockEngine . GetConnectedBlocks ( Id ) ;
/// <summary>
/// Removes this block.
/// </summary>
/// <returns>True if the block exists and could be removed.</returns>
public bool Remove ( ) = > RemovalEngine . RemoveBlock ( Id ) ;
2020-05-22 22:06:49 +00:00
/// <summary>
2020-07-18 23:13:39 +00:00
/// Returns the rigid body of the chunk of blocks this one belongs to during simulation.
2020-05-22 22:06:49 +00:00
/// Can be used to apply forces or move the block around while the simulation is running.
/// </summary>
2020-10-02 14:40:06 +00:00
/// <returns>The SimBody of the chunk or null if the block doesn't exist or not in simulation mode.</returns>
2020-05-24 17:29:02 +00:00
public SimBody GetSimBody ( )
2020-05-22 22:06:49 +00:00
{
2020-07-15 19:58:24 +00:00
return BlockEngine . GetBlockInfo ( this ,
2020-10-02 14:40:06 +00:00
( GridConnectionsEntityStruct st ) = > st . machineRigidBodyId ! = uint . MaxValue
? new SimBody ( st . machineRigidBodyId , st . clusterId )
: null ) ;
2020-05-22 22:06:49 +00:00
}
2020-11-13 20:35:53 +00:00
/// <summary>
2020-11-13 22:59:37 +00:00
/// Creates a copy of the block in the game with the same properties, stats and wires.
2020-11-13 20:35:53 +00:00
/// </summary>
/// <returns></returns>
public T Copy < T > ( ) where T : Block
{
2021-04-19 17:32:14 +00:00
var block = PlaceNew < T > ( Type , Position ) ;
block . Rotation = Rotation ;
block . Color = Color ;
block . Material = Material ;
block . UniformScale = UniformScale ;
block . Scale = Scale ;
2020-11-13 20:35:53 +00:00
block . copiedFrom = Id ;
return block ;
}
2020-07-18 23:42:32 +00:00
private void OnPlacedInit ( object sender , BlockPlacedRemovedEventArgs e )
{ //Member method instead of lambda to avoid constantly creating delegates
if ( e . ID ! = Id ) return ;
Placed - = OnPlacedInit ; //And we can reference it
InitData = default ; //Remove initializer as it's no longer valid - if the block gets removed it shouldn't be used again
2020-11-13 20:35:53 +00:00
if ( copiedFrom ! = EGID . Empty )
BlockCloneEngine . CopyBlockStats ( copiedFrom , Id ) ;
2020-07-18 23:42:32 +00:00
}
2020-05-18 03:19:16 +00:00
public override string ToString ( )
{
2020-05-24 17:29:02 +00:00
return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Type)}: {Type}, {nameof(Color)}: {Color}, {nameof(Exists)}: {Exists}" ;
2020-05-18 03:19:16 +00:00
}
2020-06-04 22:20:35 +00:00
public bool Equals ( Block other )
{
if ( ReferenceEquals ( null , other ) ) return false ;
if ( ReferenceEquals ( this , other ) ) return true ;
return Id . Equals ( other . Id ) ;
}
public bool Equals ( EGID other )
{
return Id . Equals ( other ) ;
}
public override bool Equals ( object obj )
{
if ( ReferenceEquals ( null , obj ) ) return false ;
if ( ReferenceEquals ( this , obj ) ) return true ;
if ( obj . GetType ( ) ! = this . GetType ( ) ) return false ;
return Equals ( ( Block ) obj ) ;
}
public override int GetHashCode ( )
{
return Id . GetHashCode ( ) ;
}
2020-05-18 03:19:16 +00:00
public static void Init ( )
{
GameEngineManager . AddGameEngine ( PlacementEngine ) ;
GameEngineManager . AddGameEngine ( MovementEngine ) ;
GameEngineManager . AddGameEngine ( RotationEngine ) ;
GameEngineManager . AddGameEngine ( RemovalEngine ) ;
GameEngineManager . AddGameEngine ( BlockEngine ) ;
2020-06-02 01:12:40 +00:00
GameEngineManager . AddGameEngine ( BlockEventsEngine ) ;
2020-06-03 23:42:13 +00:00
GameEngineManager . AddGameEngine ( ScalingEngine ) ;
2020-08-03 16:45:38 +00:00
GameEngineManager . AddGameEngine ( SignalEngine ) ;
2020-11-13 20:35:53 +00:00
GameEngineManager . AddGameEngine ( BlockCloneEngine ) ;
2020-08-03 16:45:38 +00:00
Wire . signalEngine = SignalEngine ; // requires same functionality, no need to duplicate the engine
2020-05-18 03:19:16 +00:00
}
2020-05-21 00:42:02 +00:00
/// <summary>
/// Convert the block to a specialised block class.
/// </summary>
/// <returns>The block.</returns>
/// <typeparam name="T">The specialised block type.</typeparam>
2020-05-22 00:00:31 +00:00
public T Specialise < T > ( ) where T : Block
2020-05-18 03:19:16 +00:00
{
// 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
2020-07-13 19:55:48 +00:00
//Lets improve that using delegates
2020-07-15 19:58:24 +00:00
var block = New < T > ( Id . entityID , Id . groupID ) ;
2020-09-18 19:19:39 +00:00
if ( this . InitData . Group ! = null )
{
block . InitData = this . InitData ;
Placed + = block . OnPlacedInit ; //Reset InitData of new object
}
2020-07-15 19:58:24 +00:00
return block ;
2020-05-18 03:19:16 +00:00
}
#if DEBUG
public static EntitiesDB entitiesDB
{
get
{
return BlockEngine . GetEntitiesDB ( ) ;
}
}
#endif
}
2020-05-13 12:02:36 +00:00
}