From a3075433f0b223a21e202b56216e1a7f73d30ee6 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Sun, 2 Mar 2025 19:58:38 +0300 Subject: [PATCH] Move collision box and model to the room itself --- BoundaryBox.h | 19 +++++++++++----- GameObjectManager.cpp | 52 +++++++++++++++++-------------------------- GameObjectManager.h | 18 +++++++-------- RenderSystem.cpp | 5 ++--- Room.h | 12 +++++++++- 5 files changed, 57 insertions(+), 49 deletions(-) diff --git a/BoundaryBox.h b/BoundaryBox.h index d90c672..264dc57 100644 --- a/BoundaryBox.h +++ b/BoundaryBox.h @@ -15,6 +15,10 @@ public: // Прямоугольная граница комнаты class BoundaryBox { public: + BoundaryBox() + { + } + BoundaryBox(float width, float height) : halfWidth(width/2) , halfHeight(height/2) {} @@ -25,8 +29,8 @@ public: } private: - float halfWidth; - float halfHeight; + float halfWidth = 0; + float halfHeight = 0; }; // Круглая коллизия для объектов @@ -70,8 +74,13 @@ private: // Менеджер коллизий class CollisionManager { public: + CollisionManager() + { + + } + void setRoomBoundary(float width, float height) { - roomBoundary = std::make_unique(width, height); + roomBoundary = BoundaryBox(width, height); } void addCollider(std::shared_ptr collider) { @@ -80,7 +89,7 @@ public: bool checkCollision(const Vector3f& position) const { // Проверяем границы комнаты - if (roomBoundary && !roomBoundary->isInside(position)) { + if (!roomBoundary.isInside(position)) { return true; } @@ -94,7 +103,7 @@ public: } private: - std::unique_ptr roomBoundary; + BoundaryBox roomBoundary; std::vector> colliders; }; diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 98a24e9..d408622 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -33,7 +33,7 @@ void GameObjectManager::initialize() { current_room_index = 0; objects_in_inventory = 0; - coneTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./conus.bmp")); + //coneTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./conus.bmp")); // Load models /* @@ -47,7 +47,12 @@ void GameObjectManager::initialize() { loadingThread = std::thread([this]() { - textMesh = ZL::LoadFromTextFile("./oneroom001.txt"); + preloadedRoomMeshArr.resize(1); + preloadedRoomMeshArr[0] = ZL::LoadFromTextFile("./oneroom001.txt"); + preloadedRoomMeshArr[0].Scale(10); + preloadedRoomMeshArr[0].Move(Vector3f{ 0, 93, 0 }); + + violaIdleModel.LoadFromFile("./idleviola001.txt"); violaWalkModel.LoadFromFile("./walkviolla001.txt"); sideThreadLoadingCompleted = true; @@ -61,29 +66,6 @@ void GameObjectManager::initialize() { std::function loadingFunction3 = [this]() { - /* - testObjMesh = LoadFromObjFile("./chair_01.obj"); - testObjMesh.Scale(10); - testObjMesh.SwapZandY(); - testObjMeshMutable.data = testObjMesh; - testObjMeshMutable.RefreshVBO();*/ - - - textMesh.Scale(10); - //textMesh.SwapZandY(); - //textMesh.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundX(M_PI * 0.5))); - textMesh.Move(Vector3f{ 0, 93, 0 }); - - //coneMesh = ZL::LoadFromTextFile("./cone001.txt"); // Add ZL:: namespace - //coneMesh.Scale(200); - - - textMeshMutable.AssignFrom(textMesh); - textMeshMutable.RefreshVBO(); - //coneMeshMutable.AssignFrom(coneMesh); - //coneMeshMutable.RefreshVBO(); - - // Create active object ActiveObject ao1; @@ -123,6 +105,11 @@ void GameObjectManager::initialize() { room_1.objects.push_back(ao1); room_1.sound_name = "Symphony No.6 (1st movement).ogg"; room_1.roomLogic = createRoom1Logic(); + room_1.textMesh = preloadedRoomMeshArr[0]; + room_1.textMeshMutable.AssignFrom(room_1.textMesh); + room_1.collisionMgr.setRoomBoundary(800, 800); + room_1.collisionMgr.addCollider(std::make_shared(Vector3f{ 80, 0, 200 }, Vector3f{ 400, 0, 400 })); + rooms.push_back(room_1); aoMgr.addActiveObject(ao1); @@ -130,6 +117,11 @@ void GameObjectManager::initialize() { room_2.roomTexture = std::make_shared(CreateTextureDataFromBmp24("./background.bmp")); room_2.sound_name = "Symphony No.6 (1st movement).ogg"; room_2.roomLogic = createRoom2Logic(); + room_2.textMesh = preloadedRoomMeshArr[0]; + room_2.textMeshMutable.AssignFrom(room_2.textMesh); + room_2.collisionMgr.setRoomBoundary(800, 800); + room_2.collisionMgr.addCollider(std::make_shared(Vector3f{ 80, 0, 200 }, Vector3f{ 400, 0, 400 })); + rooms.push_back(room_2); activeObjects = rooms[current_room_index].objects; @@ -152,7 +144,7 @@ void GameObjectManager::initialize() { inventoryIconMeshMutable.AssignFrom(inventoryIconMesh); inventoryIconMeshMutable.RefreshVBO(); - roomTexturePtr = rooms[current_room_index].roomTexture; + //roomTexturePtr = rooms[current_room_index].roomTexture; AddItemToInventory("book1", std::make_shared(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), objects_in_inventory + 1); objects_in_inventory++; @@ -163,9 +155,7 @@ void GameObjectManager::initialize() { //SDL_ShowCursor(SDL_DISABLE); SDL_SetRelativeMouseMode(SDL_TRUE); - collisionMgr.setRoomBoundary(800, 800); - collisionMgr.addCollider(std::make_shared(Vector3f{80, 0, 200}, Vector3f{400, 0, 400})); - + return true; }; @@ -178,7 +168,7 @@ void GameObjectManager::initialize() { void GameObjectManager::switch_room(int index){ current_room_index = index; - roomTexturePtr = rooms[current_room_index].roomTexture; + //roomTexturePtr = rooms[current_room_index].roomTexture; //audioPlayer.reset(); // This deletes the current AudioPlayer @@ -436,7 +426,7 @@ void GameObjectManager::updateScene(size_t ms) { std::cout.flush(); // Заменяем проверку walkArea.isInside() на проверку через collisionMgr - if (collisionMgr.checkCollision(characterNewPos) == false) { + if (rooms[current_room_index].collisionMgr.checkCollision(characterNewPos) == false) { Environment::cameraShift = newPosition; Environment::characterPos = characterNewPos; /* diff --git a/GameObjectManager.h b/GameObjectManager.h index c21b989..d32b067 100644 --- a/GameObjectManager.h +++ b/GameObjectManager.h @@ -4,10 +4,9 @@ #include "AudioPlayerAsync.h" #include #include -#include // Добавляем include для std::list +#include #include "ActiveObject.h" #include "Room.h" -#include "BoundaryBox.h" // Добавляем включение #ifdef __linux__ #include #endif @@ -28,7 +27,7 @@ public: void checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView); // Добавляем новый метод std::shared_ptr testObjTexturePtr; - std::shared_ptr roomTexturePtr; + //std::shared_ptr roomTexturePtr; std::shared_ptr coneTexturePtr; //ZL::VertexDataStruct colorCubeMesh; @@ -43,11 +42,11 @@ public: ZL::BoneSystem violaWalkModel; ZL::VertexRenderStruct violaWalkModelMutable; - ZL::VertexDataStruct textMesh; - ZL::VertexRenderStruct textMeshMutable; + std::vector preloadedRoomMeshArr; - ZL::VertexDataStruct coneMesh; // Раскомментировали - ZL::VertexRenderStruct coneMeshMutable; // Раскомментировали + + //ZL::VertexDataStruct coneMesh; // Раскомментировали + //ZL::VertexRenderStruct coneMeshMutable; // Раскомментировали std::vector activeObjects; std::vector rooms; @@ -72,18 +71,19 @@ public: std::thread loadingThread; bool sideThreadLoadingCompleted = false; + int current_room_index; private: + //int animationCounter = 0; int lastMouseX = 0; // Добавляем переменные для хранения позиции мыши int lastMouseY = 0; - int current_room_index; + bool isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const; void worldToScreenCoordinates(Vector3f objectPos, // Добавляем метод Matrix4f projectionModelView, int screenWidth, int screenHeight, int& screenX, int& screenY); BoundaryBox walkArea{800.0f, 800.0f}; // Зона для ходьбы 800x800 - CollisionManager collisionMgr; // Добавляем менеджер коллизий }; } // namespace ZL diff --git a/RenderSystem.cpp b/RenderSystem.cpp index 0152d68..4317e52 100644 --- a/RenderSystem.cpp +++ b/RenderSystem.cpp @@ -171,9 +171,8 @@ void RenderSystem::drawWorld(GameObjectManager& gameObjects) { } // Draw room - glBindTexture(GL_TEXTURE_2D, gameObjects.roomTexturePtr->getTexID()); - renderer.DrawVertexRenderStruct(gameObjects.textMeshMutable); - + glBindTexture(GL_TEXTURE_2D, gameObjects.rooms[gameObjects.current_room_index].roomTexture->getTexID()); + renderer.DrawVertexRenderStruct(gameObjects.rooms[gameObjects.current_room_index].textMeshMutable); Matrix4f latestProjectionModelView = renderer.GetProjectionModelViewMatrix(); diff --git a/Room.h b/Room.h index 9ce5939..4982d3f 100644 --- a/Room.h +++ b/Room.h @@ -4,8 +4,9 @@ #include "Math.h" #include #include "ActiveObject.h" -#include +#include +#include "BoundaryBox.h" namespace ZL { struct Room{ @@ -13,7 +14,16 @@ struct Room{ std::vector objects; std::string sound_name; + ZL::VertexDataStruct textMesh; + ZL::VertexRenderStruct textMeshMutable; + + CollisionManager collisionMgr; + std::function roomLogic; + Room() + { + } + }; }