From 656df84cfdbc859857238fc5fdf11139eace72d6 Mon Sep 17 00:00:00 2001 From: Allan Legemaate Date: Sat, 29 Mar 2025 17:36:58 -0400 Subject: [PATCH] fix: separate draw --- src/block.cpp | 32 -- src/block.h | 29 -- src/main.cpp | 901 ++++++++++++++++++++++++++------------------------ 3 files changed, 472 insertions(+), 490 deletions(-) delete mode 100644 src/block.cpp delete mode 100644 src/block.h diff --git a/src/block.cpp b/src/block.cpp deleted file mode 100644 index bcbb9a7..0000000 --- a/src/block.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "block.h" - -void Block::SetImages(const std::string& path1, const std::string& path2) { - images[0] = asw::assets::loadTexture(path1); - images[1] = asw::assets::loadTexture(path2); -} - -int Block::GetX() const { - return x; -} - -int Block::GetY() const { - return y; -} - -void Block::SetX(int newValue) { - x = newValue; -} - -void Block::SetY(int newValue) { - y = newValue; -} - -void Block::draw() { - if (frame <= 4) { - asw::draw::sprite(images[0], asw::Vec2(x, y)); - } else { - asw::draw::sprite(images[1], asw::Vec2(x, y)); - } - - frame += (frame + 1) % 16; -} diff --git a/src/block.h b/src/block.h deleted file mode 100644 index fd4c2c0..0000000 --- a/src/block.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include -#include -#include - -class Block { - public: - Block() = default; - - void SetImages(const std::string& path1, const std::string& path2); - - int GetY() const; - int GetX() const; - - void SetX(int newValue); - void SetY(int newValue); - - void draw(); - - private: - int x{0}; - int y{0}; - - int frame{0}; - - std::array images; - asw::Sample sound; -}; diff --git a/src/main.cpp b/src/main.cpp index 25ce15a..5002150 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,7 +100,7 @@ int map[24][32] = {{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}}; // Define bitmaps -asw::Texture character[4]; +std::array character; asw::Texture background; asw::Texture robot; asw::Texture wall; @@ -109,7 +109,6 @@ asw::Texture ground; asw::Texture box; asw::Texture menu; asw::Texture help_menu; -asw::Texture cursor[2]; asw::Texture levelSelect; asw::Texture levelSelectLeft; asw::Texture levelSelectRight; @@ -138,10 +137,6 @@ asw::Sample winsound; asw::Sample door; asw::Sample click; -void restart(); -void update(); -void restartlevel(); -void loadLevel(char fileName[]); void changeMap(); // Timers @@ -191,135 +186,135 @@ void rest(int ms) { } } -void setupGame(bool first) { +void setupGame() { won = 0; haveBroom = 0; x = 40; y = 40; lives = 3; characterRotation = 0; +} - if (first) { - gameScreen = 0; - currentLevel = 1; - score = 0; - levelText = ""; - - if (perspective == 0) { - character[0] = asw::assets::loadTexture( - "assets/images/blocks/3d/character_down.png"); - character[1] = - asw::assets::loadTexture("assets/images/blocks/3d/character_up.png"); - character[2] = asw::assets::loadTexture( - "assets/images/blocks/3d/character_left.png"); - character[3] = asw::assets::loadTexture( - "assets/images/blocks/3d/character_right.png"); - - wall = asw::assets::loadTexture("assets/images/blocks/3d/wall.png"); - wall2 = asw::assets::loadTexture("assets/images/blocks/3d/wall2.png"); - robot = asw::assets::loadTexture("assets/images/blocks/3d/robot.png"); - box = asw::assets::loadTexture("assets/images/blocks/3d/box.png"); - scrap = asw::assets::loadTexture("assets/images/blocks/3d/scrap.png"); - broom = asw::assets::loadTexture("assets/images/blocks/3d/broom.png"); - garbagecan = - asw::assets::loadTexture("assets/images/blocks/3d/garbagecan.png"); - janitorroom = - asw::assets::loadTexture("assets/images/blocks/3d/janitor_room.png"); - janitorroomopen = asw::assets::loadTexture( - "assets/images/blocks/3d/janitor_room_open.png"); - wood_floor = - asw::assets::loadTexture("assets/images/blocks/3d/wood_floor.png"); - } - - if (perspective == 1) { - character[0] = - asw::assets::loadTexture("assets/images/blocks/2d/character.png"); - character[1] = - asw::assets::loadTexture("assets/images/blocks/2d/character.png"); - character[2] = - asw::assets::loadTexture("assets/images/blocks/2d/character.png"); - character[3] = - asw::assets::loadTexture("assets/images/blocks/2d/character.png"); - - wall = asw::assets::loadTexture("assets/images/blocks/2d/wall.png"); - wall2 = asw::assets::loadTexture("assets/images/blocks/2d/wall2.png"); - robot = asw::assets::loadTexture("assets/images/blocks/2d/robot.png"); - box = asw::assets::loadTexture("assets/images/blocks/2d/box.png"); - scrap = asw::assets::loadTexture("assets/images/blocks/2d/scrap.png"); - broom = asw::assets::loadTexture("assets/images/blocks/2d/broom.png"); - garbagecan = - asw::assets::loadTexture("assets/images/blocks/2d/garbagecan.png"); - janitorroom = - asw::assets::loadTexture("assets/images/blocks/2d/janitor_room.png"); - janitorroomopen = asw::assets::loadTexture( - "assets/images/blocks/2d/janitor_room_open.png"); - wood_floor = - asw::assets::loadTexture("assets/images/blocks/2d/wood_floor.png"); - } - - menu = asw::assets::loadTexture("assets/images/menu.png"); - background = asw::assets::loadTexture("assets/images/background.png"); - help_menu = asw::assets::loadTexture("assets/images/help.png"); - intro = asw::assets::loadTexture("assets/images/intro.png"); - splash = asw::assets::loadTexture("assets/images/splash.png"); - winscreen = asw::assets::loadTexture("assets/images/winscreen.png"); - levelSelect = asw::assets::loadTexture("assets/images/levelSelect.png"); - levelSelectLeft = - asw::assets::loadTexture("assets/images/levelSelectLeft.png"); - levelSelectRight = - asw::assets::loadTexture("assets/images/levelSelectRight.png"); - - // Sounds and music - sweep = asw::assets::loadSample("assets/sfx/sweep.wav"); - explode = asw::assets::loadSample("assets/sfx/explode.wav"); - trash = asw::assets::loadSample("assets/sfx/trash.wav"); - oof = asw::assets::loadSample("assets/sfx/oof.wav"); - winsound = asw::assets::loadSample("assets/sfx/winsound.wav"); - door = asw::assets::loadSample("assets/sfx/door.wav"); - hitwall = asw::assets::loadSample("assets/sfx/hitwall.wav"); - boxhitwall = asw::assets::loadSample("assets/sfx/boxhitwall.wav"); - boxslide = asw::assets::loadSample("assets/sfx/boxslide.wav"); - click = asw::assets::loadSample("assets/sfx/click.wav"); - - song = asw::assets::loadMusic("assets/sfx/music.ogg"); - - // Sets button images - start.SetImages("assets/images/buttons/start.png", - "assets/images/buttons/start_hover.png"); - help.SetImages("assets/images/buttons/help.png", - "assets/images/buttons/help_hover.png"); - quit.SetImages("assets/images/buttons/quit.png", - "assets/images/buttons/quit_hover.png"); - mode.SetImages("assets/images/buttons/mode_3d.png", - "assets/images/buttons/mode_3d_hover.png"); - back.SetImages("assets/images/buttons/back.png", - "assets/images/buttons/back_hover.png"); - - // Sets button positions - start.SetX(380); - help.SetX(380); - quit.SetX(380); - mode.SetX(380); - back.SetX(380); - - start.SetY(240); - help.SetY(380); - quit.SetY(520); - mode.SetY(660); - back.SetY(40); - - // Sets time - startTime = clock(); - currentTime = clock(); +void initGame() { + setupGame(); + + gameScreen = 0; + currentLevel = 1; + score = 0; + levelText = ""; + + if (perspective == 0) { + character[0] = + asw::assets::loadTexture("assets/images/blocks/3d/character_down.png"); + character[1] = + asw::assets::loadTexture("assets/images/blocks/3d/character_up.png"); + character[2] = + asw::assets::loadTexture("assets/images/blocks/3d/character_left.png"); + character[3] = + asw::assets::loadTexture("assets/images/blocks/3d/character_right.png"); + + wall = asw::assets::loadTexture("assets/images/blocks/3d/wall.png"); + wall2 = asw::assets::loadTexture("assets/images/blocks/3d/wall2.png"); + robot = asw::assets::loadTexture("assets/images/blocks/3d/robot.png"); + box = asw::assets::loadTexture("assets/images/blocks/3d/box.png"); + scrap = asw::assets::loadTexture("assets/images/blocks/3d/scrap.png"); + broom = asw::assets::loadTexture("assets/images/blocks/3d/broom.png"); + garbagecan = + asw::assets::loadTexture("assets/images/blocks/3d/garbagecan.png"); + janitorroom = + asw::assets::loadTexture("assets/images/blocks/3d/janitor_room.png"); + janitorroomopen = asw::assets::loadTexture( + "assets/images/blocks/3d/janitor_room_open.png"); + wood_floor = + asw::assets::loadTexture("assets/images/blocks/3d/wood_floor.png"); + } - // Sets Font - font = asw::assets::loadFont("assets/fonts/dosis.ttf", 16); + if (perspective == 1) { + character[0] = + asw::assets::loadTexture("assets/images/blocks/2d/character.png"); + character[1] = + asw::assets::loadTexture("assets/images/blocks/2d/character.png"); + character[2] = + asw::assets::loadTexture("assets/images/blocks/2d/character.png"); + character[3] = + asw::assets::loadTexture("assets/images/blocks/2d/character.png"); + + wall = asw::assets::loadTexture("assets/images/blocks/2d/wall.png"); + wall2 = asw::assets::loadTexture("assets/images/blocks/2d/wall2.png"); + robot = asw::assets::loadTexture("assets/images/blocks/2d/robot.png"); + box = asw::assets::loadTexture("assets/images/blocks/2d/box.png"); + scrap = asw::assets::loadTexture("assets/images/blocks/2d/scrap.png"); + broom = asw::assets::loadTexture("assets/images/blocks/2d/broom.png"); + garbagecan = + asw::assets::loadTexture("assets/images/blocks/2d/garbagecan.png"); + janitorroom = + asw::assets::loadTexture("assets/images/blocks/2d/janitor_room.png"); + janitorroomopen = asw::assets::loadTexture( + "assets/images/blocks/2d/janitor_room_open.png"); + wood_floor = + asw::assets::loadTexture("assets/images/blocks/2d/wood_floor.png"); } + menu = asw::assets::loadTexture("assets/images/menu.png"); + background = asw::assets::loadTexture("assets/images/background.png"); + help_menu = asw::assets::loadTexture("assets/images/help.png"); + intro = asw::assets::loadTexture("assets/images/intro.png"); + splash = asw::assets::loadTexture("assets/images/splash.png"); + winscreen = asw::assets::loadTexture("assets/images/winscreen.png"); + levelSelect = asw::assets::loadTexture("assets/images/levelSelect.png"); + levelSelectLeft = + asw::assets::loadTexture("assets/images/levelSelectLeft.png"); + levelSelectRight = + asw::assets::loadTexture("assets/images/levelSelectRight.png"); + + // Sounds and music + sweep = asw::assets::loadSample("assets/sfx/sweep.wav"); + explode = asw::assets::loadSample("assets/sfx/explode.wav"); + trash = asw::assets::loadSample("assets/sfx/trash.wav"); + oof = asw::assets::loadSample("assets/sfx/oof.wav"); + winsound = asw::assets::loadSample("assets/sfx/winsound.wav"); + door = asw::assets::loadSample("assets/sfx/door.wav"); + hitwall = asw::assets::loadSample("assets/sfx/hitwall.wav"); + boxhitwall = asw::assets::loadSample("assets/sfx/boxhitwall.wav"); + boxslide = asw::assets::loadSample("assets/sfx/boxslide.wav"); + click = asw::assets::loadSample("assets/sfx/click.wav"); + + song = asw::assets::loadMusic("assets/sfx/music.ogg"); + + // Sets button images + start.SetImages("assets/images/buttons/start.png", + "assets/images/buttons/start_hover.png"); + help.SetImages("assets/images/buttons/help.png", + "assets/images/buttons/help_hover.png"); + quit.SetImages("assets/images/buttons/quit.png", + "assets/images/buttons/quit_hover.png"); + mode.SetImages("assets/images/buttons/mode_3d.png", + "assets/images/buttons/mode_3d_hover.png"); + back.SetImages("assets/images/buttons/back.png", + "assets/images/buttons/back_hover.png"); + + // Sets button positions + start.SetX(380); + help.SetX(380); + quit.SetX(380); + mode.SetX(380); + back.SetX(380); + + start.SetY(240); + help.SetY(380); + quit.SetY(520); + mode.SetY(660); + back.SetY(40); + + // Sets time + startTime = clock(); + currentTime = clock(); + + // Sets Font + font = asw::assets::loadFont("assets/fonts/dosis.ttf", 16); + // Background Music - if (first) { - asw::sound::playMusic(song, 255); - } + asw::sound::playMusic(song, 255); } // Update Blocks @@ -390,7 +385,7 @@ void resetBlocks(int newI, int newT) { } // Controls Character Movements -void charactermove() { +void characterMove() { if (asw::input::wasKeyPressed(asw::input::Key::UP) || asw::input::wasKeyPressed(asw::input::Key::W)) { characterRotation = 128; @@ -621,6 +616,43 @@ void charactermove() { mousePressed = false; } +void updateRobots() { + for (int i = 0; i < 24; i++) { + for (int t = 0; t < 32; t++) { + if (tiles[i][t].value == V_ROBOT) { + int random_integer = asw::random::between(0, 4); + + if (random_integer == 1 && tiles[i - 1][t].value == V_EMPTY) { + tiles[i - 1][t].value = V_ROBOT; + tiles[i][t].value = V_EMPTY; + } + + if (random_integer == 2 && tiles[i][t + 1].value == V_EMPTY) { + tiles[i][t + 1].value = V_ROBOT; + tiles[i][t].value = V_EMPTY; + } + + if (random_integer == 3 && tiles[i + 1][t].value == V_EMPTY) { + tiles[i + 1][t].value = V_ROBOT; + tiles[i][t].value = V_EMPTY; + } + + if (random_integer == 4 && tiles[i][t - 1].value == V_EMPTY) { + tiles[i][t - 1].value = V_ROBOT; + tiles[i][t].value = V_EMPTY; + } + + if (i == y / 40 && t == x / 40) { + asw::sound::play(oof, 2000, 122, 0); + lives -= 1; + x = 40; + y = 40; + } + } + } + } +} + // Change tiles void changeMap() { finalFile = "assets/levels/level" + convertInt(currentLevel) + ".map"; @@ -673,12 +705,6 @@ void changeMap() { } resetBlocks(-1, -1); - - if (gameScreen == 3) { - asw::draw::textCenter(font, levelText, asw::Vec2(640, 460), - asw::util::makeColor(255, 255, 255)); - rest(1000); - } } else if (gameScreen == 3) { gameScreen = 4; } @@ -686,367 +712,384 @@ void changeMap() { // Updates games void game() { - setupGame(true); + // Splash + if (gameScreen == 0) { + return; + } - while (!asw::core::exit) { - asw::core::update(); - asw::display::present(); - asw::display::clear(); + // Menu + else if (gameScreen == 1) { + if (helpOpen) { + if (asw::input::keyboard.anyPressed || + asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { + helpOpen = false; + } + } else { + if (asw::input::wasKeyPressed(asw::input::Key::ESCAPE)) { + asw::core::exit = true; + } + } - // Splash - if (gameScreen == 0) { - asw::display::clear(); - asw::draw::sprite(intro, asw::Vec2(0, 0)); - asw::display::present(); - rest(1000); - asw::display::clear(); - asw::draw::sprite(splash, asw::Vec2(0, 0)); - asw::display::present(); - rest(1000); - gameScreen = 1; - asw::display::clear(); - asw::draw::sprite(menu, asw::Vec2(0, 0)); - asw::display::present(); - continue; + // Checks for mouse press + if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { + if (start.Hover()) { + gameScreen = 2; + setupGame(); + return; + } else if (help.Hover()) { + helpOpen = true; + } else if (quit.Hover()) { + asw::core::exit = true; + } else if (mode.Hover()) { + if (perspective == 0) { + perspective = 1; + initGame(); + mode.SetImages("assets/images/buttons/mode_2d.png", + "assets/images/buttons/mode_2d_hover.png"); + } else if (perspective == 1) { + perspective = 0; + initGame(); + mode.SetImages("assets/images/buttons/mode_3d.png", + "assets/images/buttons/mode_3d_hover.png"); + } + } } + } - // Menu - else if (gameScreen == 1) { - // Draws Menu - asw::draw::sprite(menu, asw::Vec2(0, 0)); + // Level Select + if (gameScreen == 2) { + // Change map + changeMap(); - // Draws Buttons - start.draw(); - help.draw(); - quit.draw(); - mode.draw(); + // Go to menu + if (asw::input::wasKeyPressed(asw::input::Key::ESCAPE)) { + gameScreen = 1; + return; + } - if (helpOpen) { - helpOpen = true; - asw::draw::sprite(help_menu, asw::Vec2(0, 0)); + // Click buttons + if (collision(asw::input::mouse.x, asw::input::mouse.x, -1, 200, + asw::input::mouse.y, asw::input::mouse.y, 0, 960)) { + if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT) && + currentLevel > 1) { + asw::sound::play(click, 255, 125, 0); + currentLevel -= 1; + setupGame(); + rest(140); + } + } else if (collision(asw::input::mouse.x, asw::input::mouse.x, 1080, 1280, + asw::input::mouse.y, asw::input::mouse.y, 0, 960)) { + if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { + finalFile = + "assets/levels/level" + convertInt(currentLevel + 1) + ".map"; - if (asw::input::keyboard.anyPressed || - asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { - helpOpen = false; - } - } else { - if (asw::input::wasKeyPressed(asw::input::Key::ESCAPE)) { - asw::core::exit = true; + if (fexists(finalFile.c_str())) { + asw::sound::play(click, 255, 125, 0); + currentLevel += 1; + setupGame(); + rest(140); } } - - // Checks for mouse press + } else if (collision(asw::input::mouse.x, asw::input::mouse.x, 320, 978, + asw::input::mouse.y, asw::input::mouse.y, 220, 718)) { if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { - if (start.Hover()) { - gameScreen = 2; - setupGame(false); - continue; - } else if (help.Hover()) { - helpOpen = true; - } else if (quit.Hover()) { - asw::core::exit = true; - } else if (mode.Hover()) { - if (perspective == 0) { - perspective = 1; - setupGame(true); - mode.SetImages("assets/images/buttons/mode_2d.png", - "assets/images/buttons/mode_2d_hover.png"); - } else if (perspective == 1) { - perspective = 0; - setupGame(true); - mode.SetImages("assets/images/buttons/mode_3d.png", - "assets/images/buttons/mode_3d_hover.png"); - } - } + gameScreen = 3; + changeMap(); + setupGame(); + return; } } - // Level Select - if (gameScreen == 2) { - // Change map + if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT) && + back.Hover() == true) { + gameScreen = 1; + return; + } + } + + // Ingame Loops + if (gameScreen == 3) { + // Go to menu + if (asw::input::wasKeyPressed(asw::input::Key::ESCAPE)) { + gameScreen = 1; + score = 0; + return; + } + + // Changes move + moving = false; + + // Update Clock + currentTime = clock(); + + // Checks if movther + if (int(currentTime - startTime) / CLOCKS_PER_SEC > (elaspedTime)) { + moving = true; + } + + // Updates Elasped Time + elaspedTime = int(currentTime - startTime) / CLOCKS_PER_SEC; + + // Die + if (tiles[y / 40][x / 40].value == V_ROBOT) { + lives -= 1; + x = 40; + y = 40; + asw::sound::play(oof, 255, 122, 0); + } + + // Updates robots positions + if (step % 40 == 0) { + updateRobots(); + } + + // Restart Map + if (asw::input::wasKeyPressed(asw::input::Key::R)) { changeMap(); + } - // Die - if (tiles[y / 40][x / 40].value == V_ROBOT) { - lives -= 1; - x = 40; - y = 40; - asw::sound::play(oof, 255, 122, 0); + // Pause Game + if (asw::input::wasKeyPressed(asw::input::Key::P)) { + while (!(asw::input::wasKeyPressed(asw::input::Key::P) || + asw::input::wasKeyPressed(asw::input::Key::ESCAPE))) { + asw::display::clear(); + asw::draw::text(font, "Paused press P to resume", + asw::Vec2(420, 440), + asw::util::makeColor(0, 0, 0)); + asw::core::update(); + asw::display::present(); } + } - // Go to menu - if (asw::input::wasKeyPressed(asw::input::Key::ESCAPE)) { - gameScreen = 1; - continue; - } + // Finish Level + if (doneLevel == 1) { + asw::sound::play(winsound, 255, 122, 0); + currentLevel += 1; + lives += 1; + changeMap(); + } - asw::draw::sprite(levelSelect, asw::Vec2(0, 0)); - back.draw(); - asw::draw::stretchSprite(background, - asw::Quad(320, 220, 640, 480)); - asw::draw::textCenter(font, "Level: " + std::to_string(currentLevel), - asw::Vec2(640, 760), - asw::util::makeColor(0, 0, 0)); - - // Mini tiles tiles - for (int i = 0; i < 24; i++) { - for (int t = 0; t < 32; t++) { - if (tiles[i][t].image[0] != nullptr && tiles[i][t].value != 1) { - if (perspective == 0) { - asw::draw::stretchSprite( - tiles[i][t].image[0], - asw::Quad(t * 20 + 320, i * 20 + 220, 30, 30)); - } else if (perspective == 1) { - asw::draw::stretchSprite( - tiles[i][t].image[0], - asw::Quad(t * 20 + 320, i * 20 + 220, 20, 20)); - } - } + // Die + if (lives <= 0) { + changeMap(); + score = 0; + setupGame(); + } + + // Draws Tiles + for (int i = 0; i < 24; i++) { + for (int t = 0; t < 32; t++) { + // Reset blocks + if (tiles[i][t].value != map[i][t]) { + resetBlocks(i, t); } + + map[i][t] = tiles[i][t].value; } + } - // Click buttons - if (collision(asw::input::mouse.x, asw::input::mouse.x, -1, 200, - asw::input::mouse.y, asw::input::mouse.y, 0, 960)) { - asw::draw::sprite(levelSelectLeft, asw::Vec2(0, 0)); + // Checks for character move + characterMove(); - if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT) && - currentLevel > 1) { - asw::sound::play(click, 255, 125, 0); - currentLevel -= 1; - setupGame(false); - rest(140); - } - } else if (collision(asw::input::mouse.x, asw::input::mouse.x, 1080, 1280, - asw::input::mouse.y, asw::input::mouse.y, 0, 960)) { - asw::draw::sprite(levelSelectRight, asw::Vec2(1080, 0)); - - if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { - finalFile = - "assets/levels/level" + convertInt(currentLevel + 1) + ".map"; - - if (fexists(finalFile.c_str())) { - asw::sound::play(click, 255, 125, 0); - currentLevel += 1; - setupGame(false); - rest(140); - } - } - } else if (collision(asw::input::mouse.x, asw::input::mouse.x, 320, 978, - asw::input::mouse.y, asw::input::mouse.y, 220, - 718)) { - if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { - gameScreen = 3; - changeMap(); - setupGame(false); - continue; - } - } + // Rests (regulates game speed) + rest(100); - if (asw::input::wasButtonPressed(asw::input::MouseButton::LEFT) && - back.Hover() == true) { - gameScreen = 1; - continue; - } + } else if (gameScreen == 4) { + if (asw::input::wasKeyPressed(asw::input::Key::SPACE) || + asw::input::wasKeyPressed(asw::input::Key::RETURN) || + asw::input::wasKeyPressed(asw::input::Key::ESCAPE) || + asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { + gameScreen = 1; + return; } + } - // Ingame Loops - if (gameScreen == 3) { - // Go to menu - if (asw::input::wasKeyPressed(asw::input::Key::ESCAPE)) { - gameScreen = 1; - score = 0; - continue; - } + // Adds one to step + step++; +} - // Changes move - moving = false; +// Updates games +void drawGame() { + // Splash + if (gameScreen == 0) { + asw::display::clear(); + asw::draw::sprite(intro, asw::Vec2(0, 0)); + asw::display::present(); + rest(1000); + asw::display::clear(); + asw::draw::sprite(splash, asw::Vec2(0, 0)); + asw::display::present(); + rest(1000); + gameScreen = 1; + asw::display::clear(); + asw::draw::sprite(menu, asw::Vec2(0, 0)); + asw::display::present(); + return; + } - // Update Clock - currentTime = clock(); + // Menu + else if (gameScreen == 1) { + // Draws Menu + asw::draw::sprite(menu, asw::Vec2(0, 0)); - // Checks if movther - if (int(currentTime - startTime) / CLOCKS_PER_SEC > (elaspedTime)) { - moving = true; - } + // Draws Buttons + start.draw(); + help.draw(); + quit.draw(); + mode.draw(); - // Updates Elasped Time - elaspedTime = int(currentTime - startTime) / CLOCKS_PER_SEC; - - // Draw background - asw::draw::sprite(background, asw::Vec2(0, 0)); - - // Updates robots positions - if (step % 40 == 0) { - for (int i = 0; i < 24; i++) { - for (int t = 0; t < 32; t++) { - if (tiles[i][t].value == V_ROBOT) { - int random_integer = asw::random::between(0, 4); - - if (random_integer == 1 && tiles[i - 1][t].value == V_EMPTY) { - tiles[i - 1][t].value = V_ROBOT; - tiles[i][t].value = V_EMPTY; - } - - if (random_integer == 2 && tiles[i][t + 1].value == V_EMPTY) { - tiles[i][t + 1].value = V_ROBOT; - tiles[i][t].value = V_EMPTY; - } - - if (random_integer == 3 && tiles[i + 1][t].value == V_EMPTY) { - tiles[i + 1][t].value = V_ROBOT; - tiles[i][t].value = V_EMPTY; - } - - if (random_integer == 4 && tiles[i][t - 1].value == V_EMPTY) { - tiles[i][t - 1].value = V_ROBOT; - tiles[i][t].value = V_EMPTY; - } - - if (i == y / 40 && t == x / 40) { - asw::sound::play(oof, 2000, 122, 0); - lives -= 1; - x = 40; - y = 40; - } - } - } - } - } + if (helpOpen) { + asw::draw::sprite(help_menu, asw::Vec2(0, 0)); + } + } - // Restart Map - if (asw::input::wasKeyPressed(asw::input::Key::R)) { - changeMap(); - } + // Level Select + if (gameScreen == 2) { + asw::draw::sprite(levelSelect, asw::Vec2(0, 0)); + back.draw(); + asw::draw::stretchSprite(background, asw::Quad(320, 220, 640, 480)); + asw::draw::textCenter(font, "Level: " + std::to_string(currentLevel), + asw::Vec2(640, 760), + asw::util::makeColor(0, 0, 0)); - // Pause Game - if (asw::input::wasKeyPressed(asw::input::Key::P)) { - while (!(asw::input::wasKeyPressed(asw::input::Key::P) || - asw::input::wasKeyPressed(asw::input::Key::ESCAPE))) { - asw::display::clear(); - asw::draw::text(font, "Paused press P to resume", - asw::Vec2(420, 440), + asw::draw::textCenter(font, levelText, asw::Vec2(640, 800), asw::util::makeColor(0, 0, 0)); - asw::core::update(); - asw::display::present(); + + // Mini tiles tiles + for (int i = 0; i < 24; i++) { + for (int t = 0; t < 32; t++) { + if (tiles[i][t].image[0] != nullptr && tiles[i][t].value != 1) { + if (perspective == 0) { + asw::draw::stretchSprite( + tiles[i][t].image[0], + asw::Quad(t * 20 + 320, i * 20 + 220, 30, 30)); + } else if (perspective == 1) { + asw::draw::stretchSprite( + tiles[i][t].image[0], + asw::Quad(t * 20 + 320, i * 20 + 220, 20, 20)); + } } } + } - // Finish Level - if (doneLevel == 1) { - asw::sound::play(winsound, 255, 122, 0); - currentLevel += 1; - lives += 1; - changeMap(); - } + // Click buttons + if (collision(asw::input::mouse.x, asw::input::mouse.x, -1, 200, + asw::input::mouse.y, asw::input::mouse.y, 0, 960)) { + asw::draw::sprite(levelSelectLeft, asw::Vec2(0, 0)); + } else if (collision(asw::input::mouse.x, asw::input::mouse.x, 1080, 1280, + asw::input::mouse.y, asw::input::mouse.y, 0, 960)) { + asw::draw::sprite(levelSelectRight, asw::Vec2(1080, 0)); + } + } - // Die - if (lives <= 0) { - changeMap(); - score = 0; - setupGame(false); - } + // Ingame Loops + if (gameScreen == 3) { + // Draw background + asw::draw::sprite(background, asw::Vec2(0, 0)); - // Draws Tiles - for (int i = 0; i < 24; i++) { - for (int t = 0; t < 32; t++) { - if (y / 40 == i && x / 40 == t) { - // Draws Character - if (perspective == 0) { - // rotate_sprite( character, x + 5, y + 5, - // ftofix(characterRotation)); - if (characterRotation == 0) { - asw::draw::sprite(character[0], asw::Vec2(x, y)); - } else if (characterRotation == 128) { - asw::draw::sprite(character[1], asw::Vec2(x, y)); - } else if (characterRotation == 64) { - asw::draw::sprite(character[2], asw::Vec2(x, y)); - } else if (characterRotation == 192) { - asw::draw::sprite(character[3], asw::Vec2(x, y)); - } - } else { + // Draws Tiles + for (int i = 0; i < 24; i++) { + for (int t = 0; t < 32; t++) { + if (y / 40 == i && x / 40 == t) { + // Draws Character + if (perspective == 0) { + // rotate_sprite( character, x + 5, y + 5, + // ftofix(characterRotation)); + if (characterRotation == 0) { asw::draw::sprite(character[0], asw::Vec2(x, y)); + } else if (characterRotation == 128) { + asw::draw::sprite(character[1], asw::Vec2(x, y)); + } else if (characterRotation == 64) { + asw::draw::sprite(character[2], asw::Vec2(x, y)); + } else if (characterRotation == 192) { + asw::draw::sprite(character[3], asw::Vec2(x, y)); } - - // Draws broom if needed - if (asw::input::isKeyDown(asw::input::Key::SPACE) && - haveBroom == 1) { - if (perspective == 0) { - asw::draw::rotateSprite(broom, asw::Vec2(x + 10, y + 10), - characterRotation); - } else { - asw::draw::sprite(broom, asw::Vec2(x, y)); - } - } - } - - // Reset blocks - if (tiles[i][t].value != map[i][t]) { - resetBlocks(i, t); + } else { + asw::draw::sprite(character[0], asw::Vec2(x, y)); } - map[i][t] = tiles[i][t].value; - - if (tiles[i][t].image[0] != nullptr) { + // Draws broom if needed + if (asw::input::isKeyDown(asw::input::Key::SPACE) && haveBroom == 1) { if (perspective == 0) { - asw::draw::stretchSprite( - tiles[i][t].image[0], - asw::Quad(t * 40, i * 40, 60, 60)); + asw::draw::rotateSprite(broom, asw::Vec2(x + 10, y + 10), + characterRotation); } else { - asw::draw::stretchSprite( - tiles[i][t].image[0], - asw::Quad(t * 40, i * 40, 40, 40)); + asw::draw::sprite(broom, asw::Vec2(x, y)); } } } + + if (tiles[i][t].image[0] != nullptr) { + if (perspective == 0) { + asw::draw::stretchSprite(tiles[i][t].image[0], + asw::Quad(t * 40, i * 40, 60, 60)); + } else { + asw::draw::stretchSprite(tiles[i][t].image[0], + asw::Quad(t * 40, i * 40, 40, 40)); + } + } } + } + + // Draws Stats + asw::draw::text(font, "Score:" + std::to_string(score), + asw::Vec2(0, 0), + asw::util::makeColor(255, 255, 255)); - // Draws Stats - asw::draw::text(font, "Score:" + std::to_string(score), - asw::Vec2(0, 0), - asw::util::makeColor(255, 255, 255)); + asw::draw::textRight(font, "Robots Left:" + std::to_string(robotsLeft), + asw::Vec2(1280, 0), + asw::util::makeColor(255, 255, 255)); - asw::draw::textRight(font, "Robots Left:" + std::to_string(robotsLeft), - asw::Vec2(1280, 0), - asw::util::makeColor(255, 255, 255)); + asw::draw::textCenter(font, "Lives:" + std::to_string(lives), + asw::Vec2(640, 0), + asw::util::makeColor(255, 255, 255)); - asw::draw::textCenter(font, "Lives:" + std::to_string(lives), - asw::Vec2(640, 0), - asw::util::makeColor(255, 255, 255)); + } else if (gameScreen == 4) { + asw::draw::sprite(winscreen, asw::Vec2(0, 0)); + asw::draw::text(font, std::to_string(score), asw::Vec2(310, 400), + asw::util::makeColor(0, 0, 0)); - // Checks for character move - charactermove(); + if (asw::input::wasKeyPressed(asw::input::Key::SPACE) || + asw::input::wasKeyPressed(asw::input::Key::RETURN) || + asw::input::wasKeyPressed(asw::input::Key::ESCAPE) || + asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { + gameScreen = 1; + return; + } + } - // Rests (regulates game speed) - rest(100); + // Adds one to step + step++; +} - } else if (gameScreen == 4) { - asw::draw::sprite(winscreen, asw::Vec2(0, 0)); - asw::draw::text(font, std::to_string(score), asw::Vec2(310, 400), - asw::util::makeColor(0, 0, 0)); +/// Scene stuff to get working in browser +enum class GameState { Main }; - if (asw::input::wasKeyPressed(asw::input::Key::SPACE) || - asw::input::wasKeyPressed(asw::input::Key::RETURN) || - asw::input::wasKeyPressed(asw::input::Key::ESCAPE) || - asw::input::wasButtonPressed(asw::input::MouseButton::LEFT)) { - gameScreen = 1; - continue; - } - } +class MainScene : public asw::scene::Scene { + public: + using asw::scene::Scene::Scene; - // Adds one to step - step++; + void update(float deltaTime) override { + Scene::update(deltaTime); + game(); } -} + + void draw() override { drawGame(); } +}; // Main functions run here int main() { // Initializing - asw::core::init(1280, 960, 1); + asw::core::init(1280, 960); asw::display::setTitle("Mazes"); + initGame(); + // Starts Game - game(); + auto app = asw::scene::SceneManager(); + app.registerScene(GameState::Main, app); + app.setNextScene(GameState::Main); + app.start(); return 0; }