added weapon sounds and AI doesnt shoot if not in range

This commit is contained in:
bMorgan01 2021-04-27 18:43:40 -06:00
parent d1a7053149
commit 8016bab3e8
8 changed files with 48 additions and 13 deletions

View file

@ -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));

View file

@ -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")

View file

@ -34,6 +34,18 @@ public:
name = std::move(_name);
};
void shoot(std::vector<Shootable*> &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<Shootable*> &projectiles) {
if (status != ATTACKING && isHostile()) {

View file

@ -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());

View file

@ -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);

2
Ship.h
View file

@ -24,7 +24,7 @@ public:
void update();
void shoot(std::vector<Shootable*> &projectiles);
virtual void shoot(std::vector<Shootable*> &projectiles);
float getTurnRate() const;

View file

@ -7,12 +7,15 @@
#include "Projectile.h"
#include "GameSprite.h"
#include <SFML/Audio.hpp>
#include <iostream>
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

Binary file not shown.