Example #1
0
        public static Vector3 GetDirection(this VoxelSide side)
        {
            switch (side)
            {
            case VoxelSide.TOP:
                return(Vector3.UnitY);

            case VoxelSide.BOTTOM:
                return(-Vector3.UnitY);

            case VoxelSide.NORTH:
                return(-Vector3.UnitZ);

            case VoxelSide.SOUTH:
                return(Vector3.UnitZ);

            case VoxelSide.EAST:
                return(Vector3.UnitX);

            case VoxelSide.WEST:
                return(-Vector3.UnitX);

            default:
                return(Vector3.Zero);
            }
        }
Example #2
0
 public VoxelModifier(int x, int y, int z, VoxelSide side, VoxleModifierAction action)
 {
     this.x = x;
     this.y = y;
     this.z = z;
     this.side = side;
     this.action = action;
 }
Example #3
0
 public VoxelModifier(int x, int y, int z, VoxelSide side, VoxleModifierAction action)
 {
     this.x      = x;
     this.y      = y;
     this.z      = z;
     this.side   = side;
     this.action = action;
 }
Example #4
0
 public Voxel(VoxelWorldChunk parentChunk, VoxelType type, IntVec3 pos, BoxCollider collider)
 {
     ParentChunk = parentChunk;
     Collider    = collider;
     SetType(type);
     Exposed      = false;
     Position     = pos;
     ExposedSides = VoxelSide.All;
 }
Example #5
0
 public Voxel(VoxelWorldChunk parentChunk, VoxelType type, IntVec3 pos, BoxCollider collider)
 {
     ParentChunk = parentChunk;
     Collider = collider;
     SetType(type);
     Exposed = false;
     Position = pos;
     ExposedSides = VoxelSide.All;
 }
Example #6
0
        public void SetSide(float2 size, float2 position, VoxelSide side)
        {
            int uvPointer = 0;

            if (side == VoxelSide.Up)
            {
                uvPointer = 0;
            }
            else if (side == VoxelSide.Down)
            {
                uvPointer += 4;
            }
            // up
            else if (side == VoxelSide.Left)
            {
                uvPointer += 8;
            }
            // down
            else if (side == VoxelSide.Right)
            {
                uvPointer += 12;
            }
            if (side == VoxelSide.Forward)
            {
                uvPointer += 16;
            }
            if (side == VoxelSide.Back)
            {
                uvPointer += 20;
            }
            if (side == VoxelSide.Left || side == VoxelSide.Right)
            {
                // reverse
                uvs[uvPointer + 3] = new float2(position.x, position.y);
                uvs[uvPointer]     = new float2(position.x + size.x, position.y);
                uvs[uvPointer + 1] = new float2(position.x + size.x, position.y + size.y);
                uvs[uvPointer + 2] = new float2(position.x, position.y + size.y);

                /*
                 * uvs[uvPointer + 3] = float2.zero;
                 * uvs[uvPointer] = float2.zero;
                 * uvs[uvPointer + 1] = float2.zero;
                 * uvs[uvPointer + 2] = float2.zero;*/
            }
            else
            {
                uvs[uvPointer]     = new float2(position.x, position.y);
                uvs[uvPointer + 1] = new float2(position.x + size.x, position.y);
                uvs[uvPointer + 2] = new float2(position.x + size.x, position.y + size.y);
                uvs[uvPointer + 3] = new float2(position.x, position.y + size.y);
            }
        }
 public static bool OnTryChangeBlockUser(ModLoader.OnTryChangeBlockUserData userData)
 {
     if (!userData.isPrimaryAction)
     {
         VoxelSide side = userData.voxelHitSide;
         string    suffix;
         if (side == VoxelSide.xPlus)
         {
             suffix = "right";
         }
         else if (side == VoxelSide.xMin)
         {
             suffix = "left";
         }
         else if (side == VoxelSide.yPlus)
         {
             suffix = "bottom";
         }
         else if (side == VoxelSide.yMin)
         {
             suffix = "top";
         }
         else if (side == VoxelSide.zPlus)
         {
             suffix = "front";
         }
         else if (side == VoxelSide.zMin)
         {
             suffix = "back";
         }
         else
         {
             return(true);
         }
         ushort newType = userData.typeToBuild;
         string typename;
         if (newType != userData.typeTillNow && ItemTypes.IndexLookup.TryGetName(newType, out typename))
         {
             string otherTypename = typename + suffix;
             ushort otherIndex;
             if (ItemTypes.IndexLookup.TryGetIndex(otherTypename, out otherIndex))
             {
                 Vector3Int position = userData.VoxelToChange;
                 ThreadManager.InvokeOnMainThread(delegate() {
                     ServerManager.TryChangeBlock(position, otherIndex, ServerManager.SetBlockFlags.DefaultAudio);
                 }, 0.1f);
             }
         }
     }
     return(true);
 }
