Files
aee/source/game/bola.cpp
T

63 lines
1.5 KiB
C++

#include "game/bola.hpp"
#include <cstdlib>
#include "core/jail/jgame.hpp"
Bola::Bola(Jd8::Surface gfx, Prota* sam)
: Sprite(gfx) {
this->sam_ = sam;
entitat.frames.reserve(2);
entitat.frames.push_back({30, 155, 15, 15});
entitat.frames.push_back({45, 155, 15, 15});
entitat.animacions.resize(1);
entitat.animacions[0].frames = {0, 1};
this->cur_frame = 0;
this->o = 0;
this->cycles_per_frame_ = 4;
this->x = 20;
this->y = 100;
this->contador_ = 0;
}
void Bola::draw() {
if (this->contador_ == 0) {
Sprite::draw();
}
}
void Bola::update() {
if (this->contador_ == 0) {
// Augmentem la x
this->x++;
if (this->x == 280) {
this->contador_ = 200;
}
// Augmentem el frame
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;
}
}
// Comprovem si ha tocat a Sam
if (this->x > (this->sam_->x - 7) && this->x < (this->sam_->x + 7) && this->y > (this->sam_->y - 7) && this->y < (this->sam_->y + 7)) {
this->contador_ = 200;
Info::ctx.vida--;
if (Info::ctx.vida == 0) {
this->sam_->o = 5;
}
}
} else {
this->contador_--;
if (this->contador_ == 0) {
this->x = 20;
}
}
}