Select Git revision
GameWindow.cpp
-
Moritz Freund authoredMoritz Freund authored
GameWindow.cpp 3.79 KiB
#include "GameWindow.hpp"
#include "TilesheetManager.hpp"
GameWindow::GameWindow(const int32_t width, const int32_t height, const float_t scale) noexcept(false)
: SDLApplication("ISAAC is Back", width, height), m_Score(0), m_Scale(scale), m_level("level.h5"),
m_world(b2Vec2({0.0f, 0.0f})), m_tilesheetManager(m_Renderer, m_level.getSpritesheet(), m_Scale),
m_bulletManager(&m_world, m_tilesheetManager, m_level),
m_enemyManager(&m_world, m_bulletManager, m_tilesheetManager, m_level),
m_objectManager(m_level, &m_world, m_tilesheetManager),
m_player(&m_world, b2Vec2({static_cast<float>(width) / 2, static_cast<float>(height) / 2}),
b2Vec2({16.0f, 16.0f}), 1.0f, m_Scale, 1.0f, m_bulletManager, m_tilesheetManager,
m_level.getPlayer().getSprites(), m_level.getPlayer().getHealth(), 5),
m_gameContext(m_level, m_enemyManager, m_bulletManager, m_tilesheetManager, m_objectManager),
m_Camera(width, height, m_level.getRooms().at(2).getWidth() * m_level.getSpritesheet().getTileSize() * m_Scale,
m_level.getRooms().at(2).getHeight() * m_level.getSpritesheet().getTileSize() * m_Scale)
{
m_world.SetContactListener(&m_contactListener);
m_gameContext.initialize();
/*
m_boundingboxes.push_back(std::make_unique<StaticObject>(&m_world, b2Vec2({m_Width / 2.0f, 0.0f}),
b2Vec2({static_cast<float>(m_Width), 0.1f}), m_Scale, 1.0f,
m_level.getPlayer().getSprites()));
m_boundingboxes.push_back(std::make_unique<StaticObject>(
&m_world, b2Vec2({m_Width / 2.0f, static_cast<float>(m_Height)}), b2Vec2({static_cast<float>(m_Width), 0.1f}),
m_Scale, 1.0f, m_level.getPlayer().getSprites()));
m_boundingboxes.push_back(std::make_unique<StaticObject>(&m_world, b2Vec2({0.0f, m_Height / 2.0f}),
b2Vec2({0.1f, static_cast<float>(m_Height)}), m_Scale,
1.0f, m_level.getPlayer().getSprites()));
m_boundingboxes.push_back(std::make_unique<StaticObject>(
&m_world, b2Vec2({static_cast<float>(m_Width), m_Height / 2.0f}), b2Vec2({0.1f, static_cast<float>(m_Height)}),
m_Scale, 1.0f, m_level.getPlayer().getSprites()));
*/
}
GameWindow::~GameWindow()
{
m_boundingboxes.clear();
}
void GameWindow::draw()
{
// Background color
SDL_SetRenderDrawColor(m_Renderer, 61, 37, 59, 255);
SDL_RenderClear(m_Renderer);
m_tilesheetManager.renderBackground(m_gameContext.getCurrentRoom(), m_Camera);
m_player.render(m_tilesheetManager, m_Camera);
m_bulletManager.render(m_Camera);
m_enemyManager.render(m_Camera);
}
void GameWindow::update(float timeStep, int32_t velocityIterations, int32_t positionIterations)
{
m_player.handle_input();
m_enemyManager.randomEnemyShoot(getPlayerPosition());
m_world.Step(timeStep, velocityIterations, positionIterations);
m_bulletManager.removeObsoleteProjectiles(m_contactListener.playerBulletsToDestroy,
m_contactListener.enemyBulletsToDestroy);
m_enemyManager.update(m_contactListener.enemiesToDestroy);
m_contactListener.playerBulletsToDestroy.clear(); // clear list for next cycle
m_contactListener.enemyBulletsToDestroy.clear();
m_contactListener.enemiesToDestroy.clear();
b2Vec2 playerPos = m_player.getPosition();
m_Camera.updateDirect(static_cast<int>(playerPos.x), static_cast<int>(playerPos.y));
}
int32_t GameWindow::w() const
{
return m_Width;
}
int32_t GameWindow::h() const
{
return m_Height;
}
void GameWindow::handleEvent(SDL_Event &event)
{
}
b2Vec2 GameWindow::getPlayerPosition() const
{
return m_player.getPosition();
}