fixed a seg fault on add weapon

This commit is contained in:
bMorgan01 2021-04-27 16:41:31 -06:00
parent 065d543d4d
commit d1a7053149
3 changed files with 123 additions and 157 deletions

View file

@ -3,10 +3,10 @@ project(SFML_Template)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
include_directories("D:/Program Files/mingw64/include") 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)
target_link_directories(SFML_Template PUBLIC "D:/Program Files/mingw64/lib") target_link_directories(SFML_Template PUBLIC "D:/Program Files/mingw-w64/mingw64/x86_64-w64-mingw32/lib")
target_link_libraries(SFML_Template sfml-graphics sfml-system sfml-window sfml-audio) target_link_libraries(SFML_Template sfml-graphics sfml-system sfml-window sfml-audio)

View file

@ -354,7 +354,8 @@ void Game::init() {
iss = std::istringstream(input); iss = std::istringstream(input);
results = std::vector<std::string>((std::istream_iterator<std::string>(iss)), results = std::vector<std::string>((std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>()); std::istream_iterator<std::string>());
if (results.size() <= 5) systems[systems.size() - 1]->addPlanet( if (results.size() <= 5)
systems[systems.size() - 1]->addPlanet(
new Planet(*planets[stoi(results[0])], stof(results[1]), window.getSize().x * stof(results[2]), new Planet(*planets[stoi(results[0])], stof(results[1]), window.getSize().x * stof(results[2]),
window.getSize().y * stof(results[3]), stof(results[4]))); window.getSize().y * stof(results[3]), stof(results[4])));
else { else {
@ -444,10 +445,8 @@ void Game::init() {
sf::Vector2f oldPos; sf::Vector2f oldPos;
sf::Vector2f currentPos; 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, 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);
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 ProjectileWeapon(
Projectile(laser, sf::IntRect(29, 207, 651, 91), 10, 4, 3, 676, 513, 3, 15, 25, 1500), 40));
int playerMoney = 100000; int playerMoney = 100000;
System *currentSystem = systems[0]; System *currentSystem = systems[0];
@ -568,13 +567,8 @@ void Game::init() {
int randXpos = roll(gen); int randXpos = roll(gen);
int randYpos = roll(gen); int randYpos = roll(gen);
ships.push_back(new COMShip(*shipTextures[n], shipTextureScales[n], 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()));
currentSystem->getPlanets()[1]->getXPos() + randXpos, 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));
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));
} }
//planet pointer //planet pointer
@ -938,12 +932,7 @@ void Game::init() {
if (res == COMShip::WARPING) { if (res == COMShip::WARPING) {
sound.setBuffer(warp); sound.setBuffer(warp);
sound.setVolume((GameSprite::distance(player->getPosition(), s->getPosition()) > 2000) ? 0 : 100 * sound.setVolume((GameSprite::distance(player->getPosition(), s->getPosition()) > 2000) ? 0 : 100 *(2000 -GameSprite::distance(player->getPosition(),s->getPosition())) /2000);
(2000 -
GameSprite::distance(
player->getPosition(),
s->getPosition())) /
2000);
sound.play(); sound.play();
if (player->getTarget() == s) { if (player->getTarget() == s) {
@ -965,23 +954,11 @@ void Game::init() {
int randXpos = roll(gen); int randXpos = roll(gen);
int randYpos = roll(gen); int randYpos = roll(gen);
ships.push_back(new COMShip(*shipTextures[n], shipTextureScales[n], 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()));
currentSystem->getPlanets()[1]->getXPos() + randXpos, 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));
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));
sound.setBuffer(warp); sound.setBuffer(warp);
sound.setVolume( 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);
(GameSprite::distance(player->getPosition(), ships[ships.size() - 1]->getPosition()) > 2000) ? 0
:
100 *
(2000 - GameSprite::distance(player->getPosition(), ships[ships.size() - 1]->getPosition())) /
2000);
sound.play(); sound.play();
messageLog.push_front(new sf::Text("A ship entered the system.", oxan, 18)); messageLog.push_front(new sf::Text("A ship entered the system.", oxan, 18));
@ -1152,15 +1129,8 @@ void Game::init() {
int randXpos = roll(gen); int randXpos = roll(gen);
int randYpos = roll(gen); int randYpos = roll(gen);
ships.push_back(new COMShip(*shipTextures[n], shipTextureScales[n], 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()));
currentSystem->getPlanets()[1]->getXPos() + randXpos, 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));
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));
} }
mainView.setCenter(player->getXPos(), player->getYPos()); mainView.setCenter(player->getXPos(), player->getYPos());
@ -1824,8 +1794,7 @@ void Game::init() {
} }
sysPopText.setString(std::to_string(systems[i]->getPop()) + " billion"); sysPopText.setString(std::to_string(systems[i]->getPop()) + " billion");
sysStrenText.setString(std::to_string(systems[i]->getStren())); sysStrenText.setString(std::to_string(systems[i]->getStren()));
} } else {
else {
sysInfoTitle.setString("???"); sysInfoTitle.setString("???");
sysStatusText.setString("Unexplored"); sysStatusText.setString("Unexplored");
sysStatusText.setFillColor(sf::Color::White); sysStatusText.setFillColor(sf::Color::White);

View file

@ -105,8 +105,5 @@ void Ship::setTarget(Ship *_target) {
} }
void Ship::addWeapon(Weapon *w) { void Ship::addWeapon(Weapon *w) {
Weapon *copiedWeapon; weapons.push_back(w);
*copiedWeapon = *w;
weapons.push_back(copiedWeapon);
} }