Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <string>
- enum Direction {
- Forward = 1,
- Backward = -1,
- Up = -1,
- Down = 1,
- Stand = 0
- };
- 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; //время 1 итерации цикла while (window.isOpen()) в функции main
- double character_speed = 1;
- double character_weight = 1;
- bool jump = false; //при нажатии стрелки вверх = true
- bool gravity_to_ground = false; //при start_jump_momentum = 0 gravity_to_ground = true
- double before_jump_y_position, jump_momentum;
- sf::Clock jump_time;
- 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);
- }
- }
- void Jump() {
- if (!gravity_to_ground) {
- if (jump_momentum > 0) {
- jump_momentum -= 9.8 * jump_time.getElapsedTime().asMicroseconds() * 0.00001 * character_weight;
- sprite.move(0, -jump_momentum* 0.000025);
- }
- else {
- gravity_to_ground = true;
- jump_momentum = 0;
- jump_time.restart();
- }
- }
- else {
- if (before_jump_y_position > sprite.getPosition().y) {
- jump_momentum += 9.8 * jump_time.getElapsedTime().asMicroseconds() * 0.00001 * character_weight;
- sprite.move(0, jump_momentum*0.000025);
- }
- else {
- gravity_to_ground = false;
- jump = false;
- jump_momentum = 0;
- sprite.setPosition(sprite.getPosition().x, before_jump_y_position);
- }
- }
- }
- 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 / character_weight;
- }
- }
- virtual double GetCharacterSpeed() const {
- return character_speed;
- }
- virtual void SetCharacterWeight(double character_weight) {
- if (character_weight > 0) {
- this->character_weight = character_weight / 70;
- character_speed /= this->character_weight;
- }
- }
- virtual double GetCharacterWeight() const {
- return character_weight;
- }
- virtual void SetTime() {
- if (jump) {
- Jump();
- }
- 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 horizontal_direction, Direction vertical_direction = Direction::Stand) {
- if (horizontal_direction || vertical_direction != Stand) {
- character_last_position = sprite.getPosition();
- if (horizontal_direction == Forward || Backward) {
- CharacterDirection(horizontal_direction);
- }
- if (vertical_direction == Up) {
- if (!jump) {
- before_jump_y_position = sprite.getPosition().y;
- jump_momentum = character_speed * 1000 * character_weight;
- jump_time.restart();
- jump = true;
- }
- }
- sprite.move(horizontal_direction*character_speed*time * 0.000025, 0);
- sprite.setTextureRect(sf::IntRect((character_animation->GetCharacterTexture(time * (character_speed / 7))*(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) {}
- };
- void CharacterManagement(Character*&object, bool&stand) {
- if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Down))) {
- object->Move(Direction::Stand);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
- object->Move(Direction::Forward, Direction::Up);
- stand = false;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
- object->Move(Direction::Backward, Direction::Up);
- stand = false;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
- object->Move(Direction::Forward);
- stand = false;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
- object->Move(Direction::Backward);
- stand = false;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
- object->Move(Direction::Stand, Direction::Up);
- stand = false;
- }
- }
- int main() {
- setlocale(LC_ALL, "ru");
- sf::RenderWindow window(sf::VideoMode(1000, 285), "Uganda adventure");
- 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); //7 - средняя скорость
- main_character->SetCharacterWeight(70); //70 - средний вес
- 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();
- }
- }
- CharacterManagement(main_character, stand);
- 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