Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #92 from lucat1/clean_up/game
Browse files Browse the repository at this point in the history
Game module cleanup
  • Loading branch information
foxyseta authored Sep 4, 2021
2 parents 99b0d58 + 99bfc82 commit 2f84be1
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 242 deletions.
277 changes: 142 additions & 135 deletions src/game/combat_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,150 @@
*/
#include "combat_manager.hpp"
#include "gameplay_manager.hpp"

using Game::CombatManager;

CombatManager::CombatManager(MenuManager &menu_manager)
: menu_manager(menu_manager) {}

Nostd::Matrix<Data::Pawns::Item *>::iterator CombatManager::get_item() {
auto &position = menu_manager.get_world().player.second;
return position.get_fragment()->items[position.get_y()][position.get_x()];
void CombatManager::manage_items() {
auto item = get_item();
if (item.value() != nullptr) {
auto &hero = menu_manager.get_world().player.first;
hero.interact(*item.value());
menu_manager.set_message(Nostd::String(hero.name())
.append(" found ")
.append(item.value()->name())
.append("!"));
auto &items = menu_manager.get_world().items;
// erase the Item which is being consumed
for (auto p = items.begin(); p != items.end(); p++)
if (item.value() == &p->first) {
item.value() = nullptr;
items.erase(p, std::next(p));
break;
}
}
}

void CombatManager::manage_projectiles() {
auto &projectiles = menu_manager.get_world().projectiles;
auto &player = menu_manager.get_world().player;
for (auto p = projectiles.begin(); p != projectiles.end();) {
bool to_be_destroyed = p->first.is_expired();
if (!to_be_destroyed) {
Nostd::Matrix<Data::Pawns::Enemy *>::iterator enemy =
get_enemy(p->second);
if (p->first.was_casted_by_player()) {
// hit enemy
if (enemy.value() != nullptr) {
enemy.value()->kill();
player.first.award();
menu_manager.set_message(Nostd::String(enemy.value()->name())
.append(" was hit by ")
.append(p->first.name())
.append("."));
enemy.value() = nullptr;
}
} else if (p->second == player.second) {
// hit player
player.first.interact(p->first);
to_be_destroyed = true;
menu_manager.set_message(Nostd::String(player.first.name())
.append(" was hit by ")
.append(p->first.name())
.append("!"));
}
if (!to_be_destroyed) {
// move projectile
Nostd::Matrix<Data::Pawns::Projectile *>::iterator projectile_pointer =
get_projectile(p->second);
projectile_pointer.value() = nullptr;
if (move_projectile(p->first, &p->second) &&
GameplayManager::can_dig(get_mapunit(p->second))) {
projectile_pointer = get_projectile(p->second);
if (projectile_pointer.value() == nullptr)
projectile_pointer.value() = &p->first;
else
to_be_destroyed = true;
} else
to_be_destroyed = true;
}
}
if (to_be_destroyed) {
// destroy projectile
get_projectile(p->second).value() = nullptr;
p = projectiles.erase(p, std::next(p));
} else {
p->first.count_movement();
p++;
}
}
}

void CombatManager::manage_enemies() {
auto &enemies = menu_manager.get_world().enemies;
auto &hero = menu_manager.get_world().player.first;
for (auto e = enemies.begin(); e != enemies.end();) {
bool to_be_destroyed = e->first.is_dead();
auto enemy_pointer = get_enemy(e->second);
if (!to_be_destroyed) {
if (e->second == menu_manager.get_world().player.second) {
// the Enemy bumps into the player
hero.interact(e->first);
menu_manager.set_message(Nostd::String(hero.name())
.append(" was hit by ")
.append(e->first.name())
.append("!"));
to_be_destroyed = true;
} else
enemy_act(e);
}
// destroy Enemy
if (to_be_destroyed) {
e = enemies.erase(e, std::next(e));
enemy_pointer.value() = nullptr;
} else
e++;
}
}

void CombatManager::use_skill() {
auto &hero = menu_manager.get_world().player.first;
hero.interact(hero.skill());
menu_manager.set_message(Nostd::String(hero.name())
.append(" used ")
.append(hero.skill().name())
.append("."));
cast_skill(hero.skill(), menu_manager.get_world().player.second);
}

void CombatManager::use_superskill() {
auto &hero = menu_manager.get_world().player.first;
if (hero.attempt_super_skill()) {
hero.interact(hero.superskill());
menu_manager.set_message(Nostd::String(hero.name())
.append(" used ")
.append(hero.superskill().name())
.append("!"));
cast_skill(hero.superskill(), menu_manager.get_world().player.second);
} else
menu_manager.set_message(Nostd::String(hero.name())
.append(" lacks the mana for ")
.append(hero.superskill().name())
.append("..."));
}

Data::MapUnit CombatManager::get_mapunit(World::Position position) {
return position.get_fragment()
->map_chunk->at(position.get_y())
.at(position.get_x())
.value();
}

Nostd::Matrix<Data::Pawns::Enemy *>::iterator
CombatManager::get_enemy(World::Position position) {
return position.get_fragment()
->enemies.at(position.get_y())
.at(position.get_x());
Nostd::Matrix<Data::Pawns::Item *>::iterator CombatManager::get_item() {
auto &position = menu_manager.get_world().player.second;
return position.get_fragment()->items[position.get_y()][position.get_x()];
}

Nostd::Matrix<Data::Pawns::Projectile *>::iterator
Expand All @@ -40,8 +163,15 @@ CombatManager::get_projectile(World::Position position) {
.at(position.get_x());
}

bool CombatManager::move_projectiles(Data::Pawns::Projectile projectile,
World::Position *position) {
Nostd::Matrix<Data::Pawns::Enemy *>::iterator
CombatManager::get_enemy(World::Position position) {
return position.get_fragment()
->enemies.at(position.get_y())
.at(position.get_x());
}

bool CombatManager::move_projectile(Data::Pawns::Projectile projectile,
World::Position *position) {
switch (projectile.get_x()) { // x
case -1:
if (!position->move_left())
Expand Down Expand Up @@ -70,8 +200,9 @@ void CombatManager::cast_skill(Data::Pawns::Skill skill,
World::Position position) {
for (auto projectile : skill.projectiles()) {
World::Position projectile_position = position;
if (move_projectiles(projectile, &projectile_position) &&
if (move_projectile(projectile, &projectile_position) &&
GameplayManager::can_dig(get_mapunit(projectile_position))) {
// the Projectile can be spawned
menu_manager.get_world().projectiles.push_back(
{projectile, projectile_position});
projectile_position.get_fragment()
Expand Down Expand Up @@ -158,127 +289,3 @@ void CombatManager::enemy_act(
cast_skill(e->first.skill(), e->second);
}
}

void CombatManager::manage_items() {
auto item = get_item();
if (item.value() != nullptr) {
auto &hero = menu_manager.get_world().player.first;
hero.interact(*item.value());
menu_manager.set_message(Nostd::String(hero.name())
.append(" found ")
.append(item.value()->name())
.append("!"));
auto &items = menu_manager.get_world().items;
for (auto p = items.begin(); p != items.end(); p++)
if (item.value() == &p->first) {
item.value() = nullptr;
items.erase(p, std::next(p));
break;
}
}
}

void CombatManager::manage_projectiles() {
auto &projectiles = menu_manager.get_world().projectiles;
auto &player = menu_manager.get_world().player;
for (auto p = projectiles.begin(); p != projectiles.end();) {
bool to_be_destroyed = p->first.is_expired();
if (!to_be_destroyed) {
Nostd::Matrix<Data::Pawns::Enemy *>::iterator enemy =
get_enemy(p->second);
if (p->first.was_casted_by_player()) {
// hit enemy
if (enemy.value() != nullptr) {
enemy.value()->kill();
player.first.award();
menu_manager.set_message(Nostd::String(enemy.value()->name())
.append(" was hit by ")
.append(p->first.name())
.append("."));
enemy.value() = nullptr;
}
} else if (p->second == player.second) {
// hit player
player.first.interact(p->first);
to_be_destroyed = true;
menu_manager.set_message(Nostd::String(player.first.name())
.append(" was hit by ")
.append(p->first.name())
.append("!"));
}
if (!to_be_destroyed) {
// move projectile
Nostd::Matrix<Data::Pawns::Projectile *>::iterator projectile_pointer =
get_projectile(p->second);
projectile_pointer.value() = nullptr;
if (move_projectiles(p->first, &p->second) &&
GameplayManager::can_dig(get_mapunit(p->second))) {
projectile_pointer = get_projectile(p->second);
if (projectile_pointer.value() == nullptr)
projectile_pointer.value() = &p->first;
else
to_be_destroyed = true;
} else
to_be_destroyed = true;
}
}
if (to_be_destroyed) {
// destroy projectile
get_projectile(p->second).value() = nullptr;
p = projectiles.erase(p, std::next(p));
} else {
p->first.count_movement();
p++;
}
}
}

void CombatManager::manage_enemies() {
auto &enemies = menu_manager.get_world().enemies;
auto &hero = menu_manager.get_world().player.first;
for (auto e = enemies.begin(); e != enemies.end();) {
bool to_be_destroyed = e->first.is_dead();
auto enemy_pointer = get_enemy(e->second);
if (!to_be_destroyed) {
if (e->second == menu_manager.get_world().player.second) {
hero.interact(e->first);
menu_manager.set_message(Nostd::String(hero.name())
.append(" was hit by ")
.append(e->first.name())
.append("!"));
to_be_destroyed = true;
} else
enemy_act(e);
// TODO
}
if (to_be_destroyed) {
e = enemies.erase(e, std::next(e));
enemy_pointer.value() = nullptr;
} else
e++;
}
}
void CombatManager::use_skill() {
auto &hero = menu_manager.get_world().player.first;
hero.interact(hero.skill());
menu_manager.set_message(Nostd::String(hero.name())
.append(" used ")
.append(hero.skill().name())
.append("."));
cast_skill(hero.skill(), menu_manager.get_world().player.second);
}
void CombatManager::use_superskill() {
auto &hero = menu_manager.get_world().player.first;
if (hero.attempt_super_skill()) {
hero.interact(hero.superskill());
menu_manager.set_message(Nostd::String(hero.name())
.append(" used ")
.append(hero.superskill().name())
.append("!"));
cast_skill(hero.superskill(), menu_manager.get_world().player.second);
} else
menu_manager.set_message(Nostd::String(hero.name())
.append(" lacks the mana for ")
.append(hero.superskill().name())
.append("..."));
}
40 changes: 27 additions & 13 deletions src/game/combat_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,43 @@
#include "../world/random_generator.hpp"
#include "../world/world.hpp"
#include "menu_manager.hpp"
namespace Game {

namespace Game {
/*A CombatManager implements the combat logic, allowing items,
projectiles and enemies to take life. It also lets the player
cast their skills.
*/
class CombatManager {
public:
CombatManager(MenuManager &);

void manage_items();
void manage_projectiles();
void manage_enemies();
void use_skill();
void use_superskill();

private:
Nostd::Matrix<Data::Pawns::Item *>::iterator get_item();
MenuManager &menu_manager;

World::RandomGenerator random_generator;

// Returns the MapUnit/Item/Projectile/Enemy (or an iterator to it)
// in the given Position.
Data::MapUnit get_mapunit(World::Position);
Nostd::Matrix<Data::Pawns::Enemy *>::iterator get_enemy(World::Position);
Nostd::Matrix<Data::Pawns::Item *>::iterator get_item();
Nostd::Matrix<Data::Pawns::Projectile *>::iterator
get_projectile(World::Position);
World::RandomGenerator random_generator;
bool move_projectiles(Data::Pawns::Projectile, World::Position *);
Nostd::Matrix<Data::Pawns::Enemy *>::iterator get_enemy(World::Position);

// Updates the Position of a Projectile, returning true if and only if
// this operation was successful.
bool move_projectile(Data::Pawns::Projectile, World::Position *);

// Casts a new Skill starting from the given Position.
void cast_skill(Data::Pawns::Skill, World::Position);
void enemy_act(
Nostd::List<Nostd::Pair<Data::Pawns::Enemy, World::Position>>::iterator);

public:
CombatManager(MenuManager &);
void manage_items();
void manage_projectiles();
void manage_enemies();
void use_skill();
void use_superskill();
};
} // namespace Game

Expand Down
Loading

0 comments on commit 2f84be1

Please sign in to comment.