public override void MoveCollideX(Collider collider)
 {
     base.MoveCollideX(collider);
     Speed.X = 0;
     ExtraSpeed.X = 0;
 }
 public override void MoveCollideY(Collider collider)
 {
     base.MoveCollideY(collider);
     if (SumSpeedY > 0) OnGround = true;
     Speed.Y = 0;
     ExtraSpeed.Y = 0;
 }
Пример #3
0
 /// <summary>
 /// Checks for a collision against a specific Collider and returns true or false.
 /// </summary>
 /// <param name="x">The x position to check.</param>
 /// <param name="y">The y position to check.</param>
 /// <param name="c">The Collider to check.</param>
 /// <returns>True if there was a collision.</returns>
 public bool Overlap(float x, float y, Collider c)
 {
     return (Collide(x, y, c) != null);
 }
Пример #4
0
        /// <summary>
        /// Giant function that checks for overlaps between many different types
        /// </summary>
        /// <param name="first">The first collider to test.</param>
        /// <param name="second">The second collider to test.</param>
        /// <returns>True if overlapping something</returns>
        internal static bool OverlapTest(Collider first, Collider second)
        {
            #region Box vs Box
            if (first is BoxCollider && second is BoxCollider) {
                if (first.Right <= second.Left) return false;
                if (first.Bottom <= second.Top) return false;
                if (first.Left >= second.Right) return false;
                if (first.Top >= second.Bottom) return false;
                return true;
            }
            #endregion

            #region Box vs Circle
            else if ((first is BoxCollider && second is CircleCollider) || (first is CircleCollider && second is BoxCollider)) {
                CircleCollider circle;
                BoxCollider box;
                if (first is CircleCollider) {
                    circle = first as CircleCollider;
                    box = second as BoxCollider;
                }
                else {
                    circle = second as CircleCollider;
                    box = first as BoxCollider;
                }

                //check is c center point is in the rect
                if (Util.InRect(circle.CenterX, circle.CenterY, box.Left, box.Top, box.Width, box.Height)) {
                    return true;
                }

                //check to see if any corners are in the circle
                if (Util.DistanceRectPoint(circle.CenterX, circle.CenterY, box.Left, box.Top, box.Width, box.Height) < circle.Radius) {
                    return true;
                }

                //check to see if any lines on the box intersect the circle
                Line2 boxLine;

                boxLine = new Line2(box.Left, box.Top, box.Right, box.Top);
                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                boxLine = new Line2(box.Right, box.Top, box.Right, box.Bottom);
                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                boxLine = new Line2(box.Right, box.Bottom, box.Left, box.Bottom);
                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                boxLine = new Line2(box.Left, box.Bottom, box.Left, box.Top);
                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                return false;
            }
            #endregion

            #region Box vs Grid
            else if ((first is BoxCollider && second is GridCollider) || (first is GridCollider && second is BoxCollider)) {
                BoxCollider box;
                GridCollider grid;
                if (first is BoxCollider) {
                    box = first as BoxCollider;
                    grid = second as GridCollider;
                }
                else {
                    box = second as BoxCollider;
                    grid = first as GridCollider;
                }

                if (box.Right <= grid.Left) return false;
                if (box.Bottom <= grid.Top) return false;
                if (box.Left >= grid.Right) return false;
                if (box.Top >= grid.Bottom) return false;

                //This is a quick fix and might have to be addressed later
                if (grid.GetRect(box.Left, box.Top, box.Right - 1, box.Bottom - 1, false)) return true;
                return false;
            }
            #endregion

            #region Circle vs Circle
            else if (first is CircleCollider && second is CircleCollider) {
                CircleCollider
                    circle1 = first as CircleCollider,
                    circle2 = second as CircleCollider;
                if (Util.Distance(circle1.CenterX, circle1.CenterY, circle2.CenterX, circle2.CenterY) < circle1.Radius + circle2.Radius) {
                    return true;
                }
                return false;
            }
            #endregion

            #region Box vs Line
            else if ((first is BoxCollider && second is LineCollider) || (first is LineCollider && second is BoxCollider)) {
                BoxCollider box;
                LineCollider line;
                if (first is BoxCollider) {
                    box = first as BoxCollider;
                    line = second as LineCollider;
                }
                else {
                    box = second as BoxCollider;
                    line = first as LineCollider;
                }

                if (line.Line2.IntersectsRect(box.Left, box.Top, box.Width, box.Height)) return true;

                return false;
            }
            #endregion

            #region Line vs Line
            else if (first is LineCollider && second is LineCollider) {
                return (first as LineCollider).Line2.Intersects((second as LineCollider).Line2);
            }
            #endregion

            #region Line vs Grid
            else if ((first is LineCollider && second is GridCollider) || (first is GridCollider && second is LineCollider)) {
                //check any tiles along the line segment, somehow?
                LineCollider line;
                GridCollider grid;
                if (first is LineCollider) {
                    line = first as LineCollider;
                    grid = second as GridCollider;
                }
                else {
                    line = second as LineCollider;
                    grid = first as GridCollider;
                }

                //make a rectangle out of the line segment, check for any tiles in that rectangle

                //if there are tiles in there, loop through and check each one as a rectangle against the line
                if (grid.GetRect(line.Left, line.Top, line.Right, line.Bottom, false)) {
                    float rectX, rectY;
                    int
                        gridx = grid.GridX(line.Left),
                        gridy = grid.GridY(line.Top),
                        gridx2 = grid.GridX(line.Right),
                        gridy2 = grid.GridY(line.Bottom);

                    for (int i = gridx; i <= gridx2; i++) {
                        for (int j = gridy; j <= gridy2; j++) {
                            if (grid.GetTile(i, j)) {
                                rectX = i * grid.TileWidth + grid.Left;
                                rectY = j * grid.TileHeight + grid.Top;
                                if (Util.InRect((float)line.Line2.PointA.X, (float)line.Line2.PointA.Y, rectX, rectY, grid.TileWidth, grid.TileHeight)) {
                                    return true;
                                }
                                if (Util.InRect((float)line.Line2.PointB.X, (float)line.Line2.PointB.Y, rectX, rectY, grid.TileWidth, grid.TileHeight)) {
                                    return true;
                                }
                                if (line.Line2.IntersectsRect(rectX, rectY, grid.TileWidth, grid.TileHeight)) {
                                    return true;
                                }

                            }
                        }
                    }
                }
                return false;

            }
            #endregion

            #region Circle vs Grid
            else if ((first is CircleCollider && second is GridCollider) || (first is GridCollider && second is CircleCollider)) {
                //make a rectangle out of the circle, check for any tiles in that rectangle
                //if there are tiles, check each tile as a rectangle against the circle
                CircleCollider circle;
                GridCollider grid;
                if (first is CircleCollider) {
                    circle = first as CircleCollider;
                    grid = second as GridCollider;
                }
                else {
                    circle = second as CircleCollider;
                    grid = first as GridCollider;
                }

                int gridx, gridy, gridx2, gridy2;
                gridx = (int)(Util.SnapToGrid(circle.Left - grid.Left, grid.TileWidth) / grid.TileWidth);
                gridy = (int)(Util.SnapToGrid(circle.Top - grid.Top, grid.TileHeight) / grid.TileHeight);
                gridx2 = (int)(Util.SnapToGrid(circle.Right - grid.Left, grid.TileWidth) / grid.TileWidth);
                gridy2 = (int)(Util.SnapToGrid(circle.Bottom - grid.Top, grid.TileHeight) / grid.TileHeight);

                //if (grid.GetRect(gridx, gridy, gridx2, gridy2, false)) {
                if (grid.GetRect(circle.Left, circle.Top, circle.Right, circle.Bottom, false)) {
                    float rectX, rectY;
                    for (int i = gridx; i <= gridx2; i++) {
                        for (int j = gridy; j <= gridy2; j++) {
                            if (grid.GetTile(i, j)) {
                                rectX = (i * grid.TileWidth) + grid.Left;
                                rectY = (j * grid.TileHeight) + grid.Top;

                                //check is c center point is in the rect
                                if (Util.InRect(circle.CenterX, circle.CenterY, rectX, rectY, grid.TileWidth, grid.TileHeight)) {
                                    return true;
                                }

                                //check to see if any corners are in the circle
                                if (Util.DistanceRectPoint(circle.CenterX, circle.CenterY, rectX, rectY, grid.TileWidth, grid.TileHeight) < circle.Radius) {
                                    //return true;
                                }

                                //check to see if any lines on the box intersect the circle
                                Line2 boxLine;

                                boxLine = new Line2(rectX, rectY, rectX + grid.TileWidth, rectY);
                                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                                boxLine = new Line2(rectX + grid.TileWidth, rectY, rectX + grid.TileWidth, rectY + grid.TileHeight);
                                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                                boxLine = new Line2(rectX + grid.TileWidth, rectY + grid.TileHeight, rectX, rectY + grid.TileHeight);
                                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                                boxLine = new Line2(rectX, rectY + grid.TileHeight, rectX, rectY);
                                if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true;

                            }
                        }
                    }
                }
                return false;

            }
            #endregion

            #region Grid vs Grid
            else if (first is GridCollider && second is GridCollider) {
                //loop through one grid, check tile as rectangle in second grid?
                //first check if grids even touch with basic box test
                //then check each tile on first as a rect against second's tilemap
                //maybe optimize by looping through the smaller (area wise) tilemap

                if (!Util.IntersectRectangles(first.Left, first.Top, first.Width, first.Height, second.Left, second.Top, second.Width, second.Height)) {
                    return false;
                }

                GridCollider small, large;

                if ((first as GridCollider).TileArea < (second as GridCollider).TileArea) {
                    small = first as GridCollider;
                    large = second as GridCollider;
                }
                else {
                    small = second as GridCollider;
                    large = first as GridCollider;
                }

                for (int i = 0; i < small.TileColumns; i++) {
                    for (int j = 0; j < small.TileRows; j++) {
                        if (small.GetTile(i, j)) {
                            //check rects
                            float rectx, recty;
                            rectx = i * small.TileWidth + small.Left;
                            recty = j * small.TileHeight + small.Top;
                            if (large.GetRect(rectx, recty, rectx + small.TileWidth, recty + small.TileHeight, false)) {
                                return true;
                            }
                        }
                    }
                }

                return false;

            }
            #endregion

            #region Line vs Circle
            else if ((first is LineCollider && second is CircleCollider) || (first is CircleCollider && second is LineCollider)) {
                CircleCollider circle;
                LineCollider line;
                if (first is LineCollider) {
                    line = first as LineCollider;
                    circle = second as CircleCollider;
                }
                else {
                    line = second as LineCollider;
                    circle = first as CircleCollider;
                }

                if (line.Line2.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) {
                    return true;
                }
                return false;
            }
            #endregion

            #region Point vs Point
            else if (first is PointCollider && second is PointCollider) {
                if (first.Left != second.Left) return false;
                if (first.Top != second.Top) return false;
                return true;
            }
            #endregion

            #region Point vs Circle
            else if ((first is PointCollider && second is CircleCollider) || (first is CircleCollider && second is PointCollider)) {
                PointCollider point;
                CircleCollider circle;

                if (first is PointCollider) {
                    point = first as PointCollider;
                    circle = second as CircleCollider;
                }
                else {
                    point = second as PointCollider;
                    circle = first as CircleCollider;
                }

                if (Util.Distance(point.Left, point.Top, circle.CenterX, circle.CenterY) < circle.Radius) return true;
                return false;
            }
            #endregion

            #region Point vs Box
            else if ((first is PointCollider && second is BoxCollider) || (first is BoxCollider && second is PointCollider)) {
                PointCollider point;
                BoxCollider box;

                if (first is PointCollider) {
                    point = first as PointCollider;
                    box = second as BoxCollider;
                }
                else {
                    point = second as PointCollider;
                    box = first as BoxCollider;
                }

                if (Util.InRect(point.Left, point.Top, box.Left, box.Top, box.Width, box.Height)) return true;
                return false;
            }
            #endregion

            #region Point vs Grid
            else if ((first is PointCollider && second is GridCollider) || (first is GridCollider && second is PointCollider)) {
                PointCollider point;
                GridCollider grid;

                if (first is PointCollider) {
                    point = first as PointCollider;
                    grid = second as GridCollider;
                }
                else {
                    point = second as PointCollider;
                    grid = first as GridCollider;
                }

                int gridx, gridy;
                gridx = (int)Util.SnapToGrid(point.Left, grid.TileWidth);
                gridy = (int)Util.SnapToGrid(point.Top, grid.TileHeight);

                if (grid.GetTile(gridx, gridy)) return true;
                return false;
            }
            #endregion

            #region Point vs Line
            else if ((first is PointCollider && second is LineCollider) || (first is LineCollider && second is PointCollider)) {
                PointCollider point;
                LineCollider line;

                if (first is PointCollider) {
                    point = first as PointCollider;
                    line = second as LineCollider;
                }
                else {
                    point = second as PointCollider;
                    line = first as LineCollider;
                }

                //first take care of weird cases that might result in division by 0
                Line2 line2 = line.Line2;
                if (line2.X1 == line2.X2) {
                    if (line2.Y1 == line2.Y2) {
                        if (point.Left == line2.X1 && point.Top == line2.Y1) {
                            return true;
                        }
                    }
                    if (point.Left == line2.X1 && point.Top >= Math.Min(line2.Y1, line2.Y2) && point.Top <= Math.Max(line2.Y1, line2.Y2)) {
                        return true;
                    }
                }
                if (line2.Y1 == line2.Y2) {
                    if (point.Top == line2.Y1 && point.Left >= Math.Min(line2.X1, line2.X2) && point.Left <= Math.Max(line2.X1, line2.X2)) {
                        return true;
                    }
                }

                //if no special cases, this should work!
                if ((point.Left - line2.X1) / (line2.X2 - line2.X1) == (point.Top - line2.Y1) / (line2.Y2 - line2.Y1)) return true;
                return false;
            }
            #endregion

            #region Pixel vs Pixel
            else if ((first is PixelCollider && second is PixelCollider)) {
                //AABB test first

                var pixel1 = first as PixelCollider;
                var pixel2 = second as PixelCollider;

                if (first.Right <= second.Left) return false;
                if (first.Left >= second.Right) return false;
                if (first.Top >= second.Bottom) return false;
                if (first.Bottom <= second.Top) return false;

                //get intersecting area

                float x1 = Math.Max(first.Left, second.Left);
                float x2 = Math.Min(first.Right, second.Right);

                float y1 = Math.Max(first.Top, second.Top);
                float y2 = Math.Min(first.Bottom, second.Bottom);

                //scan both pixels and see if they collide

                var p1 = first as PixelCollider;
                var p2 = second as PixelCollider;
                for (var i = x1; i < x2; i++) {
                    for (var j = y1; j < y2; j++) {
                        var xx = (int)Math.Floor(i - first.Left);
                        var yy = (int)Math.Floor(j - first.Top);

                        if (p1.PixelAt(xx, yy)) {
                            xx = (int)Math.Floor(i - second.Left);
                            yy = (int)Math.Floor(j - second.Top);
                            if (p2.PixelAt(xx, yy)) {
                                return true;
                            }
                        }
                    }
                }

                return false;
            }
            #endregion

            #region Pixel vs Box
            else if ((first is PixelCollider && second is BoxCollider) || (first is BoxCollider && second is PixelCollider)) {
                PixelCollider pixel;
                BoxCollider box;

                if (first is PixelCollider) {
                    pixel = first as PixelCollider;
                    box = second as BoxCollider;
                }
                else {
                    pixel = second as PixelCollider;
                    box = first as BoxCollider;
                }

                //AABB test

                if (box.Right <= pixel.Left) return false;
                if (box.Left >= pixel.Right) return false;
                if (box.Top >= pixel.Bottom) return false;
                if (box.Bottom <= pixel.Top) return false;

                //get intersecting area

                float x1 = Math.Max(box.Left, pixel.Left);
                float x2 = Math.Min(box.Right, pixel.Right);

                float y1 = Math.Max(box.Top, pixel.Top);
                float y2 = Math.Min(box.Bottom, pixel.Bottom);

                //scan for pixels in area

                for (var i = x1; i < x2; i++) {
                    for (var j = y1; j < y2; j++) {
                        var xx = (int)Math.Floor(i - pixel.Left);
                        var yy = (int)Math.Floor(j - pixel.Top);

                        if (pixel.PixelAt(xx, yy)) {
                            return true;
                        }
                    }
                }

                return false;

            }
            #endregion

            #region Pixel vs Circle
            else if ((first is PixelCollider && second is CircleCollider) || (first is CircleCollider && second is PixelCollider)) {
                PixelCollider pixel;
                CircleCollider circle;

                if (first is PixelCollider) {
                    pixel = first as PixelCollider;
                    circle = second as CircleCollider;
                }
                else {
                    pixel = second as PixelCollider;
                    circle = first as CircleCollider;
                }

                //circle to rectangle collision first

                bool firstCollisionCheck = false;

                //check is c center point is in the rect
                if (Util.InRect(circle.CenterX, circle.CenterY, pixel.Left, pixel.Top, pixel.Width, pixel.Height)) {
                    firstCollisionCheck = true;
                }

                if (!firstCollisionCheck) {
                    //check to see if any corners are in the circle
                    if (Util.DistanceRectPoint(circle.CenterX, circle.CenterY, pixel.Left, pixel.Top, pixel.Width, pixel.Height) < circle.Radius) {
                        firstCollisionCheck = true;
                    }
                }

                if (!firstCollisionCheck) {
                    //check to see if any lines on the box intersect the circle
                    Line2 boxLine;

                    boxLine = new Line2(pixel.Left, pixel.Top, pixel.Right, pixel.Top);
                    if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true;

                    if (!firstCollisionCheck) {
                        boxLine = new Line2(pixel.Right, pixel.Top, pixel.Right, pixel.Bottom);
                        if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true;
                    }

                    if (!firstCollisionCheck) {
                        boxLine = new Line2(pixel.Right, pixel.Bottom, pixel.Left, pixel.Bottom);
                        if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true;
                    }

                    if (!firstCollisionCheck) {
                        boxLine = new Line2(pixel.Left, pixel.Bottom, pixel.Left, pixel.Top);
                        if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true;
                    }

                }

                if (!firstCollisionCheck) return false;

                //get intersecting area as if circle was rect

                //scan for pixels in area

                //only check pixels inside circle radius

                //okay the math for that is super hard so we're doing it the easy + dumb wy

                //dumb way, scan through pixels to see if one of the pixels is in the circle?

                for (var xx = 0; xx < pixel.Width; xx++) {
                    for (var yy = 0; yy < pixel.Height; yy++) {
                        var pixelx = xx + pixel.Left;
                        var pixely = yy + pixel.Top;
                        if (Util.Distance(pixelx, pixely, circle.CenterX, circle.CenterY) <= circle.Radius + 1) {
                            if (pixel.PixelAt(xx, yy)) {
                                return true;
                            }
                        }
                    }
                }
            }
            #endregion

            #region Pixel vs Point
            else if ((first is PixelCollider && second is PointCollider) || (first is PointCollider && second is PixelCollider)) {
                PixelCollider pixel;
                PointCollider point;

                if (first is PixelCollider) {
                    pixel = first as PixelCollider;
                    point = second as PointCollider;
                }
                else {
                    pixel = second as PixelCollider;
                    point = first as PointCollider;
                }

                //rectangle to point first
                if (!Util.InRect(point.Left, point.Top, pixel.Left, pixel.Top, pixel.Width, pixel.Height)) return false;

                //check for pixel at point
                if (pixel.PixelAtRelative((int)point.Left, (int)point.Top)) {
                    return true;
                }
                return false;
            }
            #endregion

            #region Pixel vs Line
            else if ((first is PixelCollider && second is LineCollider) || (first is LineCollider && second is PixelCollider)) {
                PixelCollider pixel;
                LineCollider line;

                if (first is PixelCollider) {
                    pixel = first as PixelCollider;
                    line = second as LineCollider;
                }
                else {
                    pixel = second as PixelCollider;
                    line = first as LineCollider;
                }

                //line to rectangle first

                if (!line.Line2.IntersectsRect(pixel.Left, pixel.Top, pixel.Width, pixel.Height)) return false;

                //dumb way, check all pixels for distance to line == 0
                for (var xx = 0; xx < pixel.Width; xx++) {
                    for (var yy = 0; yy < pixel.Height; yy++) {
                        if (pixel.PixelAt(xx, yy)) {
                            if (Util.DistanceLinePoint(xx + pixel.Left, yy + pixel.Top, line.Line2) < 0.1f) {
                                return true;
                            }
                        }
                    }
                }

            }
            #endregion

            #region Pixel vs Grid
            else if ((first is PixelCollider && second is GridCollider) || (first is GridCollider && second is PixelCollider)) {
                PixelCollider pixel;
                GridCollider grid;

                if (first is PixelCollider) {
                    pixel = first as PixelCollider;
                    grid = second as GridCollider;
                }
                else {
                    pixel = second as PixelCollider;
                    grid = first as GridCollider;
                }

                //collide rectangle into tiles first
                if (pixel.Right <= grid.Left) return false;
                if (pixel.Bottom <= grid.Top) return false;
                if (pixel.Left >= grid.Right) return false;
                if (pixel.Top >= grid.Bottom) return false;

                //This is a quick fix and might have to be addressed later
                if (!grid.GetRect(pixel.Left, pixel.Top, pixel.Right - 1, pixel.Bottom - 1, false)) return false;

                //go through tiles that pixel is overlapping
                for (var xx = 0; xx < pixel.Width; xx++) {
                    for (var yy = 0; yy < pixel.Height; yy++) {
                        float checkx = xx + pixel.Left;
                        float checky = yy + pixel.Top;
                        if (grid.GetTileAtPosition(checkx, checky)) {
                            if (pixel.PixelAt(xx, yy)) {
                                return true;
                            }
                        }
                    }
                }
            }
            #endregion

            #region Unknown
            return false;
            #endregion
        }
