private void PlaceAreaMarker() { Vector3D cameraPos, cameraDir; if (MySession.Static.GetCameraControllerEnum() == MyCameraControllerEnum.ThirdPersonSpectator || MySession.Static.GetCameraControllerEnum() == MyCameraControllerEnum.Entity) { var headMatrix = MySession.Static.ControlledEntity.GetHeadMatrix(true, true); cameraPos = headMatrix.Translation; cameraDir = headMatrix.Forward; } else { cameraPos = MySector.MainCamera.Position; cameraDir = MySector.MainCamera.WorldMatrix.Forward; } List <MyPhysics.HitInfo> hitInfos = new List <MyPhysics.HitInfo>(); MyPhysics.CastRay(cameraPos, cameraPos + cameraDir * 100, hitInfos, MyPhysics.CollisionLayers.ObjectDetectionCollisionLayer); if (hitInfos.Count == 0) { return; } MyPhysics.HitInfo?closestValidHit = null; foreach (var hitInfo in hitInfos) { var ent = hitInfo.HkHitInfo.GetHitEntity(); if (ent is MyCubeGrid) { closestValidHit = hitInfo; break; } else if (ent is MyVoxelMap) { closestValidHit = hitInfo; break; } } if (closestValidHit.HasValue) { MyAreaMarkerDefinition definition = AreaMarkerDefinition; Debug.Assert(definition != null, "Area marker definition cannot be null!"); if (definition == null) { return; } Vector3D position = closestValidHit.Value.Position; var forward = Vector3D.Reject(cameraDir, Vector3D.Up); if (Vector3D.IsZero(forward)) { forward = Vector3D.Forward; } var positionAndOrientation = new MyPositionAndOrientation(position, Vector3D.Normalize(forward), Vector3D.Up); MyObjectBuilder_AreaMarker objectBuilder = (MyObjectBuilder_AreaMarker)MyObjectBuilderSerializer.CreateNewObject(definition.Id); objectBuilder.PersistentFlags = MyPersistentEntityFlags2.Enabled | MyPersistentEntityFlags2.InScene; objectBuilder.PositionAndOrientation = positionAndOrientation; if (objectBuilder.IsSynced) { SerializableDefinitionId id = definition.Id; MyMultiplayer.RaiseStaticEvent(x => CreateNewPlaceArea, id, positionAndOrientation); } else { MyAreaMarker flag = MyEntityFactory.CreateEntity <MyAreaMarker>(objectBuilder); flag.Init(objectBuilder); MyEntities.Add(flag); } } }
private void PlaceAreaMarker() { Vector3D cameraPos, cameraDir; if (MySession.GetCameraControllerEnum() == Common.ObjectBuilders.MyCameraControllerEnum.ThirdPersonSpectator || MySession.GetCameraControllerEnum() == Common.ObjectBuilders.MyCameraControllerEnum.Entity) { var headMatrix = MySession.ControlledEntity.GetHeadMatrix(true, true); cameraPos = headMatrix.Translation; cameraDir = headMatrix.Forward; } else { cameraPos = MySector.MainCamera.Position; cameraDir = MySector.MainCamera.WorldMatrix.Forward; } List<MyPhysics.HitInfo> hitInfos = new List<MyPhysics.HitInfo>(); MyPhysics.CastRay(cameraPos, cameraPos + cameraDir * 100, hitInfos, MyPhysics.ObjectDetectionCollisionLayer); if (hitInfos.Count == 0) return; MyPhysics.HitInfo? closestValidHit = null; foreach (var hitInfo in hitInfos) { var ent = hitInfo.HkHitInfo.Body.GetEntity(); if (ent is MyCubeGrid) { closestValidHit = hitInfo; break; } else if (ent is MyVoxelMap) { closestValidHit = hitInfo; break; } } if (closestValidHit.HasValue) { Vector3D position = closestValidHit.Value.Position; MyAreaMarkerDefinition definition = AreaMarkerDefinition; //MyDefinitionManager.Static.TryGetDefinition(new MyDefinitionId(typeof(MyObjectBuilder_AreaMarkerDefinition), "ForestingArea"), out definition); m_tmpAreas.Clear(); MyPlaceAreas.GetAllAreas(m_tmpAreas); foreach (var area in m_tmpAreas) { if (area.AreaType == AreaMarkerDefinition.Id.SubtypeId) { area.Entity.Close(); } } m_tmpAreas.Clear(); Debug.Assert(definition != null, "Area marker definition cannot be null!"); if (definition == null) return; var forward = Vector3D.Reject(cameraDir, Vector3D.Up); if (Vector3D.IsZero(forward)) forward = Vector3D.Forward; var flag = new MyAreaMarker(new MyPositionAndOrientation(position, Vector3D.Normalize(forward), Vector3D.Up), definition); MyEntities.Add(flag); } }