diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-04-07 17:57:53 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-04-07 17:57:53 +0100 |
commit | 884caeb57d6b0b94a21df4412f39d81925e58000 (patch) | |
tree | ea5d55f6d857b448320fa9eab92dc83eeafcc1b3 | |
parent | 7d1ab6bd8a1e9711fb99d29b350437fb7784ddc4 (diff) | |
download | Arider-884caeb57d6b0b94a21df4412f39d81925e58000.tar.gz Arider-884caeb57d6b0b94a21df4412f39d81925e58000.zip |
Improved animation class and added background
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/animation.hpp | 26 | ||||
-rw-r--r-- | include/levelloader.hpp | 16 | ||||
-rw-r--r-- | src/animation.cpp | 23 | ||||
-rw-r--r-- | src/game.cpp | 33 | ||||
-rw-r--r-- | src/levelloader.cpp | 46 | ||||
-rw-r--r-- | src/player.cpp | 5 |
7 files changed, 105 insertions, 45 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e421dc6..733df91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/src/animation.cpp ${CMAKE_SOURCE_DIR}/src/character.cpp ${CMAKE_SOURCE_DIR}/src/game.cpp + ${CMAKE_SOURCE_DIR}/src/levelloader.cpp ${CMAKE_SOURCE_DIR}/src/main.cpp ${CMAKE_SOURCE_DIR}/src/player.cpp) diff --git a/include/animation.hpp b/include/animation.hpp index 122dc91..da24fd5 100644 --- a/include/animation.hpp +++ b/include/animation.hpp @@ -10,31 +10,39 @@ #include <unordered_map> #include <vector> +enum class AnimationState +{ + IDLE, + MOVING, + JUMPING, + CROUCHING, +}; + class Animation { private: - std::unordered_map<std::string, std::vector<yage::Texture>> frame_animations_; - std::string current_animation_; + std::unordered_map<AnimationState, std::vector<yage::Texture>> frame_animations_; + AnimationState current_animation_; int current_index_=0; public: - Animation() {} + Animation(); - void pushFrame(const std::string &animation_name, const std::string &texture_path); + void pushFrame(AnimationState state, const std::string &texture_path); yage::Texture currentFrame() const; - void start(const std::string &animation_name); + void start(AnimationState state); void nextFrame(); template<typename First, typename Second, typename ...Rest> - void initializeAnimation(const std::string &animation_name, First &&first, Second &&second, Rest &&...rest) + void initializeAnimation(AnimationState state, First &&first, Second &&second, Rest &&...rest) { - frame_animations_[animation_name].push_back(yage::ResourceManager::getTexture(std::forward<First>(first))); + frame_animations_[state].push_back(yage::ResourceManager::getTexture(std::forward<First>(first))); initializeAnimation(std::forward<Second>(second), std::forward<Rest>(rest)...); } template<typename Last> - void initializeAnimation(const std::string &animation_name, Last &&last) + void initializeAnimation(AnimationState state, Last &&last) { - frame_animations_[animation_name].push_back(yage::ResourceManager::getTexture(std::forward<Last>(last))); + frame_animations_[state].push_back(yage::ResourceManager::getTexture(std::forward<Last>(last))); } }; diff --git a/include/levelloader.hpp b/include/levelloader.hpp new file mode 100644 index 0000000..3d11a00 --- /dev/null +++ b/include/levelloader.hpp @@ -0,0 +1,16 @@ +#ifndef LEVEL_LOADER_HPP +#define LEVEL_LOADER_HPP + +#include "player.hpp" + +#include <YAGE/spritebatch.hpp> + +#include <string> + +class LevelLoader { +public: + static void loadLevel(const std::string &level_path, std::string &level, int &level_width, int &level_height); + static void drawLevel(yage::SpriteBatch &sprite_batch, const std::string &level, int level_width); +}; + +#endif diff --git a/src/animation.cpp b/src/animation.cpp index d9bafbb..928c500 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -2,22 +2,26 @@ #include <iostream> -void Animation::pushFrame(const std::string &animation_name, const std::string &texture_path) +Animation::Animation() : current_animation_(AnimationState::IDLE) +{} + +void Animation::pushFrame(AnimationState state, const std::string &texture_path) { - frame_animations_[animation_name].push_back(yage::ResourceManager::getTexture(texture_path)); + frame_animations_[state].push_back(yage::ResourceManager::getTexture(texture_path)); } yage::Texture Animation::currentFrame() const { - return frame_animations_.find(current_animation_)->second[current_index_]; + int current_index=current_index_; + if(current_index_>(int)frame_animations_.find(current_animation_)->second.size()-1) + current_index=0; + return frame_animations_.find(current_animation_)->second[current_index]; } -void Animation::start(const std::string &animation_name) +void Animation::start(AnimationState state) { - if(current_animation_!=animation_name) - { - current_animation_=animation_name; - } + if(current_animation_!=state) + current_animation_=state; } void Animation::nextFrame() @@ -26,7 +30,4 @@ void Animation::nextFrame() ++current_index_; else current_index_=0; - std::cout<<"id: "<<frame_animations_.find(current_animation_)->second[current_index_].id<<'\n'; - std::cout<<"index: "<<current_index_<<'\n'; - std::cout<<"size: "<<(int)frame_animations_.find(current_animation_)->second.size()<<'\n'; } diff --git a/src/game.cpp b/src/game.cpp index 851579d..a59e62d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -37,19 +37,13 @@ void Game::initSystems() window_.create("Arider", screen_width_, screen_height_, yage::WindowFlags::SHOWN); initShaders(); - player_.create(glm::vec2(0.f, 0.f), glm::vec2(66, 92), glm::vec2(5.f, 5.f)); - player_.animation_.pushFrame("idle", "res/textures/Player/p3_front.png"); + player_.create(glm::vec2(0.f, 70.f), glm::vec2(66, 92), glm::vec2(5.f, 5.f)); + player_.animation_.pushFrame(AnimationState::IDLE, "res/textures/Player/p3_front.png"); for(int i=1; i<=11; ++i) - { if(i<10) - { - player_.animation_.pushFrame("move", "res/textures/Player/p3_walk/PNG/p3_walk0"+std::to_string(i)+".png"); - } + player_.animation_.pushFrame(AnimationState::MOVING, "res/textures/Player/p3_walk/PNG/p3_walk0"+std::to_string(i)+".png"); else - { - player_.animation_.pushFrame("move", "res/textures/Player/p3_walk/PNG/p3_walk"+std::to_string(i)+".png"); - } - } + player_.animation_.pushFrame(AnimationState::MOVING, "res/textures/Player/p3_walk/PNG/p3_walk"+std::to_string(i)+".png"); } void Game::initShaders() @@ -80,29 +74,16 @@ void Game::processInput() } } - player_.animation_.start("idle"); player_.idle(); if(input_manager_.isKeyPressed(SDLK_w)) - { - player_.animation_.start("idle"); player_.moveUp(); - } if(input_manager_.isKeyPressed(SDLK_s)) - { - player_.animation_.start("idle"); player_.moveDown(); - } if(input_manager_.isKeyPressed(SDLK_d)) - { - player_.animation_.start("move"); player_.moveRight(); - } if(input_manager_.isKeyPressed(SDLK_a)) - { - player_.animation_.start("move"); player_.moveLeft(); - } if(time_%3==0) player_.animation_.nextFrame(); @@ -152,8 +133,10 @@ void Game::renderSprites() player_.renderSprite(sprite_batch_); // drawing the background - sprite_batch_.draw(glm::vec4(0.f, 0.f, 2560, 2560), glm::vec4(0.f, 0.f, 10.f, 10.f), yage::ResourceManager::getTexture("res/textures/bg_castle.png").id, yage::Color(255, 255, 255, 255), -1.f); - + sprite_batch_.draw(glm::vec4(0.f, 0.f, 2560.f, 2560.f), glm::vec4(0.f, 0.f, 10.f, 10.f), yage::ResourceManager::getTexture("res/textures/bg_castle.png").id, yage::Color(255, 255, 255, 255), -2.f); + sprite_batch_.draw(glm::vec4(0.f, 0.f, 700.f, 70.f), glm::vec4(0.f, 0.f, 10.f, 1.f), yage::ResourceManager::getTexture("res/textures/Tiles/grassMid.png").id, yage::Color(255, 255, 255, 255), -1.f); + sprite_batch_.draw(glm::vec4(700.f, 0.f, 70.f, 70.f), glm::vec4(0.f, 0.f, 1.f, 1.f), yage::ResourceManager::getTexture("res/textures/Tiles/grassCliffRight.png").id, yage::Color(255, 255, 255, 255), -1.f); + sprite_batch_.end(); sprite_batch_.render(); } diff --git a/src/levelloader.cpp b/src/levelloader.cpp new file mode 100644 index 0000000..b4c356f --- /dev/null +++ b/src/levelloader.cpp @@ -0,0 +1,46 @@ +#include "levelloader.hpp" + +#include <fstream> +#include <stdexcept> + +void LevelLoader::loadLevel(const std::string &level_path, std::string &level, int &level_width, int &level_height) +{ + std::ifstream level_file(level_path); + + if(!level_file.is_open()) + throw std::runtime_error("Could not load level: '"+level_path+"'"); + + // initialize inputs to be empty + level=""; + level_width=0; + level_height=0; + + std::string level_line; + while(getline(level_file, level_line)) + { + level+=level_line; + if(level_width<(int)level_line.length()) + level_width=level_line.length(); + ++level_height; + } +} + +void LevelLoader::drawLevel(yage::SpriteBatch &sprite_batch, const std::string &level, int level_width) +{ + for(int i=0; i<(int)level.size(); ++i) + { + switch(level[i]) + { + case 'b': // brick + break; + case 'e': // empty + break; + case '@': // player + break; + case ' ': + break; + default: + throw std::runtime_error("Could not recognize '"+std::to_string(level[i])+"' in level file"); + } + } +} diff --git a/src/player.cpp b/src/player.cpp index f0bc35c..565989a 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -9,27 +9,32 @@ void Player::moveUp() { uv_=glm::vec4(0.f, 0.f, 1.f, 1.f); position_+=glm::vec2(0.f, speed_.y); + animation_.start(AnimationState::IDLE); } void Player::moveRight() { uv_=glm::vec4(0.f, 0.f, 1.f, 1.f); position_+=glm::vec2(speed_.x, 0.f); + animation_.start(AnimationState::MOVING); } void Player::moveDown() { uv_=glm::vec4(0.f, 0.f, 1.f, 1.f); position_+=glm::vec2(0.f, -speed_.y); + animation_.start(AnimationState::IDLE); } void Player::moveLeft() { uv_=glm::vec4(1.f, 0.f, -1.f, 1.f); position_+=glm::vec2(-speed_.x, 0.f); + animation_.start(AnimationState::MOVING); } void Player::idle() { uv_=glm::vec4(1.f, 0.f, -1.f, 1.f); + animation_.start(AnimationState::IDLE); } |