negtab

Classes

Aug 25th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | Source Code | 0 0
  1. #pragma once
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <cstdint>
  6.  
  7. using int8  = int8_t;
  8. using int16 = int16_t;
  9. using int32 = int32_t;
  10. using uint8 = uint8_t;
  11. using uint16 = uint16_t;
  12. using uint32 = uint32_t;
  13.  
  14. namespace RpgGame
  15. {
  16.     class Player;
  17.     class Game;
  18.  
  19.     enum class GameState { CreatePlayer, Menu, Battle, Inventory, Instruction };
  20.     enum class BattleAction { Attack, Magic, Item, Run };
  21.     enum class BattleState { SelectAction, SelectTarget, SelectSkill, SelectItem, Animation, Result };
  22.     enum class BattlePhase { PlayerTurn, EnemyTurn, EndBattle };
  23.     enum class Elemental { Fire, Water, Earth, Air, Ice, Lava, Dark, Light, None };
  24.     enum class Specialization { Archer, Magician, Warrior, Thief, None };
  25.     enum class EnemiesNames {};
  26.  
  27.     struct Point { int32 x{0}, y{0}; };
  28.     struct Location { Point coord{}; std::string name; int32 h{0}, w{0}; bool isSafe{false}; };
  29.     struct Quest { bool finished{ false }; bool isMoney{ false }; std::string name; std::vector<bool> aims; uint16 reward; };
  30.     struct Item { int id{0}; int8 cost{0}; std::string name; };
  31.     struct Skill { int id{0}; std::string name; uint32 damage{0}, manaCost{0}; uint8 cooldown{0}, aimCount{0}; bool isDamaging{true}; Elemental damageType{Elemental::None}; };
  32.     struct Spec { Specialization name{Specialization::None}; uint32 buffXp{0}; uint32 buffCurrentMp{0}; };
  33.  
  34.     class GamePerson
  35.     {
  36.     public:
  37.         [[nodiscard]] virtual uint32 getAttackPower(const Skill* skill = nullptr) const = 0;
  38.         [[nodiscard]] virtual uint32 getDefense() const = 0;
  39.         [[nodiscard]] virtual uint32 getSpeed() const = 0;
  40.         [[nodiscard]] virtual std::vector<int> getAvailableSkills() const = 0;
  41.  
  42.         void killPerson() noexcept { isAlive = false; currentHp = 0; }
  43.         void resurrectPerson(int32 hp = 1) noexcept { isAlive = true; currentHp = hp; }
  44.  
  45.         [[nodiscard]] uint32 getCurrentHp() const noexcept { return currentHp; }
  46.         void setCurrentHp(uint32 hp) noexcept { currentHp = (hp < maxHp) ? hp : maxHp; }
  47.  
  48.         [[nodiscard]] uint32 getCurrentMp() const noexcept { return currentMp; }
  49.         void setCurrentMp(uint32 mp) noexcept { currentMp = (mp < maxMp) ? mp : maxMp; }
  50.  
  51.     protected:
  52.         uint32 attack{0}, defence{0}, agility{0};
  53.         bool isAlive{false};
  54.         uint32 currentHp{0}, maxHp{0};
  55.         uint32 currentMp{0}, maxMp{0};
  56.         uint8 level{1};
  57.         std::vector<int> skills;
  58.         std::vector<Elemental> resists, vulnerability;
  59.         std::map<int, uint16> currentCooldowns;
  60.     };
  61.  
  62.     class NPS
  63.     {
  64.         std::string name;
  65.         std::vector<std::string> dialogs;
  66.     };
  67.  
  68.     class Hero : public GamePerson
  69.     {
  70.     public:
  71.         void levelUp();
  72.         Spec spec;
  73.     };
  74.  
  75.     class Enemy : public GamePerson
  76.     {
  77.     public:
  78.         explicit Enemy(EnemiesNames enemy, const Player&) : name(enemy) { resurrectPerson(); }
  79.         bool killPerson() { GamePerson::killPerson(); return true; }
  80.  
  81.     private:
  82.         EnemiesNames name{};
  83.         uint32 experience{0}, gold{0};
  84.         std::vector<uint8> items;
  85.     };
  86.  
  87.     class Game
  88.     {
  89.     public:
  90.         GameState state{GameState::CreatePlayer};
  91.         std::map<int, Item> items;
  92.         std::map<int, Skill> skills;
  93.         std::vector<Location> locations;
  94.         std::vector<Player> players;
  95.         std::vector<NPS> npss;
  96.         std::vector<Quest> quests;
  97.         std::string pathToSave;
  98.     };
  99.  
  100.     class Player
  101.     {
  102.     public:
  103.         explicit Player(const std::string& name) : name(name) {}
  104.  
  105.         [[nodiscard]] int getAverageLevel() const
  106.         {
  107.             int total = 0, count = 0;
  108.             for (const auto& hero : heroes)
  109.                 total += hero.level, ++count;
  110.             return (count > 0) ? total / count : 0;
  111.         }
  112.  
  113.         [[nodiscard]] uint32 getPlayerGold() const noexcept { return gold; }
  114.         [[nodiscard]] int32 getPlayerSpeed() const noexcept { return speed; }
  115.         void setPlayerSpeed(int32 newSpeed) noexcept { speed = newSpeed; }
  116.         [[nodiscard]] std::vector<EnemiesNames> getDiscoveredEnemies() const { return discoveredEnemies; }
  117.  
  118.         [[nodiscard]] std::vector<int> getAvailableItems() const
  119.         {
  120.             std::vector<int> available;
  121.             available.reserve(itemIds.size());
  122.             for (const auto& [id, _] : itemIds)
  123.                 available.push_back(id);
  124.             return available;
  125.         }
  126.  
  127.         [[nodiscard]] Location getPlayerPlace(const Game& game) const
  128.         {
  129.             for (const auto& loc : game.locations)
  130.                 if (coords.x > loc.coord.x && coords.x < loc.coord.x + loc.w &&
  131.                     coords.y > loc.coord.y && coords.y < loc.coord.y + loc.h)
  132.                     return loc;
  133.             return {};
  134.         }
  135.  
  136.         void addDiscoveredEnemy(EnemiesNames enemy) { discoveredEnemies.push_back(enemy); }
  137.         void addItem(int itemId, uint8 count) { itemIds[itemId] += count; }
  138.  
  139.     private:
  140.         std::string name;
  141.         uint32 gold{0};
  142.         Point coords{};
  143.         int32 speed{0};
  144.         std::vector<Hero> heroes;
  145.         std::vector<EnemiesNames> discoveredEnemies;
  146.         std::map<int, uint8> itemIds;
  147.     };
  148.  
  149.     std::vector<Enemy> createEnemies(const Player& player);
  150.     void initializeGameDatabase(std::map<int, Skill>& skills, std::map<int, Item>& items);
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment