Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- level_1_97x97.png: https://image.ibb.co/caKMzx/level_1_97x97.png
- uganda_textures_one_texture_144x164.png: https://image.ibb.co/h9YP2H/uganda_textures_one_texture_144x164.png
- */
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <string>
- enum Direction {
- Forward,
- Backward,
- Stand
- };
- class Character {
- protected:
- sf::Vector2u window_size;
- std::string path_to_texture;
- sf::Image image;
- sf::Texture texture;
- sf::Sprite sprite;
- int texture_width, texture_height;
- int texture_number;
- class Animation {
- public:
- double texture;
- int texture_number;
- int start_move_texture;
- bool is_last = false;
- Animation(int texture_number, int start_move_texture) {
- this->texture_number = texture_number;
- texture = start_move_texture;
- this->start_move_texture = start_move_texture;
- }
- int GetCharacterTexture(double time) {
- if (is_last) {
- texture = start_move_texture;
- is_last = false;
- }
- if (texture < start_move_texture) {
- texture = start_move_texture;
- }
- texture += (time / 100000);
- if (texture >= texture_number) {
- texture = texture_number - 1;
- is_last = true;
- }
- return texture;
- }
- };
- Animation *character_animation;
- sf::Clock clock;
- double time = 1;
- double character_speed = 1;
- sf::Vector2f character_last_position;
- sf::Vector2f character_dimensions; //габариты персонажа
- void BehindTheMap() {
- if (sprite.getPosition().x > window_size.x - character_dimensions.x || sprite.getPosition().x < 0) {
- sprite.setPosition(character_last_position.x, character_last_position.y);
- }
- }
- public:
- Character(std::string path_to_texture, int texture_width, int texture_height, int start_move_texture, sf::Vector2f start_position, sf::Vector2u window_size) {
- this->path_to_texture = path_to_texture;
- image.loadFromFile(path_to_texture);
- image.createMaskFromColor(sf::Color(0, 14, 88));
- texture.loadFromImage(image);
- sprite.setTexture(texture);
- sprite.setPosition(start_position);
- character_dimensions.x = this->texture_width = texture_width;
- character_dimensions.y = this->texture_height = texture_height;
- this->window_size = window_size;
- SetTextureForSprite(1, 1);
- texture_number = image.getSize().x / texture_width;
- character_animation = new Animation(texture_number, start_move_texture);
- }
- ~Character() {
- if (character_animation != nullptr) {
- delete character_animation;
- }
- }
- virtual sf::Sprite GetSprite() const {
- return sprite;
- }
- virtual void SetTextureForSprite(int x, int y) {
- sprite.setTextureRect(sf::IntRect(x, y, texture_width, texture_height));
- }
- virtual void SetCharacterSpeed(double character_speed) {
- if (character_speed >= 0) {
- this->character_speed = character_speed;
- }
- }
- virtual double GetCharacterSpeed() const {
- return character_speed;
- }
- virtual void SetTime() {
- time = clock.getElapsedTime().asMicroseconds();
- clock.restart();
- }
- void CharacterDirection(Direction direction) {
- if (direction == Direction::Forward) {
- sprite.setOrigin(0, 0);
- sprite.setScale(character_dimensions.x / texture_width, character_dimensions.y / texture_height);
- }
- else if (direction == Direction::Backward) {
- sprite.setOrigin(texture_width, 0);
- sprite.setScale(-character_dimensions.x / texture_width, character_dimensions.y / texture_height);
- }
- }
- virtual void Move(Direction direction) {
- if (direction != Direction::Stand) {
- character_last_position = sprite.getPosition();
- if (direction == Direction::Forward) {
- CharacterDirection(direction);
- sprite.move(character_speed*time * 0.00003, 0);
- }
- else if (direction == Direction::Backward) {
- CharacterDirection(direction);
- sprite.move(-(character_speed * time * 0.00003), 0);
- }
- sprite.setTextureRect(sf::IntRect((character_animation->GetCharacterTexture(time)*(texture_width + 2)) + 1, 1, texture_width, texture_height));
- BehindTheMap();
- }
- else {
- sprite.setTextureRect(sf::IntRect(1, 1, texture_width, texture_height));
- character_animation->texture = 1;
- }
- }
- virtual void SetCharacterScale(double width_div, double height_div) {
- sprite.setScale(1 / width_div, 1 / height_div);
- character_dimensions.x /= width_div;
- character_dimensions.y /= height_div;
- }
- virtual sf::Vector2f GetCharacterDimensions() const {
- return character_dimensions;
- }
- };
- class UgandaKnuckles : public Character {
- public:
- UgandaKnuckles(std::string path_to_texture, int texture_width, int texture_height, int start_move_texture, sf::Vector2f start_position, sf::Vector2u window_size)
- :Character(path_to_texture, texture_width, texture_height, start_move_texture, start_position, window_size) {}
- };
- class Map {
- protected:
- sf::Vector2u window_size;
- std::string path_to_map_textures;
- sf::Image image;
- sf::Texture texture;
- sf::Sprite sprite;
- public:
- Map(std::string path_to_map_textures, sf::Vector2u window_size) {
- this->path_to_map_textures = path_to_map_textures;
- this->window_size = window_size;
- image.loadFromFile(path_to_map_textures);
- image.createMaskFromColor(sf::Color(0, 14, 88));
- texture.loadFromImage(image);
- sprite.setTexture(texture);
- }
- virtual void SetSprite(int x, int y, int texture_width, int texture_height) {
- if (x < image.getSize().x && y < image.getSize().y) {
- sprite.setTextureRect(sf::IntRect(x, y, texture_width, texture_height));
- }
- }
- virtual sf::Sprite GetSprite() const {
- return sprite;
- }
- virtual void SetPosition(double x, double y) {
- sprite.setPosition(x, y);
- }
- int GetImageWidth() {
- return image.getSize().x;
- }
- int GetImageHeight() {
- return image.getSize().y;
- }
- };
- class Level1 : public Map {
- public:
- Level1(std::string path_to_map_textures, sf::Vector2u window_size)
- :Map(path_to_map_textures, window_size) {}
- };
- int main() {
- setlocale(LC_ALL, "ru");
- sf::RenderWindow window(sf::VideoMode(1000, 285), "Test");
- sf::Event window_event;
- Map *level1 = new Level1("level_1_97x97.png", window.getSize());
- Character *main_character = new UgandaKnuckles("uganda_textures_one_texture=144x164.png", 144, 164, 2, sf::Vector2f(0, 135), window.getSize());
- main_character->SetCharacterSpeed(7);
- main_character->SetCharacterScale(2.5, 2.5);
- bool stand = true;
- //std::cout << "Размеры персонажа " << main_character->GetCharacterDimensions().x << "x" << main_character->GetCharacterDimensions().y << std::endl;
- while (window.isOpen()) {
- main_character->SetTime();
- if (window.pollEvent(window_event)) {
- if (window_event.type == sf::Event::Closed) {
- window.close();
- }
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
- main_character->Move(Direction::Stand);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
- main_character->Move(Direction::Forward);
- stand = false;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
- main_character->Move(Direction::Backward);
- stand = false;
- }
- if (stand) {
- main_character->Move(Direction::Stand);
- }
- stand = true;
- window.clear();
- level1->SetSprite(1, 1, 97, 97); //спрайт неба
- for (int i = 0, x = -20; i != 11; ++i, x += 97) {
- level1->SetPosition(x, 0);
- window.draw(level1->GetSprite());
- level1->SetPosition(x, 97);
- window.draw(level1->GetSprite());
- }
- level1->SetSprite(98, 1, 144, 141); //спрайт солнца
- level1->SetPosition(800, 30);
- window.draw(level1->GetSprite());
- level1->SetSprite(1, 98, 97, 97); //спрайт земли
- for (int i = 0, x = -20; i != 11; ++i, x += 97) {
- level1->SetPosition(x, 192);
- window.draw(level1->GetSprite());
- }
- window.draw(main_character->GetSprite());
- window.display();
- }
- delete level1;
- delete main_character;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment