コード例 #1
0
        /// <summary>
        /// Converts world position to be snapped to closest grid.
        /// </summary>
        /// <param name="worldPos">World position.</param>
        /// <param name="gridSize">Grid size.</param>
        /// <param name="staticGridAlignToCenter">Is grid align to static.</param>
        /// <returns></returns>
        public CoordSystemData SnapWorldPosToClosestGrid(ref Vector3D worldPos, double gridSize, bool staticGridAlignToCenter)
        {

            m_lastSelectedCoordSys = m_selectedCoordSys;

            MyLocalCoordSys localCoordSys = null;

            localCoordSys = this.GetClosestCoordSys(ref worldPos);

            // If no coord sys found, return origin(0,0,0) with no rotation!
            if (localCoordSys == null)
            {
                localCoordSys = new MyLocalCoordSys(m_coorsSystemSize);
                m_selectedCoordSys = 0;
            }
            else
                m_selectedCoordSys = localCoordSys.Id;

            if (m_selectedCoordSys == 0)
            {
                m_localCoordExist = false;
            }
            else
            {
                m_localCoordExist = true;
            }

            if (m_selectedCoordSys != m_lastSelectedCoordSys)
            {
                m_selectionChanged = true;
                if(OnCoordinateChange != null)
                    OnCoordinateChange();
            }
            else
                m_selectionChanged = false;

            //if (!m_localCoordExist && m_selectionChanged)
            //{
            //    this.Disable();
            //}
            //else if (m_selectionChanged && m_lastSelectedCoordSys == 0)
            //{
            //    this.Enable();
            //}

            CoordSystemData coordData = new CoordSystemData();

            Quaternion rotation = localCoordSys.Origin.Rotation;
            Quaternion invRotation = Quaternion.Inverse(rotation);
            Vector3D position = localCoordSys.Origin.Position;

            Vector3D vec = worldPos - position;
            vec = Vector3D.Transform(vec, invRotation);

            MyCoordinateSystem.GetPosRoundedToGrid(ref vec, gridSize, staticGridAlignToCenter);

            coordData.LocalSnappedPos = vec;

            vec = Vector3D.Transform(vec, rotation);

            MyTransformD localCoordsTransform = new MyTransformD();
            localCoordsTransform.Position = position + vec;
            localCoordsTransform.Rotation = rotation;

            coordData.SnappedTransform = localCoordsTransform;
            coordData.Origin = localCoordSys.Origin;

            return coordData;
        }
コード例 #2
0
        /// <summary>
        /// Registers cube grid under given local coord system.
        /// </summary>
        /// <param name="cubeGrid">Cube grid to register.</param>
        /// <param name="coordSys">Local coord system.</param>
        private void RegisterCubeGrid(MyCubeGrid cubeGrid, MyLocalCoordSys coordSys)
        {

            cubeGrid.OnClose += CubeGrid_OnClose;
            cubeGrid.OnPhysicsChanged += CubeGrid_OnPhysicsChanged;
            cubeGrid.LocalCoordSystem = coordSys.Id;
            coordSys.EntityConuter++;
        }
コード例 #3
0
 /// <summary>
 /// Only creates coord system. Call only on client in reaction on server create.
 /// </summary>
 /// <param name="transform">Origin of the coord system.</param>
 /// <param name="coordSysId">Coord system id that should be used in creation.</param>
 private void CreateCoordSys_ClientInternal(ref MyTransformD transform, long coordSysId)
 {
     MyLocalCoordSys localCoordSys = new MyLocalCoordSys(transform, m_coorsSystemSize);
     localCoordSys.Id = coordSysId;
     m_localCoordSystems.Add(coordSysId, localCoordSys);
 }
コード例 #4
0
        /// <summary>
        /// Creates coord system and sends it to clients. Should be called only on server.
        /// </summary>
        /// <param name="cubeGrid">Cube grid that is an origin.</param>
        /// <param name="staticGridAlignToCenter">Indcates if grid should be aligned to center or no.</param>
        public void CreateCoordSys(MyCubeGrid cubeGrid, bool staticGridAlignToCenter, bool sync = false)
        {
            //In me this system is not working for now (will change after implementing planets there)
            //if(MyPerGameSettings.Game == GameEnum.ME_GAME)
            //{
            //    return;
            //}
            
            Debug.Assert(Sync.IsServer, "Called on client. This method should be called only on server.");

            MyTransformD origin = new MyTransformD(cubeGrid.PositionComp.WorldMatrix);
            origin.Rotation.Normalize();
            float gridSize = cubeGrid.GridSize;

            if (!staticGridAlignToCenter)
            {
                origin.Position -= (origin.Rotation.Forward + origin.Rotation.Right + origin.Rotation.Up) * gridSize * 0.5f;
            }

            MyLocalCoordSys localCoordSys = new MyLocalCoordSys(origin, m_coorsSystemSize);
            long newId = m_lastCoordSysId++; // Just raise by one. There wont be so much id's for long to be overflooded.
            localCoordSys.Id = newId;
            m_localCoordSystems.Add(newId, localCoordSys);

            if (cubeGrid.LocalCoordSystem != 0)
                this.UnregisterCubeGrid(cubeGrid);

            this.RegisterCubeGrid(cubeGrid, localCoordSys);

            MyCreateCoordSysBuffer createCoordSysBuffer = new MyCreateCoordSysBuffer();
            createCoordSysBuffer.Position = origin.Position;
            createCoordSysBuffer.Rotation = origin.Rotation;
            createCoordSysBuffer.Id = newId;

            if(sync)
                MyMultiplayer.RaiseStaticEvent(x => CoordSysCreated_Client, createCoordSysBuffer);

        }
コード例 #5
0
        public override void Init(MyObjectBuilder_SessionComponent sessionComponent)
        {
            base.Init(sessionComponent);

            var coordSysBuilder = sessionComponent as MyObjectBuilder_CoordinateSystem;

            this.m_lastCoordSysId = coordSysBuilder.LastCoordSysId;
            
            foreach(var coordSys in coordSysBuilder.CoordSystems)
            {
                MyTransformD origin = new MyTransformD();
                origin.Position = coordSys.Position;
                origin.Rotation = coordSys.Rotation;

                MyLocalCoordSys newCoordSys = new MyLocalCoordSys(origin, m_coorsSystemSize);
                newCoordSys.Id = coordSys.Id;

                m_localCoordSystems.Add(coordSys.Id, newCoordSys);

            }

        }