// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 using System; using OpenTK; #if USE16_BIT using BlockID = System.UInt16; #else using BlockID = System.Byte; #endif namespace ClassicalSharp { /// Describes the picked/selected block by the user and its position. public class PickedPos { /// Minimum world coordinates of the block's bounding box. public Vector3 Min; /// Maximum world coordinates of the block's bounding box. public Vector3 Max; /// Exact world coordinates at which the ray intersected this block. public Vector3 Intersect; /// Integer world coordinates of the block. public Vector3I BlockPos; /// Integer world coordinates of the neighbouring block that is closest to the player. public Vector3I TranslatedPos { get { return BlockPos + offsets[(byte)Face]; } } /// Whether this instance actually has a selected block currently. public bool Valid = true; /// Face of the picked block that is closet to the player. public BlockFace Face; /// Block ID of the picked block. public BlockID Block; /// Mark this as having a selected block, and /// calculates the closest face of the selected block's position. public void SetAsValid(int x, int y, int z, Vector3 min, Vector3 max, BlockID block, Vector3 intersect) { Min = min; Max = max; BlockPos = new Vector3I(x, y, z); Valid = true; Block = block; Intersect = intersect; float dist = float.PositiveInfinity; TestAxis(intersect.X - Min.X, ref dist, BlockFace.XMin); TestAxis(intersect.X - Max.X, ref dist, BlockFace.XMax); TestAxis(intersect.Y - Min.Y, ref dist, BlockFace.YMin); TestAxis(intersect.Y - Max.Y, ref dist, BlockFace.YMax); TestAxis(intersect.Z - Min.Z, ref dist, BlockFace.ZMin); TestAxis(intersect.Z - Max.Z, ref dist, BlockFace.ZMax); } /// Mark this as not having a selected block. public void SetAsInvalid() { Valid = false; BlockPos = Vector3I.MinusOne; Face = (BlockFace)6; Block = 0; } void TestAxis(float dAxis, ref float dist, BlockFace fAxis) { dAxis = Math.Abs(dAxis); if (dAxis >= dist) return; dist = dAxis; Face = fAxis; } static Vector3I[] offsets = new Vector3I[] { Vector3I.UnitX, -Vector3I.UnitX, Vector3I.UnitY, -Vector3I.UnitY, Vector3I.UnitZ, -Vector3I.UnitZ, Vector3I.Zero, }; } }