added game logic class and lambda function for room

This commit is contained in:
maka70vv 2025-03-02 09:41:54 +06:00
parent b7f8a0f67a
commit d46e5da520
5 changed files with 43 additions and 13 deletions

View File

@ -2,6 +2,7 @@
#include "Environment.h" #include "Environment.h"
#include "ObjLoader.h" #include "ObjLoader.h"
#include "Inventory.h" #include "Inventory.h"
#include "QuestScripts.h"
#include "TextModel.h" // Add this include for LoadFromTextFile #include "TextModel.h" // Add this include for LoadFromTextFile
namespace ZL { namespace ZL {
@ -13,12 +14,8 @@ void GameObjectManager::initialize() {
current_room_index = 0; current_room_index = 0;
std::cout << "Hello x1" << std::endl;
coneTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp24("./conus.bmp")); coneTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp24("./conus.bmp"));
std::cout << "Hello x2" << std::endl;
// Load models // Load models
colorCubeMesh = CreateCube3D(5.0); colorCubeMesh = CreateCube3D(5.0);
colorCubeMeshMutable.data = CreateCube3D(5.0); colorCubeMeshMutable.data = CreateCube3D(5.0);
@ -30,23 +27,15 @@ void GameObjectManager::initialize() {
testObjMeshMutable.data = testObjMesh; testObjMeshMutable.data = testObjMesh;
testObjMeshMutable.RefreshVBO(); testObjMeshMutable.RefreshVBO();
std::cout << "Hello x2" << std::endl;
textMesh = ZL::LoadFromTextFile("./mesh001.txt"); // Add ZL:: namespace textMesh = ZL::LoadFromTextFile("./mesh001.txt"); // Add ZL:: namespace
coneMesh = ZL::LoadFromTextFile("./cone001.txt"); // Add ZL:: namespace coneMesh = ZL::LoadFromTextFile("./cone001.txt"); // Add ZL:: namespace
coneMesh.Scale(200); coneMesh.Scale(200);
std::cout << "Hello x3" << std::endl;
textMeshMutable.AssignFrom(textMesh); textMeshMutable.AssignFrom(textMesh);
textMeshMutable.RefreshVBO(); textMeshMutable.RefreshVBO();
coneMeshMutable.AssignFrom(coneMesh); coneMeshMutable.AssignFrom(coneMesh);
coneMeshMutable.RefreshVBO(); coneMeshMutable.RefreshVBO();
std::cout << "Hello x4" << std::endl;
// Load bone animations // Load bone animations
bx.LoadFromFile("mesh_armature_and_animation_data.txt"); bx.LoadFromFile("mesh_armature_and_animation_data.txt");
@ -68,11 +57,13 @@ void GameObjectManager::initialize() {
room_1.roomTexture = std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")); room_1.roomTexture = std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp"));
room_1.objects.push_back(ao1); room_1.objects.push_back(ao1);
room_1.sound_name = "file_example_OOG_5MG.ogg"; room_1.sound_name = "file_example_OOG_5MG.ogg";
room_1.roomLogic = createRoom1Logic();
rooms.push_back(room_1); rooms.push_back(room_1);
Room room_2; Room room_2;
room_2.roomTexture = std::make_shared<Texture>(CreateTextureDataFromBmp24("./background.bmp")); room_2.roomTexture = std::make_shared<Texture>(CreateTextureDataFromBmp24("./background.bmp"));
room_2.sound_name = "Symphony No.6 (1st movement).ogg"; room_2.sound_name = "Symphony No.6 (1st movement).ogg";
room_2.roomLogic = createRoom1Logic();
rooms.push_back(room_2); rooms.push_back(room_2);
activeObjects = rooms[current_room_index].objects; activeObjects = rooms[current_room_index].objects;
@ -235,6 +226,10 @@ void GameObjectManager::updateScene(size_t ms) {
ao.highlighted = (dist < 50.f); ao.highlighted = (dist < 50.f);
} }
if (rooms[current_room_index].roomLogic) {
rooms[current_room_index].roomLogic(*this, ms);
}
} }
bool GameObjectManager::isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const { bool GameObjectManager::isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const {

21
QuestScripts.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "QuestScripts.h"
#include "GameObjectManager.h"
#include "Inventory.h"
#include <iostream>
namespace ZL
{
std::function<void(GameObjectManager&, size_t)> createRoom1Logic()
{
return [](GameObjectManager& gom, size_t ms)
{
// if (gom.inventory.HasItem("book")) {
// std::cout << "[Room 1] Игрок поднял книгу!\n";
//
// gom.switch_room(1);
// }
};
}
}

9
QuestScripts.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <functional>
namespace ZL {
class GameObjectManager;
std::function<void(GameObjectManager&, size_t)> createRoom1Logic();
}

5
Room.h
View File

@ -4,11 +4,16 @@
#include "Math.h" #include "Math.h"
#include <memory> #include <memory>
#include "ActiveObject.h" #include "ActiveObject.h"
#include <functional>
namespace ZL namespace ZL
{ {
struct Room{ struct Room{
std::shared_ptr<ZL::Texture> roomTexture; std::shared_ptr<ZL::Texture> roomTexture;
std::vector<ActiveObject> objects; std::vector<ActiveObject> objects;
std::string sound_name; std::string sound_name;
std::function<void(class GameObjectManager&, size_t)> roomLogic;
}; };
} }

View File

@ -1,7 +1,7 @@
g++ Game.cpp main.cpp Math.cpp OpenGlExtensions.cpp Physics.cpp Renderer.cpp \ g++ Game.cpp main.cpp Math.cpp OpenGlExtensions.cpp Physics.cpp Renderer.cpp \
ShaderManager.cpp TextureManager.cpp Utils.cpp BoneAnimatedModel.cpp \ ShaderManager.cpp TextureManager.cpp Utils.cpp BoneAnimatedModel.cpp \
ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.cpp \ ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.cpp \
Inventory.cpp Environment.cpp GameObjectManager.cpp RenderSystem.cpp \ Inventory.cpp Environment.cpp GameObjectManager.cpp RenderSystem.cpp QuestScripts.cpp \
-o sdl_app -O2 -std=c++17 \ -o sdl_app -O2 -std=c++17 \
-I cmakeaudioplayer/include \ -I cmakeaudioplayer/include \
$(pkg-config --cflags --libs sdl2 gl) \ $(pkg-config --cflags --libs sdl2 gl) \