Example #8
0
 public VoxelRay(Vector3 source, Vector3 direction)
 {
     this.source   = source;
     SourceVoxel   = new Pipliz.Vector3Int(source);
     NextVoxel     = SourceVoxel;
     dirNormalized = direction;
     tDelta.x      = ((dirNormalized.x != 0f) ? Pipliz.Math.Abs(1f / dirNormalized.x) : 1E+07f);
     tDelta.y      = ((dirNormalized.y != 0f) ? Pipliz.Math.Abs(1f / dirNormalized.y) : 1E+07f);
     tDelta.z      = ((dirNormalized.z != 0f) ? Pipliz.Math.Abs(1f / dirNormalized.z) : 1E+07f);
     tMax.x        = tMaxHelper(source.x, dirNormalized.x);
     tMax.y        = tMaxHelper(source.y, dirNormalized.y);
     tMax.z        = tMaxHelper(source.z, dirNormalized.z);
     LastDirMin    = VoxelSide.None;
 }
Example #9
0
        public static void OnTryChangeBlockUser(ModLoader.OnTryChangeBlockData userData)
        {
            if (userData.CallbackState == ModLoader.OnTryChangeBlockData.ECallbackState.Cancelled)
            {
                return;
            }

            if (userData.TypeNew == BuiltinBlocks.Dirt)
            {
                if (ItemTypes.TryGetType(userData.TypeOld, out ItemTypes.ItemType itemtype))
                {
                    if (itemtype.IsFertile && itemtype.CustomDataNode.TryGetAs <float>("fertilizervalue", out float result))
                    {
                        userData.TypeNew = userData.TypeOld;
                    }
                }
            }

            if (userData.CallbackOrigin == ModLoader.OnTryChangeBlockData.ECallbackOrigin.ClientPlayerManual)
            {
                VoxelSide side    = userData.PlayerClickedData.VoxelSideHit;
                ushort    newType = userData.TypeNew;
                string    suffix  = "bottom";

                switch (side)
                {
                case VoxelSide.yPlus:
                    suffix = "bottom";
                    break;

                case VoxelSide.yMin:
                    suffix = "top";
                    break;
                }

                if (newType != userData.TypeOld && ItemTypes.IndexLookup.TryGetName(newType, out string typename))
                {
                    string otherTypename = typename + suffix;

                    if (ItemTypes.IndexLookup.TryGetIndex(otherTypename, out ushort otherIndex))
                    {
                        Vector3Int position = userData.Position;
                        ThreadManager.InvokeOnMainThread(delegate()
                        {
                            ServerManager.TryChangeBlock(position, otherIndex);
                        }, 0.1f);
                    }
                }
            }
        }
Example #10
0
            public bool WalkNextVoxel(float maxDistance)
            {
                float min = Pipliz.Math.Min(tMax.x, tMax.y, tMax.z);

                if (tMax.x == min)
                {
                    int sign = (dirNormalized.x >= 0f) ? 1 : (-1);
                    if (((float)NextVoxel.x - source.x - 0.5f * (float)sign) * (float)sign * tDelta.x > maxDistance)
                    {
                        return(false);
                    }
                    NextVoxel.x += sign;
                    tMax.x      += tDelta.x;
                    LastDirMin   = VoxelSide.xMin;
                }
                else if (tMax.y == min)
                {
                    int sign2 = (dirNormalized.y >= 0f) ? 1 : (-1);
                    if (((float)NextVoxel.y - source.y - 0.5f * (float)sign2) * (float)sign2 * tDelta.y > maxDistance)
                    {
                        return(false);
                    }
                    NextVoxel.y += sign2;
                    tMax.y      += tDelta.y;
                    LastDirMin   = VoxelSide.yMin;
                }
                else
                {
                    int sign3 = (dirNormalized.z >= 0f) ? 1 : (-1);
                    if (((float)NextVoxel.z - source.z - 0.5f * (float)sign3) * (float)sign3 * tDelta.z > maxDistance)
                    {
                        return(false);
                    }
                    NextVoxel.z += sign3;
                    tMax.z      += tDelta.z;
                    LastDirMin   = VoxelSide.zMin;
                }
                return(true);
            }
