diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp index 0525b733..c276a7a9 100644 --- a/Crawler/Crawler.cpp +++ b/Crawler/Crawler.cpp @@ -17,6 +17,7 @@ #include "config.h" #include "safemap.h" #include "Key.h" +#include "Menu.h" INCLUDE_EMITTER_LIST @@ -76,6 +77,7 @@ Crawler::Crawler() bool Crawler::OnUserCreate(){ InitializeDefaultKeybinds(); InitializeLevels(); + Menu::InitializeMenus(); circleCooldownPoints.push_back({0,0}); for(int i=0;i<=360;i+=4){ @@ -141,6 +143,7 @@ bool Crawler::OnUserUpdate(float fElapsedTime){ UpdateCamera(fElapsedTime); RenderWorld(fElapsedTime); RenderHud(); + RenderMenu(); return true; } @@ -170,6 +173,8 @@ bool Crawler::DownReleased(){ } void Crawler::HandleUserInput(float fElapsedTime){ + if(!Menu::stack.empty())return; //A window being opened means there's no user input allowed. + bool setIdleAnimation=true; if(GetKey(F1).bPressed){ ConsoleShow(F1); @@ -357,6 +362,10 @@ void Crawler::HandleUserInput(float fElapsedTime){ } } } + + if(GetKey(I).bPressed){ + Menu::OpenMenu(TEST); + } } void Crawler::UpdateCamera(float fElapsedTime){ @@ -1661,4 +1670,13 @@ void Crawler::ReduceBossEncounterMobCount(){ std::cout<<"WARNING! Boss Encounter mob count is less than zero, THIS SHOULD NOT BE HAPPENING!"<0){ + Menu::stack.back()->Update(this); + } + if(Menu::stack.size()>0){ + Menu::stack.back()->Draw(this); + } } \ No newline at end of file diff --git a/Crawler/Crawler.h b/Crawler/Crawler.h index 07b5dc12..bc36414b 100644 --- a/Crawler/Crawler.h +++ b/Crawler/Crawler.h @@ -75,6 +75,7 @@ public: void UpdateBullets(float fElapsedTime); void RenderWorld(float fElapsedTime); void RenderHud(); + void RenderMenu(); void AddEffect(std::unique_ptrforeground,std::unique_ptrbackground); //If back is true, places the effect in the background void AddEffect(std::unique_ptrforeground,bool back=false); diff --git a/Crawler/Crawler.vcxproj b/Crawler/Crawler.vcxproj index 07c6112e..8703f1e7 100644 --- a/Crawler/Crawler.vcxproj +++ b/Crawler/Crawler.vcxproj @@ -270,8 +270,11 @@ + + + @@ -309,6 +312,8 @@ + + @@ -322,6 +327,8 @@ + + diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index cce5e8ec..58135953 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -40,6 +40,9 @@ {3d2f7a3f-5781-45ab-a66d-c6d57d9de13c} + + {f90a901c-026c-4c22-9fab-58927f1e5536} + @@ -141,6 +144,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + @@ -251,6 +263,18 @@ Source Files + + Source Files\Interface + + + Source Files\Interface + + + Source Files\Interface + + + Source Files\Interface + diff --git a/Crawler/Key.cpp b/Crawler/Key.cpp index fd2a0f86..257ccb49 100644 --- a/Crawler/Key.cpp +++ b/Crawler/Key.cpp @@ -193,7 +193,7 @@ std::map,std::string> GenericKey::keyLiteral={ {{KEY, OEM_7},"\""}, {{KEY, OEM_8},"\\"}, {{KEY, CAPS_LOCK},"CAP LK"}, - {{KEY, ENUM_END},""}, + {{KEY, olc::ENUM_END},""}, {{MOUSE, Mouse::LEFT},"L.MOUSE"}, {{MOUSE, Mouse::RIGHT},"R.MOUSE"}, {{MOUSE, Mouse::MIDDLE},"M.MOUSE"}, diff --git a/Crawler/Menu.cpp b/Crawler/Menu.cpp new file mode 100644 index 00000000..4f5b7333 --- /dev/null +++ b/Crawler/Menu.cpp @@ -0,0 +1,104 @@ +#include "Crawler.h" +#include "Menu.h" + +bool Menu::MOUSE_NAVIGATION=true; +std::vectorMenu::stack; +std::mapMenu::menus; + +Menu::Menu(){} + +Menu::Menu(vf2d size) + :size(size){} + +void Menu::InitializeMenus(){ + stack.reserve(32); + menus[TEST]=InitializeTestMenu(); + menus[TEST_2]=InitializeTestSubMenu(); + + for(MenuType type=TEST;typeGetScreenSize()/2-size/2; + + for(MenuButton&button:buttons){ + button.hovered=false; + } + if(!MOUSE_NAVIGATION){ + if(selection!=-1)buttons[selection].hovered=true; + }else{ + for(MenuButton&button:buttons){ + if(geom2d::overlaps(geom2d::rect{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){ + button.hovered=true; + } + } + } + if(game->GetKey(RIGHT).bPressed||game->GetKey(DOWN).bPressed){ + MOUSE_NAVIGATION=false; + selection=(selection+1)%buttons.size(); + } + if(game->GetKey(LEFT).bPressed||game->GetKey(UP).bPressed){ + MOUSE_NAVIGATION=false; + selection--; + if(selection<0)selection+=buttons.size(); + } + if(game->GetMouse(0).bPressed||game->GetKey(ENTER).bPressed||game->GetKey(SPACE).bPressed){ + MOUSE_NAVIGATION=game->GetMouse(0).bPressed; //If a click occurs we use mouse controls. + if(!MOUSE_NAVIGATION){ + MenuSelect(game); + //Key presses automatically highlight the first button if it's not highlighted. + if(selection==-1&&buttons.size()>0){ + selection=0; + } + }else{//Mouse click. + selection=-1; + int index=0; + for(MenuButton&button:buttons){ + if(geom2d::overlaps(geom2d::rect{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){ + selection=index; + break; + } + index++; + } + MenuSelect(game); + } + } + for(MenuButton&button:buttons){ + button.Update(game); + } +}; + +void Menu::Draw(Crawler*game){ + vf2d upperLeftPos=game->GetScreenSize()/2-size/2; + game->FillRectDecal(upperLeftPos,size,VERY_DARK_BLUE); + for(MenuButton&button:buttons){ + button.Draw(game,upperLeftPos); + } +}; + +void Menu::OpenMenu(MenuType menu){ + stack.clear(); + stack.push_back(&(menus[menu])); +} \ No newline at end of file diff --git a/Crawler/Menu.h b/Crawler/Menu.h index 31b4bd48..e9027f3a 100644 --- a/Crawler/Menu.h +++ b/Crawler/Menu.h @@ -6,13 +6,25 @@ class Crawler; class Menu{ - std::stack*stack=nullptr; + friend class Crawler; + friend class Player; + static bool MOUSE_NAVIGATION; + static std::mapmenus; std::vectorbuttons; + int selection=-1; vf2d size; //Size in tiles (24x24), every menu will be tile-based - +public: Menu(); - void AddButton(vf2d pos); - void Update(float fElapsedTime); + Menu(vf2d size); + void AddButton(const MenuButton&button); + void Update(Crawler*game); void Draw(Crawler*game); + static void InitializeMenus(); + static void OpenMenu(MenuType menu); + static std::vectorstack; +private: + void MenuSelect(Crawler*game); + static const Menu InitializeTestMenu(); + static const Menu InitializeTestSubMenu(); }; diff --git a/Crawler/MenuButton.cpp b/Crawler/MenuButton.cpp new file mode 100644 index 00000000..d5a41a05 --- /dev/null +++ b/Crawler/MenuButton.cpp @@ -0,0 +1,22 @@ +#include "Crawler.h" +#include "Menu.h" + +MenuButton::MenuButton(geom2d::rectrect,std::string label,Decal*icon,MenuFunc onClick) + :rect(rect),label(label),menuDest(MenuType::ENUM_END),icon(icon),onClick(onClick),hoverEffect(0){} + +MenuButton::MenuButton(geom2d::rectrect,std::string label,MenuType menuDest,Decal*icon,MenuFunc onClick) + :rect(rect),label(label),menuDest(menuDest),icon(icon),onClick(onClick),hoverEffect(0){} + +void MenuButton::Update(Crawler*game){ + if(hovered){ + hoverEffect=std::min(1.f,hoverEffect+game->GetElapsedTime()); + }else{ + hoverEffect=std::max(0.f,hoverEffect-game->GetElapsedTime()); + } +} + +void MenuButton::Draw(Crawler*game,vf2d parentPos){ + game->FillRectDecal(rect.pos+parentPos,rect.size,PixelLerp(VERY_DARK_BLUE,CYAN,hoverEffect)); + game->DrawRectDecal(rect.pos+parentPos,rect.size,GREY); + game->DrawStringPropDecal(rect.pos+parentPos+rect.size/2-game->GetTextSizeProp(label)/2,label); +} \ No newline at end of file diff --git a/Crawler/MenuButton.h b/Crawler/MenuButton.h index 3008d68e..fa5b3ea1 100644 --- a/Crawler/MenuButton.h +++ b/Crawler/MenuButton.h @@ -1,17 +1,24 @@ #pragma once -#include "olcPixelGameEngine.h" +#include "olcUTIL_Geometry2D.h" +#include "MenuType.h" #include class Menu; class Crawler; class MenuButton{ - Menu*menuDest; - vf2d size; + friend class Menu; + MenuType menuDest; + geom2d::rectrect; std::string label; Decal*icon; - std::functiononClick; - - void Update(float fElapsedTime); - void Draw(Crawler*game); + MenuFunc onClick; + bool hovered=false; +private: + float hoverEffect=0; +public: + MenuButton(geom2d::rectrect,std::string label,Decal*icon,MenuFunc onClick); + MenuButton(geom2d::rectrect,std::string label,MenuType menuDest,Decal* icon,MenuFunc onClick); + void Update(Crawler*game); + void Draw(Crawler*game,vf2d parentPos); }; \ No newline at end of file diff --git a/Crawler/MenuType.h b/Crawler/MenuType.h new file mode 100644 index 00000000..63cd5286 --- /dev/null +++ b/Crawler/MenuType.h @@ -0,0 +1,14 @@ +#pragma once + +class Menu; +class Crawler; + +typedef std::function MenuFunc; + +enum MenuType{ + TEST, + TEST_2, + /////////////////////////////////////////////////////////// + /*DO NOT REMOVE!!*/ENUM_END//////////////////////////////// + /////////////////////////////////////////////////////////// +}; \ No newline at end of file diff --git a/Crawler/Player.cpp b/Crawler/Player.cpp index c6abdc8d..eece8e1f 100644 --- a/Crawler/Player.cpp +++ b/Crawler/Player.cpp @@ -8,6 +8,7 @@ #include "safemap.h" #include "utils.h" #include "Key.h" +#include "Menu.h" INCLUDE_MONSTER_DATA INCLUDE_MONSTER_LIST @@ -344,56 +345,60 @@ void Player::Update(float fElapsedTime){ SetX(newX); SetY(newY); } - if(CanAct()&&attack_cooldown_timer==0&&Crawler::KEY_ATTACK.Held()){ - AutoAttack(); - } + if(Menu::stack.empty()){ + + if(CanAct()&&attack_cooldown_timer==0&&Crawler::KEY_ATTACK.Held()){ + AutoAttack(); + } - auto AllowedToCast=[&](Ability&ability){return !ability.precastInfo.precastTargetingRequired&&GetState()!=State::ANIMATION_LOCK;}; - //If pressed is set to false, uses held instead. - auto CheckAndPerformAbility=[&](Ability&ability,InputGroup key){ - if(ability.name!="???"){ - if(CanAct(ability)){ - if(ability.cooldown==0&&GetMana()>=ability.manaCost){ - if(key.Held()||key.Released()&&&ability==castPrepAbility&&GetState()==State::PREP_CAST){ - if(GetState()==State::CASTING){ - SetState(State::NORMAL); - castInfo.castTimer=castInfo.castTotalTime=0; - } - if(AllowedToCast(ability)&&ability.action(this,{})){ - ability.cooldown=ability.COOLDOWN_TIME; - mana-=ability.manaCost; - }else - if(ability.precastInfo.precastTargetingRequired&&GetState()==State::NORMAL){ - PrepareCast(ability); + auto AllowedToCast=[&](Ability&ability){return !ability.precastInfo.precastTargetingRequired&&GetState()!=State::ANIMATION_LOCK;}; + //If pressed is set to false, uses held instead. + auto CheckAndPerformAbility=[&](Ability&ability,InputGroup key){ + if(ability.name!="???"){ + if(CanAct(ability)){ + if(ability.cooldown==0&&GetMana()>=ability.manaCost){ + if(key.Held()||key.Released()&&&ability==castPrepAbility&&GetState()==State::PREP_CAST){ + if(GetState()==State::CASTING){ + SetState(State::NORMAL); + castInfo.castTimer=castInfo.castTotalTime=0; + } + if(AllowedToCast(ability)&&ability.action(this,{})){ + ability.cooldown=ability.COOLDOWN_TIME; + mana-=ability.manaCost; + }else + if(ability.precastInfo.precastTargetingRequired&&GetState()==State::NORMAL){ + PrepareCast(ability); + } } + } else + if(ability.cooldown==0&&GetMana()GetPlayer()->Hurt(20,game->GetPlayer()->OnUpperLevel(),game->GetPlayer()->GetZ()); + }; + + testMenu.AddButton(MenuButton({{24*4,24*3},{24*3,24*1}},"Hurt Player",nullptr,HurtPlayer)); + + testMenu.AddButton(MenuButton({{24*2,24*4.5},{24*4,24*1}},"Open Another\n Menu",TEST_2,nullptr,doNothing)); + + return testMenu; +} \ No newline at end of file diff --git a/Crawler/TestSubMenu.cpp b/Crawler/TestSubMenu.cpp new file mode 100644 index 00000000..b7afc13a --- /dev/null +++ b/Crawler/TestSubMenu.cpp @@ -0,0 +1,20 @@ +#include "Crawler.h" +#include "Menu.h" + +const Menu Menu::InitializeTestSubMenu(){ + Menu testSubMenu({24*4,24*5}); + + MenuFunc goBack=[](Menu&menu,Crawler*game){ + menu.stack.pop_back(); + }; + + testSubMenu.AddButton(MenuButton({{24*1,24*1},{24*2,24*1}},"Go Back",nullptr,goBack)); + + MenuFunc quitWindow=[](Menu&menu,Crawler*game){ + menu.stack.clear(); + }; + + testSubMenu.AddButton(MenuButton({{24*1,24*3},{24*3,24*1}},"Close Window",nullptr,quitWindow)); + + return testSubMenu; +} \ No newline at end of file diff --git a/Crawler/Version.h b/Crawler/Version.h index c5be7f36..139c3748 100644 --- a/Crawler/Version.h +++ b/Crawler/Version.h @@ -2,7 +2,7 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_PATCH 0 -#define VERSION_BUILD 1576 +#define VERSION_BUILD 1615 #define stringify(a) stringify_(a) #define stringify_(a) #a