Sign Up
Login
API
FAQ
Tools
Trends
Archive
PASTEBIN
new paste
trends
API
tools
faq
Guest User
-
Public Pastes
Untitled
28 sec ago
Creepy encounter b...
30 sec ago
Untitled
36 sec ago
Untitled
45 sec ago
Untitled
46 sec ago
Untitled
1 min ago
Untitled
1 min ago
Untitled
Lua | 1 min ago
BitBucket
- Backup your code in the cloud!
Host unlimited private projects, for free.
SIGN UP
takes 10 seconds, and it's free!
New Paste
#include "projectile.h" Projectile::Projectile(const r2& rects, const f2& speed, const float& damage, Character* character, ParticleEffects* PE, const float& lifetime, const float& tracking, const bool& active, const float& bounce, const float& explosive, const float& pierce, const float& collisionDelay) { setRectangles(rects); active_ = active; speed_ = speed; damage_ = damage; tracking_ = tracking; bounce_ = bounce; explosive_ = explosive; pierce_ = pierce; lifeTime_ = lifetime; clock_.restart(); collision_ = false; velocity_ = speed_.length(); collisionDelay_ = collisionDelay; character_ = character; particleEffects_ = PE; setAngle( - atan2f(speed_.y, speed_.x)); } Projectile::Projectile(const Projectile& prj) { *this = prj; setAngle( - atan2f(speed_.y, speed_.x)); } void Projectile::setSpeedAngle(const float& angle) { speed_ = f2(sin(angle), cos(angle)) * velocity_; } void Projectile::changeSpeedAngle(const float& angle) { setSpeedAngle(getSpeedAngle() + angle); } float Projectile::getSpeedAngle() const { return atan2f(speed_.x, speed_.y); } // checks and handles collision events void Projectile::updateProjectile(const float& gameRate, vector<Character*> enemies) { // this prevents dragging the window_ to glitch through the floor // this also means that if the game gets below 5 fps movement will stop working if (gameRate < 5) { return; } sf::Time time = clock_.getElapsedTime(); float t = time.asSeconds(); if (t > lifeTime_) { active_ = false; } if (!active_) { return; } particleEffects_->effect(trailEffect_, center()); f2 target = character_->center(); setPosition(getPosition () + speed_ / gameRate); // this code block handles tracking a target if (tracking_ > 0) { setAngle( - atan2f(speed_.y, speed_.x)); float angleSpeed = atan2f(speed_.x, speed_.y); f2 line = target - center(); float angleTarget = atan2f(line.x, line.y); // this is the angle between the target and the direction of the projectile float angle = angleSpeed - angleTarget; if (angle > - 0.2 && angle < 0.2) { // do nothing } // some kind of vector magic else if (speed_.x * (target.y - first().center().y) - speed_.y * (target.x - first().center().x) > 0) { angleSpeed -= tracking_ / gameRate; speed_ = velocity_ * f2(sin(angleSpeed), cos(angleSpeed)); } else if (speed_.x * (target.y - first().center().y) - speed_.y * (target.x - first().center().x) < 0) { angleSpeed += tracking_ / gameRate; speed_ = velocity_ * f2(sin(angleSpeed), cos(angleSpeed)); } } // collision with enemies int col = 0; for (int i = 0; i < enemies.size(); ++i) { if (enemies[i]->intersects (getRectangle()) && clock_.getElapsedTime().asSeconds() > collisionDelay_) { if (explosive_ > 0) { particleEffects_->effect(explosionEffect_, center()); for (int i = 0; i < enemies.size(); ++i) { if (distance(*enemies[i]) < 100) { enemies[i]->changeHealth(- damage_ * explosive_); } } } enemies[i]->changeHealth(- damage_); ++col; // piercing object if (pierce_ > 0 && bounce_ <= 0) { particleEffects_->effect(pierceEffect_, center()); pierce_ -= velocity_ / gameRate; } // this code block handles bouncing projectiles if (bounce_ > 0 && collision_ == false) { setAngle( - atan2f(speed_.y, speed_.x)); particleEffects_->effect(explosionEffect_, center()); collision_ = true; bounce_ -= 1; int direction = enemies[i]->getDirection(center()); if (direction == 0) { // this is the normal unit vector of the colliding surface f2 normal = (enemies[i]->bottomLeft() - enemies[i]->topLeft()) / enemies[i]->getHeight(); // the part of speed tangentiall to the normal vector f2 tangent = (normal * speed_) * normal; speed_ = speed_ - 2 * tangent; } if (direction == 1) { // this is the normal unit vector of the colliding surface f2 normal = (enemies[i]->bottomLeft() - enemies[i]->bottomRight()) / enemies[i]->getWidth(); // the part of speed tangential to the normal vector f2 tangent = (normal * speed_) * normal; speed_ = speed_ - 2 * tangent; } if (direction == 2) { // this is the normal unit vector of the colliding surface f2 normal = (enemies[i]->topLeft() - enemies[i]->bottomLeft()) / enemies[i]->getHeight(); // the part of speed tangential to the normal vector f2 tangent = (normal * speed_) * normal; speed_ = speed_ - 2 * tangent; } if (direction == 3) { // this is the normal unit vector of the colliding surface f2 normal = (enemies[i]->bottomRight() - enemies[i]->bottomLeft()) / enemies[i]->getWidth(); // the part of speed tangential to the normal vector f2 tangent = (normal * speed_) * normal; speed_ = speed_ - 2 * tangent; } } // the ride is over if (bounce_ == 0 && pierce_ <= 0) { particleEffects_->effect(standardEffect_, center()); active_ = false; } } } if (col == 0) { collision_ = false; } } void Projectile::update(const bool& active) { active_ = active; } #include "MainGame.h" using sf::Vector2i; ///////// ////// public ///////// //// // constructor //// MainGame::MainGame (string fileName, f2 resolution) { // the create member is used here instead of a constructor or assignment // because the window is initialized with no arguments as a class variable and the class is non copyable window_.create(sf::VideoMode (resolution.x, resolution.y), "ITO"); texture_.loadFromFile (fileName); vertices_.setPrimitiveType (sf::Quads); // r2* charge_ does not call the default constructor // so the sprite needs to be created here or it would be undefined charge_ = new r2(); // setting class variables charging_ = false; firing_ = false; playerHealthBar_ = Rectangle (f2 (0, 20), f2 (50, 15)); move_ = true; aim_ = true; tempControl_ = true; cone_ = false; wave_ = false; multiplayer_ = true; // seed random numbers with time srand (time (NULL)); } //// // main window_ loops //// // these functions contain the main draw loop and call every relevant function void MainGame::mainGame (sf::Text text) { // constructing the menu vector<string> vs; // every string is an image and will become a button in the menu vs.push_back ("decrease.png"); vs.push_back ("increase.png"); vs.push_back ("spawn.png"); vs.push_back ("remove.png"); Menu menu (window_, vs); // the first clock keeps track of the framerate // the second clock keeps track of the gamerate aka the amount of loops per second // the third clock is used to control the firing rate sf::Clock clock; sf::Clock clock2; sf::Clock clock3; while (window_.isOpen ()) { if (!menu._active) { // these three lines calculate the amount of loops per second sf::Time elapsed2 = clock2.restart (); float loopspersecond = 1 / elapsed2.asSeconds (); gameRate_ = loopspersecond; // setting up view sf::View view; sf::View view1 = playerView (view, float (2500)); // playerview is a seperate function window_.setView (view1); ////game logic // handles collision and movement inputs movePlayer (); // checks for projectile collision and moves the projectiles updateProjectiles (); // handles player health HP (); // keeps the combo bar in the right place setBar(); // keeps charge in the right place setCharge(); // handles sending and receiving packets if (multiplayer_) { multiplayer(); } // this handles continuesly firing float fireRate = 50; if (firing_ && clock3.getElapsedTime().asSeconds() > float(1) / fireRate) // the last number indicates the amount of shots per second { clock3.restart(); for (int i = 0; i < temp.size(); ++i) { temp[i]->clock_.restart(); temp[i]->update(true); float speed = 2500; if (aim_ && !cone_) { f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); temp[i]->speed_ = f2(sin(angle), cos(angle)) * temp[i]->speed_.length(); } if (cone_ && aim_) { f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); float factor = float(i) / temp.size(); angle += factor * 3.14 / 2 - 3.14 / 4; temp[i]->setSpeedAngle(angle); } temp[i]->setPosition(player_.center()); Projectile* prj = new Projectile(*temp[i]); projectiles.push_back(prj); } } } sf::Event event; while (window_.pollEvent (event)) { // handles menu inputs menu.normalMenu (window_, event); // these if statements handle menu output aka allow the user to control parts of the game through the menu if (menu.pressed == 0) { menu._active = false; } if (menu.pressed == 1) { } if (menu.pressed == 2) { multiplayer_ = true; client(); menu.pressed = - 1; menu._active = false; } if (menu.pressed == 3) { multiplayer_ = true; server(); menu.pressed = - 1; menu._active = false; } // all basic functions of the game are disabled when the menu is active if (!menu._active) { playerShoot(event); } switch (event.type) { case sf::Event::Closed : window_.close (); break; if (!menu._active) { case sf::Event::KeyPressed : { // this function handles creating combos cast(event); if (event.key.code == sf::Keyboard::Escape) { menu.pressed = - 1; menu._active = true; } break; } } default : break; } } // this if statements keeps the refresh rate at 120 fps or less // this also seperates the frame rate from the game logic if (clock.getElapsedTime().asSeconds() > float(1) / 120) { // set up for framerate display sf::Time elapsed = clock.restart (); float framerate = 1 / elapsed.asSeconds (); text.setPosition (player_.center () - viewSize_ / 2); text.setString (numberToString(framerate)); // doesn't work for unknown reasons // start of clear draw display cycle window_.clear (sf::Color::White); if (!menu._active) { // the window is saved as a class variable so nothing needs to be passed to the main draw function draw(); } window_.draw (text); menu.drawMenu (window_); window_.display(); } } } void MainGame::levelEditor (sf::Text text) { // for the menu vector<string> vs; vs.push_back ("back.png"); vs.push_back ("savegame.png"); vs.push_back ("quitgame.png"); vs.push_back ("loadgame.png"); Menu menu (window_, vs); menu._active = false; // for the framerate sf::Clock clock; f2 mpos1; f2 mpos2; float delta = 0; while (window_.isOpen ()) { if (menu.pressed != - 1) { menu._active = false; } if (menu.pressed == 0) { // do nothing } if (menu.pressed == 1) { saveLevel ("levelName"); } if (menu.pressed == 2) { loadLevel ("levelName"); } if (menu.pressed == 3) { window_.close (); } if (!menu._active) { menu.pressed = - 1; freeMove (); // handling view sf::View view; sf::View view1 = playerView (view, float (1700) - delta * 50); window_.setView (view1); // framerate and framerate display sf::Time elapsed = clock.restart (); float framerate = 1 / elapsed.asSeconds (); text.setPosition (player_.first().center () - f2 (viewSize_ / 2 - 45, viewSize_ / 2 - 45)); text.setString ("fps " + numberToString (framerate) + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t level editor"); gameRate_ = framerate; } sf::Event event; while (window_.pollEvent (event)) { menu.normalMenu (window_, event); switch (event.type) { case sf::Event::Closed : window_.close (); break; case sf::Event::KeyPressed : { if (event.key.code == sf::Keyboard::Escape) { if (menu._active) { menu._active = false; } else { menu._active = true; } } break; } if (!menu._active) { case sf::Event::MouseButtonPressed : { if (event.mouseButton.button == sf::Mouse::Left && sf::Keyboard::isKeyPressed (sf::Keyboard::LShift)) { mpos1 = getRealMousePosition (); } if (event.mouseButton.button == sf::Mouse::Right) { ; } break; } case sf::Event::MouseButtonReleased : { if (sf::Keyboard::isKeyPressed (sf::Keyboard::LShift)) { mpos2 = getRealMousePosition (); setBlock (pointsToRectangle (mpos1, mpos2), Rectangle (10, 10, 90, 90)); } break; } case sf::Event::MouseWheelMoved : { delta += float (event.mouseWheel.delta); break; } } default : // do nothing break; } } window_.clear (sf::Color::White); draw (); window_.draw (text); menu.drawMenu (window_); window_.display (); } } //// // game logic //// // these functions are all placed in the window.isopen loop void MainGame::setBar () { for (int i = 0; i < bar.size (); ++i) { bar[i].setPosition(player_.center() + f2(- 7.5, 15 * i - 50)); } } void MainGame::setCharge() { // this is purely for show if (charging_) { f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); charge_->setPosition(player_.center() + f2(sin(angle), cos(angle)) * 20); } else { charge_->setRectangle(Rectangle()); } } // handles player movement and collision void MainGame::movePlayer () // controls all player movement { // this prevents dragging the window_ to glitch through the floor // this also means that if the game gets below 5 fps movement will stop working if (gameRate_ < 5) { return; } // set the player to the proper position f2 playerPosition = player_.topLeft (); bool leftcol = false; // collision in all four directions bool rightcol = false; bool bottomcol = false; bool topcol = false; float CHI = 1; // determines the elasticity of collision float MU = 0.1; // constant air resistance factor float G = 400; // force of gravity in pixels / time^2 mass is not included in the program float V = 400; // player speed float A = 830; // player acceleration float playerMass = 2; // maximum speed is determined by acceleration and air resistance bool space = sf::Keyboard::isKeyPressed (sf::Keyboard::Space); bool a = sf::Keyboard::isKeyPressed (sf::Keyboard::A); bool d = sf::Keyboard::isKeyPressed (sf::Keyboard::D); bool w = sf::Keyboard::isKeyPressed (sf::Keyboard::W); bool s = sf::Keyboard::isKeyPressed (sf::Keyboard::S); bool shiftSpace = sf::Keyboard::isKeyPressed (sf::Keyboard::Space) && sf::Keyboard::isKeyPressed (sf::Keyboard::LShift); bool shiftA = sf::Keyboard::isKeyPressed (sf::Keyboard::A) && sf::Keyboard::isKeyPressed (sf::Keyboard::LShift); bool shiftD = sf::Keyboard::isKeyPressed (sf::Keyboard::D) && sf::Keyboard::isKeyPressed (sf::Keyboard::LShift); // everything is divided by gamerate to keep the speed of the game constant with varying gamerates // this allows you to control the movement of the character if (move_) { if (space) { playerSpeed_.y += - A / gameRate_; } if (s) { playerSpeed_.y += A / gameRate_; } if (a) { playerSpeed_.x += - A / gameRate_; } if (d) { playerSpeed_.x += A / gameRate_; } } for (vector<r2*>::iterator iterBlocks = blocks.begin (); iterBlocks != blocks.end (); ++iterBlocks) { vector<int> collision = player_.collision((*iterBlocks)->getRectangle()); vector<int>::iterator iterCollision = collision.begin(); if (collision.size () != 0) { float depth = collision[1] + collision[3]; depth /= 100; float d2 = collision[0] + collision[2]; d2 /= 100; vector<int>::iterator collidingSide = max_element (collision.begin (), collision.end ()); if (collidingSide == iterCollision) { playerSpeed_.y = 0; playerPosition.y += depth / 2; topcol = true; } ++iterCollision; if (collidingSide == iterCollision) { playerSpeed_.x = 0; playerPosition.x += d2 / 2; leftcol = true; } ++iterCollision; if (collidingSide == iterCollision) { playerSpeed_.y = 0; playerPosition.y += - depth / 2; bottomcol = true; } ++iterCollision; if (collidingSide == iterCollision) { playerSpeed_.x = 0; playerPosition.x += - d2 / 2; rightcol = true; } } } if (!space && !a && !d && !s) { playerAcceleration_.y = G; } if (bottomcol) { playerAcceleration_.y = 0; } if (playerSpeed_.x > 0) // simulate air resistance playerSpeed_.x += - playerSpeed_.x * MU / gameRate_; if (playerSpeed_.x < 0) playerSpeed_.x += - playerSpeed_.x * MU / gameRate_; if (playerSpeed_.x > 0) playerSpeed_.y += - playerSpeed_.y * MU / gameRate_; if (playerSpeed_.y < 0) playerSpeed_.y += - playerSpeed_.y * MU / gameRate_; playerAcceleration_ += playerForce_ / playerMass; playerSpeed_ += playerAcceleration_ / gameRate_; playerPosition += playerSpeed_ / gameRate_; player_.setPosition (playerPosition.x, playerPosition.y); } // allows player to noclip and handles movement input void MainGame::freeMove () { // set the player to the proper position f2 playerPosition = player_.topLeft (); float A = 800; bool a = sf::Keyboard::isKeyPressed (sf::Keyboard::A); bool d = sf::Keyboard::isKeyPressed (sf::Keyboard::D); bool w = sf::Keyboard::isKeyPressed (sf::Keyboard::W); bool s = sf::Keyboard::isKeyPressed (sf::Keyboard::S); if (w) { playerPosition.y += - A / gameRate_; // everything is divided by framerate to keep the speed of the game constant with varying framerates } if (s) { playerPosition.y += A / gameRate_; } if (a) { playerPosition.x += - A / gameRate_; } if (d) { playerPosition.x += A / gameRate_; } player_.setPosition (playerPosition.x, playerPosition.y); } // handles ingame health bar void MainGame::HP () { float hp = playerHealthBar_.getWidth (); for (int i = 0; i < projectiles2.size(); ++i) { if (player_.contains (projectiles2[i]->center ())) { hp -= 1; } } playerHealthBar_.setPosition (player_.getPosition () + f2 (0, - 20)); playerHealthBar_.setWidth (hp); } // handles moving projectiles and projectile collision void MainGame::updateProjectiles () { for (int i = 0; i < projectiles.size(); ++i) { projectiles[i]->updateProjectile(gameRate_, getRealMousePosition(), blocks, player2_, player_); if (projectiles[i]->active_ == false) { if (projectiles[i]->explosive_ > 0) { explosion(projectiles[i]); } delete projectiles[i]; projectiles.erase(projectiles.begin() + i); } } } // handles sending and receiving packets for multiplayer void MainGame::multiplayer () { sf::Packet packetSend; packetSend << playerHealthBar_ << player_; for (int i = 0; i < projectiles.size (); ++i) { packetSend << *projectiles[i]; } // TCP socket: // this clock is for calculating the delay sf::Clock clock; if (socket_.send (packetSend) == sf::Socket::Done) { clock.restart(); } sf::Packet packetRecieve; // TCP socket: // if the packet is received the proper values are inserted into class variables if (socket_.receive (packetRecieve) == sf::Socket::Done) { // calculate ping sf::Time t = clock.restart (); ping_ = t.asMilliseconds (); packetRecieve >> player2HP_; packetRecieve >> player2_; // the math with the datasize here is to get the right amount of projectiles // since there is a dynamic amount of projectiles we need to call the datasize to get right amount of projectiles // so the number that is subtracted is everything that is added before the projectiles // and the number that that is devided by is the size of each projectile cout<<(packetRecieve.getDataSize () - 60) / 52<<endl; for (int i = 0; i < (packetRecieve.getDataSize () - 60) / 52; ++i) { Projectile* projectile = new Projectile(); packetRecieve >> *projectile; projectiles2.push_back (projectile); } } } //// // event based functions //// void MainGame::server() { sf::Socket::Status status = socket_.connect ("192.168.123.180", 7777); if (status == sf::Socket::Done) { cout<<"server succes"<<endl; } } void MainGame::client() { sf::TcpListener listener; // bind the listener to a port if (listener.listen (7777) == sf::Socket::Done) { cout<<"port succes"<<endl; } // accept a new connection if (listener.accept (socket_) == sf::Socket::Done) { cout<<"client succes"<<endl; } } // saves blocks in level to file void MainGame::saveLevel (string fileName) { std::ofstream outputFile (fileName, std::ios::out); for (std::vector<r2*>::iterator iterBlocks = blocks.begin (); iterBlocks != blocks.end (); ++iterBlocks) { outputFile << (*iterBlocks)->first().getLeft () << std::endl; outputFile << (*iterBlocks)->first().getTop () << std::endl; outputFile << (*iterBlocks)->first().getWidth () << std::endl; outputFile << (*iterBlocks)->first().getHeight () << std::endl; outputFile << (*iterBlocks)->first().getAngle () << std::endl; } } // loads blocks from file to level void MainGame::loadLevel (string fileName) { blocks.clear (); int i = 0; float left; float top; float width; float height; float angle; std::ifstream inputFile (fileName, std::ios::in); std::string line; while (std::getline (inputFile, line)) { if (i == 0) { left = atoi (line.c_str ()); } if (i == 1) { top = atoi (line.c_str ()); } if (i == 2) { width = atoi (line.c_str ()); } if (i == 3) { height = atoi (line.c_str ()); } if (i == 4) { angle = atoi (line.c_str ()); i = - 1; setBlock (Rectangle (left, top, width, height, angle), Rectangle (10, 10, 90, 90)); } ++i; } } // places blocks in the level void MainGame::setBlock (Rectangle positionBox, Rectangle textureBox) { r2 *spr = new r2 (positionBox, textureBox); blocks.push_back (spr); } // handles shooting projectiles // is placed in pollevent loop void MainGame::playerShoot (sf::Event event) { // not even close to done Projectile* projectile = new Projectile(r2(Rectangle(0, 0, 40, 20), Rectangle(1100, 280, 40, 20)), f2(0, 0), 5, 10); projectile->update(false); int size = projectiles.size(); switch (event.type) { case sf::Event::MouseButtonPressed : { if (event.mouseButton.button == sf::Mouse::Left && combo_.size() >= 1) { charging_ = true; switch (combo_[0]) { case 0 : { if (combo_.size() >= 2) { // energy based attacks switch (combo_[1]) { case 0 : { // charged projectile float speed = 2500; f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); projectile->speed_ = f2(sin(angle), cos(angle)) * speed; break; } case 1 : { // continuesly firing projectiles float speed = 2500; f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); projectile->speed_ = f2(sin(angle), cos(angle)) * speed; firing_ = true; break; } case 2 : { // melee charged break; } case 3 : { // melee continues break; } default : { // never triggered break; } } } if (combo_.size() >= 3) { // controls debuffs aplied to the character in favor of damage switch (combo_[2]) { case 0 : { // free move and aim move_ = true; aim_ = true; break; } case 1 : { // no moving move_ = false; break; } case 2 : { // no aiming aim_ = false; break; } case 3 : { // no moving or aiming move_ = false; aim_ = false; break; } default : { // never triggered break; } } } if (combo_.size() >= 4) { switch (combo_[3]) { case 0 : { // single projectile break; } case 1 : { // cone cone_ = true; int amount = 10; for (int i = 0; i < amount; ++i) { f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); float factor = float(i) / amount; angle += factor * 3.14 / 2 - 3.14 / 4; projectile->setSpeedAngle(angle); temp.push_back(new Projectile(*projectile)); } break; } case 2 : { // aoe aim_ = false; int amount = 50; for (int i = 0; i < amount; ++i) { float angle = (i * 2 * 3.14 / amount) - 3.14; projectile->setSpeedAngle(angle); temp.push_back(new Projectile(*projectile)); } break; } case 3 : { // trap projectile->speed_ = f2(0, 0); break; } default : { // never triggered break; } } } for (int i = 4; i < 9; ++i) { if (combo_.size() >= i + 1) { switch (combo_[i]) { case 0 : { // tracking projectile->tracking_ += 3; for (int i = 0; i < temp.size(); ++i) { temp[i]->tracking_ += 3; } break; } case 1 : { // explosive projectile->explosive_ += 1; for (int i = 0; i < temp.size(); ++i) { temp[i]->explosive_ += 1; } break; } case 2 : { projectile->bounce_ += 3; for (int i = 0; i < temp.size(); ++i) { temp[i]->bounce_ += 3; } // bounce break; } case 3 : { // pierce projectile->pierce_ += 70; for (int i = 0; i < temp.size(); ++i) { temp[i]->pierce_ += 70; } break; } default : { // never triggered break; } } } } for (int i = 9; i < combo_.size(); ++i) // modifier for combos larger than 9 { switch (combo_[i]) { case 0 : { // increase size f2 size = projectile->getSize(); size += 5; projectile->setSize(size); for (int i = 0; i < temp.size(); ++i) { f2 size = temp[i]->getSize(); size += 5; temp[i]->setSize(size); } break; } case 1 : { // decrease size f2 size = projectile->getSize(); size -= 5; projectile->setSize(size); for (int i = 0; i < temp.size(); ++i) { f2 size = temp[i]->getSize(); size -= 5; temp[i]->setSize(size); } break; } case 2 : { // increase speed projectile->speed_ += 500; for (int i = 0; i < temp.size(); ++i) { temp[i]->speed_ += 500; } break; } case 3 : { // decrease speed projectile->speed_ -= 500; for (int i = 0; i < temp.size(); ++i) { temp[i]->speed_ -= 500; } break; } default : { // never triggered break; } } } break; } case 1 : { // movement / teleportation break; } case 2 : { // telekinesis break; } case 3 : { // time break; } } if (tempControl_) { temp.push_back(projectile); tempControl_ = false; } charge_ = new r2(Rectangle (0, 0, 40, 20), Rectangle(1200, 350, 50, 50)); } break; } case sf::Event::MouseButtonReleased : { if (event.mouseButton.button == sf::Mouse::Left) { for (int i = 0; i < temp.size(); ++i) { temp[i]->clock_.restart(); temp[i]->update(true); if (aim_ && !cone_) { f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); temp[i]->speed_ = f2(sin(angle), cos(angle)) * temp[i]->speed_.length(); } if (cone_ && aim_) { f2 mp (sf::Mouse::getPosition (window_)); mp -= f2(window_.getSize()) / 2; float angle = atan2f (mp.x, mp.y); float factor = float(i) / temp.size(); angle += factor * 3.14 / 2 - 3.14 / 4; temp[i]->setSpeedAngle(angle); } temp[i]->setPosition(player_.center()); projectiles.push_back(new Projectile(*temp[i])); } charging_ = false; firing_ = false; aim_ = true; move_ = true; tempControl_ = true; combo_.clear(); bar.clear(); temp.clear(); } } default : { break; } } } // handles spell casting // is placed in keypressed event case void MainGame::cast(sf::Event event) { switch (event.key.code) { case sf::Keyboard::Q : { combo_.push_back(0); bar.push_back(r2(Rectangle(0, 0, 15, 15), Rectangle(1200, 350, 15, 15))); break; } case sf::Keyboard::W : { combo_.push_back(1); bar.push_back(r2(Rectangle(0, 0, 15, 15), Rectangle(1700, 350, 15, 15))); break; } case sf::Keyboard::E : { combo_.push_back(2); bar.push_back(r2(Rectangle(0, 0, 15, 15), Rectangle(2100, 350, 15, 15))); break; } case sf::Keyboard::R : { bar.push_back(r2(Rectangle(0, 0, 15, 15), Rectangle(950, 650, 15, 15))); combo_.push_back(3); break; } } } //// // support functions (functions called by other functions) //// Rectangle MainGame::pointsToRectangle (f2 p1, f2 p2) { // creates a rectangle from 2 corner points f2 size = p2 - p1; return Rectangle (p1, size); } void MainGame::setPlayer (Rectangle positionBox, Rectangle textureBox) { player_ = r2(positionBox, textureBox); } // support function for draw void MainGame::setQuad (Rectangle positionBox, Rectangle textureBox, int i) { sf::Vertex* quad = &vertices_[i]; if ( positionBox.topLeft () == f2(0, 0) && positionBox.bottomLeft () == f2(0, 0) && positionBox.bottomRight ()== f2(0, 0) && positionBox.topRight () == f2(0, 0) && textureBox.topLeft () == f2(0, 0) && textureBox.bottomLeft () == f2(0, 0) && textureBox.bottomRight () == f2(0, 0) && textureBox.topRight () == f2(0, 0) ) { return; } quad[0].position = positionBox.topLeft (); quad[1].position = positionBox.bottomLeft (); quad[2].position = positionBox.bottomRight (); quad[3].position = positionBox.topRight (); quad[0].texCoords = textureBox.topLeft (); quad[1].texCoords = textureBox.bottomLeft (); quad[2].texCoords = textureBox.bottomRight (); quad[3].texCoords = textureBox.topRight (); } void MainGame::setQuad (r2 sprt, int i) { setQuad(sprt.getRectangle(), sprt.second, i); } sf::View MainGame::playerView (sf::View view, float size) { // keeps the view in the right position view.setCenter (player_.center ()); view.setSize (f2 (size, size)); viewSize_ = size; return view; } f2 MainGame::getRealMousePosition () // doesn't work { // it seems the to be very hard to get the actual mouseposition when using a moving and resized viewport f2 mousePosition = sf::Mouse::getPosition (window_); mousePosition -= f2(window_.getSize()) / 2; float distance = mousePosition.length() * viewSize_ / f2(window_.getSize()).length(); float angle = atan2f(mousePosition.x, mousePosition.y); return player_.center () + distance * f2(sin(angle), cos(angle)); } // shoots a bunch of projectiles in random directions void MainGame::explosion(Projectile* projectile) { projectile->clock_.restart(); int amount = projectile->explosive_ + 5; for (int i = 0; i < amount; ++i) { float angle = (0.5 - float(rand()) / RAND_MAX) * 2 * 3.14; f2 speed = f2(sin(angle), cos(angle)) * projectile->speed_.length(); projectile->explosive_ -= 1; projectile->speed_ = speed; // a copy needs to be created because the projectile is destroyed right after this call to this function(see updateProjectiles) Projectile* projectileCopy = new Projectile(*projectile); projectileCopy->pierce_ = 10; projectileCopy->update(true); projectiles.push_back(projectileCopy); } } //// // draw functions //// void MainGame::draw () { vertices_.resize (28 + blocks.size () * 4 + projectiles.size () * 4 + bar.size() * 4 + projectiles2.size() * 4); int vertex = 0; for (int i = 0; i < blocks.size (); ++i) { setQuad (blocks[i]->getRectangle(), blocks[i]->second, vertex); vertex += 4; } for (int i = 0; i < projectiles.size (); ++i) { setQuad (*projectiles[i], vertex); vertex += 4; } for (int i = 0; i < projectiles2.size (); ++i) { setQuad (*projectiles2[i], vertex); vertex += 4; } projectiles2.clear(); setQuad (charge_->getRectangle(), charge_->second, vertex); vertex += 4; setQuad (player_, vertex); vertex += 4; setQuad(playerHealthBar_, Rectangle(850, 650, 10, 10), vertex); vertex += 4; setQuad (player2_.getRectangle(), player_.second, vertex); vertex += 4; setQuad(player2HP_, Rectangle(850, 650, 10, 10), vertex); vertex += 4; for (int i = 0; i < bar.size (); ++i) { setQuad (bar[i], vertex); vertex += 4; } window_.draw (vertices_, &texture_); vertices_.clear (); } #include <SFML/Graphics.hpp> #include "MainGame.h" using std::string; using std::vector; using std::cout; using std::endl; using sf::Vector2f; using std::list; using std::string; int main () { MainGame game("maintexture.png"); vector<Rectangle> v2f; // for starting blocks v2f.push_back(Rectangle(0 , 0 , 100 , 1000)); v2f.push_back(Rectangle(0 , 0 , 1000, 100 )); v2f.push_back(Rectangle(0 , 1000, 1000, 100 )); //v2f.push_back(Rectangle(1000, 0 , 100 , 1000)); v2f.push_back(Rectangle( - 3000 , 3000, 10000 , 10000)); vector<Rectangle> v2f2; // this one is for texture coords for (int i = 0; i < 100; ++i) // 100 is number of blocks { v2f2.push_back(Rectangle(10, 10, 90, 90)); } Rectangle v2f3(5875, 50, 50, 100); for (int i = 0; i < v2f.size(); ++i) { game.setBlock(v2f[i], v2f2[i]); } game.setPlayer(Rectangle(300, 300, 50, 100), v2f3); sf::Font font; font.loadFromFile("arial.ttf"); sf::Text text; text.setColor(sf::Color::Red); text.setCharacterSize(35); text.setFont(font); game.mainGame(text); return 0; }
Optional Paste Settings
Syntax Highlighting:
None
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
ActionScript
ActionScript 3
Ada
AIMMS
ALGOL 68
Apache Log
AppleScript
APT Sources
ARM
ASM (NASM)
ASP
Asymptote
autoconf
Autohotkey
AutoIt
Avisynth
Awk
BASCOM AVR
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BNF
BOO
BrainFuck
C
C (WinAPI)
C for Macs
C Intermediate Language
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CMake
COBOL
CoffeeScript
ColdFusion
CSS
Cuesheet
D
Dart
DCL
DCPU-16
DCS
Delphi
Delphi Prism (Oxygene)
Diff
DIV
DOT
E
Easytrieve
ECMAScript
Eiffel
Email
EPC
Erlang
F#
Falcon
FO Language
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
Game Maker
GDB
Genero
Genie
GetText
Go
Groovy
GwBasic
Haskell
Haxe
HicEst
HQ9 Plus
HTML
HTML 5
Icon
IDL
INI file
Inno Script
INTERCAL
IO
ISPF Panel Definition
J
Java
Java 5
JavaScript
JCL
jQuery
JSON
Julia
KiXtart
Latex
LDIF
Liberty BASIC
Linden Scripting
Lisp
LLVM
Loco Basic
Logtalk
LOL Code
Lotus Formulas
Lotus Script
LScript
Lua
M68000 Assembler
MagikSF
Make
MapBasic
MatLab
mIRC
MIX Assembler
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MPASM
MXML
MySQL
Nagios
NetRexx
newLISP
Nginx
Nimrod
None
NullSoft Installer
Oberon 2
Objeck Programming Langua
Objective C
OCalm Brief
OCaml
Octave
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
ParaSail
PARI/GP
Pascal
Pawn
PCRE
Per
Perl
Perl 6
PHP
PHP Brief
Pic 16
Pike
Pixel Bender
PL/SQL
PostgreSQL
PostScript
POV-Ray
Power Shell
PowerBuilder
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
q/kdb+
QBasic
QML
R
Racket
Rails
RBScript
REBOL
REG
Rexx
Robots
RPM Spec
Ruby
Ruby Gnuplot
Rust
SAS
Scala
Scheme
Scilab
SCL
SdlBasic
Smalltalk
Smarty
SPARK
SPARQL
SQF
SQL
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
Tera Term
thinBasic
TypoScript
Unicon
UnrealScript
UPC
Urbi
Vala
VB.NET
VBScript
Vedit
VeriLog
VHDL
VIM
Visual Pro Log
VisualBasic
VisualFoxPro
WhiteSpace
WHOIS
Winbatch
XBasic
XML
Xorg Config
XPP
YAML
Z80 Assembler
ZXBasic
Paste Expiration:
Never
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
Paste Exposure:
Public
Unlisted
Private (members only)
Paste Name / Title:
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Top