Example #11
0
        private Vector2 GetTexCoords(Voxel voxel, VoxelSide side)
        {
            var type = _types[voxel.BlockType];

            switch (voxel.Orientation)
            {
            case VoxelSide.TOP:
                return
                    (side == VoxelSide.TOP ? type.TopTexCoords :
                     side == VoxelSide.BOTTOM ? type.BottomTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.BOTTOM:
                return
                    (side == VoxelSide.TOP ? type.BottomTexCoords :
                     side == VoxelSide.BOTTOM ? type.TopTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.NORTH:
                return
                    (side == VoxelSide.TOP ? type.SouthTexCoords :
                     side == VoxelSide.BOTTOM ? type.NorthTexCoords :
                     side == VoxelSide.NORTH ? type.TopTexCoords :
                     side == VoxelSide.SOUTH ? type.BottomTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.SOUTH:
                return
                    (side == VoxelSide.TOP ? type.NorthTexCoords :
                     side == VoxelSide.BOTTOM ? type.SouthTexCoords :
                     side == VoxelSide.NORTH ? type.BottomTexCoords :
                     side == VoxelSide.SOUTH ? type.TopTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.EAST:
                return
                    (side == VoxelSide.TOP ? type.WestTexCoords :
                     side == VoxelSide.BOTTOM ? type.EastTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.TopTexCoords :
                     side == VoxelSide.WEST ? type.BottomTexCoords : type.TopTexCoords);

            case VoxelSide.WEST:
                return
                    (side == VoxelSide.TOP ? type.EastTexCoords :
                     side == VoxelSide.BOTTOM ? type.WestTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.BottomTexCoords :
                     side == VoxelSide.WEST ? type.TopTexCoords : type.TopTexCoords);

            default:
                return(type.TopTexCoords);
            }
        }
Example #12
0
 public GateState(GatePosition state, VoxelSide rotation, Vector3Int pos)
 {
     State       = state;
     Orientation = rotation;
     Position    = pos;
 }
Example #13
0
            public bool Intersects(BoundsPip bounds, out float dist, out VoxelSide hitSide)
            {
                dist    = 0f;
                hitSide = VoxelSide.None;
                float txmin;
                float tmin = txmin = (bounds.Min.x - source.x) / dirNormalized.x;
                float txmax;
                float tmax = txmax = (bounds.Max.x - source.x) / dirNormalized.x;

                if (tmin > tmax)
                {
                    Helper.Swap(ref tmin, ref tmax);
                }
                float tymin = (bounds.Min.y - source.y) / dirNormalized.y;
                float tymax = (bounds.Max.y - source.y) / dirNormalized.y;

                if (tymin > tymax)
                {
                    Helper.Swap(ref tymin, ref tymax);
                }
                if (tmin > tymax || tymin > tmax)
                {
                    return(false);
                }
                if (tymin > tmin)
                {
                    tmin = tymin;
                }
                if (tymax < tmax)
                {
                    tmax = tymax;
                }
                float tzmin = (bounds.Min.z - source.z) / dirNormalized.z;
                float tzmax = (bounds.Max.z - source.z) / dirNormalized.z;

                if (tzmin > tzmax)
                {
                    Helper.Swap(ref tzmin, ref tzmax);
                }
                if (tmin > tzmax || tzmin > tmax)
                {
                    return(false);
                }
                if (tzmin > tmin)
                {
                    tmin = tzmin;
                }
                if (tzmax < tmax)
                {
                }
                if (tmin < 0f)
                {
                    return(false);
                }
                dist = tmin;
                if (tmin == txmin || tmin == txmax)
                {
                    hitSide = ((dirNormalized.x < 0f) ? VoxelSide.xPlus : VoxelSide.xMin);
                }
                else if (tmin == tymin || tmin == tymax)
                {
                    hitSide = ((dirNormalized.y < 0f) ? VoxelSide.yPlus : VoxelSide.yMin);
                }
                else
                {
                    hitSide = ((dirNormalized.z < 0f) ? VoxelSide.zPlus : VoxelSide.zMin);
                }
                return(true);
            }
Example #14
0
 public static Quaternion GetQuaternion(this VoxelSide side)
 {
     return(new Quaternion(GetDirection(side), 1));
 }
Example #15
0
        private static void AddBlock(Action <Voxel, VoxelSide, Quad> vertexProcessor, Voxel voxel, float x, float y, float z, VoxelSide side, float voxelSize)
        {
            switch (side)
            {
            case VoxelSide.BOTTOM:
                vertexProcessor(voxel, side, new Quad(new Vector3(x, y, z) * voxelSize, new Vector3(x, y, z + 1) * voxelSize, new Vector3(x + 1, y, z + 1) * voxelSize, new Vector3(x + 1, y, z) * voxelSize, -Vector3.UnitY));
                break;

            case VoxelSide.EAST:
                vertexProcessor(voxel, side, new Quad(new Vector3(x + 1, y, z + 1) * voxelSize, new Vector3(x + 1, y + 1, z + 1) * voxelSize, new Vector3(x + 1, y + 1, z) * voxelSize, new Vector3(x + 1, y, z) * voxelSize, Vector3.UnitX));
                break;

            case VoxelSide.WEST:
                vertexProcessor(voxel, side, new Quad(new Vector3(x, y, z) * voxelSize, new Vector3(x, y + 1, z) * voxelSize, new Vector3(x, y + 1, z + 1) * voxelSize, new Vector3(x, y, z + 1) * voxelSize, -Vector3.UnitX));
                break;

            case VoxelSide.TOP:
                vertexProcessor(voxel, side, new Quad(new Vector3(x, y + 1, z + 1) * voxelSize, new Vector3(x, y + 1, z) * voxelSize, new Vector3(x + 1, y + 1, z) * voxelSize, new Vector3(x + 1, y + 1, z + 1) * voxelSize, Vector3.UnitY));
                break;

            case VoxelSide.NORTH:
                vertexProcessor(voxel, side, new Quad(new Vector3(x + 1, y, z) * voxelSize, new Vector3(x + 1, y + 1, z) * voxelSize, new Vector3(x, y + 1, z) * voxelSize, new Vector3(x, y, z) * voxelSize, -Vector3.UnitZ));
                break;

            case VoxelSide.SOUTH:
                vertexProcessor(voxel, side, new Quad(new Vector3(x, y, z + 1) * voxelSize, new Vector3(x, y + 1, z + 1) * voxelSize, new Vector3(x + 1, y + 1, z + 1) * voxelSize, new Vector3(x + 1, y, z + 1) * voxelSize, Vector3.UnitZ));
                break;
            }
        }
Example #16
0
        private Vector2 GetTexCoords(VoxelType type, VoxelSide orientation, VoxelSide side)
        {
            switch (orientation)
            {
            case VoxelSide.TOP:
                return
                    (side == VoxelSide.TOP ? type.TopTexCoords :
                     side == VoxelSide.BOTTOM ? type.BottomTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.BOTTOM:
                return
                    (side == VoxelSide.TOP ? type.BottomTexCoords :
                     side == VoxelSide.BOTTOM ? type.TopTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.NORTH:
                return
                    (side == VoxelSide.TOP ? type.SouthTexCoords :
                     side == VoxelSide.BOTTOM ? type.NorthTexCoords :
                     side == VoxelSide.NORTH ? type.TopTexCoords :
                     side == VoxelSide.SOUTH ? type.BottomTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.SOUTH:
                return
                    (side == VoxelSide.TOP ? type.NorthTexCoords :
                     side == VoxelSide.BOTTOM ? type.SouthTexCoords :
                     side == VoxelSide.NORTH ? type.BottomTexCoords :
                     side == VoxelSide.SOUTH ? type.TopTexCoords :
                     side == VoxelSide.EAST ? type.EastTexCoords :
                     side == VoxelSide.WEST ? type.WestTexCoords : type.TopTexCoords);

            case VoxelSide.EAST:
                return
                    (side == VoxelSide.TOP ? type.WestTexCoords :
                     side == VoxelSide.BOTTOM ? type.EastTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.TopTexCoords :
                     side == VoxelSide.WEST ? type.BottomTexCoords : type.TopTexCoords);

            case VoxelSide.WEST:
                return
                    (side == VoxelSide.TOP ? type.EastTexCoords :
                     side == VoxelSide.BOTTOM ? type.WestTexCoords :
                     side == VoxelSide.NORTH ? type.NorthTexCoords :
                     side == VoxelSide.SOUTH ? type.SouthTexCoords :
                     side == VoxelSide.EAST ? type.BottomTexCoords :
                     side == VoxelSide.WEST ? type.TopTexCoords : type.TopTexCoords);

            default:
                return(type.TopTexCoords);
            }
        }