예제 #1
0
        private MyEntitySubpart LoadSubpartFromName(string name)
        {
            MyEntitySubpart subpart;

            Subparts.TryGetValue(name, out subpart);

            // simply return the subpart if it exists in the dictionary
            if (subpart != null)
            {
                return(subpart);
            }

            // otherwise load it now and add to dictionary
            subpart = new MyEntitySubpart();

            string fileName = Path.Combine(Path.GetDirectoryName(Model.AssetName), name) + ".mwm";

            subpart.Render.EnableColorMaskHsv = Render.EnableColorMaskHsv;
            subpart.Render.ColorMaskHsv       = Render.ColorMaskHsv;
            subpart.Init(null, fileName, this, null);

            // add to dictionary
            Subparts[name] = subpart;

            if (InScene)
            {
                subpart.OnAddedToScene(this);
            }

            return(subpart);
        }
예제 #2
0
        private void GetBarrelAndMuzzle()
        {
            MyEntitySubpart barrel;

            if (Subparts.TryGetValue("Barrel", out barrel))
            {
                m_barrel = barrel;
            }

            var model = Engine.Models.MyModels.GetModelOnlyDummies(BlockDefinition.Model);

            m_gunBase.LoadDummies(model.Dummies);

            // backward compatibility for models without dummies or old dummies
            if (!m_gunBase.HasDummies)
            {
                if (model.Dummies.ContainsKey("Muzzle"))
                {
                    m_gunBase.AddMuzzleMatrix(MyAmmoType.HighSpeed, model.Dummies["Muzzle"].Matrix);
                }
                else
                {
                    Matrix muzzleMatrix = Matrix.CreateTranslation(new Vector3(0, 0, -1));
                    m_gunBase.AddMuzzleMatrix(MyAmmoType.HighSpeed, muzzleMatrix);
                }
            }
        }
예제 #3
0
        public override void UpdateVisual()
        {
            base.UpdateVisual();
            MyEntitySubpart barrel;

            if (Subparts.TryGetValue("Barrel", out barrel))
            {
                m_barrel = barrel;
            }
        }
예제 #4
0
        private void InitSubparts()
        {
            if (!CubeGrid.CreatePhysics)
            {
                return;
            }
            int             i        = 1;
            StringBuilder   partName = new StringBuilder();
            MyEntitySubpart foundPart;

            m_subparts.Clear();
            while (true)
            {
                partName.Clear().Append("HangarDoor_door").Append(i++);
                Subparts.TryGetValue(partName.ToString(), out foundPart);
                if (foundPart == null)
                {
                    break;
                }
                m_subparts.Add(foundPart);
            }
            ;

            UpdateDoorPosition();

            if (CubeGrid.Projector != null)
            {
                //This is a projected grid, don't add collisions for subparts
                return;
            }
            foreach (var subpart in m_subparts)
            {
                if (subpart != null && subpart.Physics == null)
                {
                    if ((subpart.ModelCollision.HavokCollisionShapes != null) && (subpart.ModelCollision.HavokCollisionShapes.Length > 0))
                    {
                        var shape = subpart.ModelCollision.HavokCollisionShapes[0];
                        subpart.Physics           = new Engine.Physics.MyPhysicsBody(subpart, RigidBodyFlag.RBF_DOUBLED_KINEMATIC | RigidBodyFlag.RBF_KINEMATIC);
                        subpart.Physics.IsPhantom = false;
                        Vector3 center = subpart.PositionComp.LocalVolume.Center;
                        subpart.Physics.CreateFromCollisionObject(shape, center, WorldMatrix, null, MyPhysics.KinematicDoubledCollisionLayer);
                        subpart.Physics.Enabled = true;
                    }
                }
            }

            CubeGrid.OnHavokSystemIDChanged -= CubeGrid_HavokSystemIDChanged;
            CubeGrid.OnHavokSystemIDChanged += CubeGrid_HavokSystemIDChanged;
            CubeGrid.OnPhysicsChanged       -= CubeGrid_OnPhysicsChanged;
            CubeGrid.OnPhysicsChanged       += CubeGrid_OnPhysicsChanged;
            if (CubeGrid.Physics != null)
            {
                UpdateHavokCollisionSystemID(CubeGrid.Physics.HavokCollisionSystemID);
            }
        }
예제 #5
0
        private void LoadSubparts()
        {
            DisposeSubpartsPhysics();

            Debug.Assert(!this.Closed);
            if (this.Closed)
            {
                // When closed, the welding callback will call this, but the model will already be unloaded
                return;
            }

            if (!Subparts.TryGetValue("PistonSubpart1", out m_subpart1))
            {
                return;
            }
            if (!m_subpart1.Subparts.TryGetValue("PistonSubpart2", out m_subpart2))
            {
                return;
            }
            if (!m_subpart2.Subparts.TryGetValue("PistonSubpart3", out Subpart3))
            {
                return;
            }

            MyModelDummy dummy;

            if (Subpart3.Model.Dummies.TryGetValue("TopBlock", out dummy))
            {
                m_constraintBasePos = dummy.Matrix.Translation;
            }
            if (Model.Dummies.TryGetValue("subpart_PistonSubpart1", out dummy))
            {
                m_subpartsConstraintPos = dummy.Matrix.Translation;
                m_subpart1LocPos        = m_subpartsConstraintPos;
            }
            if (m_subpart1.Model.Dummies.TryGetValue("subpart_PistonSubpart2", out dummy))
            {
                m_subpart2LocPos = dummy.Matrix.Translation;
            }
            if (m_subpart2.Model.Dummies.TryGetValue("subpart_PistonSubpart3", out dummy))
            {
                m_subpart3LocPos = dummy.Matrix.Translation;
            }


            if (!CubeGrid.CreatePhysics)
            {
                return;
            }

            InitSubpartsPhysics();
        }
        protected override void FillSubparts()
        {
            MyEntitySubpart foundPart;

            m_subparts.Clear();
            if (Subparts.TryGetValue("DoorLeft", out foundPart))
            {
                m_subparts.Add(foundPart);
            }
            if (Subparts.TryGetValue("DoorRight", out foundPart))
            {
                m_subparts.Add(foundPart);
            }
        }
예제 #7
0
 private bool LoadPropeller()
 {
     if (BlockDefinition.PropellerUse && BlockDefinition.PropellerEntity != null)
     {
         MyEntitySubpart propeller;
         if (Subparts.TryGetValue(BlockDefinition.PropellerEntity, out propeller))
         {
             m_propellerEntity       = propeller;
             m_propellerIdleRatio    = BlockDefinition.PropellerIdleSpeed / BlockDefinition.PropellerFullSpeed;
             m_propellerMaxDistance  = BlockDefinition.PropellerMaxDistance * BlockDefinition.PropellerMaxDistance;
             m_propellerAcceleration = (1f / BlockDefinition.PropellerAcceleration) * VRage.Game.MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS;
             m_propellerDeceleration = (1f / BlockDefinition.PropellerDeceleration) * VRage.Game.MyEngineConstants.UPDATE_STEP_SIZE_IN_SECONDS;
             return(true);
         }
     }
     return(false);
 }
예제 #8
0
        private void LoadSubparts()
        {
            DisposeSubpartsPhysics();

            if (!Subparts.TryGetValue("PistonSubpart1", out m_subpart1))
            {
                return;
            }
            if (!m_subpart1.Subparts.TryGetValue("PistonSubpart2", out m_subpart2))
            {
                return;
            }
            if (!m_subpart2.Subparts.TryGetValue("PistonSubpart3", out Subpart3))
            {
                return;
            }

            MyModelDummy dummy;

            if (Subpart3.Model.Dummies.TryGetValue("TopBlock", out dummy))
            {
                m_constraintBasePos = dummy.Matrix.Translation;
            }
            if (Model.Dummies.TryGetValue("subpart_PistonSubpart1", out dummy))
            {
                m_subpartsConstraintPos = dummy.Matrix.Translation;
                m_subpart1LocPos        = m_subpartsConstraintPos;
            }
            if (m_subpart1.Model.Dummies.TryGetValue("subpart_PistonSubpart2", out dummy))
            {
                m_subpart2LocPos = dummy.Matrix.Translation;
            }
            if (m_subpart2.Model.Dummies.TryGetValue("subpart_PistonSubpart3", out dummy))
            {
                m_subpart3LocPos = dummy.Matrix.Translation;
            }


            if (!CubeGrid.CreatePhysics)
            {
                return;
            }

            InitSubpartsPhysics();
        }
