Exemplo n.º 1
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider2D} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            tsCollider = GetComponent <TSCollider2D>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <TSTransform2D>();
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = _rotation * FP.Deg2Rad;
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
Exemplo n.º 2
0
        internal void Update(GameObject otherGO, Physics2D.Contact c)
        {
            if (this.gameObject == null)
            {
                this.gameObject = otherGO;
                this.collider   = this.gameObject.GetComponent <TSCollider2D>();
                this.rigidbody  = this.gameObject.GetComponent <TSRigidBody2D>();
                this.transform  = this.collider.tsTransform;
            }

            if (c != null)
            {
                if (contacts[0] == null)
                {
                    contacts[0] = new TSContactPoint2D();
                }

                TSVector2 normal;
                Physics2D.FixedArray2 <TSVector2> points;

                c.GetWorldManifold(out normal, out points);

                contacts[0].normal = normal;
                contacts[0].point  = points[0];

                this.relativeVelocity = c.CalculateRelativeVelocity();
            }
        }
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider2D} attached.
         **/
        public void Initialize()
        {
            if (m_Initialized)
            {
                return;
            }

            m_TSCollider = GetComponent <TSCollider2D>();

            if (transform.parent != null)
            {
                m_TSParent = transform.parent.GetComponent <TSTransform2D>();
            }

            if (!m_Serialized)
            {
                UpdateEditMode();
            }

            if (m_TSCollider != null)
            {
                if (m_TSCollider.isBodyInitialized)
                {
                    m_TSCollider.body.TSPosition    = m_Position + m_TSCollider.center;
                    m_TSCollider.body.TSOrientation = m_Rotation * FP.Deg2Rad;
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            m_Initialized = true;
        }
Exemplo n.º 4
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider2D} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            tsCollider = GetComponent <TSCollider2D>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <TSTransform2D>();
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = _rotation * FP.Deg2Rad;
                }
            }

            #if STORE_DATA
            _trName = name;
            #endif

            initialized = true;
            position    = _position;
            UpdatePlayMode();
        }
Exemplo n.º 5
0
        private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation)
        {
            ICollider[] tsColliders = go.GetComponentsInChildren <ICollider>();
            if (tsColliders != null)
            {
                for (int index = 0, length = tsColliders.Length; index < length; index++)
                {
                    PhysicsManager.instance.AddBody(tsColliders[index]);
                }
            }

            TSTransform rootTSTransform = go.GetComponent <TSTransform>();

            if (rootTSTransform != null)
            {
                rootTSTransform.Initialize();

                rootTSTransform.position = position;
                rootTSTransform.rotation = rotation;
            }

            TSTransform[] tsTransforms = go.GetComponentsInChildren <TSTransform>();
            if (tsTransforms != null)
            {
                for (int index = 0, length = tsTransforms.Length; index < length; index++)
                {
                    TSTransform tsTransform = tsTransforms[index];

                    if (tsTransform != rootTSTransform)
                    {
                        tsTransform.Initialize();
                    }
                }
            }

            TSTransform2D rootTSTransform2D = go.GetComponent <TSTransform2D>();

            if (rootTSTransform2D != null)
            {
                rootTSTransform2D.Initialize();

                rootTSTransform2D.position = new TSVector2(position.x, position.y);
                rootTSTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z;
            }

            TSTransform2D[] tsTransforms2D = go.GetComponentsInChildren <TSTransform2D>();
            if (tsTransforms2D != null)
            {
                for (int index = 0, length = tsTransforms2D.Length; index < length; index++)
                {
                    TSTransform2D tsTransform2D = tsTransforms2D[index];

                    if (tsTransform2D != rootTSTransform2D)
                    {
                        tsTransform2D.Initialize();
                    }
                }
            }
        }
Exemplo n.º 6
0
        /**
         *  @brief Creates a new {@link TSRigidBody} when there is no one attached to this GameObject.
         **/
        public void Awake()
        {
            tsTransform = this.GetComponent <TSTransform2D>();
            tsRigidBody = this.GetComponent <TSRigidBody2D>();

            if (lossyScale == TSVector.one)
            {
                lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
            }
        }
Exemplo n.º 7
0
        public void AddBody(ICollider i_Collider)
        {
            if (i_Collider == null)
            {
                return;
            }

            bool is2dCollider = (i_Collider is TSCollider2D);

            Debug.Assert(is2dCollider, "3D Physics is not supported.");

            if (!is2dCollider)
            {
                return;
            }

            TSCollider2D tsCollider = (TSCollider2D)i_Collider;

            Debug.Assert(tsCollider.body == null, "Body already added.");

            if (tsCollider.body != null)
            {
                return;
            }

            tsCollider.Initialize(m_World);

            m_GameObjectMap[tsCollider.body] = tsCollider.gameObject;

            Transform    parent         = tsCollider.transform.parent;
            TSCollider2D parentCollider = (parent != null) ? parent.GetComponentInParent <TSCollider2D>() : null;

            if (parentCollider != null)
            {
                Physics2D.Body childBody = tsCollider.body;

                TSTransform2D parentTransform   = parentCollider.GetComponent <TSTransform2D>();
                TSTransform2D colliderTransform = tsCollider.GetComponent <TSTransform2D>();

                childBody.bodyConstraints.Add(new ConstraintHierarchy2D(parentCollider.body, tsCollider.body, (colliderTransform.position + tsCollider.center) - (parentTransform.position + parentCollider.center)));
            }

            TSRigidBody2D attachedRigidBody = tsCollider.GetComponent <TSRigidBody2D>();

            if (attachedRigidBody != null)
            {
                m_RigidBodies.Add(attachedRigidBody);
            }

            m_World.ProcessAddedBodies();
        }
        private void InitializeObject(TrueSyncObject i_TrueSyncObject)
        {
            if (i_TrueSyncObject == null)
            {
                return;
            }

            // Register colliders on physics manager.

            for (int index = 0; index < i_TrueSyncObject.colliderCount; ++index)
            {
                TSCollider2D collider = i_TrueSyncObject.GetColliderByIndex(index);

                if (collider == null)
                {
                    continue;
                }

                PhysicsManager.AddBody(collider);
            }

            // Init transforms.

            for (int index = 0; index < i_TrueSyncObject.transformCount; ++index)
            {
                TSTransform2D t = i_TrueSyncObject.GetTransformByIndex(index);

                if (t == null)
                {
                    continue;
                }

                t.Initialize();
            }

            // Init rigidbody, if any.

            for (int index = 0; index < i_TrueSyncObject.rigidbodyCount; ++index)
            {
                TSRigidBody2D rigidbody = i_TrueSyncObject.GetRigidBodyByIndex(index);

                if (rigidbody == null)
                {
                    continue;
                }

                rigidbody.Initialize();
            }
        }
        internal void Update(GameObject i_OtherGO, Physics2D.Contact i_Contact)
        {
            gameObject = i_OtherGO;

            if (i_OtherGO != null)
            {
                collider  = i_OtherGO.GetComponent <TSCollider2D>();
                rigidbody = i_OtherGO.GetComponent <TSRigidBody2D>();
                transform = i_OtherGO.GetComponent <TSTransform2D>();
            }
            else
            {
                collider  = null;
                rigidbody = null;
                transform = null;
            }

            if (i_Contact != null)
            {
                if (contacts[0] == null)
                {
                    contacts[0] = new TSContactPoint2D();
                }

                TSVector2 normal;
                Physics2D.FixedArray2 <TSVector2> points;

                i_Contact.GetWorldManifold(out normal, out points);

                contacts[0].normal = normal;
                contacts[0].point  = points[0];

                relativeVelocity = i_Contact.CalculateRelativeVelocity();
            }
            else
            {
                relativeVelocity = TSVector2.zero;

                if (contacts[0] != null)
                {
                    contacts[0].normal = TSVector2.zero;
                    contacts[0].point  = TSVector2.zero;
                }
            }
        }
Exemplo n.º 10
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            TSTransform2D tsTransform2D = target as TSTransform2D;

            if (tsTransform2D == null)
            {
                return;
            }

            string positionLabel = "Position --> (" + tsTransform2D.position.x + ", " + tsTransform2D.position.y + ")";
            string rotationLabel = "Rotation --> (" + tsTransform2D.rotation + ")";

            string serializedLable = (tsTransform2D.serialized) ? "Serialized: OK!" : "WARNING: Not serialized";

            EditorGUILayout.LabelField(positionLabel, EditorStyles.label);
            EditorGUILayout.LabelField(rotationLabel, EditorStyles.label);

            EditorGUILayout.Space();

            EditorGUILayout.LabelField(serializedLable, EditorStyles.label);
        }
        // MonoBehaviour's INTERFACE

        public void Awake()
        {
            m_TSTransform = GetComponent <TSTransform2D>();
            m_TSRigidBody = GetComponent <TSRigidBody2D>();
        }