Advertisement
Guest User

Parent Child Component

a guest
Aug 4th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.         [Serializable]
  6.         [EditorCreatedObject]
  7.         public class EditorEntity_GroupSetup : SceneEntity
  8.         {
  9.             private bool _SetupMode = false;
  10.  
  11.             [EditorProperty(true, Description = "GroupSetupMode", ToolTipText = "This is to allow turning on or off grouping setup. False, the group moves based on setup distance. True, no children moves will be actioned.")]
  12.             public bool PlayerForce
  13.             {
  14.                 get { return _SetupMode; }
  15.                 set { _SetupMode = value; PassThrough.GroupSetupMode = value; }
  16.             }
  17.  
  18.             public override void SetObjectData(SerializationInfo info, StreamingContext c)
  19.             {
  20.                 base.SetObjectData(info, c);
  21.                 SerializationHelper.DeserializeField(ref _SetupMode, info, "GroupSetupMode", true);
  22.                 PassThrough.GroupSetupMode = _SetupMode;
  23.             }
  24.  
  25.             public override void GetObjectData(SerializationInfo info, StreamingContext c)
  26.             {
  27.                 base.GetObjectData(info, c);
  28.                 info.AddValue("GroupSetupMode", _SetupMode);
  29.             }
  30.  
  31.         }
  32.  
  33.  
  34.  
  35.         [Serializable]
  36.         public static class PassThrough
  37.         {
  38.             // This static variable is used to marshal the values between the Custom Editor SceneEntity and the Component
  39.             // It also holds the default value, so if the component is added without the Custom Editor SceneEntity it will still do something.
  40.             public static float PlayerForce = 0.5f;
  41.  
  42.             public static float PointsSmall = 1f;
  43.             public static float PointsMedium = 10f;
  44.             public static float PointsLarge = 50f;
  45.             public static float PointsXLarge = 100f;
  46.             public static float PointsBonus = 20f;
  47.  
  48.  
  49.             public static float LevelPoints = 0;
  50.  
  51.             public static bool GroupSetupMode_Primed = false;
  52.             public static bool GroupSetupMode = false;
  53.             public static Matrix ParentMatrix = new Matrix();
  54.             public static Vector3 ParentTranslation = new Vector3();
  55.         }
  56.  
  57.  
  58.  
  59.         [Serializable]
  60.         public class Component_Parent : BaseComponentAutoSerialization<ISceneEntity>
  61.         {
  62.             public override void OnUpdate(GameTime gameTime)
  63.             {
  64.                 PassThrough.ParentMatrix = ParentObject.World;
  65.                 PassThrough.ParentTranslation = ParentObject.World.Translation;
  66.  
  67.  
  68.             }
  69.         }
  70.  
  71.  
  72.  
  73.         [Serializable]
  74.         public class Component_Child : BaseComponentAutoSerialization<ISceneEntity>
  75.         {
  76.             Vector3 _parentOffset;
  77.             Quaternion _parentQuaternionOffset;
  78.  
  79.             public override void OnUpdate(GameTime gameTime)
  80.             {
  81.                 // Get a sceneobject from the ParentObject
  82.                 SceneObject sceneobject = (SceneObject)ParentObject;
  83.  
  84.                 // This relies on the position never being at 0,0,0 for setup, so please don't do that
  85.                 // or change it with more look ups so that you don't need to rely on a Zero Vector3 :-)
  86.                 if (PassThrough.GroupSetupMode || _parentOffset == Vector3.Zero)
  87.                 {
  88.                     if (PassThrough.ParentTranslation != Vector3.Zero)
  89.                     {
  90.                         // Get the distance between the child and the parent which we keep as the offset
  91.                         _parentOffset = sceneobject.World.Translation - PassThrough.ParentTranslation;
  92.                     }
  93.                 }
  94.                 else
  95.                 {
  96.                     if (_parentOffset != Vector3.Zero)
  97.                     {
  98.                         // Decompose World Matrix (Child)
  99.                         Quaternion rotationQ = new Quaternion();
  100.                         Vector3 childLocation = new Vector3();
  101.                         Vector3 childScale = new Vector3();
  102.                         sceneobject.World.Decompose(out childScale, out rotationQ, out childLocation);
  103.  
  104.                         // Decompose World Matrix (Parent)
  105.                         Quaternion parentQ = new Quaternion();
  106.                         Vector3 parentLocation = new Vector3();
  107.                         Vector3 parentScale = new Vector3();
  108.                         PassThrough.ParentMatrix.Decompose(out parentScale, out parentQ, out parentLocation);
  109.  
  110.  
  111.                         Matrix pLocation = Matrix.CreateTranslation(parentLocation);
  112.                         Matrix pRotation = Matrix.CreateFromQuaternion(parentQ);
  113.  
  114.                         Matrix rotation2 = Matrix.CreateFromQuaternion(_parentQuaternionOffset);
  115.  
  116.                         Matrix newWorld = pRotation * pLocation;
  117.  
  118.                         Vector3 testTranslation = newWorld.Translation + ((newWorld.Left * _parentOffset.X) + (newWorld.Up * _parentOffset.Y) + (newWorld.Forward * _parentOffset.Z));
  119.                         Matrix cScale = Matrix.CreateScale(childScale);
  120.  
  121.                         sceneobject.World = cScale * (pRotation * (Matrix.CreateTranslation(testTranslation)));
  122.                     }
  123.                 }
  124.             }
  125.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement