Refactor of turn managing
This commit is contained in:
parent
01dcd14236
commit
b9a52e6192
6 changed files with 83 additions and 35 deletions
|
@ -3,7 +3,7 @@
|
|||
Combat::Combat() {
|
||||
ia = false;
|
||||
// ia = true;
|
||||
attacking = playerOneTurn = true;
|
||||
state = CombatState::player_atk;
|
||||
player = new Player(0);
|
||||
// enemy = new IaEnemy(1);
|
||||
enemy = new Player(1);
|
||||
|
@ -18,12 +18,38 @@ Combat::Combat() {
|
|||
Combat::Combat(bool ia) {
|
||||
this->ia = ia;
|
||||
player = new Player(0);
|
||||
attacking = playerOneTurn = true;
|
||||
state = CombatState::player_atk;
|
||||
if (ia) enemy = new IaEnemy(1);
|
||||
else enemy = new Player(1);
|
||||
initShader();
|
||||
}
|
||||
|
||||
bool Combat::isAttack() const {
|
||||
return state == CombatState::player_atk or state == CombatState::enemy_atk;
|
||||
}
|
||||
|
||||
bool Combat::isPlayerOne() const {
|
||||
return state == CombatState::player_def or state == CombatState::player_atk;
|
||||
}
|
||||
|
||||
void Combat::updateHalo() {
|
||||
switch(state) {
|
||||
case CombatState::player_def:
|
||||
case CombatState::enemy_def:
|
||||
_shaderHalo.setParameter("type", 0.0f);
|
||||
break;
|
||||
case CombatState::enemy_atk:
|
||||
_shaderHalo.setParameter("type", 1.0f);
|
||||
break;
|
||||
case CombatState::player_atk:
|
||||
_shaderHalo.setParameter("type", 2.0f);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Combat::initShader() {
|
||||
time = 0;
|
||||
_text.create(W_WIDTH, W_HEIGHT);
|
||||
|
@ -42,9 +68,8 @@ void Combat::initShader() {
|
|||
sf::IntRect rect = sf::IntRect(0, 0, _plataformT.getSize().x/2, _plataformT.getSize().y);
|
||||
_plataform.setTextureRect(rect);
|
||||
_shaderHalo.loadFromFile(WORK_DIR+"Resources/halo.frag", sf::Shader::Fragment);
|
||||
_shaderHalo.setParameter("type", 0.0f);
|
||||
_shaderHalo.setParameter("time", time);
|
||||
|
||||
updateHalo();
|
||||
}
|
||||
|
||||
void Combat::update(float deltaTime, sf::RenderWindow *window) {
|
||||
|
@ -58,7 +83,7 @@ void Combat::update(float deltaTime, sf::RenderWindow *window) {
|
|||
|
||||
|
||||
|
||||
if (playerOneTurn) {
|
||||
if (isPlayerOne()) {
|
||||
if(_halo.getPosition().x != W_WIDTH*0.05f)
|
||||
animationTo(false, deltaTime);
|
||||
}
|
||||
|
@ -67,14 +92,10 @@ void Combat::update(float deltaTime, sf::RenderWindow *window) {
|
|||
animationTo(true, deltaTime);
|
||||
}
|
||||
|
||||
if (!attacking) _shaderHalo.setParameter("type", 0.0f);
|
||||
else {
|
||||
if (playerOneTurn)_shaderHalo.setParameter("type", 2.0f);
|
||||
else _shaderHalo.setParameter("type", 1.0f);
|
||||
}
|
||||
updateHalo();
|
||||
|
||||
sf::IntRect rect;
|
||||
if (playerOneTurn)
|
||||
if (isPlayerOne())
|
||||
rect = sf::IntRect(0, 0, _plataformT.getSize().x/2, _plataformT.getSize().y);
|
||||
else
|
||||
rect = sf::IntRect(_plataformT.getSize().x/2, 0, _plataformT.getSize().x/2, _plataformT.getSize().y);
|
||||
|
@ -93,34 +114,38 @@ void Combat::draw(sf::RenderWindow *window) {
|
|||
}
|
||||
|
||||
void Combat::updateEvents(sf::Event e) {
|
||||
if (playerOneTurn) {
|
||||
bool aux = player->event(e);
|
||||
if (!aux) { //end of player one ritm
|
||||
|
||||
if (!attacking && !ia) {
|
||||
if(!player->hitBy(enemy->getAttack())) {
|
||||
scoreEnemy->incrisScore();
|
||||
}
|
||||
}
|
||||
else playerOneTurn = aux;
|
||||
attacking = !attacking;
|
||||
}
|
||||
if (isPlayerOne()) {
|
||||
bool compasFinish = !player->event(e);
|
||||
enemyManager(compasFinish);
|
||||
}
|
||||
else if (!ia) {
|
||||
bool aux = !enemy->event(e);
|
||||
enemyManager(aux); //end of player two not ia ritm
|
||||
bool compasFinish = !enemy->event(e);
|
||||
enemyManager(compasFinish);
|
||||
}
|
||||
}
|
||||
|
||||
void Combat::enemyManager(bool aux) {
|
||||
if (aux) {
|
||||
if (!attacking) {
|
||||
if(!enemy->hitBy(player->getAttack())) {
|
||||
void Combat::enemyManager(bool compasFinish) {
|
||||
if(compasFinish) {
|
||||
Compas compas;
|
||||
if(isPlayerOne()) compas = player->getAttack();
|
||||
else compas = enemy->getAttack();
|
||||
|
||||
if(!isAttack()) {
|
||||
if(!ia) {
|
||||
bool hit;
|
||||
if(isPlayerOne()) hit = !player->hitBy(compas);
|
||||
else hit = !enemy->hitBy(compas);
|
||||
if(!hit) {
|
||||
if(isPlayerOne())
|
||||
scoreEnemy->incrisScore();
|
||||
else
|
||||
scorePlayer->incrisScore();
|
||||
}
|
||||
}
|
||||
else playerOneTurn = aux;
|
||||
attacking = !attacking;
|
||||
}
|
||||
else if(compas.isFailed())
|
||||
state = (CombatState::combatState) ((((int) state)+1) % 4);
|
||||
state = (CombatState::combatState) ((((int) state)+1) % 4);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ public:
|
|||
void draw(sf::RenderWindow *window) final override;
|
||||
void updateEvents(sf::Event e) final override;
|
||||
private:
|
||||
bool playerOneTurn, ia, attacking;
|
||||
bool ia;
|
||||
CombatState::combatState state;
|
||||
Actor *player, *enemy;
|
||||
float time;
|
||||
|
||||
|
@ -34,6 +35,9 @@ private:
|
|||
void initShader();
|
||||
void enemyManager(bool aux);
|
||||
void animationTo(bool toEnemy, float deltaTime);
|
||||
bool isAttack() const;
|
||||
bool isPlayerOne() const;
|
||||
void updateHalo();
|
||||
};
|
||||
|
||||
#endif // COMBAT_H
|
||||
|
|
|
@ -45,4 +45,8 @@ namespace PlayerState {
|
|||
enum playerState{idle, attacking, inMidle, hurt};
|
||||
}
|
||||
|
||||
namespace CombatState {
|
||||
enum combatState{player_def = 0, player_atk = 1, enemy_def = 2, enemy_atk = 3};
|
||||
}
|
||||
|
||||
#endif // COMMONS_HPP
|
||||
|
|
|
@ -3,9 +3,11 @@
|
|||
Compas::Compas() {
|
||||
spaceTime = 1;
|
||||
isPress = false;
|
||||
failed = false;
|
||||
}
|
||||
|
||||
void Compas::start() {
|
||||
failed = false;
|
||||
if (not isPress) {
|
||||
// std::cout << "start" << std::endl;
|
||||
isPress = true;
|
||||
|
@ -32,6 +34,11 @@ void Compas::end() {
|
|||
}
|
||||
}
|
||||
|
||||
void Compas::fail() {
|
||||
failed = true;
|
||||
end();
|
||||
}
|
||||
|
||||
void Compas::incraeseTime() {
|
||||
++spaceTime;
|
||||
}
|
||||
|
@ -40,6 +47,10 @@ bool Compas::isPressed() const {
|
|||
return isPress;
|
||||
}
|
||||
|
||||
bool Compas::isFailed() const {
|
||||
return failed;
|
||||
}
|
||||
|
||||
int Compas::get (int i) const {
|
||||
return notes[i];
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
class Compas {
|
||||
private:
|
||||
bool isPress;
|
||||
bool failed;
|
||||
int spaceTime;
|
||||
std::vector<int> notes;
|
||||
int get(int i) const;
|
||||
|
@ -19,8 +20,10 @@ public:
|
|||
void start();
|
||||
void add();
|
||||
void end();
|
||||
void fail();
|
||||
void incraeseTime();
|
||||
bool isPressed() const;
|
||||
bool isFailed() const;
|
||||
bool operator ==(const Compas& d) const;
|
||||
};
|
||||
|
||||
|
|
|
@ -35,15 +35,16 @@ bool Player::event(sf::Event e) {
|
|||
}
|
||||
else {
|
||||
if (!error) {
|
||||
compas.end();
|
||||
compas.fail();
|
||||
animate = PlayerState::hurt;
|
||||
error = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (sf::Event::KeyReleased):
|
||||
if (e.key.code == sf::Keyboard::C) {
|
||||
if (compas.isPressed() and e.key.code == sf::Keyboard::C) {
|
||||
compas.end();
|
||||
return false;
|
||||
}
|
||||
|
|
Reference in a new issue