Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <string>
- #include <vector>
- #include <map>
- #include <memory>
- #include <algorithm>
- #include <math.h>
- #include <conio.h>
- #include <random>
- // math helper functions
- static int repeat(int value, int length)
- {
- return value % length;
- }
- // console helper functions
- static void sleep(int milliseconds)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
- }
- static void cls()
- {
- #ifdef WIN32
- std::system("cls");
- #else
- std::system("clear");
- #endif
- }
- static char getSingleChar()
- {
- std::string input;
- std::getline(std::cin, input);
- if(input.size() > 0)
- return input[input.size() - 1];
- else
- return ' ';
- }
- // RPG System
- template<typename T> struct Modifier
- {
- std::string name;
- std::function<void(T& value)> action;
- Modifier(std::string mName, std::function<void(T& value)> mAction) :
- name{mName},
- action{mAction}
- {
- }
- virtual void performAction(T& value) const
- {
- if(action)
- action(value);
- }
- };
- // Number of stat poins given at the start or when gaining a level
- static constexpr int newLevelStatPoints{50};
- static const std::string statNames[]
- { "Power", "Luck", "Intellect", "Charisma", "Melee weaponry", "Ranged weaponry" };
- struct Character
- {
- std::string name;
- // Stats
- int statPoints{newLevelStatPoints};
- std::map<std::string, int> stats;
- int xp{0};
- int money{0};
- float health{0};
- std::vector<Modifier<float>*> maxHealthModifiers;
- Character(std::string mName) :
- name{mName}
- {
- health = getMaxHealth();
- // init stats map
- for(int i = 0; i < 6; i++)
- {
- stats[statNames[i]] = 0;
- }
- }
- int getLevel() const noexcept
- {
- static constexpr float levelGainingSpeed{0.03f};
- return 1 + static_cast<int>(trunc(sqrtf(static_cast<float>(xp)) * levelGainingSpeed));
- }
- float getMaxHealth() const noexcept
- {
- static constexpr float startMaxHealth{100.0f};
- static constexpr float maxHealthGainingSpeed{50.0f};
- float maxHealth{startMaxHealth + 1.0f / getLevel() * maxHealthGainingSpeed};
- maxHealthModifiers.size();
- maxHealthModifiers.begin();
- for(auto& mod : maxHealthModifiers)
- mod->performAction(maxHealth);
- return maxHealth;
- }
- };
- // World
- enum CellType
- {
- BasicCell, HardenedCell, Town, Boss
- };
- struct Cell
- {
- CellType type;
- Cell() : Cell(BasicCell)
- {
- }
- Cell(CellType mType) : type{mType}
- {
- }
- };
- static const std::vector<std::string> worldNames1
- {
- "Horrifying"
- };
- static const std::vector<std::string> worldNames2
- {
- "Evil"
- };
- static const std::vector<std::string> worldNames3
- {
- "Sea"
- };
- struct World
- {
- // Size of the global map
- static constexpr int globalMapSize{100};
- std::string name;
- Cell map[globalMapSize][globalMapSize];
- std::mt19937 rnd;
- World()
- {
- for(int i{0}; i < globalMapSize; i++)
- for(int j{0}; j < globalMapSize; j++)
- map[i][j] = Cell{BasicCell};
- }
- static World generateNew()
- {
- return World{};
- }
- };
- // Player-related stuff
- static void allocatePlayerStats(std::string windowTitle, Character& player)
- {
- int selectedStat{0};
- for(;;)
- {
- cls();
- std::cout << windowTitle;
- std::cout << "Allocate your character's stats. Type w or s to select a skill, type q and e or allocate points, type y to continue.\n\n";
- std::cout << "You have " << player.statPoints << " spare points left.\n";
- for(int i{0}; i < player.stats.size(); i++)
- {
- std::cout << (selectedStat == i ? ">" : " ") << statNames[i] << ": " << player.stats[statNames[i]] << "\n";
- }
- char input{getSingleChar()};
- if(input == 'y')
- return;
- switch(input)
- {
- case 'w':
- selectedStat--;
- break;
- case 's':
- selectedStat++;
- break;
- case 'q':
- if(player.stats[statNames[selectedStat]] > 0)
- {
- player.stats[statNames[selectedStat]]--;
- player.statPoints++;
- }
- break;
- case 'e':
- if(player.statPoints > 0)
- {
- player.stats[statNames[selectedStat]]++;
- player.statPoints--;
- }
- break;
- default:
- break;
- }
- selectedStat = std::max(0, repeat(selectedStat, 6));
- }
- }
- static const std::string playerBackgrounds[]
- {
- "You are a dick.", "You are a piece of shit.", "u suck."
- };
- static void applyBackgroundEffects(Character& player, int bg)
- {
- // TODO:
- }
- static Character createPlayer()
- {
- for(;;)
- {
- Character player{""};
- while(player.name == "")
- {
- cls();
- std::cout << "Character Generation (step 1 of 3)\n----------------------------------\n\n";
- std::cout << "Enter your character's name:\n";
- std::getline(std::cin, player.name);
- }
- allocatePlayerStats("Character Generation (step 2 of 3)\n----------------------------------\n", player);
- // Background selection
- int selectedBackground{0};
- for(;;)
- {
- cls();
- std::cout << "Character Generation (step 3 of 3)\n----------------------------------\n\n";
- std::cout << "Select your character's background. Type w or s to select an item, type y to continue.\n\n";
- int cnt{0};
- for(auto& bg : playerBackgrounds)
- {
- if(selectedBackground == cnt)
- std::cout << " >" << bg << "\n";
- else
- std::cout << " " << bg << "\n";
- cnt++;
- }
- char input{getSingleChar()};
- if(input == 'y')
- break;
- switch(input)
- {
- case 'w':
- selectedBackground--;
- break;
- case 's':
- selectedBackground++;
- break;
- default:
- break;
- }
- selectedBackground = std::max(0, repeat(selectedBackground, 3));
- }
- applyBackgroundEffects(player, selectedBackground);
- // Confirmation
- cls();
- std::cout << "Character Generation (FINAL)\n----------------------------------\n";
- std::cout << "So, your character's params are:\n";
- std::cout << " Name: " << player.name << "\n";
- std::cout << " Stats:\n";
- for(int i{0}; i < player.stats.size(); i++)
- {
- std::cout << " " << statNames[i] << ": " << player.stats[statNames[i]] << "\n";
- }
- std::cout << " Background:\n";
- std::cout << " " << playerBackgrounds[selectedBackground] << "\n";
- std::cout << "\n";
- std::cout << "\nType y to confirm, type n to start over.\n";
- switch(getSingleChar())
- {
- case 'y':
- return player;
- case 'n':
- continue;
- }
- }
- }
- static void doGame(World& world, Character& player)
- {
- static constexpr int viewWidth{30};
- static constexpr int viewHeight{10};
- cls();
- std::cout << "---" << world.name << "---\n";
- for(int i{0}; i < viewWidth; i++) std::cout << "-";
- std::cout << "\n";
- std::cin.get();
- }
- int main()
- {
- std::cout << "Welcome to the ultimate journey of pirates' destiny!\n\n\n\n\n\n";
- std::cout << "\nPress any key to start...";
- std::cin.get();
- cls();
- for(int i = 0; i < 3; i++)
- {
- std::cout << ".";
- sleep(100);
- }
- Character player{createPlayer()};
- World world{World::generateNew()};
- for(;;)
- {
- doGame(world, player);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement