예제 #1
0
        protected virtual void InitSubBlocks()
        {
            if (!MyFakes.ENABLE_SUBBLOCKS)
            {
                return;
            }

            if (m_subBlocksInitialized)
            {
                return;
            }

            //  if (!Sync.IsServer)
            //    return;

            try
            {
                MyCubeBlockDefinition subBlockDefinition;
                MatrixD subBlockMatrix;
                Vector3 dummyPosition;

                var finalModel = Sandbox.Engine.Models.MyModels.GetModelOnlyDummies(BlockDefinition.Model);
                foreach (var dummy in finalModel.Dummies)
                {
                    if (!MyCubeBlock.GetSubBlockDataFromDummy(BlockDefinition, dummy.Key, dummy.Value, true, out subBlockDefinition, out subBlockMatrix, out dummyPosition))
                    {
                        continue;
                    }

                    string dummyName = dummy.Key.Substring(DUMMY_SUBBLOCK_ID.Length);

                    MySlimBlock        subblock = null;
                    MyCubeGrid         subgrid  = null;
                    MySubBlockLoadInfo subBlockLoadInfo;
                    if (m_subBlockIds.TryGetValue(dummyName, out subBlockLoadInfo))
                    {
                        MyEntity entity;
                        if (MyEntities.TryGetEntityById(subBlockLoadInfo.GridId, out entity))
                        {
                            subgrid = entity as MyCubeGrid;
                            if (subgrid != null)
                            {
                                subblock = subgrid.GetCubeBlock(subBlockLoadInfo.SubBlockPosition);
                                Debug.Assert(subblock != null, "Cannot find subblock in subgrid!");
                                if (subblock == null)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                Debug.Assert(false, "Loaded entity is not grid!");
                                continue;
                            }
                        }
                        else
                        {
                            Debug.Assert(false, "Cannot load subgrid!");
                            continue;
                        }
                    }

                    if (!m_subBlocksLoaded)
                    {
                        if (subgrid == null)
                        {
                            Debug.Assert(!subBlockMatrix.IsMirrored());
                            Matrix subGridWorldMatrix = subBlockMatrix * PositionComp.LocalMatrix * CubeGrid.WorldMatrix;

                            //TODO: Try to find better way how to sync entity ID of subblocks..
                            subgrid = MyCubeBuilder.SpawnDynamicGrid(subBlockDefinition, subGridWorldMatrix, EntityId + SubBlocks.Count + 1);
                            if (subgrid != null)
                            {
                                subblock = subgrid.GetCubeBlock(Vector3I.Zero);
                            }
                        }

                        if (subgrid == null)
                        {
                            Debug.Assert(false, "SubGrid has not been set!");
                            continue;
                        }

                        if (subblock == null || subblock.FatBlock == null)
                        {
                            Debug.Assert(false, "Fatblock cannot be null for subblocks!");
                            continue;
                        }
                    }

                    if (subblock != null)
                    {
                        SubBlocks.Add(dummyName, subblock);
                        subblock.FatBlock.SubBlockName = dummyName;
                        subblock.FatBlock.OwnerBlock   = SlimBlock;
                        subblock.FatBlock.OnClosing   += SubBlock_OnClosing;
                        Debug.Assert(SlimBlock != null);
                    }
                }
            }
            finally
            {
                m_subBlockIds.Clear();

                m_subBlocksInitialized = true;
            }
        }