diff --git a/BeamWeapon.h b/BeamWeapon.h index 5e45062..9f1925c 100644 --- a/BeamWeapon.h +++ b/BeamWeapon.h @@ -12,18 +12,20 @@ private: int duration; int framesShot = 0; public: - BeamWeapon(Beam _proj, int _duration, int frameDelay) : Weapon(frameDelay) { + BeamWeapon(Beam _proj, const sf::SoundBuffer& buffer, float volume, double effectiveAngle, int _duration, int frameDelay) : Weapon(frameDelay, effectiveAngle, buffer, volume) { projectile = std::move(_proj); duration = _duration; } - Shootable* shoot(const Ship* shooter) { + Shootable* shoot(const Ship* shooter) override { framesShot++; if (framesShot == duration) { framesShot = 0; currentFrame = 0; } + noise.play(); + projectile.setShooter((GameSprite *) shooter); double newWidth = shooter->getTarget() == nullptr ? projectile.getRange() : GameSprite::distance(shooter->getPosition(), shooter->getTarget()->getPosition()); projectile.setTextureRect(sf::IntRect(projectile.getTextureRect().left, projectile.getTextureRect().top, newWidth > projectile.getRange() ? projectile.getRange() * (1/projectile.getScale().x) : newWidth * (1/projectile.getScale().x), projectile.getTextureRect().height)); diff --git a/CMakeLists.txt b/CMakeLists.txt index 494a3d2..58d8a4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 14) include_directories("D:/Program Files/mingw-w64/mingw64/x86_64-w64-mingw32/include") -add_executable(SFML_Template main.cpp Game.cpp Game.h GameSprite.cpp GameSprite.h Ship.cpp Ship.h System.cpp System.h Planet.cpp Planet.h Collision.cpp Collision.h Menu.cpp Menu.h) +add_executable(SFML_Template main.cpp Game.cpp Game.h GameSprite.cpp GameSprite.h Ship.cpp Ship.h System.cpp System.h Planet.cpp Planet.h Collision.cpp Collision.h Menu.cpp Menu.h GameSound.h) target_link_directories(SFML_Template PUBLIC "D:/Program Files/mingw-w64/mingw64/x86_64-w64-mingw32/lib") diff --git a/COMShip.h b/COMShip.h index 1437a8b..6a31730 100644 --- a/COMShip.h +++ b/COMShip.h @@ -34,6 +34,18 @@ public: name = std::move(_name); }; + void shoot(std::vector &shots) override { + for (Weapon *w : weapons) { + double angle = -getAimAngle(target->getPosition(), getPosition()); + double leftEffectiveBorder = spritePhysics.direction + w->getEffectiveAngle()/2; + double rightEffectiveBorder = spritePhysics.direction - w->getEffectiveAngle()/2; + + if (w->canShoot() && ( angle < leftEffectiveBorder && angle > rightEffectiveBorder) && distance(target->getPosition(), getPosition()) < w->getProjectile().getRange()) { + shots.push_back(w->shoot(this)); + } + } + } + template< class RNG > Status pathfind(const sf::RenderWindow &window, RNG &gen, System *loc, Ship* player, std::vector &projectiles) { if (status != ATTACKING && isHostile()) { diff --git a/Game.cpp b/Game.cpp index 6fbd0e2..be332d1 100644 --- a/Game.cpp +++ b/Game.cpp @@ -192,14 +192,22 @@ void Game::init() { updateLoader(window, "Loading ship textures..."); } - //Load projectile textures + //Load projectile textures and sounds sf::Texture laser; Collision::CreateTextureAndBitmask(laser, "./data/Projectiles/laser.png"); + sf::Sound laserNoise; + sf::SoundBuffer laserBuffer; + laserBuffer.loadFromFile("./data/Sounds/Projectiles/laser.wav"); + sf::Texture beam; Collision::CreateTextureAndBitmask(beam, "./data/Projectiles/beam.png"); beam.setRepeated(true); + sf::Sound beamNoise; + sf::SoundBuffer beamBuffer; + beamBuffer.loadFromFile("./data/Sounds/Projectiles/beam.wav"); + //load GUI textures sf::Texture mapWin; mapWin.loadFromFile("./data/Gui/mapBox.png"); @@ -446,7 +454,7 @@ void Game::init() { sf::Vector2f currentPos; Ship *player = new Ship(ship, 50, window.getSize().x / (float) 2.0, window.getSize().y / (float) 2.0, 0, 10, -45, 2,3, 500, 100, 5); - player->addWeapon(new BeamWeapon(Beam(beam, sf::IntRect(0, 207, 535, 91), 10, 12, 1, 0, 513, 3, 1, 1500), 25, 40)); + player->addWeapon(new BeamWeapon(Beam(beam, sf::IntRect(0, 207, 535, 91), 10, 12, 1, 0, 513, 3, 1, 1500), beamBuffer, 50, 360 , 25, 40)); int playerMoney = 100000; System *currentSystem = systems[0]; @@ -568,7 +576,7 @@ void Game::init() { int randYpos = roll(gen); ships.push_back(new COMShip(*shipTextures[n], shipTextureScales[n],currentSystem->getPlanets()[1]->getXPos() + randXpos,currentSystem->getPlanets()[1]->getYPos() + randYpos, 0, 0.1, 10, 0, 2, 0, 500, 0,0, wordWrap(generateName(gen), targetWindow.getGlobalBounds().width, monkirta, 15),currentSystem->getSysRep())); - ships[ships.size() - 1]->addWeapon(new ProjectileWeapon(Projectile(laser, sf::IntRect(29, 207, 651, 91), 10, 4, 3, 676, 513, 3, 15, 25, 1500), 40)); + ships[ships.size() - 1]->addWeapon(new BeamWeapon(Beam(beam, sf::IntRect(0, 207, 535, 91), 10, 12, 1, 0, 513, 3, 1, 1500), beamBuffer, 50, 360 , 25, 40)); } //planet pointer @@ -955,7 +963,7 @@ void Game::init() { int randYpos = roll(gen); ships.push_back(new COMShip(*shipTextures[n], shipTextureScales[n],currentSystem->getPlanets()[1]->getXPos() + randXpos,currentSystem->getPlanets()[1]->getYPos() + randYpos, 0, 0.1, 10, 0, 2, 0,500, 0, 0,wordWrap(generateName(gen), targetWindow.getGlobalBounds().width, monkirta,15),currentSystem->getSysRep())); - ships[ships.size() - 1]->addWeapon(new ProjectileWeapon(Projectile(laser, sf::IntRect(29, 207, 651, 91), 10, 4, 3, 676, 513, 3, 15, 25, 1500), 40)); + ships[ships.size() - 1]->addWeapon(new ProjectileWeapon(Projectile(laser, sf::IntRect(29, 207, 651, 91), 10, 4, 3, 676, 513, 3, 15, 25, 1500), laserBuffer, 50, 10, 40)); sound.setBuffer(warp); sound.setVolume((GameSprite::distance(player->getPosition(), ships[ships.size() - 1]->getPosition()) > 2000) ? 0 : 100 * (2000 - GameSprite::distance(player->getPosition(), ships[ships.size() - 1]->getPosition())) / 2000); @@ -1130,7 +1138,7 @@ void Game::init() { int randYpos = roll(gen); ships.push_back(new COMShip(*shipTextures[n], shipTextureScales[n],currentSystem->getPlanets()[1]->getXPos() + randXpos,currentSystem->getPlanets()[1]->getYPos() + randYpos, 0, 0.1, 10, 0, 2,0, 500, 0, 0,wordWrap(generateName(gen), targetWindow.getGlobalBounds().width,monkirta, 15),currentSystem->getSysRep())); - ships[ships.size() - 1]->addWeapon(new ProjectileWeapon(Projectile(laser, sf::IntRect(29, 207, 651, 91), 10, 4, 3, 676, 513, 3, 15, 25, 1500), 40)); + ships[ships.size() - 1]->addWeapon(new ProjectileWeapon(Projectile(laser, sf::IntRect(29, 207, 651, 91), 10, 4, 3, 676, 513, 3, 15, 25, 1500), laserBuffer, 10, 50, 40)); } mainView.setCenter(player->getXPos(), player->getYPos()); diff --git a/ProjectileWeapon.h b/ProjectileWeapon.h index 125b99b..7a16d8d 100644 --- a/ProjectileWeapon.h +++ b/ProjectileWeapon.h @@ -10,15 +10,17 @@ class ProjectileWeapon : public Weapon { public: - ProjectileWeapon(Projectile _proj, int frameDelay) : Weapon(frameDelay) { + ProjectileWeapon(Projectile _proj, const sf::SoundBuffer& buffer, float volume, double effectiveAngle, int frameDelay) : Weapon(frameDelay, effectiveAngle, buffer, volume) { projectile = std::move(_proj); } - Shootable* shoot(const Ship* shooter) { + Shootable* shoot(const Ship* shooter) override { currentFrame = 0; + noise.play(); + projectile.setDirection(shooter->getDirection()); - projectile.setPosition(shooter->getPosition()); + projectile.setPosition(shooter->getXPos() + shooter->getLocalBounds().width/4 * shooter->getScale().x * cos(shooter->getDirection()*GameSprite::PI/180), shooter->getYPos() - shooter->getLocalBounds().width/4 * shooter->getScale().x * sin(shooter->getDirection()*GameSprite::PI/180)); projectile.setShooter((GameSprite *) shooter); Shootable *copied = new Shootable(projectile); diff --git a/Ship.h b/Ship.h index 5a354a9..06a2e91 100644 --- a/Ship.h +++ b/Ship.h @@ -24,7 +24,7 @@ public: void update(); - void shoot(std::vector &projectiles); + virtual void shoot(std::vector &projectiles); float getTurnRate() const; diff --git a/Weapon.h b/Weapon.h index 3002fcb..faba9bc 100644 --- a/Weapon.h +++ b/Weapon.h @@ -7,12 +7,15 @@ #include "Projectile.h" #include "GameSprite.h" +#include #include class Weapon { protected: int frameDelay, currentFrame; Shootable projectile; + sf::Sound noise; + double effectiveAngle; Weapon() { frameDelay = 0; @@ -21,8 +24,12 @@ protected: projectile = Shootable(); }; - explicit Weapon(int _frameDelay) : Weapon() { + explicit Weapon(int _frameDelay, double _effectiveAngle, const sf::SoundBuffer& _buffer, float volume) : Weapon() { + effectiveAngle = _effectiveAngle; frameDelay = _frameDelay; + + noise.setBuffer(_buffer); + noise.setVolume(volume); } public: virtual Shootable* shoot(const Ship* shooter) { @@ -42,6 +49,10 @@ public: Shootable& getProjectile() { return projectile; } + + double getEffectiveAngle() const { + return effectiveAngle; + } }; #endif //SFML_TEMPLATE_WEAPON_H diff --git a/data/Sounds/Projectiles/laser.wav b/data/Sounds/Projectiles/laser.wav new file mode 100644 index 0000000..d23bef4 Binary files /dev/null and b/data/Sounds/Projectiles/laser.wav differ