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
2017-01-21 03:18:55 +01:00

52 lines
1.5 KiB
C++

#include "character.hpp"
Character::Character(){
sf::Texture texture;
ASSERT(texture.loadFromFile(spriteFile));
height = texture.getSize().y;
width = texture.getSize().x;
next = timestamp = indexX = indexY = 0;
setTexture(texture);
sf::IntRect rect = sf::IntRect(indexX*width, indexY*height, width, height);
setTextureRect(rect);
actualState = previousState = PlayerState::idle;
}
void Character::update(float deltaTime){
timestamp += deltaTime;
if (timestamp >= frameTime){
timestamp = 0;
if (actualState == previousState){
if (actualState == PlayerState::idle){
indexX = (indexX+1)%numFrames;
}
else if (actualState == PlayerState::attacking){
if (indexX == width){
indexX = 0;
actualState = PlayerState::idle;
}
}
}
else {
indexX = 0;
if (actualState == PlayerState::idle){
indexY = 1;
actualState = PlayerState::attacking;
}
else if (actualState == PlayerState::attacking){
indexY = 0;
actualState = PlayerState::idle;
}
}
sf::IntRect rect = sf::IntRect(indexX*width, indexY*height, width, height);
setTextureRect(rect);
}
}
void Character::setState(PlayerState::playerState state){
previousState = actualState;
actualState = state;
}