#include "game/engendro.hpp"

#include <cstdlib>

#include "core/jail/jgame.hpp"

Engendro::Engendro(Jd8::Surface gfx, Uint16 x, Uint16 y)
    : Sprite(gfx) {
    entitat.frames.reserve(4);
    for (int py = 50; py <= 65; py += 15) {
        for (int px = 225; px <= 240; px += 15) {
            Frame f;
            f.w = 15;
            f.h = 15;
            f.x = px;
            f.y = py;
            entitat.frames.push_back(f);
        }
    }

    entitat.animacions.resize(1);
    entitat.animacions[0].frames = {0, 1, 2, 3, 2, 1};

    this->cur_frame = 0;
    this->vida_ = 18;
    this->x = x;
    this->y = y;
    this->o = 0;
    this->cycles_per_frame_ = 30;
}

auto Engendro::update() -> bool {
    bool mort = false;

    if (Jg::getCycleCounter() % this->cycles_per_frame_ == 0) {
        this->cur_frame++;
        if (this->cur_frame == entitat.animacions[this->o].frames.size()) {
            this->cur_frame = 0;
        }
        this->vida_--;
    }

    if (vida_ == 0) {
        mort = true;
    }

    return mort;
}
