Advertisement
Danicron

Main

Aug 26th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.60 KB | None | 0 0
  1. #define OLC_PGE_APPLICATION
  2.  
  3. #include "Map.h"
  4.  
  5. struct Node
  6. {
  7.     bool Visited = false;
  8.     bool bObstacle = false;
  9.  
  10.     int x;
  11.     int y;
  12.     float GlobalGoal;
  13.     float LocalGoal;
  14.  
  15.     std::vector<Node*> Neighbours;
  16.  
  17.     Node* parent;
  18.  
  19. };
  20.  
  21. struct Adventurers
  22. {
  23.     enum Gender
  24.     {
  25.         Male,
  26.         Female
  27.     };
  28.  
  29.     float px = 40;
  30.     float py = 40;
  31.  
  32.     std::string name;
  33.     int Gender = Male;
  34.     int Level = 1;
  35.     int Health = 0;
  36.     int Strength = 1;
  37.     int Endurance = 1;
  38.     int Dexterity = 1;
  39.     int Agility = 1;
  40.     int Exhaustion = 0;
  41.  
  42. };
  43.  
  44. class Engine : public olc::PixelGameEngine
  45. {
  46. public:
  47.     Engine()
  48.     {
  49.         sAppName = "MLaD";
  50.     }
  51.  
  52.    
  53.    
  54.  
  55.     Map *m_CurrentMap = nullptr;
  56.  
  57.    
  58.  
  59.     olc::Sprite* adhead = nullptr;
  60.     std::string tempName;
  61.  
  62.     std::vector<Adventurers> advent;
  63.  
  64.     float Tick = 0;
  65.     float TickTime = 20.f;
  66.     int Day = 1;
  67.     int Hour = 7;
  68.  
  69.     void DrawTileMap(Map* m_CurrentMap, std::vector<Tile> tiles)
  70.     {
  71.         for (size_t i = 0; i < tiles.size(); i++)
  72.         {
  73.                
  74.             DrawPartialSprite(tiles[i].x, tiles[i].y, m_CurrentMap->mapsprite, tiles[i].sx * m_CurrentMap->TileWidth, tiles[i].sy * m_CurrentMap->TileHeight, m_CurrentMap->TileWidth, m_CurrentMap->TileHeight);
  75.  
  76.         }
  77.     }
  78.  
  79.     void PopulateNext(std::vector<std::vector<float>>& Next)
  80.     {
  81.         for (int x = 0; x < m_CurrentMap->mWidth; x++)
  82.         {
  83.             for (int y = 0; y < m_CurrentMap->mHeight; y++)
  84.             {
  85.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y > 0)
  86.                 {
  87.                     Next[y * m_CurrentMap->mWidth + x][(y - 1) * m_CurrentMap->mWidth + x] = (y - 1) * m_CurrentMap->mWidth + x;
  88.                 }
  89.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y < m_CurrentMap->mHeight - 1)
  90.                 {
  91.                     Next[y * m_CurrentMap->mWidth + x][(y + 1) * m_CurrentMap->mWidth + x] = (y + 1) * m_CurrentMap->mWidth + x;
  92.                 }
  93.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x > 0)
  94.                 {
  95.                     Next[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x - 1)] = y * m_CurrentMap->mWidth + (x - 1);
  96.                 }
  97.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x < m_CurrentMap->mWidth - 1)
  98.                 {
  99.                     Next[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x + 1)] = y * m_CurrentMap->mWidth + (x + 1);
  100.                 }
  101.             }
  102.         }
  103.     }
  104.    
  105.     void PopulateDist(std::vector<std::vector<float>>& Dist)
  106.     {
  107.         for (int x = 0; x < m_CurrentMap->mWidth; x++)
  108.         {
  109.             for (int y = 0; y < m_CurrentMap->mHeight; y++)
  110.             {
  111.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y > 0)
  112.                 {
  113.                     Dist[y * m_CurrentMap->mWidth + x][(y - 1) * m_CurrentMap->mWidth + x] = 1;
  114.                 }
  115.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y < m_CurrentMap->mHeight - 1)
  116.                 {
  117.                     Dist[y * m_CurrentMap->mWidth + x][(y + 1) * m_CurrentMap->mWidth + x] = 1;
  118.                 }
  119.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x > 0)
  120.                 {
  121.                     Dist[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x - 1)] = 1;
  122.                 }
  123.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x < m_CurrentMap->mWidth - 1)
  124.                 {
  125.                     Dist[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x + 1)] = 1;
  126.                 }
  127.  
  128.             }
  129.  
  130.         }
  131.     }
  132.  
  133.     void FWAlg(std::vector<std::vector<float>>& Next, std::vector<std::vector<float>>& Dist)
  134.     {
  135.         for (size_t k = 0; k < m_CurrentMap->tiles.size(); k++)
  136.         {
  137.             for (size_t i = 0; i < Dist.size(); i++)
  138.             {
  139.                 for (size_t j = 0; j < Next.size(); j++)
  140.                 {
  141.                     if (Dist[i][j] > Dist[i][k] + Dist[k][j])
  142.                     {
  143.                         Dist[i][j] = Dist[i][k] + Dist[k][j];
  144.                         Next[i][j] = Next[i][k];
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.     }
  150.  
  151.     /*void Collision()
  152.     {
  153.         for (int i = 0; i < m_CurrentMap->mWidth; i++)
  154.         {
  155.             for (int j = 0; j < m_CurrentMap->mHeight; j++)
  156.             {
  157.                 if ()
  158.             }
  159.         }
  160.        
  161.     }*/
  162.  
  163.     void EndofDay(int& Day, int& Hour)
  164.     {
  165.         Day++;
  166.         Hour = 7;
  167.     }
  168.  
  169.     void GameTime(float& Tick, float fElapsedTime, float TickTime, int& Hour)
  170.     {
  171.         Tick += fElapsedTime;
  172.         if (Tick >= TickTime)
  173.         {
  174.             Hour++;
  175.             if (Hour == 19)
  176.             {
  177.                 EndofDay(Day, Hour);
  178.             }
  179.             Tick = 0;
  180.         }
  181.     }
  182.  
  183.     void DisplayTime(int Day, int Hour)
  184.     {
  185.         DrawString(50, ScreenHeight() - 80, "Day : " + std::to_string(Day), olc::GREEN, 2U);
  186.         DrawString(50, ScreenHeight() - 40, "Time : " + std::to_string(Hour) + ":00", olc::GREEN, 2U);
  187.     }
  188.  
  189.     void DisplayNumAdvent(std::vector<Adventurers> advent)
  190.     {
  191.         DrawString(ScreenWidth() - 280, ScreenHeight() - 680, "Adventurers : " + std::to_string(advent.size()), olc::GREEN, 2U);
  192.     }
  193.  
  194.     std::string RandNameMale()
  195.     {
  196.         tempName = std::vector<std::string>{"Tom", "Dick", "Harry", "Jeff", "David", "Sebastien", "Murdoc", "Judd", "Phil", "Mike"}[rand()% 10];
  197.         return tempName;
  198.     }
  199.     std::string RandNameFemale()
  200.     {
  201.         tempName = std::vector<std::string>{"Maureen", "Delilah", "Florence", "Isabelle", "Tamatha", "Susan", "Geraldine", "Rose", "Christine", "Holly"}[rand()% 10];
  202.         return tempName;
  203.     }
  204.  
  205.     void addAdventurer(std::vector<Adventurers>& advent, std::string& tempName, Map* m_CurrentMap)
  206.     {
  207.         if (advent.size() < 20)
  208.         {
  209.             Adventurers ad;
  210.             int GenderDec;
  211.             GenderDec = rand() % 10;
  212.             if (GenderDec < 5)
  213.             {
  214.                 ad.Gender = ad.Male;
  215.                 ad.name = RandNameMale();
  216.             }
  217.             else if (GenderDec > 5)
  218.             {
  219.                 ad.Gender = ad.Female;
  220.                 ad.name = RandNameFemale();
  221.             }
  222.             ad.px = rand() % m_CurrentMap->mWidth * m_CurrentMap->TileWidth;
  223.             ad.py = rand() % m_CurrentMap->mHeight * m_CurrentMap->TileHeight;
  224.             ad.Health = rand() % 50 + 15;
  225.             ad.Strength = rand() % 10;
  226.             ad.Endurance = rand() % 10;
  227.             ad.Dexterity = rand() % 10;
  228.             ad.Endurance = rand() % 10;
  229.             ad.Exhaustion = 0;
  230.  
  231.             advent.emplace_back(ad);
  232.         }
  233.         else
  234.         {
  235.             return;
  236.         }
  237.     }
  238.  
  239.     void MoveAdventurer(std::vector<Adventurers> advent, std::vector<std::vector<float>> Next)
  240.     {
  241.         if (advent.size() > 0)
  242.         {
  243.             int destination = m_CurrentMap->GetIndex(m_CurrentMap->mWidth / 2, m_CurrentMap->mHeight / 2);
  244.  
  245.             Next[m_CurrentMap->GetIndex(advent[0].px, advent[0].py)][destination];
  246.         }
  247.     }
  248.  
  249.     /*void testcharvect(std::vector<Adventurers> advent)
  250.     {
  251.         for (int i = 0; i < advent.size(); i++)
  252.         {
  253.             std::cout << "Name : " << advent[i].name << " " << "Health : " << advent[i].Health << std::endl;
  254.             if (advent[i].Gender == 0)
  255.             {
  256.                 std::cout << "Gender : " << " Male " << std::endl;
  257.             }
  258.             else if (advent[i].Gender == 1)
  259.             {
  260.                 std::cout << "Gender : " << " Female " << std::endl;
  261.             }
  262.         }
  263.     }*/
  264.  
  265.     void Input(std::vector<Adventurers>& advent)
  266.     {
  267.         if (GetKey(olc::SPACE).bPressed)
  268.         {
  269.             addAdventurer(advent, tempName, m_CurrentMap);
  270.         }
  271.         /*else if ()
  272.         {
  273.            
  274.         }
  275.         /*else if (GetKey(olc::).bPressed)
  276.         {
  277.  
  278.         }
  279.         else if (GetKey(olc::).bPressed)
  280.         {
  281.  
  282.         }
  283.         else if (GetKey(olc::).bPressed)
  284.         {
  285.  
  286.         }*/
  287.     }
  288.  
  289.     void DrawHead(olc::Sprite& adhead, std::vector<Adventurers> advent)
  290.     {
  291.         for (size_t i = 0; i < advent.size(); i++)
  292.         {
  293.             DrawPartialSprite(advent[i].px, advent[i].py, &adhead, 0, 0, 32, 30, 1U);
  294.         }
  295.     }
  296.  
  297.  
  298.  
  299.     bool OnUserCreate()
  300.     {
  301.         srand(time(NULL));
  302.         adhead = new olc::Sprite("AdventurerHead.png");
  303.         m_CurrentMap = new mMap1();
  304.         m_CurrentMap->AddTiles(m_CurrentMap->tiles, m_CurrentMap);
  305.         /*PopulateDist(Dist);
  306.         PopulateNext(Next);*/
  307.        
  308.  
  309.         return true;
  310.     }
  311.  
  312.    
  313.  
  314.     bool OnUserUpdate(float fElapsedTime)
  315.     {
  316.         Clear(olc::Pixel(154, 43, 7));
  317.         DrawTileMap(m_CurrentMap, m_CurrentMap->tiles);
  318.         GameTime(Tick, fElapsedTime, TickTime, Hour);
  319.         Input(advent);
  320.         SetPixelMode(olc::Pixel::ALPHA);
  321.         DrawHead(*adhead, advent);
  322.         MoveAdventurer(advent, Next);
  323.         SetPixelMode(olc::Pixel::NORMAL);
  324.         DisplayTime(Day, Hour);
  325.         DisplayNumAdvent(advent);
  326.         return true;
  327.     }
  328. };
  329.  
  330. int main()
  331. {
  332.     const int width = 1200;
  333.     const int height = 750;
  334.     Engine engine;
  335.     engine.Construct(width, height, 1, 1, false);
  336.     engine.Start();
  337.     return 0;
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement