Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Struktura Position

Antek Bizon edited this page Mar 5, 2022 · 1 revision

Struktura Position

Jej implementacja znajduje się w pliku Utils.h. Przechowuje w sobie informacje o pozycji obiektu, a także o jego rozmiarach i rozmiarach HitBoxa (używanych do kolizji). W zależności od ilości przekazanych informacji ustawiany jest również typ kształtu obiektu (RECTANGLE lub CIRCLE).

struct Position {
    enum class Type {
        RECTANGLE,
        CIRCLE
    } type;

    Vector2 center;
    float radius;
    Rectangle rectangle;
    Rectangle hbRectangle;
    Position(float x, float y, float width, float height)
        : type(Type::RECTANGLE), rectangle{x, y, width, height}, hbRectangle{0, 0, width, height} {}
    Position(float x, float y, float width, float height, float hbOffsetX, float hbOffsetY, float hbWidth, float hbHeight)
        : type(Type::RECTANGLE), rectangle{x, y, width, height}, hbRectangle{hbOffsetX, hbOffsetY, hbWidth, hbHeight} {}
    Position(float x, float y, float radius) : type(Type::CIRCLE), center{x, y}, radius{radius} {}
    Position(std::ifstream& input) {
        int t;
        Read(input, &t);
        type = static_cast<Position::Type>(t);

        if (type == Type::CIRCLE) {
            Read(input, &center.x);
            Read(input, &center.y);
            Read(input, &radius);
        }
        else {
            Read(input, &rectangle.x);
            Read(input, &rectangle.y);
            Read(input, &rectangle.width);
            Read(input, &rectangle.height);
            Read(input, &hbRectangle.x);
            Read(input, &hbRectangle.y);
            Read(input, &hbRectangle.width);
            Read(input, &hbRectangle.height);
        }
    }

    Rectangle hitBox() const {
        return {rectangle.x + hbRectangle.x, rectangle.y + hbRectangle.y, hbRectangle.width, hbRectangle.height};
    }

    void Save(std::ofstream& output) {
        int t = static_cast<int>(type);
        Write(output, &t);

        if (type == Type::CIRCLE) {
            Write(output, &center.x);
            Write(output, &center.y);
            Write(output, &radius);
        } else {
            Write(output, &rectangle.x);
            Write(output, &rectangle.y);
            Write(output, &rectangle.width);
            Write(output, &rectangle.height);            
            Write(output, &hbRectangle.x);
            Write(output, &hbRectangle.y);
            Write(output, &hbRectangle.width);
            Write(output, &hbRectangle.height);
        }
    }
};
Clone this wiki locally