public MyObjectBuilder_EntityBase BuildEntity()
{
var asteroidCenter = new VRageMath.Vector3D();
var asteroidSize = new Vector3I();
string originalFile = null;
if (IsStockVoxel)
{
var stockfile = StockVoxel.SourceFilename;
if (StockMaterial == null || StockMaterial.Value == null)
{
SourceFile = stockfile;
originalFile = SourceFile;
var asteroid = new MyVoxelMap();
asteroid.Load(stockfile, SpaceEngineersCore.Resources.GetDefaultMaterialName(), false);
asteroidCenter = asteroid.BoundingContent.Center;
asteroidSize = asteroid.BoundingContent.SizeInt() + 1; // Content size
}
else
{
var asteroid = new MyVoxelMap();
asteroid.Load(stockfile, StockMaterial.Value);
asteroid.ForceBaseMaterial(SpaceEngineersCore.Resources.GetDefaultMaterialName(), StockMaterial.Value);
SourceFile = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension);
asteroid.Save(SourceFile);
originalFile = StockVoxel.SourceFilename;
asteroidCenter = asteroid.BoundingContent.Center;
asteroidSize = asteroid.BoundingContent.SizeInt() + 1; // Content size
}
}
else if (IsFileVoxel)
{
originalFile = SourceFile;
var asteroid = new MyVoxelMap();
asteroid.Load(SourceFile, SpaceEngineersCore.Resources.GetDefaultMaterialName(), false);
asteroidCenter = asteroid.BoundingContent.Center;
asteroidSize = asteroid.BoundingContent.SizeInt() + 1; // Content size
if (StockMaterial != null && StockMaterial.Value != null)
{
asteroid.ForceBaseMaterial(SpaceEngineersCore.Resources.GetDefaultMaterialName(), StockMaterial.Value);
SourceFile = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension);
asteroid.Save(SourceFile);
}
}
else if (IsSphere)
{
string material;
if (StockMaterial != null && StockMaterial.Value != null)
material = StockMaterial.Value;
else
material = SpaceEngineersCore.Resources.GetDefaultMaterialName();
originalFile = string.Format("sphere_{0}_{1}_{2}{3}", material.ToLowerInvariant(), SphereRadius, SphereShellRadius, MyVoxelMap.V2FileExtension);
var asteroid = MyVoxelBuilder.BuildAsteroidSphere(SphereRadius > 32, SphereRadius, material, material, SphereShellRadius != 0, SphereShellRadius);
// TODO: progress bar.
asteroidCenter = asteroid.BoundingContent.Center;
asteroidSize = asteroid.BoundingContent.SizeInt() + 1; // Content size
SourceFile = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension);
asteroid.Save(SourceFile);
}
// automatically number all files, and check for duplicate filenames.
Filename = MainViewModel.CreateUniqueVoxelStorageName(originalFile);
// Figure out where the Character is facing, and plant the new constrcut right in front.
// Calculate the hypotenuse, as it will be the safest distance to place in front.
double distance = Math.Sqrt(Math.Pow(asteroidSize.X, 2) + Math.Pow(asteroidSize.Y, 2) + Math.Pow(asteroidSize.Z, 2)) / 2;
var vector = new BindableVector3DModel(_dataModel.CharacterPosition.Forward).Vector3D;
vector.Normalize();
vector = System.Windows.Media.Media3D.Vector3D.Multiply(vector, distance);
Position = new BindablePoint3DModel(Point3D.Add(new BindablePoint3DModel(_dataModel.CharacterPosition.Position).Point3D, vector));
//Forward = new BindableVector3DModel(_dataModel.CharacterPosition.Forward);
//Up = new BindableVector3DModel(_dataModel.CharacterPosition.Up);
Forward = new BindableVector3DModel(Vector3.Forward); // Asteroids currently don't have any orientation.
Up = new BindableVector3DModel(Vector3.Up);
var entity = new MyObjectBuilder_VoxelMap
{
EntityId = SpaceEngineersApi.GenerateEntityId(IDType.ASTEROID),
PersistentFlags = MyPersistentEntityFlags2.CastShadows | MyPersistentEntityFlags2.InScene,
StorageName = Path.GetFileNameWithoutExtension(Filename),
PositionAndOrientation = new MyPositionAndOrientation
{
Position = Position.ToVector3D() - asteroidCenter,
Forward = Forward.ToVector3(),
Up = Up.ToVector3()
}
};
return entity;
}