kadeska / GameCollection

This is where I will create applications with C++ and openGL. My goal is to create a 2D game. I want my game to end up as a representation of my favorite style of video game.
1 stars 0 forks source link

ToDo list #7

Open kadeska opened 2 months ago

kadeska commented 2 months ago
kadeska commented 2 months ago

Fixed game not progressing to next level. --> bdc81a9

kadeska commented 2 months ago
kadeska commented 2 months ago

Get currentDir

include

std::string getCurrentWorkingDirectory() { return std::filesystem::current_path().string(); }

std::string currentDir = getCurrentWorkingDirectory(); std::string levelPath = currentDir + "/assets/levels/level1.json";

loadLevel(levelPath);

kadeska commented 1 month ago

Things to consider to improve code.

Consider using a configuration file or a constants header file to store file paths, rather than hardcoding them. This will make your code cleaner and easier to maintain.

Instead of loading shaders in the LoadShaders function, you might create a ShaderLoader class to encapsulate this functionality. This would allow for cleaner code and better separation of concerns.

As you noted, abstracting level management into its own class would be beneficial. Consider creating a LevelLoader class that handles level loading, saving, and storage in a more organized way.

The collision detection code is straightforward, but consider encapsulating it in a CollisionManager class to handle different types of collisions and make the main game loop more readable.

Define constants and enums (like player size, ball radius, etc.) in a dedicated file or section to avoid magic numbers throughout your code. This enhances readability and maintainability.

Your reset functions could be combined into one method that resets the game state based on the current level, reducing code duplication.

You might want to implement a state machine for handling different game states (e.g., main menu, playing, paused, game over) for better structure.

Consider using smart pointers (like std::unique_ptr) for dynamic memory management to avoid manual deletion and potential memory leaks.

While you have comments, ensure they are clear and helpful. Documenting complex functions and class behaviors will aid future developers (and your future self).

kadeska commented 1 month ago

For main.cpp

Here's a revised version of your code that enhances readability and optimization, particularly in the work() function. I've reorganized the code, introduced better variable names, and encapsulated certain logic to improve clarity. Let's look at the changes:

include

include

include

include

include "program.h"

// Function declarations float executeProgram(); float algorithm_A(int input); void runTests(); int parseToInt(const std::string& str);

// Global flag for loop control bool loop = true;

// Main function int main() { std::cout << "Hello world!" << '\n'; executeProgram(); return 0; }

// Main execution function float executeProgram() { float result;

while (loop)
{
    std::string input;
    std::cout << "Please enter a number 0 through 9, or a command: " << '\n';
    std::cin >> input;

    // Command handling
    if (input == "exit" || input == "stop" || input == "abort")
    {
        return 0;
    }
    else if (input == "test")
    {
        runTests();
        continue; // Skip to the next iteration
    }
    else if (input == "engine")
    {
        std::cout << "Starting Engine... \n";
        create_Window();
        continue; // Skip to the next iteration
    }
    else if (input == "breakout")
    {
        std::cout << "Starting Breakout Game!...\n";
        startProgram();
        continue; // Skip to the next iteration
    }

    // Parse input as an integer
    int number = parseToInt(input);
    if (number >= 0 && number <= 9)
    {
        std::cout << "You entered: " << number << '\n';
        result = algorithm_A(number);
        std::cout << "OUTPUT: " << result << '\n';
    }
    else
    {
        std::cout << "ERROR! - Please enter a number 0 through 9" << '\n';
    }
}

return result;

}

// Algorithm function float algorithm_A(int input) { float x = static_cast(input); x += (x * (x / 2)); return x; }

// Parse string to int int parseToInt(const std::string& str) { try { return std::stoi(str); } catch (const std::invalid_argument&) { return -1; // Return -1 on failure } catch (const std::out_of_range&) { return -1; // Return -1 for out-of-range errors } }

// Test function void runTests() { std::cout << std::numeric_limits::is_iec559 << '\n'; std::cout << std::numeric_limits::is_iec559 << '\n'; std::cout << std::numeric_limits::is_iec559 << '\n'; }

Key Improvements:

  1. Function Names:

Renamed work() to executeProgram() for better clarity.

  1. Input Handling:

Simplified command handling using else if to make it clear that these are mutually exclusive paths.

  1. Loop Control:

Used continue statements to skip the rest of the loop when a command is executed, enhancing readability.

  1. Input Parsing:

Improved the parseToInt function to return -1 on failure instead of just catching the exception without returning a value.

  1. Comments and Structure:

Added comments for clarity and restructured some code blocks to improve flow.

  1. Use of const:

Added const to function parameters where applicable, to indicate they won’t be modified.

With these changes, the code should be easier to read and maintain while still fulfilling the same functionality. If you have any further specific requests or additional features you'd like to implement, let me know!