Пример #5
0
        /// <summary>
        /// Checks for a collision with a specific collider.
        /// </summary>
        /// <param name="x">The x position to check.</param>
        /// <param name="y">The y position to check.</param>
        /// <param name="c">The collider to check for.</param>
        /// <returns>The collider that was hit first.</returns>
        public Collider Collide(float x, float y, Collider c)
        {
            if (Entity == null) return null;
            if (Entity.Scene == null) return null;

            float tempX = Entity.X, tempY = Entity.Y;
            Entity.X = x;
            Entity.Y = y;

            if (OverlapTest(this, c)) {
                Entity.X = tempX;
                Entity.Y = tempY;
                return c;
            }

            Entity.X = tempX;
            Entity.Y = tempY;
            return null;
        }
Пример #6
0
 /// <summary>
 /// Checks for a collision with a specific Collider and returns an Entity.
 /// </summary>
 /// <param name="x">The x position to check.</param>
 /// <param name="y">The y position to check.</param>
 /// <param name="c">The Collider to check for.</param>
 /// <returns>The Entity that was hit.</returns>
 public Entity CollideEntity(float x, float y, Collider c)
 {
     return Collide(x, y, c).Entity;
 }
Пример #7
0
 /// <summary>
 /// A callback for when the horizontal sweep test hits a collision.
 /// </summary>
 /// <param name="collider"></param>
 public override void MoveCollideX(Collider collider)
 {
     Speed.X = 0;
 }
