<commit_before>#include "game_system.h"

#include "texture_manager.h"

namespace vot
{
    GameSystem *GameSystem::s_main = nullptr;

    void GameSystem::main(GameSystem *main)
    {
        s_main = main;
    }
    GameSystem *GameSystem::main()
    {
        return s_main;
    }

    GameSystem::GameSystem(sf::RenderWindow &window) :
        _window(window)
    {
        auto size = static_cast<int>(sf::Keyboard::KeyCount);
        for (auto i = 0; i < size; i++)
        {
            _keys_pressed[i] = 0u;
            _keys_released[i] = 0u;
        }
    }

    void GameSystem::init()
    {
        auto player = new vot::Player(*TextureManager::texture("player"));
        player->hitbox().radius(5.0f);
        player->location(sf::Vector2f(0.0f, 100.0f));

        this->player(player);
    
        auto window_size = _window.getSize();
        _camera.setSize(window_size.x, window_size.y);

        _background.speed(0.1f);
        _background.create();
        _background2.speed(0.05f);
        _background2.create();
        _background3.speed(0.025f);
        _background3.create();
    
        create_default_bullets();
        create_default_enemieS();

        _hud.create();
        _world_hud.create();
    }

    sf::RenderWindow &GameSystem::window() const
    {
        return _window;
    }

