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

Klasa Powerup

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

Klasa Powerup

Odpowiada za tworzenie nagród dla gracza. Wyróżniamy BOOST (zwiększenie prędkości poruszania się Player’a), DOUBLE (możliwość korzystania z dwóch harpunów jednocześnie lub podwajanie liczby min), HEAL (przywracanie jeden punkt życia), TIME (zatrzymywanie czasu), WEAPON (zmiana broni) i SCORE (dodatkowe punkty). Na Powerup’y działa grawitacja. Znikają po paru sekundach (funkcja timeLeft). Po kolizji z graczem znika i wywołuje funkcję PickAction w Game.

class Powerup : public Sprite {
public:
    enum Kind {
        BOOST,
        DOUBLE,
        HEAL,
        TIME,
        WEAPON,
        SCORE
    };

    Powerup(Game* game, float x, float y, Kind kind);
    Powerup(Game* game, std::ifstream&);
    virtual void Draw();
    virtual void Move();
    virtual void Collision(Sprite*);
    virtual void Save(std::ofstream&);
    void timeLeft();

private:
    Texture2D* texture;
    Kind kind;
    float color = 1;
    float speedY = 0;
    int frameCounter;
};

Powerup::Powerup(Game *game, float x, float y, Kind kind)
    : Sprite(game, Position(x, y, 0, 0), Type::POWERUP), kind(kind) {

    switch (kind) {
    case Kind::BOOST:
        texture = &game->textures[POWERUP_BOOST];
        break;
    case Kind::DOUBLE:
        texture = &game->textures[POWERUP_DOUBLE];
        break;
    case Kind::HEAL:
        texture = &game->textures[POWERUP_HEAL];
        break;
    case Kind::TIME:
        texture = &game->textures[POWERUP_TIME];
        break;
    case Kind::WEAPON:
        texture = &game->textures[POWERUP_WEAPON];
        break;
    case Kind::SCORE:
        texture = &game->textures[POWERUP_SCORE];
        break;
    }

    position.rectangle.width = texture->width;
    position.rectangle.height = texture->height;
    position.hbRectangle.width = texture->width;
    position.hbRectangle.height = texture->height;
    
    frameCounter = 0;
}

Powerup::Powerup(Game *game, std::ifstream& s)
    : Sprite(game, s, Type::POWERUP) {
    Read(s, &speedY);
    Read(s, &kind);
    Read(s, &frameCounter);

    switch (kind) {
    case Kind::BOOST:
        texture = &game->textures[POWERUP_BOOST];
        break;
    case Kind::DOUBLE:
        texture = &game->textures[POWERUP_DOUBLE];
        break;
    case Kind::HEAL:
        texture = &game->textures[POWERUP_HEAL];
        break;
    case Kind::TIME:
        texture = &game->textures[POWERUP_TIME];
        break;
    case Kind::WEAPON:
        texture = &game->textures[POWERUP_WEAPON];
        break;
    case Kind::SCORE:
        texture = &game->textures[POWERUP_SCORE];
        break;
    }

    position.rectangle.width = texture->width;
    position.rectangle.height = texture->height;
    position.hbRectangle.width = texture->width;
    position.hbRectangle.height = texture->height;
}

void Powerup::Draw() {
    if (state == State::ACTIVE) {
        timeLeft();
        DrawTexture(*texture, position.rectangle.x, position.rectangle.y, Fade(WHITE, color));
    }
}

void Powerup::Move() {
    if (state == State::ACTIVE) {
        speedY += 0.5f;
        position.rectangle.y += speedY;
    }
}

void Powerup::Collision(Sprite *sprite) {
    if (sprite->type == Type::BLOCK) {
        position.rectangle.y = sprite->position.rectangle.y - position.rectangle.height;
        speedY = 0;
    }
    else if (sprite->type == Type::PLAYER) {
        game->PickAction(kind);
        state = State::FINISHED;
    }
}

void Powerup::Save(std::ofstream& s) {
    Write(s, &type);
    position.Save(s);
    Write(s, &speedY);
    Write(s, &kind);
    Write(s, &frameCounter);
}

void Powerup::timeLeft() {
    if (frameCounter > 60 * 8)
        state = State::FINISHED;
    else if (frameCounter > 60 * 7 + 30)
        color += 0.03f;
    else if (frameCounter > 60 * 7)
        color -= 0.03f;
    else if (frameCounter > 60 * 6 + 30)
        color += 0.03f;
    else if (frameCounter > 60 * 6)
        color -= 0.03f;
    else if (frameCounter > 60 * 5 + 30)
        color += 0.03f;
    else if (frameCounter > 60 * 5)
        color -= 0.03f;

    frameCounter++;
}
Clone this wiki locally