Пример #8
0
 /// <summary>
 /// A callback for when the vertical sweep test hits a collision.
 /// </summary>
 /// <param name="collider"></param>
 public override void MoveCollideY(Collider collider)
 {
     Speed.Y = 0;
 }
Пример #9
0
        public override void MoveY(int speed, Collider collider = null) {
            MoveBufferY += speed;

            while (Math.Abs(MoveBufferY) >= SpeedScale) {
                int move = Math.Sign(MoveBufferY);
                if (collider != null) {
                    bool freeToMove = true;
                    Collider c = null;

                    if (move > 0) {
                        c = collider.Collide(Entity.X, Entity.Y + move, CollisionsSolid);
                        if (c == null) {
                            if (JumpThroughCollider != null) {
                                var hasPlatformBelow = JumpThroughCollider.Overlap(Entity.X, Entity.Y + move, CollisionsJumpThrough);
                                if (hasPlatformBelow) {
                                    hasPlatformBelow = false;

                                    // Check to see if only bottom 1 pixel is overlapping jump throughs
                                    var platform = JumpThroughCollider.Collide(Entity.X, Entity.Y + move, CollisionsJumpThrough);
                                    if (!JumpThroughCollider.Overlap(Entity.X, Entity.Y, platform)) {
                                        // This makes sense just trust me ok
                                        hasPlatformBelow = true;
                                    }
                                }

                                if (hasPlatformBelow) {
                                    freeToMove = false;
                                    c = JumpThroughCollider.Collide(Entity.X, Entity.Y + move, CollisionsJumpThrough);
                                }
                            }
                        }
                        else {
                            freeToMove = false;
                        }
                    }
                    else {
                        c = collider.Collide(Entity.X, Entity.Y + move, CollisionsSolid);
                        if (c != null) {
                            freeToMove = false;
                        }
                    }

                    if (freeToMove) {
                        Entity.Y += move;
                        MoveBufferY = (int)Util.Approach(MoveBufferY, 0, SpeedScale);
                    }
                    else {
                        MoveBufferY = 0;
                        MoveCollideY(c);
                    }
                }
                if (collider == null || CollisionsSolid.Count == 0) {
                    Entity.Y += move;
                    MoveBufferY = (int)Util.Approach(MoveBufferY, 0, SpeedScale);
                }
            }
        }
