完成对象间碰撞解析

This commit is contained in:
Ziyu
2025-06-16 14:44:47 +08:00
parent a120565132
commit 2e19639f86
12 changed files with 296 additions and 61 deletions
+10 -34
View File
@@ -36,8 +36,12 @@ void GameScene::init() {
}
}
// 创建 test_object
createTestObject();
// 获取玩家对象
player_ = findGameObjectByName("player");
if (!player_) {
spdlog::error("未找到玩家对象");
return;
}
Scene::init();
spdlog::trace("GameScene 初始化完成。");
@@ -54,7 +58,7 @@ void GameScene::render() {
void GameScene::handleInput() {
Scene::handleInput();
TestObject();
TestPlayer();
}
void GameScene::clean() {
@@ -63,34 +67,6 @@ void GameScene::clean() {
// --- 测试方法 ---
void GameScene::createTestObject() {
spdlog::trace("在 GameScene 中创建 test_object...");
auto test_object = std::make_unique<engine::object::GameObject>("test_object");
test_object_ = test_object.get();
// 添加组件
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());
test_object->addComponent<engine::component::PhysicsComponent>(&context_.getPhysicsEngine());
test_object->addComponent<engine::component::ColliderComponent>(
std::make_unique<engine::physics::AABBCollider>(glm::vec2(32.0f, 32.0f))
);
// 将创建好的 GameObject 添加到场景中 (一定要用std::move,否则传递的是左值)
addGameObject(std::move(test_object));
// 添加第二个游戏对象(不受重力影响),用于测试碰撞
auto test_object2 = std::make_unique<engine::object::GameObject>("test_object2");
test_object2->addComponent<engine::component::TransformComponent>(glm::vec2(50.0f, 50.0f));
test_object2->addComponent<engine::component::SpriteComponent>("assets/textures/Props/big-crate.png", context_.getResourceManager());
test_object2->addComponent<engine::component::PhysicsComponent>(&context_.getPhysicsEngine(), false);
test_object2->addComponent<engine::component::ColliderComponent>(
std::make_unique<engine::physics::CircleCollider>(16.0f)
);
addGameObject(std::move(test_object2));
spdlog::trace("test_object 创建并添加到 GameScene 中。");
}
void GameScene::testCamera() {
auto& camera = context_.getCamera();
auto& input_manager = context_.getInputManager();
@@ -100,11 +76,11 @@ void GameScene::testCamera() {
if (input_manager.isActionDown("move_right")) camera.move(glm::vec2(1, 0));
}
void GameScene::TestObject()
void GameScene::TestPlayer()
{
if (!test_object_) return;
if (!player_) return;
auto& input_manager = context_.getInputManager();
auto* pc = test_object_->getComponent<engine::component::PhysicsComponent>();
auto* pc = player_->getComponent<engine::component::PhysicsComponent>();
if (!pc) return;
if (input_manager.isActionDown("move_left")) {
pc->velocity_.x = -100.0f;
+2 -3
View File
@@ -13,7 +13,7 @@ namespace game::scene {
* @brief 主要的游戏场景,包含玩家、敌人、关卡元素等。
*/
class GameScene final: public engine::scene::Scene {
engine::object::GameObject* test_object_ = nullptr; ///< @brief 保存测试对象的指针,方便访问
engine::object::GameObject* player_ = nullptr; ///< @brief 保存玩家对象的指针,方便访问
public:
GameScene(std::string name, engine::core::Context& context, engine::scene::SceneManager& scene_manager);
@@ -27,9 +27,8 @@ public:
private:
// 测试函数
void createTestObject();
void testCamera();
void TestObject();
void TestPlayer();
void TestCollisionPairs();
};