继承SFML中的Transformable和Drawable

时间:2013-07-28 16:24:13

标签: c++ collision-detection game-physics sfml

我正在尝试从SFML中继承Transformable和Drawable,以便使我的对象......好,可转换和可绘制。我正在制作一个简单的突破游戏,但也许我会以错误的方式解决这个问题。这是我的代码:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

class Player : public sf::Transformable, public sf::Drawable {
    public:
        Player(int x, int y);
        ~Player() {};

        sf::RectangleShape p_rect;

        void doMovement(const sf::RenderWindow& window);
        sf::FloatRect getGlobalBounds() const;
    private:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
            states.transform *= getTransform();
            target.draw(p_rect, states);
        }

};

class Ball : public sf::Transformable, public sf::Drawable {
    public:
        Ball(int r, int x, int y);
        ~Ball() {};

        sf::CircleShape b_circle;

        void doXMovement();
        void doYMovement();
        bool doXCollisions(const Player& player);
        bool doYCollisions(const Player& player);
        sf::FloatRect getGlobalBounds() const;
    private:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
            states.transform *= getTransform();
            target.draw(b_circle, states);
        }

        bool right;
        bool up;
};

Player::Player(int x, int y) {
    p_rect = sf::RectangleShape(sf::Vector2f(x, y));
}

void Player::doMovement(const sf::RenderWindow& window) {
    setPosition(sf::Mouse::getPosition(window).x, 500);
    if (getPosition().x < 0)
        setPosition(0, 500);
    else if (getPosition().x > 720)
        setPosition(720, 500);
}

sf::FloatRect Player::getGlobalBounds() const {
    return getTransform().transformRect(p_rect.getGlobalBounds());
}

Ball::Ball(int r, int x, int y) {
    b_circle = sf::CircleShape(r);
    b_circle.setPosition(x, y);
    right = true;
    up = false;
}

void Ball::doXMovement() {
    if (right)
        move(1, 0);
    else
        move(-1, 0);
}

void Ball::doYMovement() {
    if (up)
        move(0, -1);
    else
        move(0, 1);
}

bool Ball::doXCollisions(const Player& player) {
    bool coll;
    if (getGlobalBounds().intersects(player.getGlobalBounds())) {
        right = !right;
        coll = true;
    } else
        coll = false;

    if (getPosition().x >= 800 - b_circle.getRadius())
        right = false;
    else if (getPosition().x <= 0)
        right = true;
    return coll;
}

bool Ball::doYCollisions(const Player& player) {
    bool coll;
    if (getGlobalBounds().intersects(player.getGlobalBounds())) {
        up = !up;
        coll = true;
    } else
        coll = false;
    if (getPosition().x <= 0)
        up = false;
    return coll;
}

sf::FloatRect Ball::getGlobalBounds() const {
    return getTransform().transformRect(b_circle.getGlobalBounds());
}

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Breakout");
    window.setMouseCursorVisible(false);
    Player player(80, 10);
    Ball ball(3, 100, 100);
    sf::Clock clock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        player.doMovement(window);
        if (clock.getElapsedTime().asMilliseconds() >= 3) {
            clock.restart();
            if (!ball.doYCollisions(player))
                ball.doXCollisions(player);
            ball.doYMovement();
            ball.doXMovement();
        }
        window.clear(sf::Color::Black);
        window.draw(player);
        window.draw(ball);
        window.display();
    }
    return 0;
}

现在按预期移动和绘制工作(差不多),但碰撞有点不稳定。首先是我的碰撞问题:

  1. 我是否需要像我一样实现getGlobalBounds函数?或者有更好的方法来处理Transformable和Drawable中包含的内容吗?
  2. 我应该直接对形状进行转换,还是应该像现在一样将转换传递给绘制函数?
  3. 绘图也发生了奇怪的事情,这可能是一个快速修复。现在,getPosition方法为我的ball对象返回了不正确的值。它返回的区域似乎向下移动了一点。可能是什么原因?

    感谢您提供任何帮助!

    编辑:欢迎任何一般的C ++技巧,我还是初学者。

1 个答案:

答案 0 :(得分:2)

如果我是你,我会定义一个名为TransformableAndDrawable的新类:

class TransformableAndDrawable : public sf::Transformable, public sf::Drawable {
    // Your code here
}

在这个类中,您应该定义可变形和可绘制类通常需要的所有成员。此外,在这个类中,您应该定义通常可以在transformable和drawable类中实现的所有方法。然后,您的类应继承自TransformableAndDrawable,如下所示:

class Player : TransformableAndDrawable {
    // Your code here
}

现在,第一个问题的答案是:如果它是一般方法,我将在TransformableAndDrawable类中实现给定方法,因此从TransformableAndDrawable继承的所有类都将具有此方法。

不是给出p_rectp_circle之类的不同名称,而是使用相同的名称命名这些成员,例如p_shape,这样您就不会有命名问题。另外,我相信你可以声明你的p_shape是一个祖先类或接口(我不知道你正在使用的库中定义了什么类)并且只在需要时指定形状的性质(无论它是否是圆形或矩形或其他东西)。

至于第二个问题:我喜欢你实施的方式,但你犯了两个错误:

  • 它不可扩展:我们想要一个通用的解决方案,这个类可以用于你现在和将来使用的任何形状,不是吗?
  • 它不够通用:当我想知道形状的全局边界时,我对形状的本质不感兴趣,我希望你的代码能够处理形状的本质而不知道它< / LI>

简而言之,您应该执行以下操作:

  1. 创建一个包装类,该类将继承自TransformableDrawable

  2. 在你的包装器类中,与形状的本质无关,尽可能通用,希望有一些类或接口是RectangleShapeCircleShape的祖先。

  3. 从包装器类继承所有可绘制和可转换的类,这样您就可以在类之间使用共享功能

  4. 如果你的包装类中的某些东西不适合从它继承的类,则覆盖该类中的方法。

  5. 编辑:

    我更详细地查看了您正在使用的库,发现有一个名为Shape的类,它是CircleShapeRectangleShape的祖先。因此,使用Shape而不是这些类,您的代码将更加通用和可重用。