Exemplo n.º 1
0
        /// <summary>
        /// Get all blocks colliding with entityBoxRel
        /// </summary>
        /// <param name="blockAccessor"></param>
        /// <param name="entityBoxRel"></param>
        /// <param name="pos"></param>
        /// <param name="blocks">The found blocks</param>
        /// <param name="alsoCheckTouch"></param>
        /// <returns>If any blocks have been found</returns>
        public bool GetCollidingCollisionBox(IBlockAccessor blockAccessor, Cuboidf entityBoxRel, Vec3d pos, out CachedCuboidList blocks, bool alsoCheckTouch = true)
        {
            blocks = new CachedCuboidList();
            BlockPos blockPos    = new BlockPos();
            Vec3d    blockPosVec = new Vec3d();
            Cuboidd  entityBox   = entityBoxRel.ToDouble().Translate(pos);


            int minX = (int)(entityBoxRel.MinX + pos.X);
            int minY = (int)(entityBoxRel.MinY + pos.Y - 1);  // -1 for the extra high collision box of fences
            int minZ = (int)(entityBoxRel.MinZ + pos.Z);
            int maxX = (int)Math.Ceiling(entityBoxRel.MaxX + pos.X);
            int maxY = (int)Math.Ceiling(entityBoxRel.MaxY + pos.Y);
            int maxZ = (int)Math.Ceiling(entityBoxRel.MaxZ + pos.Z);

            for (int y = minY; y <= maxY; y++)
            {
                for (int x = minX; x <= maxX; x++)
                {
                    for (int z = minZ; z <= maxZ; z++)
                    {
                        Block block = blockAccessor.GetBlock(x, y, z);
                        blockPos.Set(x, y, z);
                        blockPosVec.Set(x, y, z);

                        Cuboidf[] collisionBoxes = block.GetCollisionBoxes(blockAccessor, blockPos);

                        for (int i = 0; collisionBoxes != null && i < collisionBoxes.Length; i++)
                        {
                            Cuboidf collBox = collisionBoxes[i];
                            if (collBox == null)
                            {
                                continue;
                            }

                            bool colliding = alsoCheckTouch ? entityBox.IntersectsOrTouches(collBox, blockPosVec) : entityBox.Intersects(collBox, blockPosVec);
                            if (colliding)
                            {
                                blocks.Add(collBox, x, y, z, block);
                            }
                        }
                    }
                }
            }

            return(blocks.Count > 0);
        }