private bool _IsPassableAt(Vector3Int origin, Vector3Int to, bool isServer,
                                   CollisionType collisionType    = CollisionType.Player, bool inclPlayers = true, GameObject context = null,
                                   List <LayerType> excludeLayers = null, List <TileType> excludeTiles     = null, bool ignoreObjects = false)
        {
            if (ignoreObjects == false &&
                ObjectLayer.IsPassableAt(origin, to, isServer, collisionType, inclPlayers, context, excludeTiles) == false)
            {
                return(false);
            }

            TileLocation TileLcation = null;

            for (var i = 0; i < SolidLayersValues.Length; i++)
            {
                // Skip floor & base collisions if this is not a shuttle
                if (collisionType != CollisionType.Shuttle &&
                    (SolidLayersValues[i].LayerType == LayerType.Floors ||
                     SolidLayersValues[i].LayerType == LayerType.Base))
                {
                    continue;
                }

                // Skip if the current tested layer is being excluded.
                if (excludeLayers != null && excludeLayers.Contains(SolidLayersValues[i].LayerType))
                {
                    continue;
                }

                lock (PresentTiles)
                {
                    PresentTiles[SolidLayersValues[i]].TryGetValue(to, out TileLcation);
                }

                if (TileLcation?.Tile == null)
                {
                    continue;
                }
                var tile = TileLcation.Tile as BasicTile;

                // Return passable if the tile type is being excluded from checks.
                if (excludeTiles != null && excludeTiles.Contains(tile.TileType))
                {
                    continue;
                }

                if (tile.IsPassable(collisionType, origin, this) == false)
                {
                    return(false);
                }

                // if ((TileLcation.Tile as BasicTile).IsAtmosPassable() == false)
                // {
                // return false;
                // }

                // if (!SolidLayersValues[i].IsPassableAt(origin, to, isServer, collisionType: collisionType,
                // inclPlayers: inclPlayers, context: context, excludeTiles))
                // {
                // return false;
                // }
            }

            return(true);
        }
        private void OnEnable()
        {
            Layers = new Dictionary <LayerType, Layer>();
            var layersKeys             = new List <LayerType>();
            var layersValues           = new List <Layer>();
            var solidLayersValues      = new List <Layer>();
            var damageableLayersValues = new List <Layer>();

            foreach (Layer layer in GetComponentsInChildren <Layer>(true))
            {
                var type = layer.LayerType;
                Layers[type] = layer;
                layersKeys.Add(type);
                layersValues.Add(layer);
                if (type != LayerType.Effects &&
                    type != LayerType.None)
                {
                    solidLayersValues.Add(layer);
                }

                if (layer.GetComponent <TilemapDamage>())
                {
                    damageableLayersValues.Add(layer);
                }

                if (layer.LayerType == LayerType.Objects)
                {
                    ObjectLayer = layer as ObjectLayer;
                    continue;
                }

                var          ToInsertDictionary = new Dictionary <Vector3Int, TileLocation>();
                BoundsInt    bounds             = layer.Tilemap.cellBounds;
                TileLocation Tile = null;
                for (int n = bounds.xMin; n < bounds.xMax; n++)
                {
                    for (int p = bounds.yMin; p < bounds.yMax; p++)
                    {
                        Vector3Int localPlace = (new Vector3Int(n, p, 0));
                        var        getTile    = layer.Tilemap.GetTile(localPlace) as LayerTile;
                        if (getTile != null)
                        {
                            Tile = GetPooledTile();
                            Tile.TileCoordinates    = localPlace;
                            Tile.PresentMetaTileMap = this;
                            Tile.PresentlyOn        = layer;
                            Tile.Tile                      = getTile;
                            Tile.Colour                    = layer.Tilemap.GetColor(localPlace);
                            Tile.TransformMatrix           = layer.Tilemap.GetTransformMatrix(localPlace);
                            ToInsertDictionary[localPlace] = Tile;
                        }
                    }
                }

                lock (PresentTiles)
                {
                    PresentTiles[layer] = ToInsertDictionary;
                }
            }

            lock (PresentTiles)
            {
                PresentTiles[ObjectLayer] = new Dictionary <Vector3Int, TileLocation>();
            }

            LayersKeys        = layersKeys.ToArray();
            LayersValues      = layersValues.ToArray();
            SolidLayersValues = solidLayersValues.ToArray();
            damageableLayersValues.Sort((layerOne, layerTwo) =>
                                        layerOne.LayerType.GetOrder().CompareTo(layerTwo.LayerType.GetOrder()));
            DamageableLayers = damageableLayersValues.ToArray();
            PresentMatrix    = this.GetComponent <Matrix>();
            if (Application.isPlaying == false)
            {
                return;
            }
            //UpdateManager.Add(CallbackType.UPDATE, ChangeCheck);
        }
        //Gets the first hit
        public MatrixManager.CustomPhysicsHit? Raycast(
            Vector2 origin,
            Vector2 direction,
            float distance,
            LayerTypeSelection layerMask, Vector2? To = null)
        {
            if (To == null)
            {
                To = direction.normalized * distance;
            }

            if (direction.x == 0 && direction.y == 0)
            {
                direction = (To.Value - origin).normalized;
                distance  = (To.Value - origin).magnitude;
            }

            // var Beginning = (new Vector3((float) origin.x, (float) origin.y, 0).ToWorld(PresentMatrix));
            // Debug.DrawLine(Beginning + (Vector3.right * 0.09f), Beginning + (Vector3.left * 0.09f), Color.yellow, 30);
            // Debug.DrawLine(Beginning + (Vector3.up * 0.09f), Beginning + (Vector3.down * 0.09f), Color.yellow, 30);

            // var end = (new Vector3((float) To.Value.x, (float) To.Value.y, 0).ToWorld(PresentMatrix));
            // Debug.DrawLine(end + (Vector3.right * 0.09f), end + (Vector3.left * 0.09f), Color.red, 30);
            // Debug.DrawLine(end + (Vector3.up * 0.09f), end + (Vector3.down * 0.09f), Color.red, 30);

            // Debug.DrawLine(Beginning, end, Color.magenta, 30);


            Vector2 Relativetarget = To.Value - origin;
            //custom code on tile to ask if it aptly got hit, if you want custom geometry
//
//What needs to be returned
//What it hit, game object/tile
//Normal of hit
//Can be done quite easily since we know if me moving left or right

//Static manager
//Calculate if Within bounds of matrix?
//End and start World pos
//
//Calculate offset to then put onto positions, Maybe each position gets its own Calculation

            double RelativeX = 0;
            double RelativeY = 0;


            double gridOffsetx = 0;
            double gridOffsety = 0;

            int xSteps = 0;
            int ySteps = 0;

            int stepX = 0;
            int stepY = 0;

            double Offsetuntouchx = (origin.x - Math.Round(origin.x));
            double Offsetuntouchy = (origin.y - Math.Round(origin.y));

            if (direction.x < 0)
            {
                gridOffsetx = -(-0.5d + Offsetuntouchx); //0.5f  //this is So when you multiply it gives you 0.5 that some tile borders
                stepX       = -1;                        //For detecting which Tile it hits
            }
            else
            {
                gridOffsetx = -0.5d - Offsetuntouchx;                 //-0.5f
                stepX       = 1;
                //sideDistX = (mapX + 1.0 - posX) * deltaDistX;
            }

            if (direction.y < 0)
            {
                gridOffsety = -(-0.5d + Offsetuntouchy);                 // 0.5f
                stepY       = -1;
                //sideDistY = (posY - mapY) * deltaDistY;
            }
            else
            {
                gridOffsety = -0.5d - Offsetuntouchy;                 //-0.5f
                stepY       = 1;
                //sideDistY = (mapY + 1.0 - posY) * deltaDistY;
            }


            var          vec         = Vector3Int.zero; //Tile it hit Local  Coordinates
            var          vecHit      = Vector3.zero;    //Coordinates of Edge tile hit
            TileLocation TileLcation = null;

            var vexinvX = (1d / (direction.x));             //Editions need to be done here for Working offset
            var vexinvY = (1d / (direction.y));             //Needs to be conditional

            double calculationFloat = 0;

            bool LeftFaceHit = true;



            while (Math.Abs((xSteps + gridOffsetx + stepX) * vexinvX) < distance ||
                   Math.Abs((ySteps + gridOffsety + stepY) * vexinvY) < distance)
            //for (int Ai = 0; Ai < 6; Ai++)
            {
                //if (xBuildUp > yBuildUp)
                if ((xSteps + gridOffsetx + stepX) * vexinvX < (ySteps + gridOffsety + stepY) * vexinvY
                    )             // which one has a lesser multiplication factor since that will give a less Magnitude
                {
                    xSteps += stepX;

                    calculationFloat = ((xSteps + gridOffsetx) * vexinvX);

                    RelativeX = direction.x * calculationFloat;                     //Remove offset here maybe?
                    RelativeY = direction.y * calculationFloat;

                    LeftFaceHit = true;
                }
                //else if (xBuildUp < yBuildUp)
                else                 //if (xBuildUp < yBuildUp)
                {
                    ySteps          += stepY;
                    calculationFloat = ((ySteps + gridOffsety) * vexinvY);

                    RelativeX = direction.x * calculationFloat;
                    RelativeY = direction.y * calculationFloat;

                    LeftFaceHit = false;
                }


                vec.x = (int)Mathf.Round(origin.x) + xSteps;
                vec.y = (int)Mathf.Round(origin.y) + ySteps;

                vecHit.x = origin.x + (float)RelativeX;                  //+ offsetX;
                vecHit.y = origin.y + (float)RelativeY;                  // + offsetY;
                //Check point here

                if (LeftFaceHit)
                {
                    // float TestX = ((vecHit.x - 0.5f) - Mathf.Floor(vecHit.x));

                    // if (0.05f < Math.Abs(TestX))
                    // {
                    // Logger.Log("Offsetuntouchx = " + Offsetuntouchx + "\n" + "directionx = " + direction.x + "\n" +
                    // "Step = " + xSteps + "\n" + "vexinv = " + vexinvX + "\n" + "offset = " + offsetX +
                    // "\n" + "\n" + "Test =" + TestX + "\n" + "Relative =" + (RelativeX)
                    // + "\n" + "\n"
                    // + " direction.x = " +  direction.x + " calculationFloat " + calculationFloat
                    // + "\n" + " xSteps " + xSteps + " gridOffsetx " + gridOffsetx  +"  vexinvX " + vexinvX);


                    // }
                }
                else
                {
                    // float Testy = ((vecHit.y - 0.5f) - Mathf.Floor(vecHit.y));
                    // if (0.05f < Math.Abs(Testy))
                    // {
                    // Logger.Log("Offsetuntouchx = " + Offsetuntouchy + "\n" + "directionx = " + direction.y + "\n" +
                    // "Step = " + ySteps + "\n" + "vexinv = " + vexinvY + "\n" + "offset = " + offsetY +
                    // "\n" + "\n" + "Test =" + Testy + "\n" + "Relative =" + (RelativeY)
                    // + "\n" + "\n"
                    // + " direction.y = " +  direction.y + " calculationFloat " + calculationFloat
                    // + "\n" + " ySteps " + ySteps + " gridOffsety " + gridOffsety  +"  vexinvY " + vexinvY);
                    // }
                }

                for (var i = 0; i < LayersValues.Length; i++)
                {
                    if (LayersValues[i].LayerType == LayerType.Objects)
                    {
                        continue;
                    }
                    if (LTSUtil.IsLayerIn(layerMask, LayersValues[i].LayerType))
                    {
                        lock (PresentTiles)
                        {
                            PresentTiles[LayersValues[i]].TryGetValue(vec, out TileLcation);
                        }

                        // var wold = (vecHit.ToWorld(PresentMatrix));
                        // Debug.DrawLine(wold + (Vector3.right * 0.09f), wold + (Vector3.left * 0.09f), Color.green, 30);
                        // Debug.DrawLine(wold + (Vector3.up * 0.09f), wold + (Vector3.down * 0.09f), Color.green, 30);


                        // if (LeftFaceHit)
                        // {
                        // Debug.DrawLine(wold + (Vector3.up * 4f), wold + (Vector3.down * 4), Color.blue, 30);
                        // }
                        // else
                        // {
                        // Debug.DrawLine(wold + (Vector3.right * 4), wold + (Vector3.left * 4), Color.blue, 30);
                        // }

                        // ColorUtility.TryParseHtmlString("#ea9335", out var Orange);
                        // var map = ((Vector3) vec).ToWorld(PresentMatrix);
                        // Debug.DrawLine(map + (Vector3.right * 0.09f), map + (Vector3.left * 0.09f), Orange, 30);
                        // Debug.DrawLine(map + (Vector3.up * 0.09f), map + (Vector3.down * 0.09f), Orange, 30);

                        if (TileLcation != null)
                        {
                            Vector2 normal;

                            if (LeftFaceHit)
                            {
                                normal = Vector2.left * stepX;
                            }
                            else
                            {
                                normal = Vector2.down * stepY;
                            }


                            Vector3 AdjustedNormal = ((Vector3)normal).ToWorld(PresentMatrix);
                            AdjustedNormal = AdjustedNormal - (Vector3.zero.ToWorld(PresentMatrix));


                            // Debug.DrawLine(wold, wold + AdjustedNormal, Color.cyan, 30);

                            return(new MatrixManager.CustomPhysicsHit(((Vector3)vec).ToWorld(PresentMatrix),
                                                                      (vecHit).ToWorld(PresentMatrix), AdjustedNormal,
                                                                      new Vector2((float)RelativeX, (float)RelativeY).magnitude, TileLcation));
                        }
                    }
                }
            }
            return(null);
        }