Hello !
Alors après de nombreux tests j'ai pas réussi à avoir ce que je voulais, les frames n'apparaissent pas.
Donc du coup j'ai essayé avec un timer mais pas vraiment réussi à le faire non plus.
Merci d'avance !
Character.cpp#include "Character.h"
Character::Character(): m_health(100), m_mana(50), m_class(WARRIOR), m_XperiencePoints(0), m_score(0), m_healthMAX(100), m_manaMAX(50), m_XperiencePointsMAX(150), m_currentFrame(sf::Vector2i(0, 0), sf::Vector2i(32, 64)), m_isWalking(false){
/* All is initialised */
if(!m_texture_sprite.loadFromFile("resources\\2D\\tileset1.png"))
std::cout << "Can't load character texture !" << std::endl;
m_sprite.setTexture(m_texture_sprite);
m_sprite.setTextureRect(m_currentFrame);
}
Character::~Character(){ /* Nothing */ }
void Character::normalize(){
m_health = m_healthMAX;
m_mana = m_manaMAX;
m_XperiencePoints = 0;
m_score = 0;
}
//Animations
void Character::update(sf::Time elapsed){
m_currentTime += elapsed;
if (m_currentTime >= m_frameTime){
sf::IntRect downBreath(sf::Vector2i(32, 0), sf::Vector2i(32, 64));
sf::IntRect frame1(sf::Vector2i(0, 0), sf::Vector2i(32, 64));
sf::IntRect frame2(sf::Vector2i(64, 0), sf::Vector2i(48, 64));
sf::IntRect frame3(sf::Vector2i(128, 0), sf::Vector2i(48, 64));
if (m_isWalking == false){
std::cout << "BREATH" << std::endl;
m_frameTime = sf::microseconds(500);
if (m_currentFrame == frame1)
m_currentFrame = downBreath;
else
m_currentFrame = frame1;
}
if (m_isWalking){
std::cout << "WALKING" << std::endl;
m_frameTime = sf::microseconds(50);
if (m_currentFrame == frame1)
m_currentFrame = frame2;
else if (m_currentFrame == frame2)
m_currentFrame = frame3;
else if (m_currentFrame == frame3)
m_currentFrame = frame1;
}
m_currentTime = sf::microseconds(m_currentTime.asMicroseconds() % m_frameTime.asMicroseconds());
}
m_sprite.setTextureRect(m_currentFrame);
m_currentTime = sf::Time::Zero;
}
//Getters and Setters
void Character::setHealth(int health){
if(health <= m_healthMAX)
m_health = health;
}
void Character::setMana(int mana){
if(mana <= m_manaMAX)
m_mana = mana;
}
void Character::setXP(int xp){
if(xp <= m_XperiencePointsMAX)
m_XperiencePoints = xp;
}
void Character::setClass(int Nclass){ m_class = Nclass; }
void Character::setScore(int score){ m_score = score; }
void Character::enableWalking_A(bool then){ m_isWalking = then; }
void Character::setPosition(int x, int y){
m_sprite.setPosition(x, y);
// m_weapon.setPosition(m_sprite.getPosition().x + 40, m_sprite.getPosition().y + 38);
// m_armor2.setPosition(x, y);
}
void Character::move(int x, int y){
m_isWalking = true;
m_sprite.move(x, y);
// m_weapon.setPosition(m_sprite.getPosition().x + 40, m_sprite.getPosition().y + 38);
// m_armor2.amove(x, y);
}
int Character::getHealth(){ return m_health; }
int Character::getMana(){ return m_mana; }
int Character::getXP(){ return m_XperiencePoints; }
int Character::getClass(){ return m_class; }
int Character::getScore(){ return m_score; }
int Character::getPositionX(){ return m_sprite.getPosition().x; }
int Character::getPositionY(){ return m_sprite.getPosition().y; }
//Adders and Removers
void Character::addHealth(int add_health){
int resultADD;
resultADD = add_health + m_health;
if(resultADD <= m_healthMAX || resultADD > 0)
m_health += add_health;
}
void Character::addMana(int add_mana){
int resultADD;
resultADD = add_mana + m_mana;
if(resultADD <= m_manaMAX || resultADD > 0)
m_mana += add_mana;
}
void Character::addXP(int add_xp){
int resultADD;
resultADD = add_xp + m_XperiencePoints;
if(resultADD <= m_XperiencePointsMAX || resultADD > 0) //We don't loose XP beacause you try and retry and you learn from your mistakes
m_XperiencePoints += add_xp;
}
void Character::addScore(int add_score){
int resultADD;
resultADD = add_score + m_score;
if(resultADD > 0)
m_score += add_score;
}
Character.h#ifndef CHARACTER_H_INCLUDED
#define CHARACTER_H_INCLUDED
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Weapon.h"
#include "Armor.h"
class Character : public sf::Drawable{ //NAME : Dogwer :p
public:
Character();
~Character();
void normalize();
//Animations
void update(sf::Time elapsed);
//Enum for all
enum p_class{WARRIOR, WIZARD, ARCHER};
//Getters and Setters
void setHealth(int health);
void setMana(int mana);
void setXP(int xp);
void setClass(int Nclass);
void setScore(int score);
void enableWalking_A(bool then);
void setPosition(int x, int y);
void move(int x, int y);
int getHealth();
int getMana();
int getXP();
int getClass();
int getScore();
int getPositionX();
int getPositionY();
//Adders and Removers
//-- To remove, add with the inverse
void addHealth(int add_health);
void addMana(int add_mana);
void addXP(int add_xp);
void addScore(int add_score);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const{
target.draw(m_sprite, states);
}
int m_health, m_mana, m_class, m_XperiencePoints, m_score;
int m_healthMAX, m_manaMAX, m_XperiencePointsMAX;
bool m_isWalking;
sf::Texture m_texture_sprite;
sf::Sprite m_sprite;
sf::IntRect m_currentFrame;
sf::Time m_currentTime, m_frameTime;
// Weapon m_weapon;
// Armor m_armor2;
};
#endif // CHARACTER_H_INCLUDED
scene.cpp#include "Game.h"
Game::Game(sf::RenderWindow &screen){
m_character.setPosition(20, screen.getSize().y - 200);
if(!m_texture_tileset.loadFromFile("resources\\2D\\tileset1.png"))
std::cout << "Can't load tileset !" << std::endl;
if(!m_texture_background.loadFromFile("resources\\2D\\background.png"))
std::cout << "Can't load background image !" << std::endl;
m_sprite_background.setTexture(m_texture_background);
m_sprite_background.setScale(0.653, 0.6357);
}
Game::~Game(){ /* Nothing */ }
void Game::render(sf::RenderWindow &screen){
bool isPaused;
sf::View camera;
sf::View playground, shop;
PauseScreen pause_screen(screen);
playground = screen.getDefaultView();
// playground.setViewport(sf::FloatRect(0, -0.9f, 2.05f, 2.05f));
playground.setCenter(m_character.getPositionX() + 230, m_character.getPositionY() - 120);
playground.setSize(500, 500);
int clampX = (playground.getSize().x / 2), clampXMax = screen.getSize().x - (playground.getSize().x / 2);
sf::Event events;
// sf::Time frameTime, elapsedTime, actualTime;
// sf::Clock clock;
while(screen.isOpen()){
elapsedTime = clock.restart();
while(screen.pollEvent(events)){
if(events.type == sf::Event::Closed)
screen.close();
if (events.type == sf::Event::LostFocus)
isPaused = true;
if(events.type == sf::Event::KeyPressed){
switch(events.key.code){
case sf::Keyboard::Escape:
screen.close();
break;
case sf::Keyboard::Up:
//NOTHING
break;
case sf::Keyboard::Down:
//INTERACTION
break;
case sf::Keyboard::X:
//ATTACK
break;
}
}
}
if (!isPaused){
m_character.enableWalking_A(false);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
m_character.move(-5, 0);
if(m_character.getPositionX() < clampXMax)
playground.move(-5, 0);
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
m_character.move(5, 0);
if(m_character.getPositionX() > clampX)
playground.move(5, 0);
}
}
screen.clear(sf::Color::Blue);
m_character.update(frameTime);
screen.draw(m_sprite_background);
// std::cout << "X : " << playground.getCenter().x << std::endl << "Y : " << playground.getCenter().y << std::endl << std::endl;
if(m_character.getPositionX() < 0)
m_character.setPosition(0, screen.getSize().y - 200);
else if(m_character.getPositionX() + 32 > screen.getSize().x)
m_character.setPosition(screen.getSize().x - 32, screen.getSize().y - 200);
if(playground.getCenter().x < clampX)
playground.setCenter(clampX, playground.getCenter().y);
if(playground.getCenter().x > clampXMax)
playground.setCenter(clampXMax, playground.getCenter().y);
screen.draw(m_character);
screen.setView(playground);
if (isPaused){
screen.setView(screen.getDefaultView());
screen.draw(pause_screen);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
isPaused = false;
}
screen.display();
// actualTime = clock.getElapsedTime();
// if (actualTime - elapsedTime > 50)
}
}