Exemple #1
0
        public GridSystem(Room room, int cellsX, Vector2R position, int?GridWidth = null, int?GridHeight = null)
        {
            linesToDraw   = new List <Line>();
            this.position = position;
            this.room     = room;

            this.gridWidth  = GridWidth ?? room.WorldWidth;
            this.gridHeight = GridHeight ?? room.WorldHeight;
            this.cellsX     = cellsX;

            //this.cellReach = cellReach;
            cellWidth   = (int)Math.Ceiling((double)gridWidth / (double)cellsX);
            cellHeight  = cellWidth;
            this.cellsY = (gridHeight - 1) / cellHeight + 1;
            //cellheight = gridheight / cellsY;
            grid = new List <Collider> [cellsX, cellsY];
            for (int i = 0; i < cellsX; i++)
            {
                for (int j = 0; j < cellsY; j++)
                {
                    grid[i, j] = new List <Collider>();
                }
            }

            //
            arrayGrid = new IndexArray <Collider> [cellsX][];
            for (int i = 0; i < cellsX; i++)
            {
                arrayGrid[i] = new IndexArray <Collider> [cellsY];
                for (int j = 0; j < cellsY; j++)
                {
                    arrayGrid[i][j] = new IndexArray <Collider>(100);
                }
            }
            bucketBags = new IndexArray <IndexArray <Collider> > [cellsX][];
            for (int i = 0; i < cellsX; i++)
            {
                bucketBags[i] = new IndexArray <IndexArray <Collider> > [cellsY];
                for (int j = 0; j < cellsY; j++)
                {
                    bucketBags[i][j] = new IndexArray <IndexArray <Collider> >(20);
                }
            }
            //
            distToOffsets = GenerateReachOffsetsDictionary();

            //Stopwatch stopwatch = new Stopwatch();
            //stopwatch.Start();
            int generateReach = gridWidth / 3;

            //GenerateAllReachOffsetsPerCoord(generateReach); //takes a shitload of time.. but only for the temproom?
            //stopwatch.Stop();
            //string taken = stopwatch.Elapsed.ToString();
            //Console.WriteLine("gridsystem generation time taken: " + taken);

            bucketLists = new List <List <Collider> > [cellsX, cellsY];

            ///new attempt
            GenerateReachOffsetsArray();
        }
Exemple #2
0
        public void retrieveFromAllOffsets(Collider collider, float reachDistance, Action <Collider, Collider> action)
        {
            int x = (int)collider.pos.X / cellWidth;
            int y = (int)collider.pos.Y / cellHeight;

            if (x < 0 || x >= cellsX || y < 0 || y >= cellsY)
            {
                return;
            }
            int findcount = FindCount(reachDistance);

            for (int i = 0; i < findcount; i++)
            {
                foreach (var tuple in distToOffsets.ElementAt(i).Value)
                {
                    int xx = tuple.Item1 + x;
                    int yy = tuple.Item2 + y;
                    if (xx < 0 || xx >= cellsX || yy < 0 || yy >= cellsY)
                    {
                        continue;
                    }
                    //foreach (Collider c in grid[tuple.Item1, tuple.Item2])
                    //{
                    //    action(collider, c);
                    //}
                    IndexArray <Collider> buck = arrayGrid[xx][yy];
                    int count = buck.index;
                    //if (count > 0) Console.WriteLine(count);
                    for (int j = 0; j < count; j++)
                    {
                        Collider c = arrayGrid[xx][yy].array[j];
                        if (alreadyVisited.Contains(c) || collider == c)
                        {
                            continue;
                        }
                        action(collider, c);
                    }
                }
            }
        }
Exemple #3
0
        public void retrieveOffsetArraysCollision(Collider collider, Action <Collider, Collider> action, float distance)
        {
            int effectiveX = (int)(collider.pos.X - position.X);
            int effectiveY = (int)(collider.pos.Y - position.Y);

            int x = effectiveX / cellWidth;
            int y = effectiveY / cellHeight;

            if (x < 0 || x > cellsX || y < 0 || y > cellsY)
            {
                return;
            }
            int findcount = FindCount(distance);
            int lastIndex;

            //lastIndex = reachIndexs[reachCount];
            lastIndex = reachIndexs[findcount];
            for (int coordPointer = 0; coordPointer <= lastIndex; coordPointer += 2)
            {
                int xx = offsetArray[coordPointer] + x;
                int yy = offsetArray[coordPointer + 1] + y;
                if (xx < 0 || xx >= cellsX || yy < 0 || yy >= cellsY)
                {
                    continue;
                }
                IndexArray <Collider> buck = arrayGrid[xx][yy];
                int count = buck.index;
                for (int j = 0; j < count; j++)
                {
                    Collider c = arrayGrid[xx][yy].array[j];
                    if (alreadyVisited.Contains(c) || collider == c)
                    {
                        continue;
                    }
                    action(collider, c);
                }
            }
        }