This repository has been archived on 2022-12-14. You can view files and clone it, but cannot push or open issues or pull requests.
WaveGGJ17/VaporWaveWars/character.cpp
marti.lloveras 770739c352 Animacio 2p
2017-01-21 11:47:43 +01:00

54 lines
1.4 KiB
C++

#include "character.hpp"
Character::Character(int player){
playerNum = player;
ASSERT(texture.loadFromFile(spriteFile));
height = texture.getSize().y/5;
width = texture.getSize().x/5;
next = timestamp = indexX = 0;
indexY = magicNumber;
setTexture(texture);
sf::IntRect rect = sf::IntRect(indexX*width, indexY*height, width, height);
setTextureRect(rect);
actualState = previousState = PlayerState::idle;
if (playerNum == 0){
setPosition(posX1, posY1);
magicNumber = 0;
}
else{
setPosition(posX2, posY2);
magicNumber = 2;
}
}
void Character::update(float deltaTime){
timestamp += deltaTime;
if (timestamp >= frameTime){
timestamp = 0;
sf::IntRect rect = sf::IntRect(indexX*width, indexY*height, width, height);
setTextureRect(rect);
//Acabar automaticament la animacio de attack
if (actualState == PlayerState::attacking and indexX >= 3){
setState(PlayerState::idle);
}
else {
indexX = (indexX+1)%4;
}
}
}
void Character::setState(PlayerState::playerState state){
previousState = actualState;
actualState = state;
if (state == PlayerState::idle){
indexX = 0;
indexY = 0 + magicNumber;
}
else{
indexX = 0;
indexY = 1 + magicNumber;
}
}