예제 #9
0
        private void InitSubparts()
        {
            if (!CubeGrid.CreatePhysics)
            {
                return;
            }

            Subparts.TryGetValue("DoorLeft", out m_leftSubpart);
            Subparts.TryGetValue("DoorRight", out m_rightSubpart);

            UpdateSlidingDoorsPosition();

            if (CubeGrid.Projector != null)
            {
                //This is a projected grid, don't add collisions for subparts
                return;
            }

            if (m_leftSubpart != null && m_leftSubpart.Physics == null)
            {
                if ((m_leftSubpart.ModelCollision.HavokCollisionShapes != null) && (m_leftSubpart.ModelCollision.HavokCollisionShapes.Length > 0))
                {
                    var shape = m_leftSubpart.ModelCollision.HavokCollisionShapes[0];
                    m_leftSubpart.Physics           = new Engine.Physics.MyPhysicsBody(m_leftSubpart, RigidBodyFlag.RBF_KINEMATIC);
                    m_leftSubpart.Physics.IsPhantom = false;
                    Vector3 center = new Vector3(0.35f, 0f, 0f) + m_leftSubpart.PositionComp.LocalVolume.Center;
                    m_leftSubpart.GetPhysicsBody().CreateFromCollisionObject(shape, center, WorldMatrix, null, MyPhysics.CollisionLayers.KinematicDoubledCollisionLayer);
                    m_leftSubpart.Physics.Enabled = true;
                }
            }

            if (m_rightSubpart != null && m_rightSubpart.Physics == null)
            {
                if ((m_rightSubpart.ModelCollision.HavokCollisionShapes != null) && (m_rightSubpart.ModelCollision.HavokCollisionShapes.Length > 0))
                {
                    var shape = m_rightSubpart.ModelCollision.HavokCollisionShapes[0];
                    m_rightSubpart.Physics           = new Engine.Physics.MyPhysicsBody(m_rightSubpart, RigidBodyFlag.RBF_KINEMATIC);
                    m_rightSubpart.Physics.IsPhantom = false;
                    Vector3 center = new Vector3(-0.35f, 0f, 0f) + m_rightSubpart.PositionComp.LocalVolume.Center;
                    m_rightSubpart.GetPhysicsBody().CreateFromCollisionObject(shape, center, WorldMatrix, null, MyPhysics.CollisionLayers.KinematicDoubledCollisionLayer);
                    m_rightSubpart.Physics.Enabled = true;
                }
            }
        }
        protected override void FillSubparts()
        {
            int             i        = 1;
            StringBuilder   partName = new StringBuilder();
            MyEntitySubpart foundPart;

            m_subparts.Clear();
            while (true)
            {
                partName.Clear().Append("HangarDoor_door").Append(i++);
                Subparts.TryGetValue(partName.ToString(), out foundPart);
                if (foundPart == null)
                {
                    break;
                }
                m_subparts.Add(foundPart);
            }
            ;
        }
예제 #11
0
        /// <summary></summary>
        /// <param name="c"></param>
        /// <param name="section_info">Can be null if tag data doesn't have it</param>
        /// <param name="gbi"></param>
        /// <returns></returns>
        internal bool Reconstruct(Blam.CacheFile c,
                                  global_geometry_section_info_struct section_info,
                                  geometry_block_info_struct gbi)
        {
            int index = 0;
            int x;

            byte[][] data = gbi.GeometryBlock;

            if (data == null)
            {
                return(false);
            }

            foreach (geometry_block_resource_block gb in gbi.Resources)
            {
                using (IO.EndianReader er = new BlamLib.IO.EndianReader(data[index]))
                {
                    switch (gb.Type.Value)
                    {
                        #region TagBlock
                    case (int)geometry_block_resource_type.TagBlock:
                        int count = gb.GetCount();
                        switch (gb.PrimaryLocater.Value)
                        {
                        case OffsetParts:
                            Parts.Resize(count);
                            Parts.Read(er);
                            break;

                        case OffsetSubparts:
                            Subparts.Resize(count);
                            Subparts.Read(er);
                            break;

                        case OffsetVisibilityBounds:
                            VisibilityBounds.Resize(count);
                            VisibilityBounds.Read(er);
                            break;

                        case OffsetStripIndices:
                            StripIndices.Resize(count);
                            StripIndices.Read(er);
                            break;

                        case OffsetMoppReorderTable:
                            MoppReorderTable.Resize(count);
                            MoppReorderTable.Read(er);
                            break;

                        case OffsetVertexBuffers:
                            VertexBuffers.Resize(count);
                            VertexBuffers.Read(er);
                            break;
                        }
                        break;

                        #endregion
                        #region TagData
                    case (int)geometry_block_resource_type.TagData:
                        switch (gb.PrimaryLocater.Value)
                        {
                        case OffsetVisibilityMoppCode:
                            VisibilityMoppCode.Reset(er.ReadBytes(gb.Size));
                            break;
                        }
                        break;

                        #endregion
                        #region VertexBuffer
                    case (int)geometry_block_resource_type.VertexBuffer:
                        var vb_defs = (c.TagIndexManager as InternalCacheTagIndex).kVertexBuffers;

                        var stream_readers = new Render.VertexBufferInterface.StreamReader[VertexBuffers.Count];
                        for (x = 0; x < VertexBuffers.Count; x++)
                        {
                            VertexBuffers[x].VertexBuffer.InitializeStreamReader(vb_defs, out stream_readers[x]);
                        }

                        if (RawVertices.Count == 0)
                        {
                            int vertex_count = section_info != null ?
                                               section_info.TotalVertexCount :
                                               gb.Size.Value / VertexBuffers[0].VertexBuffer.StrideSize;

                            RawVertices.Resize(vertex_count);
                        }

                        for (x = 0; x < RawVertices.Count; x++)
                        {
                            RawVertices[x].Reconstruct(section_info, gb,
                                                       er, stream_readers);
                        }
                        break;
                        #endregion
                    }
                }

                index++;
            }

            VertexBuffers.DeleteAll();

            return(true);
        }
