From 777384e51eaf66c9b5a81922e0ad0afe3b10909b Mon Sep 17 00:00:00 2001 From: serk Date: Sat, 21 Jan 2017 16:27:00 +0100 Subject: [PATCH] score --- VaporWaveWars/VaporWaveWars.pro | 6 ++-- VaporWaveWars/actor.cpp | 10 ------ VaporWaveWars/actor.hpp | 4 +-- VaporWaveWars/combat.cpp | 10 +++--- VaporWaveWars/combat.hpp | 2 ++ VaporWaveWars/score.cpp | 56 +++++++++++++++++++++++++++++++++ VaporWaveWars/score.hpp | 21 +++++++++++++ 7 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 VaporWaveWars/score.cpp create mode 100644 VaporWaveWars/score.hpp diff --git a/VaporWaveWars/VaporWaveWars.pro b/VaporWaveWars/VaporWaveWars.pro index d529735..1b2493a 100644 --- a/VaporWaveWars/VaporWaveWars.pro +++ b/VaporWaveWars/VaporWaveWars.pro @@ -16,7 +16,8 @@ SOURCES += main.cpp \ player.cpp \ iaenemy.cpp \ actor.cpp \ - soundmanager.cpp + soundmanager.cpp \ + score.cpp HEADERS += \ game.hpp \ @@ -31,4 +32,5 @@ HEADERS += \ player.hpp \ iaenemy.hpp \ actor.hpp \ - soundmanager.hpp + soundmanager.hpp \ + score.hpp diff --git a/VaporWaveWars/actor.cpp b/VaporWaveWars/actor.cpp index cbd6f6d..13ef76a 100644 --- a/VaporWaveWars/actor.cpp +++ b/VaporWaveWars/actor.cpp @@ -1,12 +1,10 @@ #include "actor.hpp" Actor::Actor() { - score = 0; animate = PlayerState::attacking; this->character = new Character(0); } Actor::Actor(int num) { - score = 0; animate = PlayerState::attacking; this->character = new Character(num); } @@ -36,11 +34,3 @@ bool Actor::hitBy(Compas enemy) const { Compas Actor::getAttack() const { return compas; } - -void Actor::upScore() { - ++score; -} - -int Actor::getScore() const { - return score; -} diff --git a/VaporWaveWars/actor.hpp b/VaporWaveWars/actor.hpp index 6d465ac..c850848 100644 --- a/VaporWaveWars/actor.hpp +++ b/VaporWaveWars/actor.hpp @@ -15,10 +15,8 @@ public: bool hitBy(Compas enemy) const; Compas getAttack() const; virtual bool event(sf::Event e) = 0; - void upScore(); - int getScore() const; + protected: - int score; Compas compas; PlayerState::playerState animate; virtual bool updateLogic(float deltaTime, sf::RenderWindow *window) = 0; diff --git a/VaporWaveWars/combat.cpp b/VaporWaveWars/combat.cpp index 3368410..b00994d 100644 --- a/VaporWaveWars/combat.cpp +++ b/VaporWaveWars/combat.cpp @@ -44,8 +44,8 @@ void Combat::draw(sf::RenderWindow *window) { window->draw(_background, &_shader); player->draw(window); enemy->draw(window); - window->draw(*scorePlayer); - window->draw(*scoreEnemy); + scorePlayer->draw(window); + scoreEnemy->draw(window); } void Combat::updateEvents(sf::Event e) { @@ -54,8 +54,7 @@ void Combat::updateEvents(sf::Event e) { if (!aux) { //end of player one ritm if (!attacking) { if(!player->hitBy(enemy->getAttack())) { - enemy->upScore(); - scoreEnemy->setScore(enemy->getScore()); + scoreEnemy->incrisScore(); } } else playerOneTurn = aux; @@ -72,8 +71,7 @@ void Combat::enemyManager(bool aux) { if (aux) { if (!attacking) { if(!enemy->hitBy(player->getAttack())) { - player->upScore(); - scorePlayer->setScore(player->getScore()); + scorePlayer->incrisScore(); } } else playerOneTurn = aux; diff --git a/VaporWaveWars/combat.hpp b/VaporWaveWars/combat.hpp index 3764fc1..70c23c9 100644 --- a/VaporWaveWars/combat.hpp +++ b/VaporWaveWars/combat.hpp @@ -8,6 +8,7 @@ #include "iaenemy.hpp" #include "actor.hpp" #include "soundmanager.hpp" +#include "score.hpp" class Combat : public Scene { public: @@ -29,6 +30,7 @@ private: sf::Sprite _background; sf::Shader _shader; + Score *scoreEnemy, *scorePlayer; void initShader(); void enemyManager(bool aux); }; diff --git a/VaporWaveWars/score.cpp b/VaporWaveWars/score.cpp new file mode 100644 index 0000000..088db66 --- /dev/null +++ b/VaporWaveWars/score.cpp @@ -0,0 +1,56 @@ +#include "score.hpp" + + +Score::Score() { + ASSERT(_texture.loadFromFile(_spriteFile)); + _scoreSprite.push_back(sf::Sprite()); + _scoreSprite[0].setTexture(_texture); + _score = 0; + setScore(); +} + + +Score::Score(int num) : Score() { + if (num == 0) { + _posX = 100; + _posY = 50; + } + else { + _posX = 600; + _posY = 50; + } + _scoreSprite[0].setPosition(_posX, _posY); +} + +void Score::draw(sf::RenderWindow *window){ + for (int i = 0; i < _scoreSprite.size(); ++i) + window->draw(_scoreSprite[i]); +} + +void Score::incrisScore() { + ++_score; + setScore(); +} + + +void Score::setScore() { + int height = _texture.getSize().y; + int width = _texture.getSize().x/10; + int s = _score; + for (int i = 0; i < _scoreSprite.size(); ++i) { + int num = s%10; + s = s/10; + sf::IntRect rect = sf::IntRect(num*width, 0, width, height); + _scoreSprite[i].setTextureRect(rect); + } + while (s > 0) { + int num = s%10; + s = s/10; + _scoreSprite.push_back(sf::Sprite()); + int i = _scoreSprite.size() - 1; + _scoreSprite[i].setTexture(_texture); + sf::IntRect rect = sf::IntRect(num*width, 0, width, height); + _scoreSprite[i].setTextureRect(rect); + _scoreSprite[i].setPosition(_posX-(width*i),_posY); + } +} diff --git a/VaporWaveWars/score.hpp b/VaporWaveWars/score.hpp new file mode 100644 index 0000000..63f87e7 --- /dev/null +++ b/VaporWaveWars/score.hpp @@ -0,0 +1,21 @@ +#ifndef SCORE_H +#define SCORE_H + +#include "commons.hpp" + +class Score { +public: + Score(); + Score(int num); + void draw(sf::RenderWindow *window); + void incrisScore(); +private: + std::vector _scoreSprite; + sf::Texture _texture; + const std::string _spriteFile = WORK_DIR+"Resources/numbers-spreadsheet.png"; + int _score; + int _posX, _posY; + void setScore(); +}; + +#endif // SCORE_H