Files
SunnyLand/src/game/scene/game_scene.cpp
T

92 lines
3.2 KiB
C++
Raw Normal View History

2025-05-27 17:23:07 +08:00
#include "game_scene.h"
#include "../../engine/core/context.h"
#include "../../engine/object/game_object.h"
#include "../../engine/component/transform_component.h"
#include "../../engine/component/sprite_component.h"
2025-05-31 09:40:40 +08:00
#include "../../engine/component/physics_component.h"
#include "../../engine/physics/physics_engine.h"
2025-05-28 18:36:04 +08:00
#include "../../engine/scene/level_loader.h"
#include "../../engine/input/input_manager.h"
#include "../../engine/render/camera.h"
2025-05-27 17:23:07 +08:00
#include <spdlog/spdlog.h>
#include <SDL3/SDL_rect.h>
namespace game::scene {
// 构造函数:调用基类构造函数
GameScene::GameScene(std::string name, engine::core::Context& context, engine::scene::SceneManager& scene_manager)
: Scene(name, context, scene_manager) {
spdlog::trace("GameScene 构造完成。");
}
void GameScene::init() {
2025-05-28 18:36:04 +08:00
// 加载关卡(level_loader通常加载完成后即可销毁,因此不存为成员变量)
engine::scene::LevelLoader level_loader;
level_loader.loadLevel("assets/maps/level1.tmj", *this);
2025-05-27 17:23:07 +08:00
// 创建 test_object
createTestObject();
Scene::init();
spdlog::trace("GameScene 初始化完成。");
}
void GameScene::update(float delta_time) {
Scene::update(delta_time);
}
void GameScene::render() {
Scene::render();
}
void GameScene::handleInput() {
Scene::handleInput();
2025-05-31 09:40:40 +08:00
TestObject();
2025-05-27 17:23:07 +08:00
}
void GameScene::clean() {
Scene::clean();
}
2025-05-28 18:36:04 +08:00
// --- 测试方法 ---
2025-05-27 17:23:07 +08:00
void GameScene::createTestObject() {
spdlog::trace("在 GameScene 中创建 test_object...");
auto test_object = std::make_unique<engine::object::GameObject>("test_object");
2025-05-31 09:40:40 +08:00
test_object_ = test_object.get();
2025-05-27 17:23:07 +08:00
// 添加组件
test_object->addComponent<engine::component::TransformComponent>(glm::vec2(100.0f, 100.0f));
test_object->addComponent<engine::component::SpriteComponent>("assets/textures/Props/big-crate.png", context_.getResourceManager());
2025-05-31 09:40:40 +08:00
test_object->addComponent<engine::component::PhysicsComponent>(&context_.getPhysicsEngine());
2025-05-27 17:23:07 +08:00
// 将创建好的 GameObject 添加到场景中 (一定要用std::move,否则传递的是左值)
addGameObject(std::move(test_object));
spdlog::trace("test_object 创建并添加到 GameScene 中。");
}
2025-05-28 18:36:04 +08:00
void GameScene::testCamera() {
auto& camera = context_.getCamera();
auto& input_manager = context_.getInputManager();
if (input_manager.isActionDown("move_up")) camera.move(glm::vec2(0, -1));
if (input_manager.isActionDown("move_down")) camera.move(glm::vec2(0, 1));
if (input_manager.isActionDown("move_left")) camera.move(glm::vec2(-1, 0));
if (input_manager.isActionDown("move_right")) camera.move(glm::vec2(1, 0));
}
2025-05-31 09:40:40 +08:00
void GameScene::TestObject()
{
if (!test_object_) return;
auto& input_manager = context_.getInputManager();
if (input_manager.isActionDown("move_left")) {
test_object_->getComponent<engine::component::TransformComponent>()->translate(glm::vec2(-1, 0));
}
if (input_manager.isActionDown("move_right")) {
test_object_->getComponent<engine::component::TransformComponent>()->translate(glm::vec2(1, 0));
}
if (input_manager.isActionPressed("jump")) {
test_object_->getComponent<engine::component::PhysicsComponent>()->setVelocity(glm::vec2(0, -400));
}
}
2025-05-27 17:23:07 +08:00
} // namespace game::scene