Пример #10
0
 /// <summary>
 /// Called when MoveX collides with a collider.
 /// </summary>
 /// <param name="collider">The collider that was hit.</param>
 public virtual void MoveCollideX(Collider collider)
 {
     if (OnCollideX != null) {
         OnCollideX();
     }
 }
Пример #11
0
        /// <summary>
        /// Move the object in the Y axis by the value of speed. Sweeping collision test.
        /// </summary>
        /// <param name="speed">The speed to move by (remember SpeedScale is applied.)</param>
        /// <param name="collider">The Collider to use when moving.</param>
        public virtual void MoveY(int speed, Collider collider = null)
        {
            MoveBufferY += speed;

            while (Math.Abs(MoveBufferY) >= SpeedScale) {
                int move = Math.Sign(MoveBufferY);
                if (collider != null) {
                    Collider c = collider.Collide(Entity.X, Entity.Y + move, CollisionsSolid);
                    if (c == null) {
                        Entity.Y += move;
                        MoveBufferY = (int)Util.Approach(MoveBufferY, 0, SpeedScale);
                    }
                    else {
                        MoveBufferY = 0;
                        MoveCollideY(c);
                    }
                }
                if (collider == null || CollisionsSolid.Count == 0) {
                    Entity.Y += move;
                    MoveBufferY = (int)Util.Approach(MoveBufferY, 0, SpeedScale);
                }
            }
        }
