Added menu transition and state system.

sigonasr2 6 months ago
parent 8551c9bacd
commit 6851b76f6a
  1. BIN
      assets/button.png
  2. BIN
      assets/hamsterplanet1.png
  3. BIN
      assets/hamsterplanet2.png
  4. BIN
      assets/hamsterplanet3.png
  5. BIN
      assets/welcometo.png
  6. 5
      src/Hamster.cpp
  7. 24
      src/HamsterGame.cpp
  8. 7
      src/HamsterGame.h
  9. 160
      src/Menu.cpp
  10. 82
      src/Menu.h
  11. 3
      src/TODO.txt

Binary file not shown.

After

Width:  |  Height:  |  Size: 797 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

@ -871,7 +871,8 @@ void Hamster::HandleAIControls(){
if(aiNodeTime>GetAIAdjustNodeTime()){ if(aiNodeTime>GetAIAdjustNodeTime()){
geom2d::line<float>playerToHamster{GetPlayer().GetPos(),GetPos()}; geom2d::line<float>playerToHamster{GetPlayer().GetPos(),GetPos()};
const float screenDistance{playerToHamster.length()*(1.325f/(HamsterGame::Game().GetCameraZ()))}; const float screenDistance{playerToHamster.length()*(1.325f/(HamsterGame::Game().GetCameraZ()))};
if(screenDistance>226){ const float playerScreenDistanceToNewNode{geom2d::line<float>(GetPlayer().GetPos(),action.pos).length()*(1.325f/(HamsterGame::Game().GetCameraZ()))};
if(screenDistance>226&&playerScreenDistanceToNewNode>226){
//Let's cheat, hehe. //Let's cheat, hehe.
pos=action.pos; pos=action.pos;
temporaryNode.reset(); temporaryNode.reset();
@ -980,7 +981,7 @@ const float Hamster::GetAILandingSpeed()const{
const float Hamster::GetAIAdjustNodeTime()const{ const float Hamster::GetAIAdjustNodeTime()const{
switch(ai.GetAIType()){ switch(ai.GetAIType()){
case HamsterAI::SMART:{ case HamsterAI::SMART:{
return 0.5f; return 1.f;
}break; }break;
case HamsterAI::NORMAL:{ case HamsterAI::NORMAL:{
return 2.f; return 2.f;

@ -22,6 +22,8 @@ HamsterGame::HamsterGame(const std::string&appName){
} }
bool HamsterGame::OnUserCreate(){ bool HamsterGame::OnUserCreate(){
CreateLayer();
EnableLayer(1U,true);
LoadPBs(); LoadPBs();
audio.SetBackgroundPlay(true); audio.SetBackgroundPlay(true);
olc::GFX3D::ConfigureDisplay(); olc::GFX3D::ConfigureDisplay();
@ -102,6 +104,11 @@ void HamsterGame::LoadGraphics(){
_LoadImage("background4.png"); _LoadImage("background4.png");
_LoadImage("background5.png"); _LoadImage("background5.png");
_LoadImage("raceprogress.png"); _LoadImage("raceprogress.png");
_LoadImage("welcometo.png");
_LoadImage("hamsterplanet1.png");
_LoadImage("hamsterplanet2.png");
_LoadImage("hamsterplanet3.png");
_LoadImage("button.png");
} }
void HamsterGame::LoadAnimations(){ void HamsterGame::LoadAnimations(){
@ -387,12 +394,10 @@ bool HamsterGame::OnUserUpdate(float fElapsedTime){
net.SetName("OneLoneHamster"); net.SetName("OneLoneHamster");
net.SetColor(hamsterColorNames[0]); net.SetColor(hamsterColorNames[0]);
SetupAndStartRace();
} }
runTime+=fElapsedTime; runTime+=fElapsedTime;
UpdateGame(fElapsedTime); menu.UpdateAndDraw(*this,fElapsedTime);
DrawGame();
return true; return true;
} }
@ -690,6 +695,19 @@ const geom2d::rect<int>HamsterGame::GetMapSpawnRect()const{
return currentMap.value().GetData().GetSpawnZone(); return currentMap.value().GetData().GetSpawnZone();
} }
void HamsterGame::SetMapSetList(const std::queue<std::string>&mapSet){
while(mapSetList.size()>0)mapSetList.pop();
mapSetList=mapSet;
}
const bool HamsterGame::HasMoreMapsToPlay()const{
return mapSetList.size()>0;
}
const std::string HamsterGame::PopNextMap(){
std::string frontMap{mapSetList.front()};
mapSetList.pop();
return frontMap;
}
int main() int main()
{ {
HamsterGame game("Project Hamster"); HamsterGame game("Project Hamster");

@ -53,6 +53,7 @@ All rights reserved.
#include "HamsterNet.h" #include "HamsterNet.h"
#include "olcPGEX_SplashScreen.h" #include "olcPGEX_SplashScreen.h"
#include "HamsterLeaderboard.h" #include "HamsterLeaderboard.h"
#include "Menu.h"
struct Letter{ struct Letter{
vf2d pos; vf2d pos;
@ -62,6 +63,7 @@ struct Letter{
class HamsterGame : public olc::PixelGameEngine class HamsterGame : public olc::PixelGameEngine
{ {
friend class Menu;
public: public:
enum class GameMode{ enum class GameMode{
GRAND_PRIX_1, GRAND_PRIX_1,
@ -105,6 +107,9 @@ public:
const int GetRaceTime(); const int GetRaceTime();
const bool RaceCountdownCompleted(); const bool RaceCountdownCompleted();
const geom2d::rect<int>GetMapSpawnRect()const; const geom2d::rect<int>GetMapSpawnRect()const;
void SetMapSetList(const std::queue<std::string>&mapSet);
const bool HasMoreMapsToPlay()const;
const std::string PopNextMap();
private: private:
void UpdateGame(const float fElapsedTime); void UpdateGame(const float fElapsedTime);
void DrawGame(); void DrawGame();
@ -180,4 +185,6 @@ private:
"Blue", "Blue",
}; };
HamsterLeaderboard leaderboard; HamsterLeaderboard leaderboard;
std::queue<std::string>mapSetList{};
Menu menu;
}; };

@ -0,0 +1,160 @@
#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Portions of this software are copyright © 2024 The FreeType
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
*/
#pragma endregion
#include "Menu.h"
#include "HamsterGame.h"
#include "util.h"
void Menu::UpdateAndDraw(HamsterGame&game,const float fElapsedTime){
menuTransitionRefreshTimer-=fElapsedTime;
if(menuTimer>0.f){
menuTimer-=fElapsedTime;
if(menuTimer<=0.f){
currentMenu=nextMenu;
}
}
game.EnableLayer(0U,menuTimer>0.f);
switch(currentMenu){
case INITIALIZE:{
Transition(FADE_OUT,TITLE_SCREEN,1.f);
}break;
case TITLE_SCREEN:{
if(game.GetKey(SPACE).bPressed||game.GetMouse(Mouse::LEFT).bPressed){
Transition(SHIFT_LEFT,MAIN_MENU,0.5f);
}
}break;
case MAIN_MENU:{
if(game.GetKey(SPACE).bPressed||game.GetMouse(Mouse::LEFT).bPressed){
game.SetupAndStartRace();
}
}break;
case GAMEPLAY:{
game.UpdateGame(fElapsedTime);
game.DrawGame();
}break;
case GAMEPLAY_RESULTS:{
game.DrawGame();
}break;
}
if(menuTimer>0.f){
DrawTransition(game);
if(menuTransitionRefreshTimer<=0.f){
menuTransitionRefreshTimer=MENU_TRANSITION_REFRESH_RATE;
}
}else Draw(game,currentMenu,game.SCREEN_FRAME.pos);
}
void Menu::Transition(const TransitionType type,const MenuType gotoMenu,const float transitionTime){
if(menuTimer>0.f)return;
menuTimer=originalMenuTimer=transitionTime;
nextMenu=gotoMenu;
currentTransition=type;
}
void Menu::DrawTransition(HamsterGame&game){
if(currentTransition==FADE_OUT){
if(menuTimer>=originalMenuTimer/2){//Fading out from old scene.
game.SetDrawTarget(1U);
Draw(game,currentMenu,game.SCREEN_FRAME.pos);
game.SetDrawTarget(nullptr);
game.Clear(BLACK);
if(menuTransitionRefreshTimer<=0.f)game.SetLayerTint(0U,{255,255,255,uint8_t(util::lerp(255,0,(menuTimer-originalMenuTimer/2)/(originalMenuTimer/2)))});
}else{//Fading into new scene.
game.SetDrawTarget(1U);
Draw(game,nextMenu,game.SCREEN_FRAME.pos);
game.SetDrawTarget(nullptr);
game.Clear(BLACK);
if(menuTransitionRefreshTimer<=0.f)game.SetLayerTint(0U,{255,255,255,uint8_t(util::lerp(0,255,menuTimer/(originalMenuTimer/2)))});
}
}else{
if(menuTransitionRefreshTimer<=0.f){
switch(currentTransition){
case SHIFT_LEFT:{
oldLayerPos=vi2d{int(util::lerp(-game.SCREEN_FRAME.size.x,0,menuTimer/originalMenuTimer)),0};
newLayerPos=vi2d{int(util::lerp(0,game.SCREEN_FRAME.size.x,menuTimer/originalMenuTimer)),0};
}break;
case SHIFT_RIGHT:{
oldLayerPos=vi2d{int(util::lerp(game.SCREEN_FRAME.size.x,0,menuTimer/originalMenuTimer)),0};
newLayerPos=vi2d{int(util::lerp(0,-game.SCREEN_FRAME.size.x,menuTimer/originalMenuTimer)),0};
}break;
case SHIFT_UP:{
oldLayerPos=vi2d{0,int(util::lerp(-game.SCREEN_FRAME.size.y,0,menuTimer/originalMenuTimer))};
newLayerPos=vi2d{0,int(util::lerp(0,game.SCREEN_FRAME.size.y,menuTimer/originalMenuTimer))};
}break;
case SHIFT_DOWN:{
oldLayerPos=vi2d{0,int(util::lerp(game.SCREEN_FRAME.size.y,0,menuTimer/originalMenuTimer))};
newLayerPos=vi2d{0,int(util::lerp(0,-game.SCREEN_FRAME.size.y,menuTimer/originalMenuTimer))};
}break;
}
}
game.SetDrawTarget(1U);
game.SetLayerOffset(1U,oldLayerPos+game.SCREEN_FRAME.pos);
Draw(game,currentMenu,oldLayerPos+game.SCREEN_FRAME.pos);
game.SetDrawTarget(nullptr);
game.SetLayerTint(0U,WHITE);
game.SetLayerOffset(0U,newLayerPos+game.SCREEN_FRAME.pos);
Draw(game,nextMenu,newLayerPos+game.SCREEN_FRAME.pos);
}
game.SetDrawTarget(nullptr);
}
void Menu::Draw(HamsterGame&game,const MenuType menu,const vi2d pos){
game.Clear(BLANK);
switch(menu){
case TITLE_SCREEN:{
game.FillRectDecal(pos,game.SCREEN_FRAME.size,{111,150,255});
game.DrawDecal(pos,game.GetGFX("welcometo.png").Decal());
if(menuTransitionRefreshTimer>0.f)game.DrawDecal(pos,game.GetGFX("hamsterplanet1.png").Decal());
if(fmod(game.GetRuntime(),2.f)<1.f)game.DrawDecal(pos,game.GetGFX("hamsterplanet2.png").Decal());
else game.DrawDecal(pos,game.GetGFX("hamsterplanet3.png").Decal());
game.border.Draw();
}break;
case MAIN_MENU:{
game.FillRectDecal(pos,game.SCREEN_FRAME.size,{});
game.DrawRotatedDecal(pos,game.GetGFX("button.png").Decal(),0.f,game.GetGFX("button.png").Sprite()->Size()/2);
game.border.Draw();
}break;
case GAMEPLAY:{
game.DrawGame();
}break;
case GAMEPLAY_RESULTS:{
game.DrawGame();
}break;
}
}

@ -0,0 +1,82 @@
#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Portions of this software are copyright © 2024 The FreeType
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
*/
#pragma endregion
#pragma once
#include "olcPixelGameEngine.h"
class HamsterGame;
class Menu{
enum MenuType{
INITIALIZE,
TITLE_SCREEN,
MAIN_MENU,
GRAND_PRIX,
SINGLE_RACE,
OPTIONS,
GAMEPLAY,
GAMEPLAY_RESULTS,
AFTER_RACE_MENU,
PAUSE,
};
enum TransitionType{
SHIFT_LEFT,
SHIFT_RIGHT,
SHIFT_UP,
SHIFT_DOWN,
FADE_OUT,
};
enum MenuState{
NORMAL,
TRANSITIONING,
};
MenuState currentState{NORMAL};
TransitionType currentTransition{FADE_OUT};
MenuType currentMenu{INITIALIZE};
MenuType nextMenu{TITLE_SCREEN};
float menuTimer{};
const float MENU_TRANSITION_REFRESH_RATE{0.15f};
float menuTransitionRefreshTimer{MENU_TRANSITION_REFRESH_RATE};
float originalMenuTimer{};
vi2d oldLayerPos{};
vi2d newLayerPos{};
void Transition(const TransitionType type,const MenuType gotoMenu,const float transitionTime);
void Draw(HamsterGame&game,const MenuType menu,const vi2d pos);
void DrawTransition(HamsterGame&game);
public:
void UpdateAndDraw(HamsterGame&game,const float fElapsedTime);
};

@ -154,7 +154,6 @@ Stage 11: Boss Battle #3 Alternate - nene
Stage 12: Boss Battle #5 V2 - nene Stage 12: Boss Battle #5 V2 - nene
Live Placement Tracking
Pulsating Animation based on terrain walking across (30 min) Pulsating Animation based on terrain walking across (30 min)
Sound Effects (1 hour) Sound Effects (1 hour)
@ -180,7 +179,7 @@ Menu Select/Back Sound
========================== ==========================
Stages VI - XII: 7 Stages (1 hour each, 7 hours total) Stages VI - XII: 7 Stages (1 hour each, 7 hours total)
Menu Navigations (2 hours) Menu Navigations (3 hours)
Grand Prix Management (1 hour) Grand Prix Management (1 hour)
Unlocks (1 hour) Unlocks (1 hour)
Leaderboard Management (1 hour) Leaderboard Management (1 hour)

Loading…
Cancel
Save