Advertisement
Danicron

MLad Project

Aug 27th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.86 KB | None | 0 0
  1. #pragma once
  2. #include "olcPGE/olcPixelGameEngine.h"
  3. struct Tile
  4. {
  5.     int x;
  6.     int y;
  7.  
  8.     int idx;
  9.     int sx;
  10.     int sy;
  11.  
  12.  
  13. };
  14.  
  15. class Map
  16. {
  17. public:
  18.     Map();
  19.     ~Map();
  20.  
  21. public:
  22.  
  23.    
  24.  
  25.     int mWidth;
  26.     int mHeight;
  27.  
  28.     const int TileWidth = 32;
  29.     const int TileHeight = 32;
  30.  
  31.  
  32.     std::string sName;
  33.     olc::Sprite* mapsprite;
  34.  
  35.     bool Create(std::string FileData, olc::Sprite* sprite, std::string name);
  36.  
  37.     int GetIndex(int x, int y);
  38.     bool GetSolid(int x, int y);
  39.  
  40.     void AddTiles(std::vector<Tile>& tiles, Map* map);
  41.  
  42.     std::vector<Tile> tiles;
  43.  
  44. private:
  45.     int *m_indices = nullptr;
  46.     bool *m_solids = nullptr;
  47. };
  48.  
  49. class mMap1 : public Map
  50. {
  51. public:
  52.     mMap1();
  53.  
  54. };
  55.  
  56. #############################################################################################################################
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. #include "Map.h"
  64.  
  65.  
  66.  
  67. Map::Map()
  68. {
  69.     mapsprite = nullptr;
  70.     mWidth = 0;
  71.     mHeight = 0;
  72.     m_solids = nullptr;
  73.     m_indices = nullptr;
  74. }
  75.  
  76.  
  77. Map::~Map()
  78. {
  79.     delete[] m_solids;
  80.     delete[] m_indices;
  81. }
  82.  
  83. int Map::GetIndex(int x, int y)
  84. {
  85.     if (x >= 0 && x < mWidth && y >= 0 && y < mHeight)
  86.     {
  87.         return m_indices[y*mWidth + x];
  88.     }
  89.     else
  90.     {
  91.         return 0;
  92.     }
  93. }
  94.  
  95. bool Map::GetSolid(int x, int y)
  96. {
  97.     if (x >= 0 && x < mWidth && y >= 0 && y < mHeight)
  98.     {
  99.         return m_solids[y*mWidth + x];
  100.     }
  101.     else
  102.     {
  103.         return true;
  104.     }
  105. }
  106.  
  107. void Map::AddTiles(std::vector<Tile>& tiles, Map* map)
  108. {
  109.     for (int i = 0; i < mWidth; i++)
  110.     {
  111.         for (int j = 0; j < mHeight; j++)
  112.         {
  113.             Tile t;
  114.             t.idx = map->GetIndex(i, j);
  115.             t.sx = t.idx % 9;
  116.             t.sy = t.idx / 9;
  117.             t.x = i * TileWidth;
  118.             t.y = j * TileHeight;
  119.  
  120.             tiles.emplace_back(t);
  121.  
  122.         }
  123.     }
  124. }
  125.  
  126. bool Map::Create(std::string FileData, olc::Sprite* sprite, std::string name)
  127. {
  128.     sName = name;
  129.     mapsprite = sprite;
  130.     std::ifstream data(FileData, std::ios::in || std::ios::binary);
  131.         if (data.is_open())
  132.         {
  133.             data >> mWidth >> mHeight;
  134.             m_solids = new bool[mWidth * mHeight];
  135.             m_indices = new int[mWidth * mHeight];
  136.             for (int i = 0; i < mWidth * mHeight; i++)
  137.             {
  138.                 data >> m_indices[i];
  139.                 data >> m_solids[i];
  140.             }
  141.  
  142.             return true;
  143.         }
  144.         return false;
  145. }
  146.  
  147. mMap1::mMap1()
  148. {
  149.     Create("MainMap.txt", new olc::Sprite("Tileset1.png"), "Main");
  150. }
  151.  
  152.  
  153. #################################################################################################################################
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. #define OLC_PGE_APPLICATION
  161.  
  162. #include "Map.h"
  163.  
  164. struct Node
  165. {
  166.     bool Visited = false;
  167.     bool bObstacle = false;
  168.  
  169.     int x;
  170.     int y;
  171.     float GlobalGoal;
  172.     float LocalGoal;
  173.  
  174.     std::vector<Node*> Neighbours;
  175.  
  176.     Node* parent;
  177.  
  178. };
  179.  
  180. struct Adventurers
  181. {
  182.     enum Gender
  183.     {
  184.         Male,
  185.         Female
  186.     };
  187.  
  188.     float px = 40;
  189.     float py = 40;
  190.  
  191.     std::string name;
  192.     int Gender = Male;
  193.     int Level = 1;
  194.     int Health = 0;
  195.     int Strength = 1;
  196.     int Endurance = 1;
  197.     int Dexterity = 1;
  198.     int Agility = 1;
  199.     int Exhaustion = 0;
  200.  
  201. };
  202.  
  203. class Engine : public olc::PixelGameEngine
  204. {
  205. public:
  206.     Engine()
  207.     {
  208.         sAppName = "MLaD";
  209.     }
  210.  
  211.     Map *m_CurrentMap = nullptr;
  212.  
  213.     int SelectedTile;
  214.  
  215.    
  216.     std::vector<std::vector<int>> Next;
  217.     std::vector<std::vector<float>> Dist;
  218.    
  219.  
  220.     olc::Sprite* adhead = nullptr;
  221.     std::string tempName;
  222.  
  223.     std::vector<Adventurers> advent;
  224.  
  225.     float Tick = 0;
  226.     float TickTime = 20.f;
  227.     int Day = 1;
  228.     int Hour = 7;
  229.  
  230.     void DrawTileMap(Map* m_CurrentMap, std::vector<Tile> tiles)
  231.     {
  232.         for (size_t i = 0; i < tiles.size(); i++)
  233.         {
  234.                
  235.             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);
  236.  
  237.         }
  238.     }
  239.     void initialiseDist(std::vector<std::vector<float>>& dist)
  240.     {
  241.         dist = std::vector<std::vector<float>>(m_CurrentMap->tiles.size());
  242.         for (int i = 0; i < dist.size(); i++)
  243.         {
  244.             dist[i] = std::vector<float>(dist.size(), INFINITY);
  245.         }
  246.     }
  247.  
  248.     void initialiseNext(std::vector<std::vector<int>>& next)
  249.     {
  250.         next = std::vector<std::vector<int>>(m_CurrentMap->tiles.size());
  251.         for (int i = 0; i < next.size(); i++)
  252.         {
  253.             next[i] = std::vector<int>(next.size(), -1);
  254.         }
  255.     }
  256.  
  257.    
  258.  
  259.     void PopulateNext(std::vector<std::vector<int>>& Next)
  260.     {
  261.         for (int x = 0; x < m_CurrentMap->mWidth; x++)
  262.         {
  263.             for (int y = 0; y < m_CurrentMap->mHeight; y++)
  264.             {
  265.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y > 0)
  266.                 {
  267.                     Next[y * m_CurrentMap->mWidth + x][(y - 1) * m_CurrentMap->mWidth + x] = (y - 1) * m_CurrentMap->mWidth + x;
  268.                 }
  269.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y < m_CurrentMap->mHeight - 1)
  270.                 {
  271.                     Next[y * m_CurrentMap->mWidth + x][(y + 1) * m_CurrentMap->mWidth + x] = (y + 1) * m_CurrentMap->mWidth + x;
  272.                 }
  273.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x > 0)
  274.                 {
  275.                     Next[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x - 1)] = y * m_CurrentMap->mWidth + (x - 1);
  276.                 }
  277.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x < m_CurrentMap->mWidth - 1)
  278.                 {
  279.                     Next[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x + 1)] = y * m_CurrentMap->mWidth + (x + 1);
  280.                 }
  281.             }
  282.         }
  283.     }
  284.    
  285.     void PopulateDist(std::vector<std::vector<float>>& Dist)
  286.     {
  287.         for (int x = 0; x < m_CurrentMap->mWidth; x++)
  288.         {
  289.             for (int y = 0; y < m_CurrentMap->mHeight; y++)
  290.             {
  291.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y > 0)
  292.                 {
  293.                     Dist[y * m_CurrentMap->mWidth + x][(y - 1) * m_CurrentMap->mWidth + x] = 1;
  294.                 }
  295.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].y < m_CurrentMap->mHeight - 1)
  296.                 {
  297.                     Dist[y * m_CurrentMap->mWidth + x][(y + 1) * m_CurrentMap->mWidth + x] = 1;
  298.                 }
  299.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x > 0)
  300.                 {
  301.                     Dist[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x - 1)] = 1;
  302.                 }
  303.                 if (m_CurrentMap->tiles[y * m_CurrentMap->mWidth + x].x < m_CurrentMap->mWidth - 1)
  304.                 {
  305.                     Dist[y * m_CurrentMap->mWidth + x][y * m_CurrentMap->mWidth + (x + 1)] = 1;
  306.                 }
  307.  
  308.             }
  309.  
  310.         }
  311.     }
  312.  
  313.     void FWAlg(std::vector<std::vector<float>>& Next, std::vector<std::vector<float>>& Dist)
  314.     {
  315.         for (size_t k = 0; k < m_CurrentMap->tiles.size(); k++)
  316.         {
  317.             for (size_t i = 0; i < Dist.size(); i++)
  318.             {
  319.                 for (size_t j = 0; j < Next.size(); j++)
  320.                 {
  321.                     if (Dist[i][j] > Dist[i][k] + Dist[k][j])
  322.                     {
  323.                         Dist[i][j] = Dist[i][k] + Dist[k][j];
  324.                         Next[i][j] = Next[i][k];
  325.                     }
  326.                 }
  327.             }
  328.         }
  329.     }
  330.  
  331.     /*void Collision()
  332.     {
  333.         for (int i = 0; i < m_CurrentMap->mWidth; i++)
  334.         {
  335.             for (int j = 0; j < m_CurrentMap->mHeight; j++)
  336.             {
  337.                 if ()
  338.             }
  339.         }
  340.        
  341.     }*/
  342.  
  343.     void EndofDay(int& Day, int& Hour)
  344.     {
  345.         Day++;
  346.         Hour = 7;
  347.     }
  348.  
  349.     void GameTime(float& Tick, float fElapsedTime, float TickTime, int& Hour)
  350.     {
  351.         Tick += fElapsedTime;
  352.         if (Tick >= TickTime)
  353.         {
  354.             Hour++;
  355.             if (Hour == 19)
  356.             {
  357.                 EndofDay(Day, Hour);
  358.             }
  359.             Tick = 0;
  360.         }
  361.     }
  362.  
  363.     void DisplayTime(int Day, int Hour)
  364.     {
  365.         DrawString(50, ScreenHeight() - 80, "Day : " + std::to_string(Day), olc::GREEN, 2U);
  366.         DrawString(50, ScreenHeight() - 40, "Time : " + std::to_string(Hour) + ":00", olc::GREEN, 2U);
  367.     }
  368.  
  369.     void DisplayNumAdvent(std::vector<Adventurers> advent)
  370.     {
  371.         DrawString(ScreenWidth() - 280, ScreenHeight() - 680, "Adventurers : " + std::to_string(advent.size()), olc::GREEN, 2U);
  372.     }
  373.  
  374.     std::string RandNameMale()
  375.     {
  376.         tempName = std::vector<std::string>{"Tom", "Dick", "Harry", "Jeff", "David", "Sebastien", "Murdoc", "Judd", "Phil", "Mike"}[rand()% 10];
  377.         return tempName;
  378.     }
  379.     std::string RandNameFemale()
  380.     {
  381.         tempName = std::vector<std::string>{"Maureen", "Delilah", "Florence", "Isabelle", "Tamatha", "Susan", "Geraldine", "Rose", "Christine", "Holly"}[rand()% 10];
  382.         return tempName;
  383.     }
  384.  
  385.     void addAdventurer(std::vector<Adventurers>& advent, std::string& tempName, Map* m_CurrentMap)
  386.     {
  387.         if (advent.size() < 20)
  388.         {
  389.             Adventurers ad;
  390.             int GenderDec;
  391.             GenderDec = rand() % 10;
  392.             if (GenderDec < 5)
  393.             {
  394.                 ad.Gender = ad.Male;
  395.                 ad.name = RandNameMale();
  396.             }
  397.             else if (GenderDec > 5)
  398.             {
  399.                 ad.Gender = ad.Female;
  400.                 ad.name = RandNameFemale();
  401.             }
  402.             ad.px = rand() % m_CurrentMap->mWidth * m_CurrentMap->TileWidth;
  403.             ad.py = rand() % m_CurrentMap->mHeight * m_CurrentMap->TileHeight;
  404.             ad.Health = rand() % 50 + 15;
  405.             ad.Strength = rand() % 10;
  406.             ad.Endurance = rand() % 10;
  407.             ad.Dexterity = rand() % 10;
  408.             ad.Endurance = rand() % 10;
  409.             ad.Exhaustion = 0;
  410.  
  411.             advent.emplace_back(ad);
  412.         }
  413.         else
  414.         {
  415.             return;
  416.         }
  417.     }
  418.  
  419.     void MoveAdventurer(std::vector<Adventurers> advent, std::vector<std::vector<int>> Next, int SelectedTile)
  420.     {
  421.         if (advent.size() > 0 && SelectedTile != NULL)
  422.         {
  423.             int destination = SelectedTile;
  424.  
  425.             Next[m_CurrentMap->GetIndex(advent[0].px, advent[0].py)][destination];
  426.         }
  427.     }
  428.  
  429.     /*void testcharvect(std::vector<Adventurers> advent)
  430.     {
  431.         for (int i = 0; i < advent.size(); i++)
  432.         {
  433.             std::cout << "Name : " << advent[i].name << " " << "Health : " << advent[i].Health << std::endl;
  434.             if (advent[i].Gender == 0)
  435.             {
  436.                 std::cout << "Gender : " << " Male " << std::endl;
  437.             }
  438.             else if (advent[i].Gender == 1)
  439.             {
  440.                 std::cout << "Gender : " << " Female " << std::endl;
  441.             }
  442.         }
  443.     }*/
  444.  
  445.     void Input(std::vector<Adventurers>& advent)
  446.     {
  447.         if (GetKey(olc::SPACE).bPressed)
  448.         {
  449.             addAdventurer(advent, tempName, m_CurrentMap);
  450.         }
  451.  
  452.         /*else if ()
  453.         {
  454.            
  455.         }
  456.         /*else if (GetKey(olc::).bPressed)
  457.         {
  458.  
  459.         }
  460.         else if (GetKey(olc::).bPressed)
  461.         {
  462.  
  463.         }
  464.         else if (GetKey(olc::).bPressed)
  465.         {
  466.  
  467.         }*/
  468.     }
  469.  
  470.     void SelectedTiles(int SelectedTile)
  471.     {
  472.         if (GetMouse(0).bReleased)
  473.         {
  474.             SelectedTile = m_CurrentMap->GetIndex(GetMouseX(), GetMouseY());
  475.  
  476.             std::cout << SelectedTile << std::endl;
  477.         }
  478.         if (GetMouse(1).bReleased)
  479.         {
  480.             SelectedTile = NULL;
  481.         }
  482.     }
  483.  
  484.     void DrawHead(olc::Sprite& adhead, std::vector<Adventurers> advent)
  485.     {
  486.         for (size_t i = 0; i < advent.size(); i++)
  487.         {
  488.             DrawPartialSprite(advent[i].px, advent[i].py, &adhead, 0, 0, 32, 30, 1U);
  489.         }
  490.     }
  491.  
  492.  
  493.  
  494.     bool OnUserCreate()
  495.     {
  496.         srand(time(NULL));
  497.         adhead = new olc::Sprite("AdventurerHead.png");
  498.         m_CurrentMap = new mMap1();
  499.         m_CurrentMap->AddTiles(m_CurrentMap->tiles, m_CurrentMap);
  500.         initialiseDist(Dist);
  501.         initialiseNext(Next);
  502.         PopulateDist(Dist);
  503.         PopulateNext(Next);
  504.        
  505.  
  506.         return true;
  507.     }
  508.  
  509.    
  510.  
  511.     bool OnUserUpdate(float fElapsedTime)
  512.     {
  513.        
  514.         Clear(olc::Pixel(154, 43, 7));
  515.         DrawTileMap(m_CurrentMap, m_CurrentMap->tiles);
  516.         SelectedTiles(SelectedTile);
  517.         GameTime(Tick, fElapsedTime, TickTime, Hour);
  518.         Input(advent);
  519.         SetPixelMode(olc::Pixel::ALPHA);
  520.         DrawHead(*adhead, advent);
  521.         MoveAdventurer(advent, Next, SelectedTile);
  522.         SetPixelMode(olc::Pixel::NORMAL);
  523.         DisplayTime(Day, Hour);
  524.         DisplayNumAdvent(advent);
  525.         return true;
  526.     }
  527. };
  528.  
  529. int main()
  530. {
  531.     const int width = 1200;
  532.     const int height = 750;
  533.     Engine engine;
  534.     engine.Construct(width, height, 1, 1, false);
  535.     engine.Start();
  536.     return 0;
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement