예제 #1
0
        public virtual void Init(MyObjectBuilder_EntityBase objectBuilder)
        {
            ProfilerShort.Begin("MyEntity.Init(objectBuilder)");
            MarkedForClose = false;
            Closed = false;
            this.Render.PersistentFlags = MyPersistentEntityFlags2.CastShadows;

            if (objectBuilder != null)
            {
                if (objectBuilder.EntityId != 0)
                    this.EntityId = objectBuilder.EntityId;
                else
                    AllocateEntityID();

                DefinitionId = objectBuilder.GetId();

                if (objectBuilder.EntityDefinitionId != null) // backward compatibility
                {
                    Debug.Assert(objectBuilder.SubtypeName == null || objectBuilder.SubtypeName == objectBuilder.EntityDefinitionId.Value.SubtypeName);
                    DefinitionId = objectBuilder.EntityDefinitionId.Value;
                }

                if (objectBuilder.PositionAndOrientation.HasValue)
                {
                    var posAndOrient = objectBuilder.PositionAndOrientation.Value;
                    MatrixD matrix = MatrixD.CreateWorld(posAndOrient.Position, posAndOrient.Forward, posAndOrient.Up);
                    MyUtils.AssertIsValid(matrix);

                    PositionComp.SetWorldMatrix((MatrixD)matrix);
                    ClampToWorld();
                }

                this.Name = objectBuilder.Name;
                this.Render.PersistentFlags = objectBuilder.PersistentFlags;

                // This needs to be called after Entity has it's valid EntityID so components when are initiliazed or added to container, they get valid EntityID
                InitComponentsExtCallback(this.Components, DefinitionId.Value.TypeId, DefinitionId.Value.SubtypeId, objectBuilder.ComponentContainer);
            }
            else
            {
                AllocateEntityID();
            }

            this.InScene = false;

            MyEntitiesInterface.SetEntityName(this, false);

            if (SyncFlag)
            {
                CreateSync();
            }
            GameLogic.Init(objectBuilder);
            ProfilerShort.End();
        }
예제 #2
0
        public virtual void Init(MyObjectBuilder_EntityBase objectBuilder)
        {
            ProfilerShort.Begin("MyEntity.Init(objectBuilder)");
            MarkedForClose = false;
            Closed = false;
            this.Render.PersistentFlags = MyPersistentEntityFlags2.CastShadows;

            if (objectBuilder != null)
            {
                if (objectBuilder.EntityId != 0)
                    this.EntityId = objectBuilder.EntityId;
                else
                    AllocateEntityID();

                DefinitionId = objectBuilder.GetId();

                if (objectBuilder.EntityDefinitionId != null) // backward compatibility
                {
                    Debug.Assert(objectBuilder.SubtypeName == null || objectBuilder.SubtypeName == objectBuilder.EntityDefinitionId.Value.SubtypeName);
                    DefinitionId = objectBuilder.EntityDefinitionId.Value;
                }

                if (objectBuilder.PositionAndOrientation.HasValue)
                {
                    var posAndOrient = objectBuilder.PositionAndOrientation.Value;

                    //GR: Check for NaN values and remove them (otherwise there will be problems wilth clusters)
                    if (posAndOrient.Position.x.IsValid() == false)
                    {
                        posAndOrient.Position.x = 0.0f;
                    }
                    if (posAndOrient.Position.y.IsValid() == false)
                    {
                        posAndOrient.Position.y = 0.0f;
                    }
                    if (posAndOrient.Position.z.IsValid() == false)
                    {
                        posAndOrient.Position.z = 0.0f;
                    }

                    MatrixD matrix = MatrixD.CreateWorld(posAndOrient.Position, posAndOrient.Forward, posAndOrient.Up);
                    //if (matrix.IsValid())
                    //    MatrixD.Rescale(ref matrix, scale);
                    MyUtils.AssertIsValid(matrix);
                    PositionComp.SetWorldMatrix((MatrixD)matrix);
                    ClampToWorld();
                }

                this.Name = objectBuilder.Name;
                this.Render.PersistentFlags = objectBuilder.PersistentFlags & ~VRage.ObjectBuilders.MyPersistentEntityFlags2.InScene;

                // This needs to be called after Entity has it's valid EntityID so components when are initiliazed or added to container, they get valid EntityID
                InitComponentsExtCallback(this.Components, DefinitionId.Value.TypeId, DefinitionId.Value.SubtypeId, objectBuilder.ComponentContainer);
            }
            else
            {
                AllocateEntityID();
            }

            Debug.Assert(!this.InScene, "Entity is in scene after creation!");

            MyEntitiesInterface.SetEntityName(this, false);

            if (SyncFlag)
            {
                CreateSync();
            }
            GameLogic.Init(objectBuilder);
            ProfilerShort.End();
        }