IMX6U-Game/src/Apps/LightGame/src/engine/LevelRenderer.cpp

87 lines
2.8 KiB
C++

#include "LevelRenderer.h"
#include "DrawContext.h"
#include "BitmapFont.h"
namespace LightGame
{
void LevelRenderer::draw(Core::DrawContext& ctx, const Level& level, const Camera2D& camera,
const LightEffectSystem& light_system, uint16_t light_level)
{
const RenderData::Tilemap& tilemap = level.get_tilemap();
if (tilemap.tiles != nullptr && tilemap.atlas != nullptr)
{
ctx.draw_tilemap(tilemap,
-camera.get_x(), -camera.get_y(),
camera.get_screen_width(), camera.get_screen_height(),
camera.get_x(), camera.get_y());
}
const auto& objects = level.get_all_objects();
for (size_t i = 0; i < objects.size(); ++i)
{
const GameObject& obj = objects[i];
if (!obj.active)
{
continue;
}
const RenderData::Sprite* sprite = obj.sprite;
if (!sprite)
{
continue;
}
const int32_t screen_x = obj.position.x - camera.get_x();
const int32_t screen_y = obj.position.y - camera.get_y();
if (screen_x + sprite->width < 0 || screen_x > camera.get_screen_width() ||
screen_y + sprite->height < 0 || screen_y > camera.get_screen_height())
{
continue;
}
const bool is_light_obj = (obj.type == GameObjectType::LightPlatform ||
obj.type == GameObjectType::ShadowPlatform ||
obj.type == GameObjectType::Door);
if (is_light_obj && !light_system.is_platform_active(obj, light_level))
{
continue;
}
ctx.draw_sprite_ex(screen_x, screen_y, *sprite, 1, obj.flip_h, false);
}
}
void LevelRenderer::draw_debug(Core::DrawContext& ctx, const Level& level,
const Camera2D& camera, const RenderData::BitmapFont& font)
{
const auto& objects = level.get_all_objects();
for (size_t i = 0; i < objects.size(); ++i)
{
const GameObject& obj = objects[i];
if (!obj.active)
{
continue;
}
const RenderData::BoundingBox2D world = obj.get_world_collider();
const int32_t sx = world.min.x - camera.get_x();
const int32_t sy = world.min.y - camera.get_y();
const int32_t w = world.max.x - world.min.x;
const int32_t h = world.max.y - world.min.y;
RenderData::Color color = RenderData::Color::Green();
if (obj.type == GameObjectType::Hazard) color = RenderData::Color::Red();
if (obj.type == GameObjectType::LightPlatform) color = RenderData::Color(255, 255, 0, 255);
if (obj.type == GameObjectType::ShadowPlatform) color = RenderData::Color(128, 0, 255, 255);
if (obj.type == GameObjectType::Door) color = RenderData::Color(0, 255, 255, 255);
ctx.draw_line(Math::Vector2Int(sx, sy), Math::Vector2Int(sx + w, sy), color);
ctx.draw_line(Math::Vector2Int(sx + w, sy), Math::Vector2Int(sx + w, sy + h), color);
ctx.draw_line(Math::Vector2Int(sx + w, sy + h), Math::Vector2Int(sx, sy + h), color);
ctx.draw_line(Math::Vector2Int(sx, sy + h), Math::Vector2Int(sx, sy), color);
}
}
}