From 6c16121c117e29a7df11d0715d74dd3f6462010d Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 8 Apr 2023 15:28:24 -0500 Subject: [PATCH] Load default map --- Faceball2030/Editor.cpp | 67 +++++++++++++++++++++++++++++++++++++++-- Faceball2030/Editor.h | 46 +++++++++++++++++++++++++++- Faceball2030/main.cpp | 10 +++++- Faceball2030/main.h | 1 + 4 files changed, 120 insertions(+), 4 deletions(-) diff --git a/Faceball2030/Editor.cpp b/Faceball2030/Editor.cpp index 3850a7e..e5517a9 100644 --- a/Faceball2030/Editor.cpp +++ b/Faceball2030/Editor.cpp @@ -1,11 +1,74 @@ #include "pixelGameEngine.h" #include "main.h" -#include "Editor.h" using namespace olc; extern FaceBall* game; void Editor::Update(float fElapsedTime){ - game->DrawStringDecal({ 0,0 }, "HEllo World!", WHITE, { 4,4 }); + if (!game->IsTextEntryEnabled()){ + if (game->GetKey(F1).bPressed || reEnableTextEntry) { + game->TextEntryEnable(true); + reEnableTextEntry = false; + } + } + else { + game->DrawStringDecal({ 0,0 }, "Enter Level (1-30): "+game->TextEntryGetString(), WHITE, {4,4}); + } +} + +void Editor::OnTextEntryComplete(const std::string& sText) { + std::stringstream ss(game->TextEntryGetString()); + int level; + ss >> level; + if (level >= 1 && level <= 30) { + game->TextEntryEnable(false); + this->level = level; + LoadLevel(); + } + else { + reEnableTextEntry = true; + } +} + +void Editor::LoadLevel() { + std::ifstream file("assets/map/map"+std::to_string(level)+".map"); + if (file.good()) { + while (file.good()) { + std::string line; + std::getline(file, line); + int width = std::atoi(line.c_str()); + std::getline(file, line); + int height = std::atoi(line.c_str()); + MAP_SIZE = { width,height }; + map.clear(); + for (int y = 0; y < MAP_SIZE.y; y++) { + std::vectorrow; + for (int x = 0; x < MAP_SIZE.x; x++) { + std::getline(file, line); + int map_data = std::atoi(line.c_str()); + Tile newTile; + newTile.blink = (map_data & 0b1000000000000000) >> 15; + newTile.facingDir = FacingDirection((map_data & 0b0110000000000000) >> 13); + newTile.enemyId = EnemyID((map_data & 0b0001111110000000) >> 7); + newTile.spawnWave = (map_data & 0b1110000) >> 4; + newTile.walls = map_data & 0b1111; + row.push_back(newTile); + } + map.push_back(row); + } + } + } + else { + std::cout << "Map Level " << level << " does not exist. Creating default map."<row; + for (int x = 0; x < MAP_SIZE.x; x++) { + row.push_back({}); + } + map.push_back(row); + } + } } \ No newline at end of file diff --git a/Faceball2030/Editor.h b/Faceball2030/Editor.h index 63c3bcf..171e5f1 100644 --- a/Faceball2030/Editor.h +++ b/Faceball2030/Editor.h @@ -2,10 +2,54 @@ #include "pixelGameEngine.h" using namespace olc; +enum class FacingDirection { + NORTH, EAST, SOUTH, WEST +}; + +enum EnemyID { + EXIT, + SHOOTME, + POWERUP_ARMOR=57, + POWERUP_SPEED=58, + POWERUP_SHOT=59, + SPECIAL_CAMO=60, + SPECIAL_STOP=61, + SPECIAL_SHIELD=62, + AREA_MAP=63, +}; + +/* +enum Wave { + WAVE0=0, + WAVE1=1, + WAVE2=2, + WAVE3=4 +}; + +enum WallDirection { + NONE = 0, + NORTH = 1, + EAST = 2, + SOUTH = 4, + WEST = 8 +};*/ + +struct Tile { + EnemyID enemyId=SHOOTME; + FacingDirection facingDir=FacingDirection::EAST; + byte spawnWave=0; + byte walls=0; + bool blink=false; +}; + class Editor { - std::string filename; + int level; vi2d MAP_SIZE; + bool reEnableTextEntry; //We must set this to true to cause Text Entry to reappear due to how the callback works. + std::vector> map; public: Editor() {} void Update(float fElapsedTime); + void OnTextEntryComplete(const std::string& sText); + void LoadLevel(); }; \ No newline at end of file diff --git a/Faceball2030/main.cpp b/Faceball2030/main.cpp index 2d4c6f3..633ce46 100644 --- a/Faceball2030/main.cpp +++ b/Faceball2030/main.cpp @@ -715,7 +715,15 @@ bool FaceBall::OnUserUpdate(float fElapsedTime) return true; } - +void FaceBall::OnTextEntryComplete(const std::string& sText) { + switch (mode) { + case GAME: { + }break; + case EDITOR: { + editor.OnTextEntryComplete(sText); + }break; + } +} int main() diff --git a/Faceball2030/main.h b/Faceball2030/main.h index 50e9090..3f6d689 100644 --- a/Faceball2030/main.h +++ b/Faceball2030/main.h @@ -182,4 +182,5 @@ class FaceBall : public PixelGameEngine void AddWall(Direction dir, vi2d gridSquare); bool OnUserCreate() override; bool OnUserUpdate(float fElapsedTime) override; + void OnTextEntryComplete(const std::string& sText) override; }; \ No newline at end of file