context: namespace SEModAPIInternal.API.Entity
  {
  	using System;
  	using System.ComponentModel;
  	using System.IO;
  	using System.Runtime.Serialization;
  	using Havok;
  	using Sandbox;
  	using SEModAPI.API;
  	using SEModAPI.API.TypeConverters;
  	using SEModAPI.API.Utility;
  	using SEModAPIInternal.API.Common;
  	using SEModAPIInternal.API.Utility;
  	using SEModAPIInternal.Support;
  	using VRage;
  	using VRage.Components;
  	using VRage.ModAPI;
  	using VRage.ObjectBuilders;
  	using VRageMath;
  
  	[DataContract( Name = "BaseEntityProxy" )]
  	public class BaseEntity : BaseObject
  	{
  		#region "Attributes"
  
  		private static Type m_internalType;
  
  		private float m_maxLinearVelocity;
  		private long m_entityId;
  		private MyPositionAndOrientation m_positionOrientation;
  		private Vector3 m_linearVelocity;
  		private Vector3 m_angularVelocity;
  		private BaseEntityNetworkManager m_networkManager;
  		private string m_displayName;
  
  		//Definition
  		public static string BaseEntityNamespace = "Sandbox.Game.Entities";
  
  		public static string BaseEntityClass = "MyEntity";
  
  		//Methods
  		public static string BaseEntityGetObjectBuilderMethod = "GetObjectBuilder";
  
  		public static string BaseEntityGetPhysicsManagerMethod = "get_Physics";
  
  		public static string BaseEntityCombineOnClosedEventMethod = "add_OnClose";
  
  		public static string BaseEntityGetIsDisposedMethod = "get_Closed";
  		public static string BaseEntityGetOrientationMatrixMethod = "get_WorldMatrix";
  
  		public static string BaseEntityGetNetManagerMethod = "SyncObject";
  
  		public static string BaseEntitySetEntityIdMethod = "set_EntityId";
  		public static string BaseEntityGetDisplayNameMethod = "get_DisplayName";
  		public static string BaseEntitySetDisplayNameMethod = "set_DisplayName";
  
  		public static string BaseEntityGetPositionManagerMethod = "get_PositionComp";
  		//public static string BaseEntityCombineOnMovedEventMethod = "";
  
  		public static string BaseEntityEntityIdField = "m_entityId";
  
  		//////////////////////////////////////////////////////////
  
  		public static string PhysicsManagerNamespace = "Sandbox.Engine.Physics";
  		public static string PhysicsManagerClass = "MyPhysicsBody";
  		public static string PhysicsManagerGetRigidBodyMethod = "get_RigidBody";
  
  		#endregion "Attributes"
  
  		#region "Constructors and Initializers"
  
  		public BaseEntity( MyObjectBuilder_EntityBase baseEntity )
  			: base( baseEntity )
  		{
  			if ( baseEntity != null )
  			{
  				m_entityId = baseEntity.EntityId;
  				if ( baseEntity.PositionAndOrientation != null )
  				{
  					m_positionOrientation = baseEntity.PositionAndOrientation.GetValueOrDefault( );
  				}
  				else
  				{
  					m_positionOrientation = new MyPositionAndOrientation( );
  					m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition( new Vector3D( -500000, -500000, -500000 ), new Vector3D( 500000, 500000, 500000 ) );
  					m_positionOrientation.Forward = new Vector3( 0, 0, 1 );
  					m_positionOrientation.Up = new Vector3( 0, 1, 0 );
  				}
  			}
  			else
  			{
  				m_entityId = 0;
  				m_positionOrientation = new MyPositionAndOrientation( );
  				m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition( new Vector3( -500000, -500000, -500000 ), new Vector3( 500000, 500000, 500000 ) );
  				m_positionOrientation.Forward = new Vector3( 0, 0, 1 );
  				m_positionOrientation.Up = new Vector3( 0, 1, 0 );
  			}
  
  			m_linearVelocity = new Vector3( 0, 0, 0 );
  			m_angularVelocity = new Vector3( 0, 0, 0 );
  			m_maxLinearVelocity = (float)104.7;
  		}
  
  		public BaseEntity( MyObjectBuilder_EntityBase baseEntity, Object backingObject )
  			: base( baseEntity, backingObject )
  		{
  			if ( baseEntity != null )
  			{
  				m_entityId = baseEntity.EntityId;
  				if ( baseEntity.PositionAndOrientation != null )
  				{
  					m_positionOrientation = baseEntity.PositionAndOrientation.GetValueOrDefault( );
  				}
  				else
  				{
  					m_positionOrientation = new MyPositionAndOrientation( );
  					m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition( new Vector3( -500000, -500000, -500000 ), new Vector3( 500000, 500000, 500000 ) );
  					m_positionOrientation.Forward = new Vector3( 0, 0, 1 );
  					m_positionOrientation.Up = new Vector3( 0, 1, 0 );
  				}
  			}
  			else
  			{
  				m_entityId = 0;
  				m_positionOrientation = new MyPositionAndOrientation( );
  				m_positionOrientation.Position = UtilityFunctions.GenerateRandomBorderPosition( new Vector3( -500000, -500000, -500000 ), new Vector3( 500000, 500000, 500000 ) );
  				m_positionOrientation.Forward = new Vector3( 0, 0, 1 );
  				m_positionOrientation.Up = new Vector3( 0, 1, 0 );
  			}
  
  			m_networkManager = new BaseEntityNetworkManager( this, GetEntityNetworkManager( BackingObject ) );
  
  			m_linearVelocity = new Vector3( 0, 0, 0 );
  			m_angularVelocity = new Vector3( 0, 0, 0 );
  			m_maxLinearVelocity = (float)104.7;
  
  			if ( EntityId != 0 )
  			{
  				GameEntityManager.AddEntity( EntityId, this );
  			}
  
  			MySandboxGame.Static.Invoke( InternalRegisterEntityMovedEvent );
  		}
  
  		#endregion "Constructors and Initializers"
  
  		#region "Properties"
  
  		[IgnoreDataMember]
  		[Category( "Entity" )]
  		[Browsable( false )]
  		[ReadOnly( true )]
  		internal static Type InternalType
  		{
  			get
  			{
  				if ( m_internalType == null )
  					m_internalType = SandboxGameAssemblyWrapper.Instance.GetAssemblyType( BaseEntityNamespace, BaseEntityClass );
  				return m_internalType;
  			}
  		}
  
  		[DataMember]
  		[Category( "Entity" )]
  		[Browsable( true )]
  		[ReadOnly( true )]
  		[Description( "Formatted Name of an entity" )]
  		public override string Name
  		{
  			get
  			{
  				return ObjectBuilder.Name == "" ? ObjectBuilder.TypeId.ToString( ) : ObjectBuilder.EntityId.ToString( );
  			}
  		}
  
  		[IgnoreDataMember]
  		[Category( "Entity" )]
  		[Browsable( false )]
  		[ReadOnly( true )]
  		internal new MyObjectBuilder_EntityBase ObjectBuilder
  		{
  			get
  			{
  				MyObjectBuilder_EntityBase entityBase = (MyObjectBuilder_EntityBase)base.ObjectBuilder;
  				if ( entityBase == null )
  					return (MyObjectBuilder_EntityBase)null;
  
  				entityBase.EntityId = m_entityId;
  				entityBase.PositionAndOrientation = m_positionOrientation;
  
  				return (MyObjectBuilder_EntityBase)base.ObjectBuilder;
  			}
  			set
  			{
  				base.ObjectBuilder = value;
  			}
  		}
  
  		[DataMember]
  		[Category( "Entity" )]
  		public virtual string DisplayName
  		{
  			get { return m_displayName; }
  			set
  			{
  				if ( m_displayName == value ) return;
  				m_displayName = value;
  				Changed = true;
  
  				if ( BackingObject != null )
  				{
  					MySandboxGame.Static.Invoke( InternalUpdateDisplayName );
  				}
  			}
  		}
  
  		[DataMember]
  		[Category( "Entity" )]
  		[Browsable( true )]
  		[Description( "The unique entity ID representing a functional entity in-game" )]
  		public long EntityId
  		{
  			get
  			{
  				if ( BackingObject == null )
  					return m_entityId;
  
  				long entityId = GetEntityId( BackingObject );
  				if ( entityId == 0 )
  					return m_entityId;
  
  				return entityId;
  			}
  			set
  			{
  				if ( m_entityId == value ) return;
  				m_entityId = value;
  				Changed = true;
  
  				if ( BackingObject != null )
  				{
  					MySandboxGame.Static.Invoke( InternalUpdateEntityId );
  				}
  			}
  		}
  
  		[IgnoreDataMember]
  		[Category( "Entity" )]
  		[Browsable( false )]
  		[ReadOnly( true )]
  		public MyPersistentEntityFlags2 PersistentFlags
  		{
  			get { return ObjectBuilder.PersistentFlags; }
  			private set
  			{
  				//Do nothing!
  			}
  		}
  
  		[IgnoreDataMember, Category( "Entity" ), Browsable( true ), ReadOnly( ( true ) )]
  		[Description( "Whether or not the entity will be rendered in the world and subject to physics calculations." )]
  		public bool InScene
  		{
  			get { return ObjectBuilder.PersistentFlags.HasFlag( MyPersistentEntityFlags2.InScene ); }
  			private set
  			{
  				if ( value )
  				{
  					ObjectBuilder.PersistentFlags |= MyPersistentEntityFlags2.InScene;
  				}
  				else
  				{
  					ObjectBuilder.PersistentFlags &= ~MyPersistentEntityFlags2.InScene;
  				}
  			}
  		}
  
  		[IgnoreDataMember]
  		[Category( "Entity" )]
  		[Browsable( false )]
  		public MyPositionAndOrientation PositionAndOrientation
  		{
  			get
  			{
  				/*
  				if (BackingObject == null)
  					return m_positionOrientation;
  
  				HkRigidBody body = PhysicsBody;
  				if (body == null || body.IsDisposed)
  					return m_positionOrientation;
  				Matrix orientationMatrix = Matrix.CreateFromQuaternion(body.Rotation);
  				MyPositionAndOrientation positionOrientation = new MyPositionAndOrientation(orientationMatrix);
  				positionOrientation.Position = body.Position;
  				 */
  
  				HkRigidBody body = PhysicsBody;
  				if ( body == null || body.IsDisposed )
  					return m_positionOrientation;
  
  				IMyEntity entity = Entity;
  				if ( entity == null )
  					return m_positionOrientation;
  
  				if ( entity.Physics == null )
  					return m_positionOrientation;
  
  				MyPositionAndOrientation positionOrientation = new MyPositionAndOrientation( entity.Physics.GetWorldMatrix( ) );
  				return positionOrientation;
  			}
  			set
  			{
  				m_positionOrientation = value;
  				Changed = true;
  
  				if ( BackingObject != null )
  				{
  					MySandboxGame.Static.Invoke( InternalUpdatePosition );
  					MySandboxGame.Static.Invoke( InternalUpdateOrientation );
  				}
  			}
  		}
  
  		[DataMember]
  		[Category( "Entity" )]
  		[TypeConverter( typeof( Vector3DTypeConverter ) )]
  		public Vector3DWrapper Position
  		{
  			get { return PositionAndOrientation.Position; }
  			set
  			{
  				m_positionOrientation.Position = value;
  				Changed = true;
  
  				if ( BackingObject != null )
  				{
  					MySandboxGame.Static.Invoke( InternalUpdatePosition );
  				}
  			}
  		}
  
  		[IgnoreDataMember]
  		[Category( "Entity" )]
  		[TypeConverter( typeof( Vector3TypeConverter ) )]
  		public Vector3Wrapper Up
  		{
  			get { return PositionAndOrientation.Up; }
  			set
  			{
  				m_positionOrientation.Up = value;
  				Changed = true;
  
  				if ( BackingObject != null )
  				{
  					MySandboxGame.Static.Invoke( InternalUpdateOrientation );
  				}
  			}
  		}
  
  		[IgnoreDataMember]
  		[Category( "Entity" )]
  		[TypeConverter( typeof( Vector3TypeConverter ) )]
  		public Vector3Wrapper Forward
  		{
  			get { return PositionAndOrientation.Forward; }
  			set
  			{
  				m_positionOrientation.Forward = value;
  				Changed = true;
  
  				if ( BackingObject != null )
  				{
  					MySandboxGame.Static.Invoke( InternalUpdateOrientation );
  				}
  			}
  		}
  
  		[DataMember]
  		[Category( "Entity" )]
  		[TypeConverter( typeof( Vector3TypeConverter ) )]
  		public Vector3Wrapper LinearVelocity
  		{
  			get
  			{
  				if ( BackingObject == null )
  					return m_linearVelocity;
  
  				HkRigidBody body = PhysicsBody;
  				if ( body == null || body.IsDisposed )
  					return m_linearVelocity;
  
  				if ( body.LinearVelocity == Vector3.Zero )
  					return m_linearVelocity;
  
  				return body.LinearVelocity;
  			}
  			set
  			{
  				m_linearVelocity = value;
  				Changed = true;
  
  				if ( BackingObject != null )
  				{
  					MySandboxGame.Static.Invoke( InternalUpdateLinearVelocity );
  				}
  			}
  		}
  
  		[DataMember]
  
gt: 		[Category( "Entity" )]