Пример #12
0
 /// <summary>
 /// Shortcut to call both move x and move y.
 /// </summary>
 /// <param name="speedX"></param>
 /// <param name="speedY"></param>
 /// <param name="collider"></param>
 public void MoveXY(int speedX, int speedY, Collider collider = null)
 {
     MoveX(speedX, collider);
     MoveY(speedY, collider);
 }
Пример #13
0
        /// <summary>
        /// Move the object in the X axis by the value of speed. Sweeping collision test.
        /// </summary>
        /// <param name="speed">The speed to move by (remember SpeedScale is applied.)</param>
        /// <param name="collider">The Collider to use when moving.</param>
        public void MoveX(int speed, Collider collider = null)
        {
            MoveBufferX += speed;

            while (Math.Abs(MoveBufferX) >= SpeedScale) {
                int move = Math.Sign(MoveBufferX);
                if (collider != null) {
                    Collider c = collider.Collide(Entity.X + move, Entity.Y, CollidesWith);
                    if (c == null) {
                        Entity.X += move;
                        MoveBufferX = (int)Util.Approach(MoveBufferX, 0, SpeedScale);
                    }
                    else {
                        MoveBufferX = 0;
                        MoveCollideX(c);
                    }
                }
                if (collider == null || CollidesWith.Count == 0) {
                    Entity.X += move;
                    MoveBufferX = (int)Util.Approach(MoveBufferX, 0, SpeedScale);
                }

            }
        }