    void GameSystem::update(float dt)
    {
        _update_counter++;
        if (_player == NULL)
        {
            return;
        }

        auto enemies = _enemy_manager.enemies();
        for (auto i = 0u; i < enemies->size(); i++)
        {
            auto enemy = (*enemies)[i].get();
            if (enemy != nullptr)
            {
                enemy->update(dt);
            }
        }

        auto bullets = _bullet_manager.bullets();
        for (auto i = 0u; i < bullets->size(); i++)
        {
            auto bullet = (*bullets)[i].get();
            if (bullet != nullptr)
            {
                bullet->update(dt);
                
                auto group = bullet->group();
                // Player bullet
                if (group == Bullet::PLAYER)
                {
                    // Search for enemies
                    for (auto i = 0u; i < enemies->size(); i++)
                    {
                        auto enemy = (*enemies)[i].get();
                        if (enemy != nullptr && enemy->hitbox().intersects(bullet->hitbox()))
                        {
                            enemy->take_damage(bullet->damage());

                            if (enemy->is_dead())
                            {
                                if (enemy == _player->target())
                                {
                                    _player->target(nullptr);
                                }
                                _enemy_manager.remove_enemy(enemy);
                            }

                            _bullet_manager.remove_bullet(bullet);
                        }
                    }
                }
                // Enemy bullet
                else if (group == Bullet::ENEMY && 
                        _player->hitbox().intersects(bullet->hitbox()))
                {
                    _player->take_damage(bullet->damage());
                    _bullet_manager.remove_bullet(bullet);
                } 
                // Remove dead (old) bullets.
                else if (bullet->dead())
                {
                    _bullet_manager.remove_bullet(bullet);
                }
            }
        }

        _player->update(dt);
        _camera.setCenter(_player->location());
        _camera.setRotation(_player->rotation());

        _background.update(dt);
        _background2.update(dt);
        _background3.update(dt);

        _hud.update(dt); 
        _world_hud.update(dt);
    }
    void GameSystem::draw(sf::RenderTarget &target, sf::RenderStates states) const
    {
        target.setView(_camera);

        target.draw(_background3, states);
        target.draw(_background2, states);
        target.draw(_background, states);

        target.draw(_bullet_manager, states);
        target.draw(_enemy_manager, states);
        if (_player != nullptr)
        {
            target.draw(*_player.get(), states);
        }

        target.draw(_world_hud);
        
        target.setView(target.getDefaultView());
        target.draw(_hud);<commit_msg>- Reordered player to be updated before bullets, so that player spawned bullets don't appear incorrectly.<commit_after>#include "game_system.h"

#include "texture_manager.h"

namespace vot
{
    GameSystem *GameSystem::s_main = nullptr;

    void GameSystem::main(GameSystem *main)
    {
        s_main = main;
    }
    GameSystem *GameSystem::main()
    {
        return s_main;
    }

    GameSystem::GameSystem(sf::RenderWindow &window) :
        _window(window)
    {
        auto size = static_cast<int>(sf::Keyboard::KeyCount);
        for (auto i = 0; i < size; i++)
        {
            _keys_pressed[i] = 0u;
            _keys_released[i] = 0u;
        }
    }

    void GameSystem::init()
    {
        auto player = new vot::Player(*TextureManager::texture("player"));
        player->hitbox().radius(5.0f);
        player->location(sf::Vector2f(0.0f, 100.0f));

        this->player(player);
    
        auto window_size = _window.getSize();
        _camera.setSize(window_size.x, window_size.y);

        _background.speed(0.1f);
        _background.create();
        _background2.speed(0.05f);
        _background2.create();
        _background3.speed(0.025f);
        _background3.create();
    
        create_default_bullets();
        create_default_enemieS();

        _hud.create();
        _world_hud.create();
    }

    sf::RenderWindow &GameSystem::window() const
    {
        return _window;
    }

    void GameSystem::update(float dt)
    {
        _update_counter++;
        if (_player == NULL)
        {
            return;
        }

        auto enemies = _enemy_manager.enemies();
        for (auto i = 0u; i < enemies->size(); i++)
        {
            auto enemy = (*enemies)[i].get();
            if (enemy != nullptr)
            {
                enemy->update(dt);
            }
        }
        
        _player->update(dt);
        _camera.setCenter(_player->location());
        _camera.setRotation(_player->rotation());


        auto bullets = _bullet_manager.bullets();
        for (auto i = 0u; i < bullets->size(); i++)
        {
            auto bullet = (*bullets)[i].get();
            if (bullet != nullptr)
            {
                bullet->update(dt);
                
                auto group = bullet->group();
                // Player bullet
                if (group == Bullet::PLAYER)
                {
                    // Search for enemies
                    for (auto i = 0u; i < enemies->size(); i++)
                    {
                        auto enemy = (*enemies)[i].get();
                        if (enemy != nullptr && enemy->hitbox().intersects(bullet->hitbox()))
                        {
                            enemy->take_damage(bullet->damage());

                            if (enemy->is_dead())
                            {
                                if (enemy == _player->target())
                                {
                                    _player->target(nullptr);
                                }
                                _enemy_manager.remove_enemy(enemy);
                            }

                            _bullet_manager.remove_bullet(bullet);
                        }
                    }
                }
                // Enemy bullet
                else if (group == Bullet::ENEMY && 
                        _player->hitbox().intersects(bullet->hitbox()))
                {
                    _player->take_damage(bullet->damage());
                    _bullet_manager.remove_bullet(bullet);
                } 
                // Remove dead (old) bullets.
                else if (bullet->dead())
                {
                    _bullet_manager.remove_bullet(bullet);
                }
            }
        }

        _background.update(dt);
        _background2.update(dt);
        _background3.update(dt);

        _hud.update(dt); 
        _world_hud.update(dt);
    }
    void GameSystem::draw(sf::RenderTarget &target, sf::RenderStates states) const
    {
        target.setView(_camera);

        target.draw(_background3, states);
        target.draw(_background2, states);
        target.draw(_background, states);

        target.draw(_bullet_manager, states);
        target.draw(_enemy_manager, states);
        if (_player != nullptr)
        {
            target.draw(*_player.get(), states);
        }

        target.draw(_world_hud);
        
        target.setView(target.getDefaultView());
        target.draw(_hud);
{'fraction_non_alphanumeric': 0.12259588347767406, 'fraction_numerical': 0.007198290406028568, 'mean_word_length': 1.3344709897610922, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4398371', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2348, 'n_words': 396}