/
haseew
/
Lab_6
Обзор
Документация
Войти
/
haseew
/
Lab_6
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Game.cpp
129 строк
5 KB
Egorchik
Lab_6
28 апр 2026, 17:44
28 апр 2026, 17:44
f0a1a80
Код
Авторство
О чём код?
#include "Game.h" #include <cmath> // реализация базового исключения игры GameException::GameException(const std::string& msg) : std::runtime_error(msg) {} // исключение при действии мертвого персонажа ActionByDeadException::ActionByDeadException() : GameException("oshibka: mertvyj personazh ne mozhet deystvovat!") {} // исключение при неверных характеристиках InvalidStatsException::InvalidStatsException(const std::string& msg) : GameException(msg) {} // конструкторы оружия Weapon::Weapon() : name("bezoruzhen"), damage(0), range(0.0) {} Weapon::Weapon(std::string n, int d, double r) : name(n), damage(d), range(r) { if (damage < 0 || range < 0) { throw InvalidStatsException("oshibka: uron i dalnost ne mogut byt otricatelnymi!"); } } std::string Weapon::get_name() const { return name; } void Weapon::hit(BaseCharacter& actor, BaseCharacter& target) { if (!actor.is_alive()) { throw ActionByDeadException(); } if (!target.is_alive()) { std::cout << "[bozhit] cel uzhe mertva!" << std::endl; return; } // расчет дистанции между персонажами на карте double dist = std::sqrt(std::pow(actor.get_x() - target.get_x(), 2) + std::pow(actor.get_y() - target.get_y(), 2)); if (dist > range) { std::cout << "[propah] cel slishkom daleko dlya " << name << " (dist: " << dist << ", range: " << range << ")" << std::endl; } else { std::cout << "[udar] " << name << " nanosit " << damage << " urona!" << std::endl; target.get_damage(damage); } } std::ostream& operator<<(std::ostream& os, const Weapon& w) { os << w.name << " (uron: " << w.damage << ", dist: " << w.range << ")"; return os; } std::istream& operator>>(std::istream& is, Weapon& w) { is >> w.name >> w.damage >> w.range; return is; } // методы базового персонажа BaseCharacter::BaseCharacter(double x, double y, int health) : pos_x(x), pos_y(y), hp(health) { if (hp < 0) { throw InvalidStatsException("oshibka: zdorovie ne mozhet byt otricatelnym!"); } } void BaseCharacter::move(double delta_x, double delta_y) { if (!is_alive()) throw ActionByDeadException(); pos_x += delta_x; pos_y += delta_y; std::cout << "[dvizhenie] peremestilsya na (" << pos_x << ", " << pos_y << ")" << std::endl; } bool BaseCharacter::is_alive() const { return hp > 0; } void BaseCharacter::get_damage(int amount) { if (amount < 0) return; hp -= amount; if (hp < 0) hp = 0; std::cout << "[uron] polucheno " << amount << " urona, tekushchee zdorovie: " << hp << std::endl; } double BaseCharacter::get_x() const { return pos_x; } double BaseCharacter::get_y() const { return pos_y; } int BaseCharacter::get_hp() const { return hp; } // методы базового врага BaseEnemy::BaseEnemy(double x, double y, Weapon w, int health) : BaseCharacter(x, y, health), weapon(w) {} void BaseEnemy::hit(BaseCharacter& target) { weapon.hit(*this, target); } std::ostream& operator<<(std::ostream& os, const BaseEnemy& e) { os << "vrag (hp: " << e.get_hp() << ", poz: " << e.get_x() << "," << e.get_y() << ")"; return os; } // методы главного героя MainHero::MainHero(double x, double y, std::string n, int health) : BaseCharacter(x, y, health), name(n), current_weapon_idx(-1) {} void MainHero::hit(BaseCharacter& target) { if (current_weapon_idx == -1 || inventory.empty()) { std::cout << "Ya bezoruzhen" << std::endl; return; } inventory[current_weapon_idx].hit(*this, target); } void MainHero::add_weapon(const Weapon& w) { inventory.push_back(w); std::cout << "Podobral <" << w.get_name() << ">" << std::endl; if (inventory.size() == 1) { current_weapon_idx = 0; } } void MainHero::next_weapon() { if (inventory.empty()) { std::cout << "Ya bezoruzhen" << std::endl; return; } if (inventory.size() == 1) { std::cout << "U menya tolko odno oruzhie" << std::endl; return; } current_weapon_idx = (current_weapon_idx + 1) % inventory.size(); std::cout << "Smenil oruzhie na <" << inventory[current_weapon_idx].get_name() << ">" << std::endl; } void MainHero::heal(int amount) { if (!is_alive()) throw ActionByDeadException(); if (amount < 0) return; hp += amount; if (hp > 200) hp = 200; // ограничение максимального здоровья по заданию std::cout << "Polechilsya, teper zdorovya <" << hp << ">" << std::endl; }