#define OLC_PGE_APPLICATION #include "pixelGameEngine.h" #if defined(__EMSCRIPTEN__) #include #define FILE_RESOLVE(url, file) emscripten_wget(url, file); emscripten_sleep(0) #else #define FILE_RESOLVE(url, file) #endif using namespace olc; enum state{ PLAYER_CLICK, PLAYER_DRAG, BOARD_MOVE, RED_WON, BLUE_WON, }; state GameState; vi2d startingpos; const float PI=3.14159; float slidedir; float lastUpdate=0; const float TIME_PER_UPDATE=0.01f; float acc=0; vf2d boardpos; // Override base class with your custom functionality class SlideAndConquer : public olc::PixelGameEngine { public: SlideAndConquer() { // Name your application sAppName = "Slide and Conquer"; } public: olc::Renderable board; olc::Renderable originalBoard; void ResetGame() { GameState=PLAYER_CLICK; for (int x=0;x<256;x++) { for (int y=0;y<256;y++) { board.Sprite()->SetPixel(x,y,originalBoard.Sprite()->GetData()[y*256+x]); } } board.Decal()->Update(); DrawSprite({0,0},board.Sprite()); } bool OnUserCreate() override { board.Load("baseboard.png"); originalBoard.Load("baseboard.png"); GameState=PLAYER_CLICK; DrawSprite({0,0},board.Sprite()); return true; } bool OnUserUpdate(float fElapsedTime) override { switch (GameState) { case PLAYER_CLICK:{ DrawStringDecal({1,1},"CLICK ON THE PLAYFIELD",BLACK,{1,2}); if (GetMouse(0).bPressed) { GameState=PLAYER_DRAG; startingpos=GetMousePos(); } }break; case PLAYER_DRAG:{ float addedAngle=round((atan2f(GetMousePos().y-startingpos.y,GetMousePos().x-startingpos.x))/(PI/8))*(PI/8); float dist=(GetMousePos()-startingpos).mag(); DrawDecal({sin(PI/2-addedAngle)*dist/30,cos(PI/2-addedAngle)*dist/30},board.Decal()); DrawPolygonDecal(nullptr, { {startingpos.x+sin(PI-addedAngle)*(dist/10),startingpos.y+cos(PI-addedAngle)*(dist/10)}, {startingpos.x+sin(-addedAngle)*(dist/10),startingpos.y+cos(-addedAngle)*(dist/10)}, {startingpos.x+sin(PI/2-addedAngle)*dist,startingpos.y+cos(PI/2-addedAngle)*dist} }, { {0,0}, {0,0}, {0,0}, },BLACK); DrawStringDecal({1,1},"CHOOSE A SLIDE DIRECTION",BLACK,{1,2}); if (GetMouse(0).bPressed) { slidedir=PI/2-addedAngle; GameState=BOARD_MOVE; boardpos={0,0}; } }break; case BOARD_MOVE:{ DrawStringDecal({1,1},"WAITING...",BLACK,{1,2}); if ((acc+=fElapsedTime)>TIME_PER_UPDATE) { acc=0; boardpos.x+=sin(slidedir); boardpos.y+=cos(slidedir); bool AllRed=true; bool AllBlue=true; if (boardpos.x>256||boardpos.x+256<0||boardpos.y>256||boardpos.y+256<0) { GameState=PLAYER_CLICK; SetDrawTarget(nullptr); for (int x=0;x<256;x++) { for (int y=0;y<256;y++) { Pixel p = GetDrawTarget()->GetData()[y*256+x]; board.Sprite()->SetPixel(x,y,p); if (p.rUpdate(); if (AllRed) { GameState=RED_WON; } if (AllBlue) { GameState=BLUE_WON; } break; } else { DrawSprite(boardpos,board.Sprite()); } } }break; case RED_WON:{ DrawStringDecal({1,1},"RED WINS! CLICK TO PLAY AGAIN",DARK_BLUE,{1,2}); if (GetMouse(0).bPressed) { ResetGame(); } }break; case BLUE_WON:{ DrawStringDecal({1,1},"BLUE WINS! CLICK TO PLAY AGAIN",DARK_RED,{1,2}); if (GetMouse(0).bPressed) { ResetGame(); } }break; } return true; } }; int main() { SlideAndConquer demo; if (demo.Construct(256, 256, 4, 4)) demo.Start(); return 0; }