Ejemplo n.º 1
0
        public void checkIfCollision(GameObject obj, Direction dir, int speed)
        {
            int xmin = obj._x;
            int xmax = obj._x + obj._width;
            int ymin = obj._y;
            int ymax = obj._y + obj._height;

            //check if hitting border
            switch (dir)
            {
                case Direction.UP:
                    if (ymin - speed < 0)
                    {
                        moveObject(obj, dir, 0 + ymin - speed);
                        obj._isColliding = true;
                    }
                    break;
                case Direction.DOWN:
                    if (ymax + speed > Engine.TILE_HEIGHT * Engine.MAP_HEIGHT)
                    {
                        moveObject(obj, dir, (Engine.TILE_HEIGHT * Engine.MAP_HEIGHT) - ymax - speed);
                        obj._isColliding = true;
                    }
                    break;
                case Direction.LEFT:
                    if (xmin - speed < 0)
                    {
                        moveObject(obj, dir, 0 + xmin - speed);
                        obj._isColliding = true;
                    }
                    break;
                case Direction.RIGHT:
                    if (xmax + speed > Engine.TILE_WIDTH * Engine.MAP_LENGTH)
                    {
                        moveObject(obj, dir, (Engine.TILE_WIDTH * Engine.MAP_LENGTH) - xmax - speed);
                        obj._isColliding = true;
                    }
                    break;
            }

            //check if hitting collidable background
            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    switch (dir)
                    {
                        case Direction.UP:
                            if (map[i, j]._collider)
                            {
                                if ((ymin - speed >= map[i, j]._y && ymin - speed <= map[i, j]._y + map[i, j]._height) &&
                                    ((xmin >= map[i, j]._x && xmin <= map[i, j]._x + map[i, j]._width) || 
                                    (xmax >= map[i, j]._x && xmax <= map[i, j]._x + map[i, j]._width)))
                                {
                                    moveObject(obj, dir, ymin - (map[i, j]._y + map[i, j]._height) - speed - 1);
                                    obj._isColliding = true;
                                }
                            }
                            break;
                        case Direction.DOWN:
                            if (map[i, j]._collider)
                            {
                                if ((ymax + speed >= map[i, j]._y && ymax + speed <= map[i, j]._y + map[i, j]._height) &&
                                    ((xmin >= map[i, j]._x && xmin <= map[i, j]._x + map[i, j]._width) || 
                                    (xmax >= map[i, j]._x && xmax <= map[i, j]._x + map[i, j]._width)))
                                {
                                    moveObject(obj, dir, (map[i, j]._y) - ymax - speed - 1);
                                    obj._isColliding = true;
                                }
                            }
                            break;
                        case Direction.LEFT:
                            if (map[i, j]._collider)
                            {
                                if ((xmin - speed >= map[i, j]._x && xmin - speed <= map[i, j]._x + map[i, j]._width) &&
                                    ((ymin >= map[i, j]._y && ymin <= map[i, j]._y + map[i, j]._height) || 
                                    (ymax >= map[i, j]._y && ymax <= map[i, j]._y + map[i, j]._height)))
                                {
                                    moveObject(obj, dir, xmin - (map[i, j]._x + map[i, j]._width) - speed - 1);
                                    obj._isColliding = true;
                                }
                            }
                            break;
                        case Direction.RIGHT:
                            if (map[i, j]._collider)
                            {
                                if ((xmax + speed >= map[i, j]._x && xmax + speed <= map[i, j]._x + map[i, j]._width) &&
                                    ((ymin >= map[i, j]._y && ymin <= map[i, j]._y + map[i, j]._height) || 
                                    (ymax >= map[i, j]._y && ymax <= map[i, j]._y + map[i, j]._height)))
                                {
                                    moveObject(obj, dir, (map[i, j]._x) - xmax - speed - 1);
                                    obj._isColliding = true;
                                }
                            }
                            break;
                    }
                }
            }

            //check if colliding with collidable gameobject
            for (int i = 0; i < gameObjects.Count; i++)
            {
                switch (dir)
                {
                    case Direction.UP:
                        if (gameObjects[i]._collider)
                        {
                            if ((ymin - speed >= gameObjects[i]._y && ymin - speed <= gameObjects[i]._y + gameObjects[i]._height) &&
                                ((xmin >= gameObjects[i]._x && xmin <= gameObjects[i]._x +gameObjects[i]._width) || 
                                (xmax >=gameObjects[i]._x && xmax <=gameObjects[i]._x +gameObjects[i]._width)))
                            {
                                moveObject(obj, dir, ymin - (gameObjects[i]._y +gameObjects[i]._height) - speed - 1);
                                obj._isColliding = true;
                            }
                        }
                        break;
                    case Direction.DOWN:
                        if (gameObjects[i]._collider)
                        {
                            if ((ymax + speed >=gameObjects[i]._y && ymax + speed <=gameObjects[i]._y +gameObjects[i]._height) &&
                                ((xmin >=gameObjects[i]._x && xmin <=gameObjects[i]._x +gameObjects[i]._width) || 
                                (xmax >=gameObjects[i]._x && xmax <=gameObjects[i]._x +gameObjects[i]._width)))
                            {
                                moveObject(obj, dir, (gameObjects[i]._y) - ymax - speed - 1);
                                obj._isColliding = true;
                            }
                        }
                        break;
                    case Direction.LEFT:
                        if (gameObjects[i]._collider)
                        {
                            if ((xmin - speed >=gameObjects[i]._x && xmin - speed <=gameObjects[i]._x +gameObjects[i]._width) &&
                                ((ymin >=gameObjects[i]._y && ymin <=gameObjects[i]._y +gameObjects[i]._height) || 
                                (ymax >=gameObjects[i]._y && ymax <=gameObjects[i]._y +gameObjects[i]._height)))
                            {
                                moveObject(obj, dir, xmin - (gameObjects[i]._x +gameObjects[i]._width) - speed - 1);
                                obj._isColliding = true;
                            }
                        }
                        break;
                    case Direction.RIGHT:
                        if (gameObjects[i]._collider)
                        {
                            if ((xmax + speed >=gameObjects[i]._x && xmax + speed <=gameObjects[i]._x +gameObjects[i]._width) &&
                                ((ymin >=gameObjects[i]._y && ymin <=gameObjects[i]._y +gameObjects[i]._height) || 
                                (ymax >=gameObjects[i]._y && ymax <=gameObjects[i]._y +gameObjects[i]._height)))
                            {
                                moveObject(obj, dir, (gameObjects[i]._x) - xmax - speed - 1);
                                obj._isColliding = true;
                            }
                        }
                        break;
                }
            }

            moveObject(obj, dir, speed);

        }
Ejemplo n.º 2
0
 public void moveObject(GameObject obj, Direction dir, int speed)
 {
     switch (dir)
     {
         case Direction.UP:
             obj._y -= speed;
             break;
         case Direction.DOWN:
             obj._y += speed;
             break;
         case Direction.LEFT:
             obj._x -= speed;
             break;
         case Direction.RIGHT:
             obj._x += speed;
             break;
     }
 }