完成物理引擎与物理组件
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
#include "../../engine/object/game_object.h"
|
||||
#include "../../engine/component/transform_component.h"
|
||||
#include "../../engine/component/sprite_component.h"
|
||||
#include "../../engine/component/physics_component.h"
|
||||
#include "../../engine/physics/physics_engine.h"
|
||||
#include "../../engine/scene/level_loader.h"
|
||||
#include "../../engine/input/input_manager.h"
|
||||
#include "../../engine/render/camera.h"
|
||||
@@ -39,7 +41,7 @@ void GameScene::render() {
|
||||
|
||||
void GameScene::handleInput() {
|
||||
Scene::handleInput();
|
||||
testCamera();
|
||||
TestObject();
|
||||
}
|
||||
|
||||
void GameScene::clean() {
|
||||
@@ -51,11 +53,12 @@ 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());
|
||||
// 将创建好的 GameObject 添加到场景中 (一定要用std::move,否则传递的是左值)
|
||||
addGameObject(std::move(test_object));
|
||||
spdlog::trace("test_object 创建并添加到 GameScene 中。");
|
||||
@@ -70,4 +73,20 @@ void GameScene::testCamera() {
|
||||
if (input_manager.isActionDown("move_right")) camera.move(glm::vec2(1, 0));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace game::scene
|
||||
Reference in New Issue
Block a user