예제 #12
0
        public void RefreshModels(string model, string modelCollision)
        {
            if (model != null)
            {
                Render.ModelStorage            = MyModels.GetModelOnlyData(model);
                PositionComp.LocalVolumeOffset = Render.GetModel().BoundingSphere.Center;
            }
            if (modelCollision != null)
            {
                m_modelCollision = MyModels.GetModelOnlyData(modelCollision);
            }

            if (Render.ModelStorage != null)
            {
                this.PositionComp.LocalAABB = Render.GetModel().BoundingBox;

                bool idAllocationState = MyEntityIdentifier.AllocationSuspended;
                try
                {
                    MyEntityIdentifier.AllocationSuspended = false;

                    if (Subparts == null)
                    {
                        Subparts = new Dictionary <string, MyEntitySubpart>();
                    }
                    else
                    {
                        foreach (var existingSubpart in Subparts)
                        {
                            Hierarchy.RemoveChild(existingSubpart.Value);
                            existingSubpart.Value.Close();
                        }
                        Subparts.Clear();
                    }

                    MyEntitySubpart.Data data = new MyEntitySubpart.Data();
                    foreach (var dummy in Render.GetModel().Dummies)
                    {
                        // Check of mirrored matrix of dummy object is under fake, because characters have mirrored dummies
                        if (MyFakes.ENABLE_DUMMY_MIRROR_MATRIX_CHECK)
                        {
                            // This should not be here but if you want to check bad matrices of other types
                            if (!(this is MyCharacter))
                            {
                                Debug.Assert(!dummy.Value.Matrix.IsMirrored());
                            }
                        }

                        if (!MyEntitySubpart.GetSubpartFromDummy(model, dummy.Key, dummy.Value, ref data))
                        {
                            continue;
                        }

                        MyEntitySubpart subpart = new MyEntitySubpart();
                        subpart.Render.EnableColorMaskHsv = Render.EnableColorMaskHsv;
                        subpart.Render.ColorMaskHsv       = Render.ColorMaskHsv;
                        subpart.Init(null, data.File, this, null);
                        subpart.PositionComp.LocalMatrix = data.InitialTransform;
                        Subparts[data.Name] = subpart;

                        if (InScene)
                        {
                            subpart.OnAddedToScene(this);
                        }
                    }
                }
                finally
                {
                    MyEntityIdentifier.AllocationSuspended = idAllocationState;
                }

                if (Render.GetModel().GlassData != null)
                {
                    Render.NeedsDraw           = true;
                    Render.NeedsDrawFromParent = true;
                }
            }
            else
            {   //entities without model has box with side length = 1 by default
                float defaultBoxHalfSize = 0.5f;
                this.PositionComp.LocalAABB = new BoundingBox(new Vector3(-defaultBoxHalfSize), new Vector3(defaultBoxHalfSize));
            }
        }