Untitled
unknown
plain_text
10 months ago
7.4 kB
20
Indexable
#include <iostream>
#include "Story.h"
#include <string>
#include "ButtonProperties.h"
#include <nlohmann/json.hpp>
#include <filesystem>
#include <SDL3/SDL.h>
#include <vector>
#include "StoryCard.h"
#include <fstream>
#include <sstream>
#include <sol/sol.hpp>
#include "engine/Dialogue.h"
#include "engine/Event.h"
#include "engine/Actor.h"
#include "engine/data_types/IscVec2.h"
#include "engine/Background.h"
#include <miniaudio.h>
#include "engine/data_types/IscAudio.h"
#include "engine/Audio.h"
using json = nlohmann::json;
Story::Story(std::string filename, json data, SDL_Renderer* renderer) {
ma_engine_init(NULL, &m_engine);
m_renderer = renderer;
m_filename = filename;
m_lua.open_libraries(sol::lib::base, sol::lib::package);
ButtonProperties buttonProperties;
buttonProperties.text = data["story_main_menu"]["button"]["text"].get<std::string>();
buttonProperties.rect = SDL_FRect{0.0f, 0.0f, 100.0f, 40.0f};
buttonProperties.roundBorder = data["story_main_menu"]["button"]["round_border"].get<bool>();
buttonProperties.color = SDL_Color{
static_cast<uint8_t>(data["story_main_menu"]["button"]["color"][0].get<int>()),
static_cast<uint8_t>(data["story_main_menu"]["button"]["color"][1].get<int>()),
static_cast<uint8_t>(data["story_main_menu"]["button"]["color"][2].get<int>()),
static_cast<uint8_t>(data["story_main_menu"]["button"]["color"][3].get<int>())
};
std::filesystem::path backgroundPath = "C:/ISCreator/Stories"/std::filesystem::path(filename)/data["story_main_menu"]["background"].get<std::string>();
m_storyMainMenu = StoryMainMenu(buttonProperties, data["card_info"]["story_name"].get<std::string>(), backgroundPath.string(), renderer);
m_storyMainMenu.initialized = true;
}
void Story::Event(SDL_Event& event) {
if (event.type == SDL_EVENT_USER) {
if (event.user.code == 0) {
if (m_dialogueHandler.dialogue.length() > 403) m_dialogueHandler.dialogue.erase(0, 1);
m_dialogueHandler.dialogue += m_dialogueHandler.tempDialogue[m_dialogueHandler.count];
m_dialogueHandler.count += 1;
} else if (event.user.code == 1) {
for (auto it = m_events.begin(); it != m_events.end();) {
if (it->eventID == *static_cast<int*>(event.user.data1)) {
std::string eventName = it->eventName;
int eventID = it->eventID;
m_events.erase(it);
if (m_eventFunctions.size() > 0) {
for (auto func:m_eventFunctions) {
func(eventName, eventID);
}
}
break;
}
}
}
}
}
void Story::RunScripts() {
std::filesystem::path scriptsPath = std::filesystem::path("C:/ISCreator/Stories/")/m_filename/"scripts";
for (auto script:std::filesystem::directory_iterator(scriptsPath)) {
if (script.path().extension() == ".lua") {
std::ifstream stream(script.path());
m_lua.script_file(script.path().string());
sol::object func = m_lua["event"];
if (func.is<sol::function>()) {
m_eventFunctions.emplace_back(func);
}
}
}
}
void Story::MakeFuncAvailableToLua() {
//Engine data types
sol::usertype<IscVec2> iscVec2Type = m_lua.new_usertype<IscVec2>("IscVec2", sol::constructors<IscVec2(float x_, float y_)>());
iscVec2Type["x"] = &IscVec2::x;
iscVec2Type["y"] = &IscVec2::y;
sol::usertype<IscActor> iscActorType = m_lua.new_usertype<IscActor>("IscActor", sol::constructors<IscActor()>());
iscActorType["name"] = &IscActor::name;
iscActorType["actorID"] = sol::readonly(&IscActor::actorID);
iscActorType["size"] = &IscActor::size;
iscActorType["position"] = &IscActor::position;
sol::usertype<IscAudio> iscAudioType = m_lua.new_usertype<IscAudio>("IscAudio", sol::constructors<IscAudio()>());
iscAudioType["name"] = &IscAudio::name;
iscAudioType["audioID"] = sol::readonly(&IscAudio::audioID);
iscAudioType["play"] = &IscAudio::Play;
iscAudioType["stop"] = &IscAudio::Stop;
iscAudioType["play_from_beginning"] = &IscAudio::PlayFromBeginning;
//Dialogue functions
m_lua.set_function("start_dialogue", StartDialogue);
m_lua.set_function("show_dialogue_box", ShowDialogueBox);
m_lua.set_function("instant_dialogue", InstantDialogue);
m_lua.set_function("clear_dialogue", ClearDialogue);
//Event functions
m_lua.set_function("add_event", AddEvent);
m_lua.set_function("remove_events_with_name", RemoveEventsWithName);
m_lua.set_function("remove_event", RemoveEvent);
m_lua.set_function("clear_events", ClearEvents);
m_lua.set_function("get_events", GetEvents);
//Actor functions
m_lua.set_function("add_actor", AddActor);
m_lua.set_function("get_actor_by_name", GetActorByName);
m_lua.set_function("get_actor_by_id", GetActorByID);
m_lua.set_function("remove_actor", RemoveActorsWithName);
m_lua.set_function("remove_actor_by_id", RemoveActorByID);
//Background functions
m_lua.set_function("set_background", SetBackground);
m_lua.set_function("clear_background", ClearBackground);
//Audio functions
m_lua.set_function("add_audio", AddAudio);
m_lua.set_function("get_audio_by_id", GetAudioByID);
}
void Story::Draw(SDL_Window* window, SDL_Color& screenColor) {
if (m_storyMainMenu.initialized) m_storyMainMenu.Draw(m_renderer, window, screenColor);
if (!m_storyMainMenu.initialized) {
if (m_background != NULL) {
SDL_FRect backgroundRect = SDL_FRect{
0.0f,
0.0f,
static_cast<float>(SDL_GetWindowSurface(window)->w),
static_cast<float>(SDL_GetWindowSurface(window)->h)
};
SDL_RenderTexture(m_renderer, m_background, NULL, &backgroundRect);
}
for (auto a = m_actors.begin(); a != m_actors.end(); a++) {
a->Draw(m_renderer);
}
}
if (!m_storyMainMenu.initialized && m_storyProperties["showDialogueBox"].get<bool>()) {
m_dialogueBoxRect.x = (static_cast<float>(SDL_GetWindowSurface(window)->w-m_dialogueBoxRect.w))/2;
m_dialogueBoxRect.y = static_cast<float>(SDL_GetWindowSurface(window)->h)-m_dialogueBoxRect.h-20;
SDL_SetRenderDrawColor(m_renderer, 100, 100, 100, 255);
SDL_RenderFillRect(m_renderer, &m_dialogueBoxRect);
SDL_RenderRect(m_renderer, &m_dialogueBoxRect);
}
if (m_dialogueHandler.dialogue != "") {
ImGui::SetCursorPos(ImVec2(m_dialogueBoxRect.x+5, m_dialogueBoxRect.y));
ImGui::PushFontSize(25.0f);
ImGui::PushTextWrapPos(ImGui::GetContentRegionAvail().x);
ImGui::Text(m_dialogueHandler.dialogue.c_str());
ImGui::PopFontSize();
}
if (m_storyMainMenu.runScripts) {
MakeFuncAvailableToLua();
RunScripts();
m_storyMainMenu.runScripts = false;
}
}
void Story::Destroy() {
if (m_storyMainMenu.initialized) m_storyMainMenu.Destroy();
ma_engine_uninit(&m_engine);
}Editor is loading...
Leave a Comment