diff options
| -rw-r--r-- | src/Enemy2.cpp | 14 | ||||
| -rw-r--r-- | src/Game.cpp | 41 | ||||
| -rw-r--r-- | src/State.cpp | 88 | ||||
| -rw-r--r-- | src/includes/Game.hpp | 8 | ||||
| -rw-r--r-- | src/includes/State.hpp | 12 |
5 files changed, 112 insertions, 51 deletions
diff --git a/src/Enemy2.cpp b/src/Enemy2.cpp index fc65942..98de1a6 100644 --- a/src/Enemy2.cpp +++ b/src/Enemy2.cpp @@ -8,7 +8,7 @@ Enemy2::Enemy2(sf::Vector2f pozicija,sf::Vector2f velicina,sf::Color boja):Entit void Enemy2::izracunajbrzinu(float igracx,float igracy,float dt) { float k=(igracy-y)/(igracx-x); - float r=200.0*dt; + float r=600.0*dt; float dx=r/std::sqrt(1+k*k); if(igracx-x<0) dx=-dx; float dy=k*dx; @@ -17,14 +17,6 @@ void Enemy2::izracunajbrzinu(float igracx,float igracy,float dt) } void Enemy2::izracunajpoz(float igracx,float igracy,float dt) { - if((x+vx-igracx)*(x+vx-igracx)+(y+vy-igracy)*(y+vy-igracy)<(x-igracx)*(x-igracx)+(y-igracy)*(y-igracy)) - { - x+=vx*dt*3; - y+=vy*dt*3; - } - else - { - x+=vx*dt; - y+=vy*dt; - } + x+=vx*dt; + y+=vy*dt; } diff --git a/src/Game.cpp b/src/Game.cpp index a0c28af..6435daa 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,7 +1,6 @@ #include"includes/Global.hpp" #include"includes/Game.hpp" -#include<iostream> void Game::initshapes() { krug.setRadius(igrac.stomprad); @@ -24,10 +23,6 @@ void Game::initshapes() } void Game::initui() { - if(!font.loadFromFile("assets/fonts/LiberationMono-Regular.ttf")) - { - std::cerr<<"Font not found\n"; - } healthtext.setFont(font); healthtext.setString("Health"); healthtext.setCharacterSize(24); @@ -49,18 +44,10 @@ void Game::initui() } void Game::inittex() { - if(!healthtex.loadFromFile("assets/images/healing.png")) - { - std::cerr<<"Texture not found\n"; - } - if(!neprijateljtex.loadFromFile("assets/images/nep.png")) - { - std::cerr<<"Texture not found\n"; - } - for(size_t i=0;i<pow.size();i++) pow.at(i).telo.setTexture(&healthtex); - for(size_t i=0;i<nep1.size();i++) nep1.at(i).telo.setTexture(&neprijateljtex); - for(size_t i=0;i<nep2.size();i++) nep2.at(i).telo.setTexture(&neprijateljtex); - for(size_t i=0;i<nep3.size();i++) nep3.at(i).telo.setTexture(&neprijateljtex); + for(size_t i=0;i<pow.size();i++) pow.at(i).telo.setTexture(healthtex); + for(size_t i=0;i<nep1.size();i++) nep1.at(i).telo.setTexture(neprijateljtex); + for(size_t i=0;i<nep2.size();i++) nep2.at(i).telo.setTexture(neprijateljtex); + for(size_t i=0;i<nep3.size();i++) nep3.at(i).telo.setTexture(neprijateljtex); } void Game::initent() { @@ -80,9 +67,13 @@ void Game::updatewin() score.setPosition(sirina*5.0/6,50); updateui(); } -Game::Game(sf::RenderWindow *glprozor) +Game::Game(sf::RenderWindow *glprozor,sf::Font mainfont,sf::Texture *healthtexture,sf::Texture *neprijateljtexture) { prozor=glprozor; + font=mainfont; + healthtex=healthtexture; + neprijateljtex=neprijateljtexture; + updatewin(); initui(); initshapes(); @@ -91,12 +82,7 @@ Game::Game(sf::RenderWindow *glprozor) } bool Game::gameover() { - if(igrac.health<=0) - { - prozor->close(); - std::cout<<"\n\nGame over. Wanna try again?\n"; - return 1; - } + if(igrac.health<=0) return 1; return 0; } void Game::updatedt() @@ -150,7 +136,6 @@ void Game::keyboard() } void Game::draw() { - prozor->clear(); if(igrac.stomptime>4.6) prozor->draw(krug); prozor->draw(igrac.telo); for(size_t i=0;i<nep1.size();i++) if(nep1.at(i).ziv) prozor->draw(nep1.at(i).telo); @@ -166,8 +151,6 @@ void Game::draw() prozor->draw(stomptext); prozor->draw(fps); prozor->draw(score); - - prozor->display(); } void Game::position() { @@ -260,8 +243,6 @@ void Game::respawn() } void Game::run() { - if(gameover()) return; - updateui(); igrac.updatest(dt); respawn(); position(); @@ -275,6 +256,6 @@ void Game::loop(bool ischanged,bool pause) keyboard(); run(); } + updateui(); updatedt(); - draw(); } diff --git a/src/State.cpp b/src/State.cpp index 510e8ca..3c73da1 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -5,13 +5,33 @@ #include<iostream> State::State() { + initwin(); + initassets(); + initui(); +} +void State::initwin() +{ prozor.create(sf::VideoMode::getFullscreenModes()[0],"RPG igra"); prozor.setFramerateLimit(60); visina=prozor.getSize().y; sirina=prozor.getSize().x; } -void State::events() +void State::initassets() { + if(!font.loadFromFile("assets/fonts/LiberationMono-Regular.ttf")) + { + std::cerr<<"Font not found\n"; + } + if(!healthtex.loadFromFile("assets/images/healing.png")) + { + std::cerr<<"Texture not found\n"; + } + if(!neprijateljtex.loadFromFile("assets/images/nep.png")) + { + std::cerr<<"Texture not found\n"; + } +} +void State::events() { sf::Event evnt; while(prozor.pollEvent(evnt)) { @@ -37,7 +57,7 @@ void State::events() } void State::keyboard() { - if(sf::Keyboard::isKeyPressed(sf::Keyboard::P)) + if(sf::Keyboard::isKeyPressed(sf::Keyboard::P)&&!kraj) { pause=1; } @@ -46,14 +66,72 @@ void State::keyboard() pause=0; ischanged=1; } + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Y)) + { + newgame=1; + } + if(sf::Keyboard::isKeyPressed(sf::Keyboard::N)) + { + prozor.close(); + } +} +void State::initui() +{ + podloga.setPosition(sirina/2.0,visina/2.0); + podloga.setFillColor(sf::Color::Black); + podloga.setOutlineColor(sf::Color::White); + podloga.setOutlineThickness(5); + podloga.setSize(sf::Vector2f(600.0f,120.0f)); + podloga.setOrigin(podloga.getSize().x/2.0,podloga.getSize().y/2.0); + + pausetext.setFont(font); + pausetext.setString("Pauza\nPritisnite Esc da nastavite igru"); + pausetext.setCharacterSize(24); + pausetext.setStyle(sf::Text::Bold); + pausetext.setFillColor(sf::Color::White); + pausetext.setOrigin(pausetext.getGlobalBounds().width/2.0,pausetext.getGlobalBounds().height/2.0); + + krajtext.setFont(font); + krajtext.setString("Kraj igre\nPritisnite Y da zapocnete novu igru"); + krajtext.setCharacterSize(24); + krajtext.setStyle(sf::Text::Bold); + krajtext.setFillColor(sf::Color::White); + krajtext.setOrigin(krajtext.getGlobalBounds().width/2.0,krajtext.getGlobalBounds().height/2.0); + + updateui(); +} +void State::updateui() +{ + pausetext.setPosition(sirina/2.0,visina/2.0); + krajtext.setPosition(sirina/2.0,visina/2.0); + podloga.setPosition(sirina/2.0,visina/2.0); } void State::loop() { - Game *igra=new Game(&prozor); + Game *igra=new Game(&prozor,font,&healthtex,&neprijateljtex); while(prozor.isOpen()) { events(); - igra->loop(ischanged,pause); - if(ischanged) ischanged=0; + kraj=igra->gameover(); + igra->loop(ischanged,pause||kraj); + if(ischanged) updateui(); + + prozor.clear(); + igra->draw(); + if(kraj) + { + prozor.draw(podloga); + prozor.draw(krajtext); + if(newgame) igra=new Game(&prozor,font,&healthtex,&neprijateljtex); + } + if(pause) + { + prozor.draw(podloga); + prozor.draw(pausetext); + } + prozor.display(); + + ischanged=0; + newgame=0; } } diff --git a/src/includes/Game.hpp b/src/includes/Game.hpp index 4418ab3..a85d384 100644 --- a/src/includes/Game.hpp +++ b/src/includes/Game.hpp @@ -11,7 +11,7 @@ class Game { private: sf::Font font; - sf::Texture healthtex,neprijateljtex; + sf::Texture *healthtex,*neprijateljtex; float dt; sf::Clock sat,time; @@ -30,12 +30,10 @@ class Game void keyboard(); void run(); - void draw(); void stompmain(); void updateui(); void updatedt(); - bool gameover(); void respawn(); void position(); void checkcollision(); @@ -47,8 +45,10 @@ class Game void updatewin(); public: Game() {} - Game(sf::RenderWindow *glprozor); + Game(sf::RenderWindow *glprozor,sf::Font font,sf::Texture *neprijateljtex,sf::Texture *healthtex); void loop(bool ischanged,bool pause); + void draw(); + bool gameover(); }; #endif diff --git a/src/includes/State.hpp b/src/includes/State.hpp index fdde7b8..453939f 100644 --- a/src/includes/State.hpp +++ b/src/includes/State.hpp @@ -7,8 +7,18 @@ class State private: sf::RenderWindow prozor; int visina,sirina; - bool ischanged=0,pause=0; + bool ischanged=0,newgame=0,pause=0,kraj=0; + sf::Font font; + sf::Texture healthtex,neprijateljtex; + + sf::RectangleShape podloga; + sf::Text pausetext,krajtext; + + void initassets(); + void initwin(); + void initui(); + void updateui(); void events(); void keyboard(); public: |
