Advertisement
Danicron

Latest - FallyDownGame

Jul 19th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.41 KB | None | 0 0
  1. #define OLC_PGE_APPLICATION
  2. #include "includes.h"
  3.  
  4. struct Particles
  5. {
  6.     float partX = 0;
  7.     float partY = 0;
  8.     olc::Pixel partCol;
  9.  
  10. };
  11.  
  12. struct Clouds
  13. {
  14.     float cX = 0;
  15.     float cY = 0;
  16.     bool doesDamage = true;
  17. };
  18.  
  19. class Engine : public olc::PixelGameEngine
  20. {
  21. public:
  22.     Engine()
  23.     {
  24.         sAppName = "Fatal Floor";
  25.     }
  26.  
  27.     enum state
  28.     {
  29.         MainMenu,
  30.         Game,
  31.         Pause,
  32.         LevelComplete,
  33.         GameOver
  34.     };
  35.  
  36.     int gState = MainMenu;
  37.  
  38.  
  39.     olc::Sprite* manspr;
  40.     olc::Sprite* skyspr;
  41.     olc::Sprite* cloudspr;
  42.  
  43.     std::vector<Clouds> clouds;
  44.     std::vector<Particles> particles;
  45.  
  46.     int width = 1200;
  47.     int height = width * 9 / 16;
  48.  
  49.     float px = 0;
  50.     float py = 0;
  51.  
  52.     bool endfin = false;
  53.  
  54.     int cloudtimer;
  55.     bool cloudspawn;
  56.  
  57.     int defAltitude = 200;
  58.     int altitude = defAltitude;
  59.  
  60.     float fFrameTime = 0;
  61.     float timeSpace = 0;
  62.     int nFrame = 0;
  63.     const int nMaxFrames = 6;
  64.     const int nMaxFrames2 = 4;
  65.     float posX = 480;
  66.     float posY = 60;
  67.  
  68.     int endloopcount = 0;
  69.  
  70.     int Hearts = 3;
  71.  
  72.     int level = 1;
  73.  
  74.     bool Complete = false;
  75.  
  76.    
  77.    
  78.  
  79.     void nextLvl(int& altitude, int defAltitude, float& fFrameTime, float& timeSpace, int& nFrame, float& posX, float& posY, int& endloopcount, int& Hearts, float& px, float& py, int& level, int& gState)
  80.     {
  81.         defAltitude += 100;
  82.         altitude = defAltitude;
  83.  
  84.         fFrameTime = 0;
  85.         timeSpace = 0;
  86.         nFrame = 0;
  87.         posX = 480;
  88.         posY = 60;
  89.  
  90.         endloopcount = 0;
  91.  
  92.         Hearts = 3;
  93.  
  94.         px = 0;
  95.         py = 0;
  96.  
  97.         posX = 480;
  98.         posY = 60;
  99.  
  100.         level++;
  101.        
  102.         Addcloud(rand() % width, height + 35);
  103.  
  104.         gState = Game;
  105.     }
  106.  
  107.     //Collision detection
  108.     void detectCol(float posX, float posY, int& Hearts, std::vector<Clouds>& clouds)
  109.     {
  110.         for (int i = 0; i < clouds.size(); i++)
  111.         {
  112.             if (clouds[i].doesDamage == true)
  113.             {
  114.                 if (posX < clouds[i].cX + cloudspr->width && posX + manspr->width / 3 > clouds[i].cX && posY < clouds[i].cY + cloudspr->height && posY + manspr->height / 4 > clouds[i].cY)
  115.                 {
  116.                     std::cout << "Collision Detected!" << std::endl;
  117.                     clouds[i].doesDamage = false;
  118.                     Hearts--;
  119.                     std::cout << clouds[i].doesDamage << std::endl;
  120.                 }
  121.             }
  122.         }
  123.     }
  124.  
  125.     //Cloud Functions
  126.     void Addcloud(float x, float y)
  127.     {
  128.         Clouds c;
  129.         c.cX = x;
  130.         c.cY = y;
  131.         clouds.emplace_back(c);
  132.     }
  133.     void Movecloud(std::vector<Clouds>& cloud, bool& cloudspawn, int& cloudtimer)
  134.     {
  135.         for (int i = 0; i < cloud.size(); i++)
  136.         {
  137.             if (cloud[i].cY < width && cloud[i].cX > 0)
  138.             {
  139.                 cloud[i].cX = rand() % width - 128;
  140.                 cloud[i].cY = height + rand()% 40;
  141.                 cloudspawn = false;
  142.                 cloud[i].doesDamage = true;
  143.                 cloudtimer = 0;
  144.             }
  145.             else
  146.             {
  147.                 cloud[i].cY -= 2.5f;
  148.             }
  149.  
  150.  
  151.         }
  152.     }
  153.     void Drawcloud(std::vector<Clouds>& cloud, olc::Sprite* cloudspr)
  154.     {
  155.         for (auto& c : cloud)
  156.         {
  157.             DrawSprite(c.cX, c.cY, cloudspr);
  158.         }
  159.     }
  160.  
  161.  
  162.     //Particle Functions
  163.     void Addpart(float x, float y)
  164.     {
  165.         Particles p;
  166.         p.partX = x;
  167.         p.partY = y;
  168.         p.partCol = olc::Pixel(rand() % 240, rand() % 240, rand() % 240);
  169.        
  170.         particles.emplace_back(p);
  171.     }
  172.     void Movepart(std::vector<Particles>& particles, int height)
  173.     {
  174.         for (int i = 0; i < particles.size(); i++)
  175.         {
  176.             if (particles[i].partY < 0)
  177.             {
  178.                 particles[i].partY = rand() % height;
  179.                 particles[i].partX = rand() % width - 60;
  180.             }
  181.             else
  182.             {
  183.                 particles[i].partY -= 2.f;
  184.             }
  185.        
  186.            
  187.         }
  188.     }
  189.     void Drawpart(std::vector<Particles>& particles)
  190.     {
  191.         for (auto& p : particles)
  192.         {
  193.             Draw(p.partX, p.partY, p.partCol);
  194.         }
  195.     }
  196.  
  197.  
  198.     //Original by zleapingbear & modified for my usage by Gorbit99
  199.     void FillScrollableSprite(olc::Sprite *spr, olc::Sprite *target, int yOffset, int& altitude, int defAltitude)
  200.     {
  201.         yOffset = (int)(skyspr->height - height) / (float)defAltitude * (defAltitude - altitude);
  202.         memcpy(target->GetData(), spr->GetData() + yOffset * target->width * sizeof(uint32_t), target->height * target->width * sizeof(uint32_t));
  203.     }
  204.  
  205.  
  206.     //Frame loops
  207.     void endLoop(float& fFrameTime, float& fElapsedTime, int& nFrame, int nMaxFrames2, olc::Sprite& manspr, int& endloopcount)
  208.     {
  209.         fFrameTime += fElapsedTime;
  210.         if (fFrameTime >= 1.2f)
  211.         {
  212.             fFrameTime -= 1.2f;
  213.             nFrame++; nFrame %= nMaxFrames2;
  214.         }
  215.  
  216.         if (nFrame == 0 && endfin == false)
  217.         {
  218.             DrawPartialSprite(posX, posY, &manspr, 64, 128, 64, 64, 2U);
  219.         }
  220.         else if (nFrame == 1 && endfin == false)
  221.         {
  222.             DrawPartialSprite(posX, posY, &manspr, 128, 128, 64, 64, 2U);
  223.         }
  224.         else if (nFrame == 2 && endfin == false)
  225.         {
  226.             DrawPartialSprite(posX, posY, &manspr, 0, 192, 64, 64, 2U);
  227.         }
  228.         else if (nFrame == 3 && endfin == false)
  229.         {
  230.             DrawPartialSprite(posX, posY, &manspr, 64, 192, 64, 64, 2U);
  231.         }
  232.         else if (nFrame == 4 && endfin == false)
  233.         {
  234.             DrawPartialSprite(posX, posY, &manspr, 128, 192, 64, 64, 2U);
  235.             if (endloopcount == 1)
  236.             {
  237.                 endfin = true;
  238.             }
  239.             else
  240.             {
  241.                 endloopcount++;
  242.             }
  243.            
  244.         }
  245.     }
  246.     void frameloop(float& fFrameTime, float& fElapsedTime, int& nFrame, int nMaxFrames, olc::Sprite& manspr)
  247.     {
  248.         fFrameTime += fElapsedTime;
  249.         if (fFrameTime >= 0.055f)
  250.         {
  251.             fFrameTime -= 0.055f;
  252.             nFrame++; nFrame %= nMaxFrames;
  253.         }
  254.  
  255.         if (nFrame == 0)
  256.         {
  257.             DrawPartialSprite(posX, posY, &manspr, 0, 0, 64, 64, 2U);
  258.         }
  259.         else if (nFrame == 1)
  260.         {
  261.             DrawPartialSprite(posX, posY, &manspr, 0, 64, 64, 64, 2U);
  262.         }
  263.         else if (nFrame == 2)
  264.         {
  265.             DrawPartialSprite(posX, posY, &manspr, 0, 64, 64, 64, 2U);
  266.         }
  267.         else if (nFrame == 3)
  268.         {
  269.             DrawPartialSprite(posX, posY, &manspr, 64, 64, 64, 64, 2U);
  270.         }
  271.         else if (nFrame == 4)
  272.         {
  273.             DrawPartialSprite(posX, posY, &manspr, 128, 0, 64, 64, 2U);
  274.         }
  275.         else if (nFrame == 5)
  276.         {
  277.             DrawPartialSprite(posX, posY, &manspr, 64, 64, 64, 64, 2U);
  278.         }
  279.         else if (nFrame == 6)
  280.         {
  281.             DrawPartialSprite(posX, posY, &manspr, 0, 64, 64, 64, 2U);
  282.         }
  283.        
  284.     }
  285.  
  286.     //Character Input
  287.     void moveChar(float& posX, float& posY, float fElapsedTime, float& fFrameTime, int& nFrame, int nMaxFrames, olc::Sprite& manspr)
  288.     {
  289.         if (altitude > 0)
  290.         {
  291.             if (GetKey(olc::RIGHT).bHeld && posX < width - 60)
  292.             {
  293.                 posX += 3.f;
  294.                 DrawPartialSprite(posX, posY, &manspr, 128, 64, 64, 64, 2U);
  295.             }
  296.             else if (GetKey(olc::LEFT).bHeld && posX > -2)
  297.             {
  298.                 posX -= 3.f;
  299.                 DrawPartialSprite(posX, posY, &manspr, 0, 128, 64, 64, 2U);
  300.             }
  301.             else
  302.             {
  303.                 frameloop(fFrameTime, fElapsedTime, nFrame, nMaxFrames, manspr);
  304.             }
  305.         }
  306.        
  307.     }
  308.  
  309.     void skyChange(int& altitude, float& timeSpace, float fElapsedTime, int& cloudtimer, bool& cloudspawn, float& py)
  310.     {
  311.        
  312.         if (altitude > 0)
  313.         {
  314.             FillScrollableSprite(skyspr, GetDrawTarget(), py, altitude, defAltitude);
  315.             timeSpace += fElapsedTime;
  316.             if (timeSpace >= 0.25f)
  317.             {
  318.                
  319.                 altitude--;
  320.                 timeSpace = 0;
  321.                 cloudtimer++;
  322.                 if (cloudtimer == 3 && !cloudspawn)
  323.                 {
  324.                     cloudspawn = true;
  325.                 }
  326.             }
  327.            
  328.         }
  329.         else
  330.         {
  331.             FillScrollableSprite(skyspr, GetDrawTarget(), py, altitude, defAltitude);
  332.             if (posY < 540)
  333.             {
  334.                 SetPixelMode(olc::Pixel::ALPHA);
  335.                 frameloop(fFrameTime, fElapsedTime, nFrame, nMaxFrames, *manspr);
  336.                 SetPixelMode(olc::Pixel::NORMAL);
  337.                 posY += 3.0f;
  338.             }
  339.             else
  340.             {
  341.                 SetPixelMode(olc::Pixel::ALPHA);
  342.                 endLoop(fFrameTime, fElapsedTime, nFrame, nMaxFrames, *manspr, endloopcount);
  343.                 SetPixelMode(olc::Pixel::NORMAL);
  344.             }
  345.            
  346.         }
  347.        
  348.     }
  349.  
  350.    
  351.    
  352.  
  353.     bool OnUserCreate()
  354.     {
  355.         srand(time(NULL));
  356.         manspr = new olc::Sprite("fally boi.png");
  357.         skyspr = new olc::Sprite("sky.png");
  358.         cloudspr = new olc::Sprite("Cloud.png");
  359.  
  360.         for (int j = 0; j < 100; j++)
  361.         {
  362.             Addpart(rand() % width - 60, rand() % height);
  363.         }
  364.         Addcloud(rand() % width, height + 35);
  365.         Addcloud(rand() % width, height + 30);
  366.  
  367.        
  368.        
  369.         return true;
  370.     }
  371.     bool OnUserUpdate(float fElapsedTime)
  372.     {
  373.  
  374.         switch (gState)
  375.         {
  376.             case MainMenu:
  377.             {
  378.                 Clear(olc::BLUE);
  379.                 DrawString(380, 50, "Fatal Floor", olc::WHITE, 5U);
  380.                 //Start Button
  381.                
  382.  
  383.                 if (GetMouseX() > 485 && GetMouseX() < 685 && GetMouseY() > 280 && GetMouseY() < 340 && gState != Game)
  384.                 {
  385.                     FillRect(483, 278, 205, 65, olc::VERY_DARK_GREY);
  386.                     if (GetMouse(0).bPressed)
  387.                     {
  388.                         gState = Game;
  389.                     }
  390.                    
  391.                 }
  392.                 FillRect(485, 280, 200, 60, olc::GREY);
  393.                 DrawString(520, 300, "Start", olc::WHITE, 3U);
  394.  
  395.  
  396.                 break;
  397.             }
  398.             case Game:
  399.             {
  400.                
  401.                
  402.                 skyChange(altitude, timeSpace, fElapsedTime, cloudtimer, cloudspawn, py);
  403.                 Drawpart(particles);
  404.                 Movepart(particles, height);
  405.                 if (cloudspawn == true && altitude > 30)
  406.                 {
  407.                     SetPixelMode(olc::Pixel::ALPHA);
  408.                     Drawcloud(clouds, cloudspr);
  409.                     Movecloud(clouds, cloudspawn, cloudtimer);
  410.                     SetPixelMode(olc::Pixel::NORMAL);
  411.                 }
  412.                 SetPixelMode(olc::Pixel::ALPHA);
  413.                 moveChar(posX, posY, fElapsedTime, fFrameTime, nFrame, nMaxFrames, *manspr);
  414.                 SetPixelMode(olc::Pixel::NORMAL);
  415.                 DrawString(80, 30, "Altitude : " + std::to_string(altitude) + "m", olc::WHITE, 2U);
  416.                 DrawString(1000, 30, "Hearts : " + std::to_string(Hearts), olc::WHITE, 2U);
  417.                 detectCol(posX, posY, Hearts, clouds);
  418.  
  419.                 if (endfin == true)
  420.                 {
  421.                     SetPixelMode(olc::Pixel::ALPHA);
  422.                     DrawPartialSprite(posX, posY, manspr, 128, 192, 64, 64, 2U);
  423.                     SetPixelMode(olc::Pixel::NORMAL);
  424.                     Complete = true;
  425.                 }
  426.  
  427.                 if (Complete)
  428.                 {
  429.                     DrawString(360, 250, "LEVEL COMPLETE!", olc::DARK_GREEN, 4U);
  430.                     FillRect(510, 300, 100, 50, olc::GREEN);
  431.                     DrawString(530, 315, "Next", olc::WHITE, 2U);
  432.                     if(GetMouseX() > 510 && GetMouseX() < 610 && GetMouseY() > 300 && GetMouseY() < 350)
  433.                     {
  434.                         if (GetMouse(0).bPressed)
  435.                         {
  436.                             nextLvl(altitude, defAltitude, fFrameTime, timeSpace, nFrame, posX, posY, endloopcount, Hearts, px, py, level, gState);
  437.                             Complete = false;
  438.                             endfin = false;
  439.                         }
  440.                     }
  441.                 }
  442.  
  443.  
  444.                
  445.  
  446.                 break;
  447.  
  448.  
  449.             }
  450.            
  451.            
  452.                 DrawString(350, 250, "LEVEL COMPLETED", olc::DARK_GREEN, 4U);
  453.                 FillRect(480, 350, 120, 50, olc::GREEN);
  454.                 DrawString(500, 370, "Next", olc::WHITE, 2U);
  455.  
  456.                 if (GetMouseX() > 480 && GetMouseX() < 600 && GetMouseY() > 350 && GetMouseY() < 4000)
  457.                 {
  458.                     if (GetMouse(0).bPressed)
  459.                     {
  460.                         nextLvl(altitude, defAltitude, fFrameTime, timeSpace, nFrame, posX, posY, endloopcount, Hearts, px, py, level, gState);
  461.                     }
  462.                    
  463.                 }
  464.                
  465.            
  466.             case GameOver:
  467.             {
  468.                 DrawString(450, 350, "GAME OVER", olc::RED, 4U);
  469.                 break;
  470.             }
  471.         }
  472.        
  473.        
  474.  
  475.         return true;
  476.     }
  477.  
  478. };
  479.  
  480. int main()
  481. {
  482.     Engine engine;
  483.     if (engine.Construct(engine.width, engine.height, 1, 1))
  484.     {
  485.         engine.Start();
  486.     }
  487.     return 0;
  488.  
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement