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 ;
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>
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</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>
/// <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 )
{
2020-07-15 19:58:24 +00:00
return PlaceNew < Block > ( block , position , rotation , color , darkness , uscale , scale , 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>
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</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>
/// <param name="player">The player who placed the block</param>
/// <returns>The placed block or null if failed</returns>
2020-07-13 19:55:48 +00:00
public static T PlaceNew < T > ( BlockIDs block , float3 position ,
2020-05-22 01:00:33 +00:00
float3 rotation = default , BlockColors color = BlockColors . Default , byte darkness = 0 ,
2020-07-13 19:55:48 +00:00
int uscale = 1 , float3 scale = default , Player player = null ) where T : Block
2020-05-22 01:00:33 +00:00
{
if ( PlacementEngine . IsInGame & & GameState . IsBuildMode ( ) )
{
2020-07-13 19:55:48 +00:00
var egid = PlacementEngine . PlaceBlock ( block , color , darkness ,
2020-07-15 19:58:24 +00:00
position , uscale , scale , player , rotation , out var initializer ) ;
var bl = New < T > ( egid . entityID , egid . groupID ) ;
bl . InitData . Group = BlockEngine . InitGroup ( initializer ) ;
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 > > ( ) ;
private static Dictionary < Type , ExclusiveGroupStruct [ ] > typeToGroup =
new Dictionary < Type , ExclusiveGroupStruct [ ] >
{
2020-11-09 21:18:25 +00:00
{ typeof ( ConsoleBlock ) , new [ ] { CommonExclusiveGroups . CONSOLE_BLOCK_GROUP } } ,
{ 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
throw new BlockTypeException ( $"Incompatible block type! Type {type.Name} belongs to group {gr.Select(g => g.ToString()).Aggregate((a, b) => a + " , " + b)} instead of {group.Value}" ) ;
}
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 ) ;
}
else if ( type ! = typeof ( Block ) )
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
}
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-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-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
{
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-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
{
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 ) ;
}
}
/// <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 ) = >
{
color . indexInPalette = ( byte ) ( val . Color + val . Darkness * 10 ) ;
2020-11-09 21:18:25 +00:00
//color.overridePaletteColour = false;
//color.needsUpdate = true;
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.overridePaletteColour = true;
//color.needsUpdate = true;
color . hasNetworkChange = true ;
2020-07-15 19:58:24 +00:00
} , value ) ;
2020-05-18 03:19:16 +00:00
}
}
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>
/// Returns the block group this block is a part of. Block groups can be placed using blueprints.
2020-11-12 01:39:58 +00:00
/// Returns null if not part of a group.<br />
/// Setting the group after the block has been initialized will not update everything properly.
/// You should only set this property on blocks newly placed by your code.
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
{
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>
/// Creates a copy of the block in the game with the same basic properties and tweakable stats.
/// </summary>
/// <returns></returns>
public T Copy < T > ( ) where T : Block
{
var block = PlaceNew < T > ( Type , Position , Rotation , Color . Color , Color . Darkness , UniformScale , Scale ) ;
block . copiedFrom = Id ;
if ( Type = = BlockIDs . ConsoleBlock
& & ( this is ConsoleBlock srcCB | | ( srcCB = Specialise < ConsoleBlock > ( ) ) ! = null )
& & ( block is ConsoleBlock dstCB | | ( dstCB = block . Specialise < ConsoleBlock > ( ) ) ! = null ) )
{
//Console block properties are set by a separate engine in the game
dstCB . Arg1 = srcCB . Arg1 ;
dstCB . Arg2 = srcCB . Arg2 ;
dstCB . Arg3 = srcCB . Arg3 ;
dstCB . Command = srcCB . Command ;
}
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
}