merge
This commit is contained in:
commit
a8a3b04b09
@ -18,4 +18,38 @@ struct ActiveObject {
|
|||||||
bool highlighted = false;
|
bool highlighted = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
class ActiveObjectManager {
|
||||||
|
public:
|
||||||
|
// Добавить или обновить объект в контейнере
|
||||||
|
void addActiveObject(const ActiveObject& object) {
|
||||||
|
activeObjects[object.name] = object;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Найти объект по имени (возвращает указатель, nullptr — если не найден)
|
||||||
|
ActiveObject* findByName(const std::string& name) {
|
||||||
|
auto it = activeObjects.find(name);
|
||||||
|
if (it != activeObjects.end()) {
|
||||||
|
return &it->second;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Найти все объекты с нужным значением highlighted
|
||||||
|
// (возвращает список указателей на найденные объекты)
|
||||||
|
std::vector<ActiveObject*> findByHighlighted(bool highlighted) {
|
||||||
|
std::vector<ActiveObject*> result;
|
||||||
|
result.reserve(activeObjects.size());
|
||||||
|
for (auto& [key, object] : activeObjects) {
|
||||||
|
if (object.highlighted == highlighted) {
|
||||||
|
result.push_back(&object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Хранение объектов по ключу name
|
||||||
|
std::unordered_map<std::string, ActiveObject> activeObjects;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -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("./violetta001.txt");
|
//bx.LoadFromFile("./violetta001.txt");
|
||||||
violaIdleModel.LoadFromFile("./idleviola001.txt");
|
violaIdleModel.LoadFromFile("./idleviola001.txt");
|
||||||
@ -88,13 +77,14 @@ void GameObjectManager::initialize() {
|
|||||||
Room room_1;
|
Room room_1;
|
||||||
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.objects.push_back(ao2);
|
room_1.sound_name = "Symphony No.6 (1st movement).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;
|
||||||
@ -138,6 +128,7 @@ void GameObjectManager::switch_room(int index){
|
|||||||
|
|
||||||
|
|
||||||
void GameObjectManager::handleEvent(const SDL_Event& event) {
|
void GameObjectManager::handleEvent(const SDL_Event& event) {
|
||||||
|
// debug room switching
|
||||||
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT) {
|
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT) {
|
||||||
switch_room(1);
|
switch_room(1);
|
||||||
}
|
}
|
||||||
@ -309,6 +300,10 @@ void GameObjectManager::updateScene(size_t ms) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rooms[current_room_index].roomLogic) {
|
||||||
|
rooms[current_room_index].roomLogic(*this, ms);
|
||||||
|
}
|
||||||
|
|
||||||
if (Environment::violaCurrentAnimation == 0)
|
if (Environment::violaCurrentAnimation == 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@ -1,39 +1,51 @@
|
|||||||
#include "Inventory.h"
|
#include "Inventory.h"
|
||||||
#include <algorithm> // Для std::remove_if
|
|
||||||
|
|
||||||
namespace ZL
|
namespace ZL
|
||||||
{
|
{
|
||||||
// Определяем глобальный инвентарь
|
// Определяем глобальную переменную
|
||||||
std::vector<InventoryItem> gInventory;
|
std::unordered_map<std::string, InventoryItem> gInventoryMap;
|
||||||
|
|
||||||
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex)
|
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex)
|
||||||
{
|
{
|
||||||
gInventory.push_back({ name, tex });
|
InventoryItem item;
|
||||||
|
item.name = name;
|
||||||
|
item.texture = tex;
|
||||||
|
|
||||||
|
// Вставляем или перезаписываем (operator[] так сделает).
|
||||||
|
gInventoryMap[name] = item;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoveItemFromInventory(const std::string name)
|
void RemoveItemFromInventory(const std::string& name)
|
||||||
{
|
{
|
||||||
gInventory.erase(
|
// erase вернёт количество удалённых элементов, если нужно проверить
|
||||||
std::remove_if(gInventory.begin(), gInventory.end(),
|
gInventoryMap.erase(name);
|
||||||
[&name](const InventoryItem& item) {
|
}
|
||||||
return item.name == name;
|
|
||||||
}),
|
InventoryItem* GetItemByName(const std::string& name)
|
||||||
gInventory.end());
|
{
|
||||||
|
// Пытаемся найти элемент по ключу
|
||||||
|
auto it = gInventoryMap.find(name);
|
||||||
|
if (it != gInventoryMap.end())
|
||||||
|
{
|
||||||
|
// Возвращаем адрес найденного InventoryItem
|
||||||
|
return &it->second;
|
||||||
|
}
|
||||||
|
// Если не нашли – nullptr
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintInventory()
|
void PrintInventory()
|
||||||
{
|
{
|
||||||
for (const auto& item : gInventory)
|
std::cout << "Inventory contents:\n";
|
||||||
|
for (auto& [itemName, item] : gInventoryMap)
|
||||||
{
|
{
|
||||||
std::cout << "Item: " << item.name
|
std::cout << " - " << itemName << "\n";
|
||||||
<< ", texture ID = "
|
|
||||||
<< (item.texture ? item.texture->getTexID() : 0)
|
|
||||||
<< std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<InventoryItem>& ReturnInventory()
|
const std::unordered_map<std::string, InventoryItem>& ReturnInventory()
|
||||||
{
|
{
|
||||||
return gInventory;
|
return gInventoryMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
15
Inventory.h
15
Inventory.h
@ -2,21 +2,20 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <unordered_map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
|
|
||||||
namespace ZL
|
namespace ZL
|
||||||
{
|
{
|
||||||
// Структура, описывающая элемент инвентаря
|
|
||||||
struct InventoryItem
|
struct InventoryItem
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
std::shared_ptr<Texture> texture;
|
std::shared_ptr<Texture> texture;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Глобальный список инвентаря
|
// Глобальное хранилище предметов
|
||||||
extern std::vector<InventoryItem> gInventory;
|
extern std::unordered_map<std::string, InventoryItem> gInventory;
|
||||||
|
|
||||||
// Добавить предмет в инвентарь
|
// Добавить предмет в инвентарь
|
||||||
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex);
|
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex);
|
||||||
@ -24,9 +23,11 @@ namespace ZL
|
|||||||
// Удалить предмет из инвентаря
|
// Удалить предмет из инвентаря
|
||||||
void RemoveItemFromInventory(const std::string& name);
|
void RemoveItemFromInventory(const std::string& name);
|
||||||
|
|
||||||
// Вывести все предметы в инвентаре
|
// Поиск предмета по имени (возвращает указатель или nullptr)
|
||||||
|
InventoryItem* GetItemByName(const std::string& name);
|
||||||
|
|
||||||
|
// Вывести весь инвентарь в консоль
|
||||||
void PrintInventory();
|
void PrintInventory();
|
||||||
|
|
||||||
// Получить список предметов инвентаря
|
const std::unordered_map<std::string, InventoryItem>& ReturnInventory();
|
||||||
const std::vector<InventoryItem>& ReturnInventory();
|
|
||||||
}
|
}
|
||||||
|
|||||||
22
QuestScripts.cpp
Normal file
22
QuestScripts.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#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)
|
||||||
|
// Simple test logic
|
||||||
|
{
|
||||||
|
if (GetItemByName("book")) {
|
||||||
|
std::cout << "[Room 1] Игрок поднял книгу!\n";
|
||||||
|
|
||||||
|
gom.switch_room(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
9
QuestScripts.h
Normal file
9
QuestScripts.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace ZL {
|
||||||
|
class GameObjectManager;
|
||||||
|
|
||||||
|
std::function<void(GameObjectManager&, size_t)> createRoom1Logic();
|
||||||
|
|
||||||
|
}
|
||||||
@ -188,17 +188,33 @@ void RenderSystem::drawUI(const GameObjectManager& gameObjects) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw inventory
|
const auto& inventoryMap = ZL::ReturnInventory();
|
||||||
const auto& inventory = ZL::ReturnInventory();
|
|
||||||
for (size_t i = 0; i < inventory.size(); ++i) {
|
// Заводим счётчик i, чтобы вычислять позицию иконки
|
||||||
renderer.PushMatrix();
|
int i = 0;
|
||||||
float xPos = Environment::width - gameObjects.INVENTORY_MARGIN - gameObjects.INVENTORY_ICON_SIZE;
|
|
||||||
float yPos = gameObjects.INVENTORY_MARGIN + i * (gameObjects.INVENTORY_ICON_SIZE + gameObjects.INVENTORY_MARGIN);
|
// Итерируемся по всем предметам,
|
||||||
renderer.TranslateMatrix(Vector3f{xPos, yPos, 0.0f});
|
for (const auto& [name, item] : inventoryMap) {
|
||||||
glBindTexture(GL_TEXTURE_2D, inventory[i].texture->getTexID());
|
renderer.PushMatrix();
|
||||||
renderer.DrawVertexRenderStruct(gameObjects.inventoryIconMeshMutable);
|
|
||||||
renderer.PopMatrix();
|
float xPos = Environment::width
|
||||||
}
|
- gameObjects.INVENTORY_MARGIN
|
||||||
|
- gameObjects.INVENTORY_ICON_SIZE;
|
||||||
|
float yPos = gameObjects.INVENTORY_MARGIN
|
||||||
|
+ i * (gameObjects.INVENTORY_ICON_SIZE
|
||||||
|
+ gameObjects.INVENTORY_MARGIN);
|
||||||
|
|
||||||
|
renderer.TranslateMatrix(Vector3f{xPos, yPos, 0.0f});
|
||||||
|
|
||||||
|
// item.texture->getTexID() – сам текстурный ID
|
||||||
|
glBindTexture(GL_TEXTURE_2D, item.texture->getTexID());
|
||||||
|
renderer.DrawVertexRenderStruct(gameObjects.inventoryIconMeshMutable);
|
||||||
|
|
||||||
|
renderer.PopMatrix();
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
renderer.PopMatrix();
|
renderer.PopMatrix();
|
||||||
renderer.PopProjectionMatrix();
|
renderer.PopProjectionMatrix();
|
||||||
|
|||||||
5
Room.h
5
Room.h
@ -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;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
2
start.sh
2
start.sh
@ -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) \
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user