public override EntityPosition GetPosition(IDynamicEntity owner) { var pos = new EntityPosition(); if (!AllowFreeMount && !owner.EntityState.IsBlockPicked) { return(pos); } if (!MountPoint.HasFlag(BlockFace.Top) && owner.EntityState.PickPointNormal.Y == 1) { return(pos); } if (!MountPoint.HasFlag(BlockFace.Bottom) && owner.EntityState.PickPointNormal.Y == -1) { return(pos); } if (!MountPoint.HasFlag(BlockFace.Sides) && (owner.EntityState.PickPointNormal.X != 0 || owner.EntityState.PickPointNormal.Z != 0)) { return(pos); } if (BlockEmptyRequired && owner.EntityState.IsBlockPicked) { //Get the chunk where the entity will be added and check if another entity is not present at the destination root block ! var workingchunk = LandscapeManager.GetChunkFromBlock(owner.EntityState.PickedBlockPosition); if (workingchunk == null) { return(pos); } foreach (var entity in workingchunk.Entities.OfType <BlockLinkedItem>().Where(x => x.Linked && x.LinkedCube == owner.EntityState.PickedBlockPosition)) { if (entity.BlockLocationRoot == owner.EntityState.NewBlockPosition && entity.LinkedCube == owner.EntityState.PickedBlockPosition) { //CubePlaced Entity already present at this location return(pos); } } } // locate the entity if (owner.EntityState.PickPointNormal.Y == 1) // = Put on TOP { if (BlockFaceCentered) { var newBlockPos = owner.EntityState.IsBlockPicked ? owner.EntityState.NewBlockPosition : owner.EntityState.PickPoint.ToCubePosition(); pos.Position = new Vector3D( newBlockPos + new Vector3(0.5f - (float)owner.EntityState.PickPointNormal.X / 2, owner.EntityState.PickPoint.Y % 1, 0.5f - (float)owner.EntityState.PickPointNormal.Z / 2) ); } else { pos.Position = new Vector3D(owner.EntityState.PickPoint); } } else if (owner.EntityState.PickPointNormal.Y == -1) //PUT on cube Bottom = (Ceiling) { pos.Position = new Vector3D(owner.EntityState.PickPoint); pos.Position.Y -= DefaultSize.Y; } else //Put on a side { if (BlockFaceCentered) { var newBlockPos = owner.EntityState.IsBlockPicked ? owner.EntityState.NewBlockPosition : owner.EntityState.PickPoint.ToCubePosition(); pos.Position = new Vector3D( newBlockPos + new Vector3(0.5f - (float)owner.EntityState.PickPointNormal.X / 2, 0.5f, 0.5f - (float)owner.EntityState.PickPointNormal.Z / 2) ); } else { pos.Position = new Vector3D(owner.EntityState.PickPoint); } pos.Position += new Vector3D(owner.EntityState.PickPointNormal.X == -1 ? -0.01 : 0, 0, owner.EntityState.PickPointNormal.Z == -1 ? -0.01 : 0); var slope = 0d; if (owner.EntityState.PickPointNormal.X == -1) { slope = -Math.PI / 2; } if (owner.EntityState.PickPointNormal.X == 1) { slope = Math.PI / 2; // ok } if (owner.EntityState.PickPointNormal.Z == -1) { slope = Math.PI; // ok } if (owner.EntityState.PickPointNormal.Z == 1) { slope = 0; } pos.Rotation = Quaternion.RotationAxis(new Vector3(0, 1, 0), (float)slope); } pos.Valid = true; return(pos); }
public override EntityPosition GetPosition(IDynamicEntity owner) { var pos = new EntityPosition(); if (!owner.EntityState.IsBlockPicked) { return(pos); } Vector3 faceOffset = BlockFaceCentered ? new Vector3(0.5f, 0.5f, 0.5f) : owner.EntityState.PickedBlockFaceOffset; var playerRotation = owner.HeadRotation.GetLookAtVector(); // locate the entity if (MountPoint.HasFlag(BlockFace.Top) && owner.EntityState.PickPointNormal.Y == 1) // = Put on TOP { pos.Position = new Vector3D(owner.EntityState.PickedBlockPosition.X + faceOffset.X, owner.EntityState.PickedBlockPosition.Y + 1f, owner.EntityState.PickedBlockPosition.Z + faceOffset.Z); } else if (MountPoint.HasFlag(BlockFace.Bottom) && owner.EntityState.PickPointNormal.Y == -1) // PUT on cube Bottom = (Ceiling) { pos.Position = new Vector3D(owner.EntityState.PickedBlockPosition.X + faceOffset.X, owner.EntityState.PickedBlockPosition.Y - 1f, owner.EntityState.PickedBlockPosition.Z + faceOffset.Z); } else if (MountPoint.HasFlag(BlockFace.Sides) && (owner.EntityState.PickPointNormal.X != 0 || owner.EntityState.PickPointNormal.Z != 0)) // Put on sides { if (BlockFaceCentered) { var newBlockPos = owner.EntityState.IsBlockPicked ? owner.EntityState.NewBlockPosition : owner.EntityState.PickPoint.ToCubePosition(); pos.Position = new Vector3D( newBlockPos + new Vector3(0.5f - (float)owner.EntityState.PickPointNormal.X / 2, 0f, 0.5f - (float)owner.EntityState.PickPointNormal.Z / 2) ); } else { pos.Position = new Vector3D(owner.EntityState.PickPoint); } pos.Position += new Vector3D(owner.EntityState.PickPointNormal.X == -1 ? -0.01 : 0, 0, owner.EntityState.PickPointNormal.Z == -1 ? -0.01 : 0); var slope = 0d; if (owner.EntityState.PickPointNormal.X == -1) { slope = -Math.PI / 2; } if (owner.EntityState.PickPointNormal.X == 1) { slope = Math.PI / 2; // ok } if (owner.EntityState.PickPointNormal.Z == -1) { slope = Math.PI; // ok } if (owner.EntityState.PickPointNormal.Z == 1) { slope = 0; } pos.Rotation = Quaternion.RotationAxis(new Vector3(0, 1, 0), (float)slope); pos.Valid = true; return(pos); } else { return(pos); } double entityRotation; if (Math.Abs(playerRotation.Z) >= Math.Abs(playerRotation.X)) { if (playerRotation.Z < 0) { entityRotation = MathHelper.Pi; pos.Orientation = ItemOrientation.North; } else { entityRotation = 0; pos.Orientation = ItemOrientation.South; } } else { if (playerRotation.X < 0) { entityRotation = MathHelper.PiOver2; pos.Orientation = ItemOrientation.East; } else { entityRotation = -MathHelper.PiOver2; pos.Orientation = ItemOrientation.West; } } // Specific Item Rotation for this instance pos.Rotation = Quaternion.RotationAxis(new Vector3(0, 1, 0), (float)entityRotation); pos.Valid = true; return(pos); }