@@ -1,5 +1,4 @@
# include < imgui . h >
# include "pch.h"
# include " pch.h "
# include "WorldLevel.h"
# include <algorithm>
@@ -42,9 +41,10 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
m_GridManager . GetTileAtIndex ( 0 , y ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > STONE ) ;
m_GridManager . GetTileAtIndex ( WORLD_WIDTH - 1 , y ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > STONE ) ;
}
Building * fuelBuilding = new Building { " buildings/fuelStation.png " , Vector2f { - 700 , - 52 } , Rectf { 0 , 0 , 50 , 50 } , TextureManager : : GetInstance ( ) } ;
fuelBuilding - > SetOnEnterHitbox ( [ ] ( ) {
Building * fuelBuilding = new Building { " buildings/fuelStation.png " , Vector2f { - 700 , - 52 } , Rectf { 0 , 0 , 50 , 50 } , TextureManager : : GetInstance ( ) } ;
fuelBuilding - > SetOnEnterHitbox ( [ ] ( )
{
ScreenManager : : GetInstance ( ) - > OpenScreen ( ScreenManager : : m_FuelScreen ) ;
} ) ;
m_Buildings . push_back ( fuelBuilding ) ;
@@ -53,31 +53,32 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
m_GridManager . GetTileAtWorldPos ( Vector2f { - 650 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 600 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_RIGHT ) ;
Building * mineralBuilding = new Building { " buildings/mineralStation.png " , Vector2f { - 350 , - 52 } , Rectf { 0 , 0 , 100 , 100 } , TextureManager : : GetInstance ( ) } ;
mineralBuilding - > SetOnEnterHitbox ( [ ] ( ) {
Building * mineralBuilding = new Building { " buildings/mineralStation.png " , Vector2f { - 350 , - 52 } , Rectf { 0 , 0 , 100 , 100 } , TextureManager : : GetInstance ( ) } ;
mineralBuilding - > SetOnEnterHitbox ( [ ] ( )
{
ScreenManager : : GetInstance ( ) - > OpenScreen ( ScreenManager : : m_SellScreen ) ;
} ) ;
m_Buildings . emplace_back ( mineralBuilding ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 400 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_LEFT ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 350 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 300 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 250 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 200 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 150 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_RIGHT ) ;
m_Buildings . emplace_back ( new Building { " buildings/junkStation.png " , Vector2f { 250 , - 52 } , Rectf { 0 , 0 , 100 , 100 } , TextureManager : : GetInstance ( ) } ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 200 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_LEFT ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 250 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 300 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 350 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 400 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 450 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_RIGHT ) ;
m_Buildings . emplace_back ( new Building { " buildings/repairStation.png " , Vector2f { 700 , - 52 } , Rectf { 0 , 0 , 100 , 100 } , TextureManager : : GetInstance ( ) } ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 650 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_LEFT ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 700 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 750 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 800 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_RIGHT ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 400 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_LEFT ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 350 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 300 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 250 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 200 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { - 150 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_RIGHT ) ;
m_Buildings . emplace_back ( new Building { " buildings/junkStation.png " , Vector2f { 250 , - 52 } , Rectf { 0 , 0 , 100 , 100 } , TextureManager : : GetInstance ( ) } ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 200 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_LEFT ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 250 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 300 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 350 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 400 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 450 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_RIGHT ) ;
m_Buildings . emplace_back ( new Building { " buildings/repairStation.png " , Vector2f { 700 , - 52 } , Rectf { 0 , 0 , 100 , 100 } , TextureManager : : GetInstance ( ) } ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 650 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_LEFT ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 700 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 750 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_MIDDLE ) ;
m_GridManager . GetTileAtWorldPos ( Vector2f { 800 , - 50 } ) - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > HARD_RIGHT ) ;
m_TopCover = TextureManager : : GetInstance ( ) - > GetTexture ( " topBackground.png " ) ;
@@ -85,8 +86,8 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
m_MainScreen = new MainScreen ( TextureManager : : GetInstance ( ) ) ;
GameManager : : GetInstance ( ) . SetMainScreen ( m_MainScreen ) ;
m_Sun = new OrbitingObject ( Vector2f { 0 , - 1000 } , 1200 , 0.1f , TextureManager : : GetInstance ( ) - > GetTexture ( " sun.png " ) ) ;
m_Moon = new OrbitingObject ( Vector2f { 0 , - 1000 } , 1200 , 0.1f , TextureManager : : GetInstance ( ) - > GetTexture ( " moon.png " ) , M_PI ) ;
m_Sun = new OrbitingObject ( Vector2f { 0 , - 1000 } , 1200 , 0.1f , TextureManager : : GetInstance ( ) - > GetTexture ( " sun.png " ) ) ;
m_Moon = new OrbitingObject ( Vector2f { 0 , - 1000 } , 1200 , 0.1f , TextureManager : : GetInstance ( ) - > GetTexture ( " moon.png " ) , ( float ) M_PI ) ;
GameManager : : GetInstance ( ) . SetFuel ( 100 ) ;
GameManager : : GetInstance ( ) . SetHullIntegrity ( 100 ) ;
@@ -94,7 +95,7 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
GameManager : : GetInstance ( ) . SetPlayer ( & m_Player ) ;
m_BackgroundMusic = new SoundStream ( " sound/bgm.wav " ) ;
m_BackgroundMusic - > Play ( - 1 ) ;
m_BackgroundMusic - > Play ( true ) ;
GameManager : : GetInstance ( ) . SetMoney ( 100 ) ;
@@ -110,9 +111,9 @@ WorldLevel::~WorldLevel() {
for ( Building * building : m_Buildings ) {
delete building ;
}
GameManager : : DestroyInstance ( ) ;
}
void WorldLevel : : Update ( float elapsedSec ) {
@@ -132,7 +133,7 @@ void WorldLevel::Update(float elapsedSec) {
m_Sun - > Update ( elapsedSec ) ;
m_Moon - > Update ( elapsedSec ) ;
for ( int x { 0 } ; x < WORLD_WIDTH ; + + x ) {
for ( int y { 0 } ; y < WORLD_HEIGHT ; + + y ) {
m_GridManager . GetTileAtIndex ( x , y ) - > Update ( m_pCamera ) ;
@@ -147,37 +148,35 @@ void WorldLevel::Update(float elapsedSec) {
building - > Update ( elapsedSec , m_Player . GetCollisionRect ( ) . getRectf ( ) ) ;
// building.IsPlayerInHitbox(Rectf{m_Player.GetCollisionRect().pos, m_Player.GetCollisionRect().size});
}
if ( m_pSelectedTile ! = nullptr ) {
if ( utils : : isMouseDown ( SDL_BUTTON_LEFT ) ) {
m_pSelectedTile - > SetTileType ( GroundTileTypeManager : : GetInstance ( ) - > AIR ) ;
}
}
if ( ! m_ScreenManager - > IsScreenOpen ( ) ) {
if ( ! m_ScreenManager - > IsScreenOpen ( ) ) {
m_Player . Update ( elapsedSec , * this ) ;
}
//Move the camera when the player gets to the edge
//Also lock the camera from going below -800 and above -100
if ( m_FollowPlayer ) {
Vector2f play erPos = m_Player . GetPosition ( ) ;
Vector2f newCam era Pos = m_p Camera- > GetPosition ( ) ;
if ( play erPos. x < newCam era Pos. x + 50 ) {
newCameraPos . x = playerPos . x - 50 ;
}
if ( play erPos. x > newCam era Pos. x + m_Viewport . width - 100 ) {
newCameraPos . x = playerPos . x - m_Viewport . width + 100 ;
}
if ( play erPos. y < newCam era Pos. y + 100 ) {
newCameraPos . y = playerPos . y - 100 ;
}
if ( play erPos. y > newCam era Pos. y + m_Viewport . height - 150 ) {
newCameraPos . y = playerPos . y - m_Viewport . height + 150 ;
}
new CameraPos . x = utils : : clamp ( newCameraPos . x , - 800 , - 100 ) ;
m_pCamera - > SetPosition ( newCameraPos ) ;
}
Vector2f playerPos = m_Player . GetPosition ( ) ;
Vector2f newCam era Pos = m_pCamera - > GetPosition ( ) ;
if ( play erPos. x < new CameraPos . x + 50 ) {
newCam era Pos. x = play erPos. x - 50 ;
}
if ( playerPos . x > newCameraPos . x + m_Viewport . width - 100 ) {
newCam era Pos. x = play erPos. x - m_Viewport . width + 100 ;
}
if ( playerPos . y < newCameraPos . y + 100 ) {
newCam era Pos. y = play erPos. y - 100 ;
}
if ( playerPos . y > newCameraPos . y + m_Viewport . height - 150 ) {
newCam era Pos. y = play erPos. y - m_Viewport . height + 150 ;
}
newCameraPos . x = utils : : clamp ( newCameraPos . x , - 800 , - 100 ) ;
m_p Camera- > SetPosition ( newCameraPos ) ;
Screen * screen = m_ScreenManager - > GetCurrentScreen ( ) ;
if ( screen ! = nullptr ) {
@@ -191,7 +190,6 @@ void WorldLevel::Update(float elapsedSec) {
// m_MainScreen->SetHullMeterValue(GameManager::GetInstance().GetHullIntegrity());
m_MainScreen - > Update ( elapsedSec ) ;
//Vector2f playerPos = m_player.GetPosition();
//Vector2f newCameraPos = playerPos;
@@ -201,14 +199,14 @@ void WorldLevel::Update(float elapsedSec) {
//m_pCamera->SetPosition(Vector2f{playerPos.x - m_viewport.width / 2, playerPos.y - m_viewport.height / 2});
}
void WorldLevel : : Draw ( ) const {
m_pCamera - > BeginRendering ( ) ;
m_Sun - > Draw ( ) ;
m_Moon - > Draw ( ) ;
// m_topCover->Draw(Vector2f{-850,-70} );
m_TopCover - > Draw ( Rectf { - 850 , - 70 , 850 , - 70 + 32 } , Rectf { 0 , 0 , WORLD_WIDTH * 50 , 32 } ) ;
m_TopCover - > Draw ( Rectf { - 850 , - 70 , 850 , - 70 + 32 } , Rectf { 0 , 0 , WORLD_WIDTH * 50 , 32 } ) ;
for ( Collision : : CollisionRect rect : m_Rects ) {
// utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
@@ -227,10 +225,10 @@ void WorldLevel::Draw() const {
// utils::FillEllipse(-5, -5, 5, 5);
// if (m_pSelectedTile != nullptr) {
// m_pSelectedTile->Draw();
// m_pSelectedTile->Draw();
// }
for ( Building * building : m_Buildings ) {
for ( Building * building : m_Buildings ) {
building - > Draw ( ) ;
}
@@ -257,127 +255,7 @@ void WorldLevel::MouseMove(const Vector2f& mousePos) {
m_MousePos = mousePos ;
}
void WorldLevel : : ProcessImGui ( ) {
ImGui : : Begin ( " Selected Tile " ) ;
std : : string tileType = " None " ;
if ( m_pSelectedTile ! = nullptr ) {
switch ( m_pSelectedTile - > GetTileType ( ) - > GetType ( ) ) {
case GroundTileTypes : : Air :
tileType = " Air " ;
break ;
case GroundTileTypes : : Dirt :
tileType = " Dirt " ;
break ;
case GroundTileTypes : : Iron :
tileType = " Iron " ;
break ;
case GroundTileTypes : : Hard :
tileType = " Hard " ;
break ;
case GroundTileTypes : : Stone :
tileType = " Stone " ;
break ;
default :
tileType = " Unknown " ;
break ;
}
}
ImGui : : Text ( " Selected Tile: %s " , tileType . c_str ( ) ) ;
ImGui : : End ( ) ;
if ( ImGui : : BeginMainMenuBar ( ) ) {
if ( ImGui : : BeginMenu ( " Properties " ) ) {
if ( ImGui : : MenuItem ( " TextureManager Info " ) ) {
m_ShowTextureManagerWindow = ! m_ShowTextureManagerWindow ;
}
if ( ImGui : : MenuItem ( " Camera Info " ) ) {
m_ShowCameraWindow = ! m_ShowCameraWindow ;
}
if ( ImGui : : MenuItem ( " Player Info " ) ) {
m_ShowPlayerInfo = ! m_ShowPlayerInfo ;
}
if ( ImGui : : MenuItem ( " Game Manager Info " ) ) {
m_ShowGameManagerInfo = ! m_ShowGameManagerInfo ;
}
ImGui : : EndMenu ( ) ;
}
if ( ImGui : : BeginMenu ( " Screens " ) ) {
if ( ImGui : : MenuItem ( " Open Fuel screen " ) ) {
ScreenManager : : GetInstance ( ) - > OpenScreen ( ScreenManager : : m_FuelScreen ) ;
}
if ( ImGui : : MenuItem ( " Open Sell screen " ) ) {
ScreenManager : : GetInstance ( ) - > OpenScreen ( ScreenManager : : m_SellScreen ) ;
}
if ( ImGui : : MenuItem ( " Close Screen " ) ) {
ScreenManager : : GetInstance ( ) - > CloseScreen ( ) ;
}
ImGui : : EndMenu ( ) ;
}
const Vector2f screenPos = utils : : GetMousePos ( ) ;
const std : : string mousePos = " Mouse Pos: ( " + std : : to_string ( screenPos . x ) + " , " + std : : to_string ( screenPos . y ) + " ) " ;
if ( ImGui : : BeginMenu ( mousePos . c_str ( ) ) ) {
ImGui : : EndMenu ( ) ;
}
if ( ImGui : : BeginMenu ( std : : to_string ( m_Fps ) . c_str ( ) ) ) {
ImGui : : EndMenu ( ) ;
}
ImGui : : EndMainMenuBar ( ) ;
}
if ( m_ShowTextureManagerWindow ) {
ImGui : : Begin ( " Texture Manager " , & m_ShowTextureManagerWindow , ImGuiWindowFlags_AlwaysAutoResize ) ;
ImGui : : Text ( " Texture loading Count: " ) ;
ImGui : : SameLine ( ) ;
ImGui : : TextColored ( ImVec4 ( 1.0f , 0.0f , 1.0f , 1.0f ) , std : : to_string ( Texture : : m_TextureCounter ) . c_str ( ) ) ;
ImGui : : End ( ) ;
}
if ( m_ShowCameraWindow ) {
ImGui : : Begin ( " Camera " , & m_ShowCameraWindow , ImGuiWindowFlags_AlwaysAutoResize ) ;
ImGui : : Text ( " Camera Position: (%f, %f) " , m_pCamera - > GetPosition ( ) . x , m_pCamera - > GetPosition ( ) . y ) ;
ImGui : : Text ( " Camera Scale: %f " , m_pCamera - > GetScale ( ) ) ;
ImGui : : Text ( " Is Right Mouse Down: %s " , utils : : isMouseDown ( 0 ) ? " true " : " false " ) ;
if ( ImGui : : Button ( " Reset Camera " ) ) {
m_pCamera - > SetPosition ( Vector2f { - m_Viewport . width / 2 , - m_Viewport . height / 2 } ) ;
m_pCamera - > SetScale ( 1.0f ) ;
}
ImGui : : Checkbox ( " Follow Player " , & m_FollowPlayer ) ;
ImGui : : End ( ) ;
}
if ( m_ShowPlayerInfo ) {
ImGui : : Begin ( " Player Info " , & m_ShowPlayerInfo , ImGuiWindowFlags_AlwaysAutoResize ) ;
ImGui : : Text ( " Player Position: (%f, %f) " , m_Player . GetPosition ( ) . x , m_Player . GetPosition ( ) . y ) ;
ImGui : : Text ( " Player Velocity: (%f, %f) " , m_Player . GetVelocity ( ) . x , m_Player . GetVelocity ( ) . y ) ;
ImGui : : End ( ) ;
m_Player . ProcessImGui ( ) ;
}
if ( m_ShowGameManagerInfo ) {
ImGui : : Begin ( " Game Manager " , & m_ShowGameManagerInfo , ImGuiWindowFlags_AlwaysAutoResize ) ;
ImGui : : Text ( " Fuel: %f " , GameManager : : GetInstance ( ) . GetFuel ( ) ) ;
ImGui : : Text ( " Hull Integrity: %d " , GameManager : : GetInstance ( ) . GetHullIntegrity ( ) ) ;
ImGui : : Text ( " Score: %d " , GameManager : : GetInstance ( ) . GetScore ( ) ) ;
ImGui : : Text ( " Money: %d " , GameManager : : GetInstance ( ) . GetMoney ( ) ) ;
ImGui : : Separator ( ) ;
if ( ImGui : : Button ( " Add 1$ " ) ) {
GameManager : : GetInstance ( ) . IncreaseMoney ( 1 ) ;
}
if ( ImGui : : Button ( " Add 10$ " ) ) {
GameManager : : GetInstance ( ) . IncreaseMoney ( 10 ) ;
}
ImGui : : SameLine ( ) ;
if ( ImGui : : Button ( " Add 100$ " ) ) {
GameManager : : GetInstance ( ) . IncreaseMoney ( 100 ) ;
}
ImGui : : End ( ) ;
}
}
WorldGridManager & WorldLevel : : GetGridManager ( ) {
return m_GridManager ;
}