Skip to content

Commit

Permalink
Random refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arf20 committed Feb 15, 2024
1 parent f588027 commit 1dca6b7
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 151 deletions.
44 changes: 22 additions & 22 deletions src/game.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
ww1game: Generic WW1 game (?)
game.cpp: Game mechanics and logic
ww1game: Generic WW1 game (?)
game.cpp: Game mechanics and logic
Copyright (C) 2022 Ángel Ruiz Fernandez
Copyright (C) 2022 Ángel Ruiz Fernandez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "main.hpp"
Expand Down Expand Up @@ -55,7 +55,7 @@ std::normal_distribution<double> soldierGauss(1.0, 0.1); // variation in sold
std::normal_distribution<double> bulletGauss(0.0, 1.0); // aim inaccuracy

// manipulate soldiers
void soldierSpawn(const std::vector<Assets::Character>::iterator& character, bool enemy) {
void Game::soldierSpawn(const std::vector<Assets::Character>::iterator& character, bool enemy) {
Game::Soldier soldier;
soldier.character = character;
if (enemy) {
Expand All @@ -82,7 +82,7 @@ void soldierSpawn(const std::vector<Assets::Character>::iterator& character, boo
Game::friendlies.push_back(soldier);
}

void soldierDeath(const std::vector<Game::Soldier>::iterator& soldier) {
void Game::soldierDeath(const std::vector<Game::Soldier>::iterator& soldier) {
if (soldier->state == Game::Soldier::DYING) return;
soldier->state = Game::Soldier::DYING;
soldier->frameCounter = 0;
Expand All @@ -92,7 +92,7 @@ void soldierDeath(const std::vector<Game::Soldier>::iterator& soldier) {
else Game::enemyCasualties++;
}

void soldierFire(const std::vector<Game::Soldier>::iterator& soldier) {
void Game::soldierFire(const std::vector<Game::Soldier>::iterator& soldier) {
if (soldier->state == Game::Soldier::DYING) return;
if (soldier->state == Game::Soldier::FIRING) return;
soldier->prevState = soldier->state;
Expand Down Expand Up @@ -211,7 +211,7 @@ bool intersectsMap(const vector& a, const vector& b) {
}

// ============== game itself ==============
void mapSetup() {
void Game::mapSetup() {
findMapPath();
Game::selectedTerrainVariant = getTerrainVariantByName(Game::selectedMap->terrainVariantName);
Game::friendlyFaction = getFactionByName(Game::selectedMap->friendlyFactionName);
Expand All @@ -237,13 +237,13 @@ void updateBullets(float deltaTime) {
// soldier collision, no friendly fire
if (bullet.fromEnemy) {
for (Game::Soldier& soldier : Game::friendlies)
if ((bullet.pos.x > soldier.pos.x) && (bullet.pos.y > soldier.pos.y) && (bullet.pos.x < soldier.pos.x + soldier.character->size.x) && (bullet.pos.y < soldier.pos.y + soldier.character->size.y)) {
if ((soldier.state != Game::Soldier::DYING) && (bullet.pos.x > soldier.pos.x) && (bullet.pos.y > soldier.pos.y) && (bullet.pos.x < soldier.pos.x + soldier.character->size.x) && (bullet.pos.y < soldier.pos.y + soldier.character->size.y)) {
soldier.health -= bullet.damage;
Game::bullets.erase(it); break;
}
} else {
for (Game::Soldier& soldier : Game::enemies)
if ((bullet.pos.x > soldier.pos.x) && (bullet.pos.y > soldier.pos.y) && (bullet.pos.x < soldier.pos.x + soldier.character->size.x) && (bullet.pos.y < soldier.pos.y + soldier.character->size.y)) {
if ((soldier.state != Game::Soldier::DYING) && (bullet.pos.x > soldier.pos.x) && (bullet.pos.y > soldier.pos.y) && (bullet.pos.x < soldier.pos.x + soldier.character->size.x) && (bullet.pos.y < soldier.pos.y + soldier.character->size.y)) {
soldier.health -= bullet.damage;
Game::bullets.erase(it); break;
}
Expand Down Expand Up @@ -319,7 +319,7 @@ void updateFaction(std::vector<Game::Soldier>& soldiers, const std::vector<Game:

// death logic
if (soldier.health <= 0)
soldierDeath(it);
Game::soldierDeath(it);

if (soldier.state == Game::Soldier::DYING) {
if (soldier.frameCounter >= soldier.character->death.size()) soldiers.erase(it);
Expand All @@ -331,7 +331,7 @@ void updateFaction(std::vector<Game::Soldier>& soldiers, const std::vector<Game:

bool mapcheck = true;
if (nearestTarget != targetEnemies.end()) {
vector muzzlePoint = {soldier.pos.x + (1.0f * soldier.character->size.x / 4.0f), soldier.pos.y + (soldier.character->size.x / 3.0f)};
vector muzzlePoint = {soldier.pos.x + (3.0f * soldier.character->size.x / 4.0f), soldier.pos.y + (soldier.character->size.x / 3.0f)};
vector targetPointBody = (nearestTarget->character->size / 2.0f) + nearestTarget->pos;
vector targetPointHead = nearestTarget->pos; targetPointHead.y += nearestTarget->character->size.y / 4.0f;

Expand Down Expand Up @@ -425,7 +425,7 @@ void updateFaction(std::vector<Game::Soldier>& soldiers, const std::vector<Game:
resetTrenches(soldiers);
}

void gameUpdate(float deltaTime) {
void Game::update(float deltaTime) {
updateBullets(deltaTime);

updateFaction(Game::friendlies, Game::enemies, deltaTime);
Expand Down
131 changes: 68 additions & 63 deletions src/loader.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
ww1game: Generic WW1 game (?)
loader.cpp: Asset loading
ww1game: Generic WW1 game (?)
loader.cpp: Asset loading
Copyright (C) 2022 Ángel Ruiz Fernandez
Copyright (C) 2022 Ángel Ruiz Fernandez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "main.hpp"
Expand Down Expand Up @@ -46,14 +46,14 @@ std::string makeNameNice(std::string str) {
return str;
}

void loadTerrains() {
if (!std::filesystem::exists(ASSET_PATH "/textures"))
void loadTerrains(std::string assetPath) {
if (!std::filesystem::exists(assetPath + "/textures"))
exit_error("Textures directory does not exist");

if (!std::filesystem::exists(ASSET_PATH "/textures/terrain"))
if (!std::filesystem::exists(assetPath + "/textures/terrain"))
exit_error("Terrain directory does not exist");

for (const auto& entryVariant : std::filesystem::directory_iterator(ASSET_PATH "/textures/terrain")) {
for (const auto& entryVariant : std::filesystem::directory_iterator(assetPath + "/textures/terrain")) {
if (!entryVariant.is_directory()) continue;

Assets::TerrainVariant variant;
Expand Down Expand Up @@ -86,11 +86,11 @@ bool sortMaps(const Assets::Map& a, const Assets::Map& b) {
return a.id < b.id;
}

void loadMaps() {
if (!std::filesystem::exists(ASSET_PATH "/campaigns"))
void loadMaps(std::string assetPath) {
if (!std::filesystem::exists(assetPath + "/campaigns"))
exit_error("Terrain directory does not exist");

for (const auto& entryCampaign : std::filesystem::directory_iterator(ASSET_PATH "/campaigns")) {
for (const auto& entryCampaign : std::filesystem::directory_iterator(assetPath + "/campaigns")) {
if (!entryCampaign.is_directory()) continue;

Assets::Campaign campaign;
Expand Down Expand Up @@ -215,11 +215,11 @@ void loadCharacterConfiguration(const std::filesystem::path& path, Assets::Chara
}
}

void loadCharacters() {
if (!std::filesystem::exists(ASSET_PATH "/textures/factions"))
void loadCharacters(std::string assetPath) {
if (!std::filesystem::exists(assetPath + "/textures/factions"))
exit_error("Terrain directory does not exist");

for (const auto& entryFaction : std::filesystem::directory_iterator(ASSET_PATH "/textures/factions")) {
for (const auto& entryFaction : std::filesystem::directory_iterator(assetPath + "/textures/factions")) {
if (!entryFaction.is_directory()) continue;

Assets::Faction faction;
Expand Down Expand Up @@ -308,11 +308,11 @@ void loadCharacters() {
}
}

void loadFonts() {
if (!std::filesystem::exists(ASSET_PATH "/fonts"))
void loadFonts(std::string assetPath) {
if (!std::filesystem::exists(assetPath + "/fonts"))
exit_error("Fonts directory does not exist");

for (const auto& entryFont : std::filesystem::directory_iterator(ASSET_PATH "/fonts")) {
for (const auto& entryFont : std::filesystem::directory_iterator(assetPath + "/fonts")) {
if (!entryFont.is_regular_file()) continue;
if (entryFont.path().extension() != ".ttf") continue;

Expand All @@ -332,17 +332,17 @@ void loadFonts() {
if (Assets::fonts[i].name == "default") Assets::defaultFont = Assets::fonts.begin() + i;
}

void loadSounds() {
if (!std::filesystem::exists(ASSET_PATH "/sounds"))
void loadSounds(std::string assetPath) {
if (!std::filesystem::exists(assetPath + "/sounds"))
exit_error("Sounds directory does not exist");

if (!std::filesystem::exists(ASSET_PATH "/sounds/sfx"))
if (!std::filesystem::exists(assetPath + "/sounds/sfx"))
exit_error("Sfx directory does not exist");

if (!std::filesystem::exists(ASSET_PATH "/sounds/sfx/factions"))
if (!std::filesystem::exists(assetPath + "/sounds/sfx/factions"))
exit_error("Sfx/factions directory does not exist");

for (const auto& entryFaction : std::filesystem::directory_iterator(ASSET_PATH "/sounds/sfx/factions")) {
for (const auto& entryFaction : std::filesystem::directory_iterator(assetPath + "/sounds/sfx/factions")) {
if (!entryFaction.is_directory()) continue;
std::string factionName = entryFaction.path().filename().string();
auto faction = getFactionByName(factionName);
Expand Down Expand Up @@ -375,13 +375,13 @@ void loadSounds() {
}
}

if (!std::filesystem::exists(ASSET_PATH "/sounds/music"))
if (!std::filesystem::exists(assetPath + "/sounds/music"))
exit_error("Music directory does not exist");

if (!std::filesystem::exists(ASSET_PATH "/sounds/music/factions"))
if (!std::filesystem::exists(assetPath + "/sounds/music/factions"))
exit_error("Music/factions directory does not exist");

for (const auto& entryFaction : std::filesystem::directory_iterator(ASSET_PATH "/sounds/music/factions")) {
for (const auto& entryFaction : std::filesystem::directory_iterator(assetPath + "/sounds/music/factions")) {
if (!entryFaction.is_directory()) continue;
std::string factionName = entryFaction.path().filename().string();
auto faction = getFactionByName(factionName);
Expand Down Expand Up @@ -452,11 +452,11 @@ SDL_Color getPixel(SDL_Surface *surface, int x, int y) {
return rgb;
}

void loadBackgrounds() {
if (!std::filesystem::exists(ASSET_PATH "/textures/backgrounds"))
void loadBackgrounds(std::string assetPath) {
if (!std::filesystem::exists(assetPath + "/textures/backgrounds"))
exit_error("Backgrounds directory does not exist");

for (const auto& entryBackground : std::filesystem::directory_iterator(ASSET_PATH "/textures/backgrounds")) {
for (const auto& entryBackground : std::filesystem::directory_iterator(assetPath + "/textures/backgrounds")) {
if (!entryBackground.is_regular_file()) continue;
if (entryBackground.path().extension() != ".png") continue;

Expand Down Expand Up @@ -486,51 +486,56 @@ void loadBackgrounds() {
}
}

void loadAssets() {
if (!std::filesystem::exists(ASSET_PATH))
exit_error("Asset directory does not exist");
bool Assets::load(std::string assetPath) {
if (!std::filesystem::exists(assetPath)) {
warning("Asset directory " + assetPath + " does not exist");
return true;
}

if (!std::filesystem::exists(ASSET_PATH "/missing_texture.png"))
exit_error("Missing texture placeholder texture missing");
// Load placeholders
if (!std::filesystem::exists(assetPath + "/missing_texture.png"))
warning("Missing texture placeholder texture missing");

if ((Assets::missingTextureTexture = IMG_LoadTexture(renderer, ASSET_PATH "/missing_texture.png")) == NULL)
exit_error_img("IMG_LoadTexture failed on missing_texture");
if ((Assets::missingTextureTexture = IMG_LoadTexture(renderer, (assetPath + "/missing_texture.png").c_str())) == NULL)
error_img("IMG_LoadTexture failed on missing_texture");

if (!std::filesystem::exists(ASSET_PATH "/missing_sound.ogg"))
exit_error("Missing sound placeholder texture missing");
if (!std::filesystem::exists(assetPath + "/missing_sound.ogg"))
warning("Missing sound placeholder texture missing");

if ((Assets::missingSoundSound = Mix_LoadWAV(ASSET_PATH "/missing_sound.ogg")) == NULL)
exit_error_sdl("Mix_LoadWAV failed on missing_sound");
if ((Assets::missingSoundSound = Mix_LoadWAV((assetPath + "/missing_sound.ogg").c_str())) == NULL)
warning("Mix_LoadWAV failed on missing_sound");

if ((Assets::missingMusicMusic = Mix_LoadMUS(ASSET_PATH "/missing_sound.ogg")) == NULL)
exit_error_sdl("Mix_LoadMUS failed on missing_sound");
if ((Assets::missingMusicMusic = Mix_LoadMUS((assetPath + "/missing_sound.ogg").c_str())) == NULL)
warning("Mix_LoadMUS failed on missing_sound");

std::cout << "Loading terrains..." << std::endl;
loadTerrains();
loadTerrains(assetPath);
std::cout << "Loading maps..." << std::endl;
loadMaps();
loadMaps(assetPath);
std::cout << "Loading characters..." << std::endl;
loadCharacters();
loadCharacters(assetPath);
std::cout << "Loading fonts..." << std::endl;
loadFonts();
loadFonts(assetPath);
std::cout << "Loading sounds..." << std::endl;
loadSounds();
loadSounds(assetPath);
std::cout << "Loading backgrounds..." << std::endl;
loadBackgrounds();
loadBackgrounds(assetPath);

if (!std::filesystem::exists(ASSET_PATH "/textures/bullet.png"))
exit_error("Bullet texture missing");
if (!std::filesystem::exists(assetPath + "/textures/bullet.png"))
warning("Bullet texture missing");

if ((Assets::bulletTexture = IMG_LoadTexture(renderer, ASSET_PATH "/textures/bullet.png")) == NULL) {
if ((Assets::bulletTexture = IMG_LoadTexture(renderer, (assetPath + "/textures/bullet.png").c_str())) == NULL) {
error_img("IMG_LoadTexture failed on assets/textures/bullet.png");
Assets::bulletTexture = Assets::missingTextureTexture;
}

if (!std::filesystem::exists(ASSET_PATH "/textures/flagpole.png"))
exit_error("Bullet texture missing");
if (!std::filesystem::exists(assetPath + "/textures/flagpole.png"))
warning("Bullet texture missing");

if ((Assets::flagpoleTexture = IMG_LoadTexture(renderer, ASSET_PATH "/textures/flagpole.png")) == NULL) {
if ((Assets::flagpoleTexture = IMG_LoadTexture(renderer, (assetPath + "/textures/flagpole.png").c_str())) == NULL) {
error_img("IMG_LoadTexture failed on assets/textures/flagpole.png");
Assets::flagpoleTexture = Assets::missingTextureTexture;
}
}

return false;
}
Loading

0 comments on commit 1dca6b7

Please sign in to comment.