Exemple #1
0
        public override CollisionResult TestCollision(PhysicalObject that, Vector2 thatNewPosition, float scrollRows)
        {
            var collisionResult = new CollisionResult();

            if (thatNewPosition.X < 0 ||
                thatNewPosition.X + that.Size.X > windowTilesSize.X ||
                thatNewPosition.Y < 0 ||
                thatNewPosition.Y + that.Size.Y > windowTilesSize.Y)
            {
                collisionResult.CollisionType = CollisionType.OffRoad;
                var x = Math.Min(thatNewPosition.X, windowTilesSize.X - that.Size.X);
                x = Math.Max(x, 0);
                var y = Math.Min(thatNewPosition.Y, windowTilesSize.Y - that.Size.Y);
                y = Math.Max(y, 0);
            }
            else
            {
                for (var deltaX = 0; deltaX < that.Size.X; deltaX++)
                {
                    for (var deltaY = 0; deltaY < that.Size.Y; deltaY++)
                    {
                        var x = (int)((thatNewPosition.X) / that.Size.X + deltaX);
                        var y = (int)((scrollRows / 2 + thatNewPosition.Y / that.Size.Y) + deltaY);

                        if ((x < 0 ||
                             x >= (windowTilesSize.X / 2)) ||
                            y < 0 ||
                            y >= (mapLines.Count() * 2))
                        {
                            collisionResult.CollisionType = CollisionType.OffRoad;
                            break;
                        }
                        else
                        {
                            var mapChar = mapLines[y][x];

                            if ("XD".Contains(mapChar))
                            {
                                collisionResult.CollisionType = CollisionType.Blocked;
                                break;
                            }
                        }
                    }
                }

                //var colorArray = new Color[(int)(that.Size.X * that.Size.Y)];
                //var extractRegion = new Rectangle(
                //    (int)Math.Round(thatNewPosition.X),
                //    (int)Math.Round(scrollRows + thatNewPosition.Y),
                //    (int)that.Size.X,
                //    (int)that.Size.Y);
                //path01.GetData<Color>(0, extractRegion, colorArray, 0, (int)(that.Size.X * that.Size.Y));
                //foreach (var color in colorArray)
                //{
                //    if (color != Color.White)
                //    {
                //        collisionResult.CollisionType = CollisionType.Blocked;
                //        break;
                //    }
                //}
            }

            return(collisionResult);
        }