Example #1
0
        // Sets the collision at the provided x- and y- coordinates to match the provided brush
        // Also handles removing old collisions if the brush is being replaced
        public void SetCollisionAt(EditorBrush brush, int x, int y, bool updateComposite)
        {
            // First, remove the old collision
            EditorBrush oldBrush = GetBrushAt(x, y);

            // Remove references based on size if the old brush was an object
            if (oldBrush != null && oldBrush.FrontAsset != null && oldBrush.FrontAsset is StarboundObject)
            {
                StarboundObject   sbObject    = (StarboundObject)oldBrush.FrontAsset;
                ObjectOrientation orientation = sbObject.GetCorrectOrientation(m_parent, x, y);

                int sizeX   = orientation.GetWidth(brush.Direction, 1);
                int sizeY   = orientation.GetHeight(brush.Direction, 1);
                int originX = orientation.GetOriginX(brush.Direction, 1);
                int originY = orientation.GetOriginY(brush.Direction, 1);

                for (int j = originX + x; j < sizeX + originX + x; ++j)
                {
                    for (int k = originY + y; k < sizeY + originY + y; ++k)
                    {
                        HashSet <List <int> > objAnchorSet = GetCollisionsAt(j, k);

                        if (objAnchorSet != null)
                        {
                            // Remove from the set if we have a match
                            objAnchorSet.RemoveWhere(coord => coord[0] == x && coord[1] == y);
                        }
                    }
                }
            }
            // Else just remove the tile we're at
            else
            {
                HashSet <List <int> > tileAnchorSet = GetCollisionsAt(x, y);

                if (tileAnchorSet != null)
                {
                    // Remove from the set if we have a match
                    tileAnchorSet.RemoveWhere(coord => coord[0] == x && coord[1] == y);
                }
            }

            // If this tile has a front component, continue
            if (brush != null && brush.FrontAsset != null)
            {
                // Collisions for objects based on size
                if (brush.FrontAsset is StarboundObject)
                {
                    var sbObject = (StarboundObject)brush.FrontAsset;
                    ObjectOrientation orientation = sbObject.GetCorrectOrientation(m_parent, x, y);

                    int sizeX   = orientation.GetWidth(brush.Direction, 1);
                    int sizeY   = orientation.GetHeight(brush.Direction, 1);
                    int originX = orientation.GetOriginX(brush.Direction, 1);
                    int originY = orientation.GetOriginY(brush.Direction, 1);

                    // Set the elements at j, k to point to the anchor at x, y
                    for (int j = originX + x; j < sizeX + originX + x; ++j)
                    {
                        for (int k = originY + y; k < sizeY + originY + y; ++k)
                        {
                            List <int> list = new List <int> {
                                x, y
                            };
                            AddCollisionAt(list, j, k);
                        }
                    }
                }
                // Collisions for non-special front tiles
                else if (!brush.IsSpecial)
                {
                    List <int> list = new List <int> {
                        x, y
                    };
                    AddCollisionAt(list, x, y);
                }
            }

            // Ship brushes have a special flag for things that block
            // foreground collisions
            if (brush is ShipBrush && ((ShipBrush)brush).ForegroundBlock)
            {
                List <int> list = new List <int> {
                    x, y
                };
                AddCollisionAt(list, x, y);
            }

            // If the update composite flag is set, rebuild
            // the composite collision map.
            if (updateComposite)
            {
                Parent.UpdateCompositeCollisionMap();
            }
        }