Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define OLC_PGE_APPLICATION
- #include "includes.h"
- struct Particles
- {
- float partX = 0;
- float partY = 0;
- olc::Pixel partCol;
- };
- struct Clouds
- {
- float cX = 0;
- float cY = 0;
- bool doesDamage = true;
- };
- class Engine : public olc::PixelGameEngine
- {
- public:
- Engine()
- {
- sAppName = "Fatal Floor";
- }
- enum state
- {
- MainMenu,
- Game,
- Pause,
- LevelComplete,
- GameOver
- };
- int gState = MainMenu;
- olc::Sprite* manspr;
- olc::Sprite* skyspr;
- olc::Sprite* cloudspr;
- std::vector<Clouds> clouds;
- std::vector<Particles> particles;
- int width = 1200;
- int height = width * 9 / 16;
- float px = 0;
- float py = 0;
- bool endfin = false;
- int cloudtimer;
- bool cloudspawn;
- int defAltitude = 200;
- int altitude = defAltitude;
- float fFrameTime = 0;
- float timeSpace = 0;
- int nFrame = 0;
- const int nMaxFrames = 6;
- const int nMaxFrames2 = 4;
- float posX = 480;
- float posY = 60;
- int endloopcount = 0;
- int Hearts = 3;
- int level = 1;
- bool Complete = false;
- 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)
- {
- defAltitude += 100;
- altitude = defAltitude;
- fFrameTime = 0;
- timeSpace = 0;
- nFrame = 0;
- posX = 480;
- posY = 60;
- endloopcount = 0;
- Hearts = 3;
- px = 0;
- py = 0;
- posX = 480;
- posY = 60;
- level++;
- Addcloud(rand() % width, height + 35);
- gState = Game;
- }
- //Collision detection
- void detectCol(float posX, float posY, int& Hearts, std::vector<Clouds>& clouds)
- {
- for (int i = 0; i < clouds.size(); i++)
- {
- if (clouds[i].doesDamage == true)
- {
- 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)
- {
- std::cout << "Collision Detected!" << std::endl;
- clouds[i].doesDamage = false;
- Hearts--;
- std::cout << clouds[i].doesDamage << std::endl;
- }
- }
- }
- }
- //Cloud Functions
- void Addcloud(float x, float y)
- {
- Clouds c;
- c.cX = x;
- c.cY = y;
- clouds.emplace_back(c);
- }
- void Movecloud(std::vector<Clouds>& cloud, bool& cloudspawn, int& cloudtimer)
- {
- for (int i = 0; i < cloud.size(); i++)
- {
- if (cloud[i].cY < width && cloud[i].cX > 0)
- {
- cloud[i].cX = rand() % width - 128;
- cloud[i].cY = height + rand()% 40;
- cloudspawn = false;
- cloud[i].doesDamage = true;
- cloudtimer = 0;
- }
- else
- {
- cloud[i].cY -= 2.5f;
- }
- }
- }
- void Drawcloud(std::vector<Clouds>& cloud, olc::Sprite* cloudspr)
- {
- for (auto& c : cloud)
- {
- DrawSprite(c.cX, c.cY, cloudspr);
- }
- }
- //Particle Functions
- void Addpart(float x, float y)
- {
- Particles p;
- p.partX = x;
- p.partY = y;
- p.partCol = olc::Pixel(rand() % 240, rand() % 240, rand() % 240);
- particles.emplace_back(p);
- }
- void Movepart(std::vector<Particles>& particles, int height)
- {
- for (int i = 0; i < particles.size(); i++)
- {
- if (particles[i].partY < 0)
- {
- particles[i].partY = rand() % height;
- particles[i].partX = rand() % width - 60;
- }
- else
- {
- particles[i].partY -= 2.f;
- }
- }
- }
- void Drawpart(std::vector<Particles>& particles)
- {
- for (auto& p : particles)
- {
- Draw(p.partX, p.partY, p.partCol);
- }
- }
- //Original by zleapingbear & modified for my usage by Gorbit99
- void FillScrollableSprite(olc::Sprite *spr, olc::Sprite *target, int yOffset, int& altitude, int defAltitude)
- {
- yOffset = (int)(skyspr->height - height) / (float)defAltitude * (defAltitude - altitude);
- memcpy(target->GetData(), spr->GetData() + yOffset * target->width * sizeof(uint32_t), target->height * target->width * sizeof(uint32_t));
- }
- //Frame loops
- void endLoop(float& fFrameTime, float& fElapsedTime, int& nFrame, int nMaxFrames2, olc::Sprite& manspr, int& endloopcount)
- {
- fFrameTime += fElapsedTime;
- if (fFrameTime >= 1.2f)
- {
- fFrameTime -= 1.2f;
- nFrame++; nFrame %= nMaxFrames2;
- }
- if (nFrame == 0 && endfin == false)
- {
- DrawPartialSprite(posX, posY, &manspr, 64, 128, 64, 64, 2U);
- }
- else if (nFrame == 1 && endfin == false)
- {
- DrawPartialSprite(posX, posY, &manspr, 128, 128, 64, 64, 2U);
- }
- else if (nFrame == 2 && endfin == false)
- {
- DrawPartialSprite(posX, posY, &manspr, 0, 192, 64, 64, 2U);
- }
- else if (nFrame == 3 && endfin == false)
- {
- DrawPartialSprite(posX, posY, &manspr, 64, 192, 64, 64, 2U);
- }
- else if (nFrame == 4 && endfin == false)
- {
- DrawPartialSprite(posX, posY, &manspr, 128, 192, 64, 64, 2U);
- if (endloopcount == 1)
- {
- endfin = true;
- }
- else
- {
- endloopcount++;
- }
- }
- }
- void frameloop(float& fFrameTime, float& fElapsedTime, int& nFrame, int nMaxFrames, olc::Sprite& manspr)
- {
- fFrameTime += fElapsedTime;
- if (fFrameTime >= 0.055f)
- {
- fFrameTime -= 0.055f;
- nFrame++; nFrame %= nMaxFrames;
- }
- if (nFrame == 0)
- {
- DrawPartialSprite(posX, posY, &manspr, 0, 0, 64, 64, 2U);
- }
- else if (nFrame == 1)
- {
- DrawPartialSprite(posX, posY, &manspr, 0, 64, 64, 64, 2U);
- }
- else if (nFrame == 2)
- {
- DrawPartialSprite(posX, posY, &manspr, 0, 64, 64, 64, 2U);
- }
- else if (nFrame == 3)
- {
- DrawPartialSprite(posX, posY, &manspr, 64, 64, 64, 64, 2U);
- }
- else if (nFrame == 4)
- {
- DrawPartialSprite(posX, posY, &manspr, 128, 0, 64, 64, 2U);
- }
- else if (nFrame == 5)
- {
- DrawPartialSprite(posX, posY, &manspr, 64, 64, 64, 64, 2U);
- }
- else if (nFrame == 6)
- {
- DrawPartialSprite(posX, posY, &manspr, 0, 64, 64, 64, 2U);
- }
- }
- //Character Input
- void moveChar(float& posX, float& posY, float fElapsedTime, float& fFrameTime, int& nFrame, int nMaxFrames, olc::Sprite& manspr)
- {
- if (altitude > 0)
- {
- if (GetKey(olc::RIGHT).bHeld && posX < width - 60)
- {
- posX += 3.f;
- DrawPartialSprite(posX, posY, &manspr, 128, 64, 64, 64, 2U);
- }
- else if (GetKey(olc::LEFT).bHeld && posX > -2)
- {
- posX -= 3.f;
- DrawPartialSprite(posX, posY, &manspr, 0, 128, 64, 64, 2U);
- }
- else
- {
- frameloop(fFrameTime, fElapsedTime, nFrame, nMaxFrames, manspr);
- }
- }
- }
- void skyChange(int& altitude, float& timeSpace, float fElapsedTime, int& cloudtimer, bool& cloudspawn, float& py)
- {
- if (altitude > 0)
- {
- FillScrollableSprite(skyspr, GetDrawTarget(), py, altitude, defAltitude);
- timeSpace += fElapsedTime;
- if (timeSpace >= 0.25f)
- {
- altitude--;
- timeSpace = 0;
- cloudtimer++;
- if (cloudtimer == 3 && !cloudspawn)
- {
- cloudspawn = true;
- }
- }
- }
- else
- {
- FillScrollableSprite(skyspr, GetDrawTarget(), py, altitude, defAltitude);
- if (posY < 540)
- {
- SetPixelMode(olc::Pixel::ALPHA);
- frameloop(fFrameTime, fElapsedTime, nFrame, nMaxFrames, *manspr);
- SetPixelMode(olc::Pixel::NORMAL);
- posY += 3.0f;
- }
- else
- {
- SetPixelMode(olc::Pixel::ALPHA);
- endLoop(fFrameTime, fElapsedTime, nFrame, nMaxFrames, *manspr, endloopcount);
- SetPixelMode(olc::Pixel::NORMAL);
- }
- }
- }
- bool OnUserCreate()
- {
- srand(time(NULL));
- manspr = new olc::Sprite("fally boi.png");
- skyspr = new olc::Sprite("sky.png");
- cloudspr = new olc::Sprite("Cloud.png");
- for (int j = 0; j < 100; j++)
- {
- Addpart(rand() % width - 60, rand() % height);
- }
- Addcloud(rand() % width, height + 35);
- Addcloud(rand() % width, height + 30);
- return true;
- }
- bool OnUserUpdate(float fElapsedTime)
- {
- switch (gState)
- {
- case MainMenu:
- {
- Clear(olc::BLUE);
- DrawString(380, 50, "Fatal Floor", olc::WHITE, 5U);
- //Start Button
- if (GetMouseX() > 485 && GetMouseX() < 685 && GetMouseY() > 280 && GetMouseY() < 340 && gState != Game)
- {
- FillRect(483, 278, 205, 65, olc::VERY_DARK_GREY);
- if (GetMouse(0).bPressed)
- {
- gState = Game;
- }
- }
- FillRect(485, 280, 200, 60, olc::GREY);
- DrawString(520, 300, "Start", olc::WHITE, 3U);
- break;
- }
- case Game:
- {
- skyChange(altitude, timeSpace, fElapsedTime, cloudtimer, cloudspawn, py);
- Drawpart(particles);
- Movepart(particles, height);
- if (cloudspawn == true && altitude > 30)
- {
- SetPixelMode(olc::Pixel::ALPHA);
- Drawcloud(clouds, cloudspr);
- Movecloud(clouds, cloudspawn, cloudtimer);
- SetPixelMode(olc::Pixel::NORMAL);
- }
- SetPixelMode(olc::Pixel::ALPHA);
- moveChar(posX, posY, fElapsedTime, fFrameTime, nFrame, nMaxFrames, *manspr);
- SetPixelMode(olc::Pixel::NORMAL);
- DrawString(80, 30, "Altitude : " + std::to_string(altitude) + "m", olc::WHITE, 2U);
- DrawString(1000, 30, "Hearts : " + std::to_string(Hearts), olc::WHITE, 2U);
- detectCol(posX, posY, Hearts, clouds);
- if (endfin == true)
- {
- SetPixelMode(olc::Pixel::ALPHA);
- DrawPartialSprite(posX, posY, manspr, 128, 192, 64, 64, 2U);
- SetPixelMode(olc::Pixel::NORMAL);
- Complete = true;
- }
- if (Complete)
- {
- DrawString(360, 250, "LEVEL COMPLETE!", olc::DARK_GREEN, 4U);
- FillRect(510, 300, 100, 50, olc::GREEN);
- DrawString(530, 315, "Next", olc::WHITE, 2U);
- if(GetMouseX() > 510 && GetMouseX() < 610 && GetMouseY() > 300 && GetMouseY() < 350)
- {
- if (GetMouse(0).bPressed)
- {
- nextLvl(altitude, defAltitude, fFrameTime, timeSpace, nFrame, posX, posY, endloopcount, Hearts, px, py, level, gState);
- Complete = false;
- endfin = false;
- }
- }
- }
- break;
- }
- DrawString(350, 250, "LEVEL COMPLETED", olc::DARK_GREEN, 4U);
- FillRect(480, 350, 120, 50, olc::GREEN);
- DrawString(500, 370, "Next", olc::WHITE, 2U);
- if (GetMouseX() > 480 && GetMouseX() < 600 && GetMouseY() > 350 && GetMouseY() < 4000)
- {
- if (GetMouse(0).bPressed)
- {
- nextLvl(altitude, defAltitude, fFrameTime, timeSpace, nFrame, posX, posY, endloopcount, Hearts, px, py, level, gState);
- }
- }
- case GameOver:
- {
- DrawString(450, 350, "GAME OVER", olc::RED, 4U);
- break;
- }
- }
- return true;
- }
- };
- int main()
- {
- Engine engine;
- if (engine.Construct(engine.width, engine.height, 1, 1))
- {
- engine.Start();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement