Beispiel #1
0
    public System.Collections.Generic.List <GameObject> getObjectsOnlyInA(WorldBox boxA, WorldBox boxB)
    {
        System.Collections.Generic.List <GameObject>    results = new System.Collections.Generic.List <GameObject>();
        System.Collections.Generic.HashSet <GameObject> localItems;
        IndexBox box1 = this.getIndexBoxFromWorldBox(boxA);
        IndexBox box2 = this.getIndexBoxFromWorldBox(boxB);

        int[] coordinates = new int[2];
        int[] indices     = new int[2];
        //int i;
        IndexBox itemIndexBox;

        //GameObject currentItem;
        // Check each box inside the larger indexbox
        for (coordinates[0] = box1.getLowCoordinate(0); coordinates[0] <= box1.getHighCoordinate(0); coordinates[0]++)
        {
            // wraparound
            indices[0] = coordinates[0] % this.numBlocks[0];
            if (indices[0] < 0)
            {
                indices[0] += this.numBlocks[0];
            }
            for (coordinates[1] = box1.getLowCoordinate(1); coordinates[1] <= box1.getHighCoordinate(1); coordinates[1]++)
            {
                // wraparound
                indices[1] = coordinates[1] % this.numBlocks[1];
                if (indices[1] < 0)
                {
                    indices[1] += this.numBlocks[1];
                }
                // Make sure that the current point isn't inside the smaller index box
                if (!(box2.contains(coordinates)))
                {
                    // Iterate over each item inside the box
                    localItems = this.worldBlocks[indices[0], indices[1]];
                    foreach (GameObject currentItem in localItems)
                    {
                        //currentItem = localItems[i];
                        itemIndexBox = this.getIndexBoxFromWorldBox(currentItem.getBoundingBox());
                        // Make sure that the item does intersect the larger box but not the smaller index box
                        if (itemIndexBox.intersects(box1) && !(itemIndexBox.intersects(box2)))
                        {
                            // Now add it to the resultant list if needed
                            if (!results.Contains(currentItem))
                            {
                                results.Add(currentItem);
                            }
                        }
                    }
                }
            }
        }
        return(results);
    }
Beispiel #2
0
    // Uses the worldBlocks array to speed up the search for objects inside the given index box. This is good when the box is small
    private System.Collections.Generic.List <GameObject> searchWorldForIndexBox(IndexBox indexBox)
    {
        System.Collections.Generic.List <GameObject>    resultantList = new System.Collections.Generic.List <GameObject>();
        System.Collections.Generic.HashSet <GameObject> localItems;
        //GameObject tempItem;
        int x1 = (int)(indexBox.getLowCoordinate(0));
        int x2 = (int)(indexBox.getHighCoordinate(0));
        int y1 = (int)(indexBox.getLowCoordinate(1));
        int y2 = (int)(indexBox.getHighCoordinate(1));
        int i, j, x, y;

        // check each block inside the bounding box
        for (i = x1; i <= x2; i++)
        {
            // wraparound
            x = i % this.numBlocks[0];
            if (x < 0)
            {
                x += this.numBlocks[0];
            }
            for (j = y1; j <= y2; j++)
            {
                // wraparound
                y = j % this.numBlocks[1];
                if (y < 0)
                {
                    y += this.numBlocks[1];
                }
                // check each item in the block
                localItems = this.worldBlocks[x, y];
                foreach (GameObject currentItem in localItems)
                {
                    //tempItem = localItems[k];
                    // make sure the item isn't there already
                    if (!resultantList.Contains(currentItem))
                    {
                        //Now we store it in the list of collisions
                        resultantList.Add(currentItem);
                    }
                }
            }
        }
        return(resultantList);
    }
Beispiel #3
0
    // put the item into the world. The location of the item usually may not move until it is removed
    // There are two ways to move an object:
    // Option 1, which is slower, is to remove it, then move it and re-add it
    // Option 2, which only supports moving one object at a time, is to call itemStartingMove and then move it and call itemEndingMove
    public void addItem(GameObject item)
    {
        // add it to the set
        this.items.Add(item);
        // figure out which boxes contain the bounding box for this item
        IndexBox boundingBox = this.getIndexRange(item);
        int      i, j, x, y;

        // for each box, put the item into it
        for (i = boundingBox.getLowCoordinate(0); i <= boundingBox.getHighCoordinate(0); i++)
        {
            // wraparound
            x = i % this.numBlocks[0];
            if (x < 0)
            {
                x += this.numBlocks[0];
            }
            for (j = boundingBox.getLowCoordinate(1); j <= boundingBox.getHighCoordinate(1); j++)
            {
                // wraparound
                y = j % this.numBlocks[1];
                if (y < 0)
                {
                    y += this.numBlocks[1];
                }
                // Now actually add it to this block
#if false
                if (!(this.worldBlocks[x, y].Contains(item)))
                {
                    this.worldBlocks[x, y].Add(item);
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine("error, WorldSearcher added an item that was already present");
                }
#else
                if (!(this.worldBlocks[x, y].Contains(item)))
                {
                    this.worldBlocks[x, y].Add(item);
                }
#endif
            }
        }
    }
Beispiel #4
0
    // Removes an item from the world, which was previously found in the indexBox
    void removeItem(GameObject item, IndexBox boundingBox)
    {
        // remove it from the set
        this.items.Remove(item);
        int i, j, x, y;

        // iterate over each box
        for (i = boundingBox.getLowCoordinate(0); i <= boundingBox.getHighCoordinate(0); i++)
        {
            // wraparound
            x = i % this.numBlocks[0];
            if (x < 0)
            {
                x += this.numBlocks[0];
            }
            for (j = boundingBox.getLowCoordinate(1); j <= boundingBox.getHighCoordinate(1); j++)
            {
                // wraparound
                y = j % this.numBlocks[1];
                if (y < 0)
                {
                    y += this.numBlocks[1];
                }
                // Now actually remove it from this block
#if false
                if (this.worldBlocks[x, y].Contains(item))
                {
                    this.worldBlocks[x, y].Remove(item);
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine("error, WorldSearcher tried to remove an item that was not present");
                }
#else
                if (this.worldBlocks[x, y].Contains(item))
                {
                    this.worldBlocks[x, y].Remove(item);
                }
#endif
            }
        }
    }
Beispiel #5
0
    public bool indexBoxContainsCoordinates(IndexBox indexBox, int[] coordinates)
    {
        int i;
        int difference;
        int modValue;

        for (i = 0; i < coordinates.Length; i++)
        {
            difference = coordinates[i] - indexBox.getLowCoordinate(i);
            modValue   = difference % this.numBlocks[i];
            if (modValue < 0)
            {
                modValue += this.numBlocks[i];
            }
            if (modValue > indexBox.getHighCoordinate(i) - indexBox.getLowCoordinate(i))
            {
                return(false);
            }
        }
        return(true);
    }