Add game framework and whitebox level

This commit is contained in:
2026-06-08 23:47:32 +08:00
parent 5525343656
commit e52cf94336
24 changed files with 778 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
#include "whitebox_scene.h"
#include "../core/game_config.h"
#include <frostbite2D/core/application.h>
#include <frostbite2D/graphics/camera.h>
#include <frostbite2D/graphics/renderer.h>
#include <algorithm>
namespace ns_game {
namespace {
void drawRect(const frostbite2D::Rect& rect, const frostbite2D::Color& color) {
frostbite2D::Renderer::get().drawQuad(rect, color);
}
} // namespace
void WhiteboxScene::onEnter() {
frostbite2D::Scene::onEnter();
level_ = CreateWhiteboxLevel();
player_ = frostbite2D::MakePtr<PlayerActor>();
player_->SetMovementWorld(&level_.collision);
AddChild(player_);
}
void WhiteboxScene::OnUpdate(float deltaTime) {
(void)deltaTime;
updateCamera();
}
void WhiteboxScene::Render() {
drawWhitebox();
frostbite2D::Scene::Render();
}
void WhiteboxScene::drawWhitebox() const {
const float worldWidth = level_.collision.levelRight - level_.collision.levelLeft;
drawRect(frostbite2D::Rect(level_.collision.levelLeft, 0.0f, worldWidth,
config::kVirtualHeight),
frostbite2D::Color(0.10f, 0.12f, 0.14f, 1.0f));
for (float x = level_.collision.levelLeft; x <= level_.collision.levelRight;
x += 80.0f) {
drawRect(frostbite2D::Rect(x, 0.0f, 1.0f, config::kVirtualHeight),
frostbite2D::Color(0.20f, 0.22f, 0.25f, 1.0f));
}
for (float y = 0.0f; y <= config::kVirtualHeight; y += 80.0f) {
drawRect(frostbite2D::Rect(level_.collision.levelLeft, y, worldWidth, 1.0f),
frostbite2D::Color(0.20f, 0.22f, 0.25f, 1.0f));
}
for (const auto& prop : level_.props) {
drawRect(prop.bounds, prop.color);
}
for (const auto& platform : level_.collision.platforms) {
drawRect(platform, frostbite2D::Color(0.62f, 0.68f, 0.73f, 1.0f));
drawRect(frostbite2D::Rect(platform.left(), platform.top(), platform.width(), 4.0f),
frostbite2D::Color(0.91f, 0.95f, 0.98f, 1.0f));
}
}
void WhiteboxScene::updateCamera() {
if (!player_) {
return;
}
auto* renderer = frostbite2D::Application::get().getRenderer();
if (!renderer || !renderer->getCamera()) {
return;
}
const auto playerPos = player_->GetTopLeftPosition();
const float targetX = std::clamp(playerPos.x - 420.0f,
level_.collision.levelLeft,
level_.collision.levelRight -
static_cast<float>(config::kVirtualWidth));
renderer->getCamera()->setPosition(frostbite2D::Vec2(targetX, 0.0f));
}
} // namespace ns_game
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include "../actor/player_actor.h"
#include "../data/level_definition.h"
#include <frostbite2D/scene/scene.h>
#include <frostbite2D/types/type_math.h>
namespace ns_game {
class WhiteboxScene : public frostbite2D::Scene {
public:
void onEnter() override;
void OnUpdate(float deltaTime) override;
void Render() override;
private:
void drawWhitebox() const;
void updateCamera();
LevelDefinition level_;
frostbite2D::Ptr<PlayerActor> player_;
};
} // namespace ns_game