From: cboissie Date: Tue, 24 May 2011 20:09:16 +0000 (+0200) Subject: -Player can lose lives X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=bc5d95a30461297c68a4062310007d1f10d89630;p=Bug_Attack.git -Player can lose lives -Enemy class modified in a more generic way --- diff --git a/defenser.cpp b/defenser.cpp index b7feeec..4941cb5 100644 --- a/defenser.cpp +++ b/defenser.cpp @@ -84,8 +84,8 @@ void WaterGun::advance(int phase) { float targetX = (*i)->x(); float targetY = (*i)->y(); - if(sqrt(pow(fabs(this->x() - targetX),2) - +pow(fabs(this->y() - targetY),2) + if(sqrt(pow(fabs(this->x() - targetX - ((*i)->getSize()-1)*16),2) + +pow(fabs(this->y() - targetY - ((*i)->getSize()-1)*16),2) ) <= _range) { // Si le défenseur ne tirait pas encore diff --git a/enemy.cpp b/enemy.cpp index 94608e4..f417193 100644 --- a/enemy.cpp +++ b/enemy.cpp @@ -60,47 +60,7 @@ QRectF Enemy::boundingRect(void) const { return QRectF(0,0,_size*32,_size*32); } - -/** -* CAFARD -**/ - - -Cafard::Cafard(Map* map,int posx,int posy, float size):Enemy(map,posx,posy,size) -{ - // Données du cafard - _type = TYPE::T_RAMPANT; - _hp = 10*size*size; - _hpMax = _hp; - _resistance = 5*size*size; - _speed = 2; - - // Images et animations - for(int i = 0; i < 3; ++i) { - QPixmap* animTemp = new QPixmap("./media/textures/ennemies/cafard" + QString::number(i+1) + ".png"); - *animTemp = animTemp->scaled(size*32,size*32); - _animPixmap.push_back(animTemp); - } - _animState = 0; // Première frame de l'animation à 0 - - // Image et taille - this->setPixmap(*_animPixmap.first()); -} - -void Cafard::hurt(int damages) { - Enemy::hurt(damages); - if(_hp <= 0 && _size >= 2) { - // A la mort d'un cafard, on créé deux cafards de taille/2 - Cafard *temp; - temp = new Cafard(_map, this->x()+ (_size-1)*16,this->y()+ (_size-1)*16, _size / 2); - // ... décale le cafard de quelques pixels - for(int i = 0 ; i < 8 ; ++i) temp->advance(1); - temp = new Cafard(_map,this->x()+ (_size-1)*16,this->y()+ (_size-1)*16, _size / 2); - } - -} - -void Cafard::advance(int phase) { +void Enemy::advance(int phase) { // Si 'phase' vaut 0, rien ne se passe if(!phase) @@ -115,6 +75,18 @@ void Cafard::advance(int phase) { // Déplace l'ennemi selon sa vitesse et la direction induite par le terrain Tile& currentTile = _map->getTileAt((this->x() + _size*16)/32,(this->y() + _size*16)/32); + + + + // L'ennemi est arrivé au "goal", on doit le détruire + if(currentTile.getTileNumber() == TYPE::GOAL) { + this->reachGoal(); + return; + } + + + + // Déplacement normal QPointF dirVect = currentTile.getDirection(); float new_x = this->x() + _speed*dirVect.x(); @@ -143,6 +115,61 @@ void Cafard::advance(int phase) { this->setRotation(90); else if(dirVect.y() == -1) this->setRotation(-90); + +} + + +void Enemy::reachGoal(void) { + + // Indique que la mort est provoquée par l'atteinte du goal + emit killedAtGoal(1); // Par défaut 1 vie perdue + // Retire de l'indexation de la map + emit enemyDestroyed(this); + +} + + +/** +* CAFARD +**/ + + +Cafard::Cafard(Map* map,int posx,int posy, float size):Enemy(map,posx,posy,size) +{ + // Données du cafard + _type = TYPE::T_RAMPANT; + _hp = 10*size*size; + _hpMax = _hp; + _resistance = 5*size*size; + _speed = 2; + + // Images et animations + for(int i = 0; i < 3; ++i) { + QPixmap* animTemp = new QPixmap("./media/textures/ennemies/cafard" + QString::number(i+1) + ".png"); + *animTemp = animTemp->scaled(size*32,size*32); + _animPixmap.push_back(animTemp); + } + _animState = 0; // Première frame de l'animation à 0 + + // Image et taille + this->setPixmap(*_animPixmap.first()); +} + +void Cafard::hurt(int damages) { + Enemy::hurt(damages); + if(_hp <= 0 && _size >= 2) { + // A la mort d'un cafard, on créé deux cafards de taille/2 + Cafard *temp; + temp = new Cafard(_map, this->x()+ (_size-1)*16,this->y()+ (_size-1)*16, _size / 2); + // ... décale le cafard de quelques pixels + for(int i = 0 ; i < 8 ; ++i) temp->advance(1); + temp = new Cafard(_map,this->x()+ (_size-1)*16,this->y()+ (_size-1)*16, _size / 2); + } + +} + +void Cafard::advance(int phase) { + Enemy::advance(phase); } diff --git a/enemy.h b/enemy.h index 4eed53c..62dd194 100644 --- a/enemy.h +++ b/enemy.h @@ -35,9 +35,13 @@ protected: int _resistance; float _speed; + virtual void advance(int phase); + void reachGoal(void); + signals: void killedByPlayer(int moneyEarned); + void killedAtGoal(int livesLost); void enemyDestroyed(Enemy* ptr); }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 63d56f6..c483744 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -46,9 +46,12 @@ MainWindow::MainWindow(QWidget *parent) : QObject::connect(_sceneMap->getWaveGenerator(),SIGNAL(setLaunchWaveButtonEnabled(bool)),ui->launchWaveButton,SLOT(setEnabled(bool))); // Gestions des compteurs + // Argent ui->creditCounter->display(_player->getMoney()); // C'est du vol QObject::connect(_player,SIGNAL(moneyValueChanged(int)),ui->creditCounter,SLOT(display(int))); - // QObject::connect(_sceneMap,SIGNAL(enemyDestroyed(int)),_player,SLOT(earnMoney(int))); + // Vies + ui->livesCounter->display(_player->getLives()); + QObject::connect(_player,SIGNAL(livesValueChanged(int)),ui->livesCounter,SLOT(display(int))); /** * Divers diff --git a/mainwindow.ui b/mainwindow.ui index 47bf477..2e2fb93 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -103,14 +103,14 @@ - + - TextLabel + Vies - + diff --git a/map.cpp b/map.cpp index 38713d0..c7c685e 100644 --- a/map.cpp +++ b/map.cpp @@ -58,8 +58,11 @@ void Map::addEnemy(Enemy *enemy) { // Sauvegarde des ennemis sur la map _enemies.push_back(enemy); + // En cas de destruction par le joueur, rapport de l'argent QObject::connect(enemy,SIGNAL(killedByPlayer(int)),_player,SLOT(earnMoney(int))); + // Si l'ennemi atteint le goal, il est détruit et fait perdre une vie au joueur + QObject::connect(enemy,SIGNAL(killedAtGoal(int)),_player,SLOT(loseLives(int))); // En cas de suppression de la mémoire, on doit aussi le désindexer depuis 'enemies' QObject::connect(enemy,SIGNAL(enemyDestroyed(Enemy*)),this,SLOT(removeEnemy(Enemy*))); diff --git a/media/maps/waves.txt b/media/maps/waves.txt index 7014931..100ee8e 100644 --- a/media/maps/waves.txt +++ b/media/maps/waves.txt @@ -1,6 +1,6 @@ -2x fourmis;fourmi:0.5:5:1 +2x fourmis;fourmi:1:10:5 15x cafards;cafard:2:15:5 -15x GIANT GIANT cafards;cafard:8:1:5 +15x GIANT GIANT cafards;cafard:2:1:5 15x guepes;guepe:1:15:5 15x moustiques;moustique:1:15:5 15x fourmis, 1x GIANT guepes;fourmi:1:15:5;guepe:1.7:1:5 diff --git a/player.cpp b/player.cpp index 3f9298a..18df21c 100644 --- a/player.cpp +++ b/player.cpp @@ -10,7 +10,8 @@ Player::Player(int money,int lives){ } -int Player::getMoney(void) { return _money; } +int Player::getMoney(void) const { return _money; } +int Player::getLives(void) const { return _lives; } void Player::earnMoney(int amount) { @@ -22,6 +23,17 @@ void Player::earnMoney(int amount) { } } +void Player::loseLives(int livesLost) { + + // Fait perdre des vies au joueur + // ... et le compteur est notifié + _lives-=livesLost; + if(_lives < 0) + _lives = 0; + + emit livesValueChanged(_lives); +} + bool Player::payMoney(int amount) { // Vérifie si le joueur peut payer diff --git a/player.h b/player.h index d79c55d..ac64172 100644 --- a/player.h +++ b/player.h @@ -17,14 +17,17 @@ public: Player(int money = DEFAULT_MONEY,int lives = DEFAULT_LIVES); bool payMoney(int amount); - int getMoney(void); + int getMoney(void) const; + int getLives(void) const; public slots: void earnMoney(int amount); + void loseLives(int livesLost); signals: void moneyValueChanged(int); + void livesValueChanged(int); }; #endif // PLAYER_H diff --git a/types.h b/types.h index 938a254..19bbba7 100644 --- a/types.h +++ b/types.h @@ -6,7 +6,7 @@ namespace TYPE { // Utilisation du bit 2^7 pour coder une information supplémentaire // concernant les diagonales (cf doc) -typedef enum { NORTH = 1 , SOUTH = 2 , EAST = 4 , WEST = 8, UP = 128 } DIRECTION; +typedef enum { NORTH = 1 , SOUTH = 2 , EAST = 4 , WEST = 8, GOAL = 32, UP = 128 } DIRECTION; typedef enum { T_RAMPANT = 1, T_VOLANT = 2 } ENTITY; typedef enum { CAFARD } ENEMY;