diff --git a/res/modules/animation.lua b/res/modules/animation.lua index bc2b29fb5..b7cce0fce 100644 --- a/res/modules/animation.lua +++ b/res/modules/animation.lua @@ -279,7 +279,7 @@ end local cached_tracks = {} -function this.load_vca(filepath) +function this.load_vca(filepath, identifier) local track = cached_tracks[filepath] if track then return track @@ -287,8 +287,16 @@ function this.load_vca(filepath) local source = file.read(filepath) local raw_track = parse_track(xml.parse_vcd(source, "track")) track = this.compile_track(raw_track, filepath) - cached_tracks[filepath] = track + cached_tracks[identifier] = track return track end +function this.get_track(identifier) + return cached_tracks[identifier] +end + +function __vc_internals.load_vca_animation(filepath, identifier) + this.load_vca(filepath, identifier) +end + return this diff --git a/res/scripts/components/animator.lua b/res/scripts/components/animator.lua index dc80066db..810ea7648 100644 --- a/res/scripts/components/animator.lua +++ b/res/scripts/components/animator.lua @@ -2,9 +2,10 @@ local rig = entity.skeleton local states = {} -function create_state(name, track, max_intencity) +function create_state(name, track_id, max_intencity) states[name] = { - track = track, + track = nil, + track_id = track_id, timer = 0.0, fade_time = 0.0, fade_timer = 0.0, @@ -47,6 +48,7 @@ local function update(delta) end end + state.track = state.track or animation.get_track(state.track_id) if state.track and intensity > 0.0 then state.track.func(rig, state.timer, intensity * state.max_intencity) end diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 56d86bf8f..d2fdc59ab 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -332,6 +332,8 @@ end world.raycast = entities.__world_raycast entities.__world_raycast = nil +animation = require "core:animation" + __vc_scripts_registry = require "core:internal/scripts_registry" file.open = require "core:internal/stream_providers/file" diff --git a/src/assets/Assets.hpp b/src/assets/Assets.hpp index 10c71093b..768ab0b17 100644 --- a/src/assets/Assets.hpp +++ b/src/assets/Assets.hpp @@ -18,6 +18,7 @@ class Assets; enum class AssetType { + ANIMATION, TEXTURE, SHADER, FONT, diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index 0043ea04d..eb23385fe 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -26,17 +26,20 @@ namespace fs = std::filesystem; static debug::Logger logger("assets-loader"); -AssetsLoader::AssetsLoader(Engine& engine, Assets& assets, const ResPaths& paths) +AssetsLoader::AssetsLoader( + Engine& engine, Assets& assets, const ResPaths& paths +) : engine(engine), assets(assets), paths(paths) { - addLoader(AssetType::SHADER, assetload::shader); - addLoader(AssetType::TEXTURE, assetload::texture); - addLoader(AssetType::FONT, assetload::font); + addLoader(AssetType::ANIMATION, assetload::animation); addLoader(AssetType::ATLAS, assetload::atlas); + addLoader(AssetType::FONT, assetload::font); addLoader(AssetType::LAYOUT, assetload::layout); - addLoader(AssetType::SOUND, assetload::sound); addLoader(AssetType::MODEL, assetload::model); addLoader(AssetType::POST_EFFECT, assetload::posteffect); + addLoader(AssetType::SHADER, assetload::shader); addLoader(AssetType::SKELETON, assetload::skeleton); + addLoader(AssetType::SOUND, assetload::sound); + addLoader(AssetType::TEXTURE, assetload::texture); } void AssetsLoader::addLoader(AssetType tag, aloader_func func) { @@ -127,24 +130,26 @@ void AssetsLoader::tryAddSound(const std::string& name) { static std::string assets_def_folder(AssetType tag) { switch (tag) { - case AssetType::FONT: - return FONTS_FOLDER; - case AssetType::SHADER: - return SHADERS_FOLDER; - case AssetType::TEXTURE: - return TEXTURES_FOLDER; + case AssetType::ANIMATION: + return ANIMATION_FOLDER; case AssetType::ATLAS: return TEXTURES_FOLDER; + case AssetType::FONT: + return FONTS_FOLDER; case AssetType::LAYOUT: return LAYOUTS_FOLDER; - case AssetType::SOUND: - return SOUNDS_FOLDER; case AssetType::MODEL: return MODELS_FOLDER; case AssetType::POST_EFFECT: return POST_EFFECTS_FOLDER; + case AssetType::SHADER: + return SHADERS_FOLDER; case AssetType::SKELETON: return SKELETONS_FOLDER; + case AssetType::SOUND: + return SOUNDS_FOLDER; + case AssetType::TEXTURE: + return TEXTURES_FOLDER; } return ""; } @@ -221,14 +226,15 @@ void AssetsLoader::processPreloadList(AssetType tag, const dv::value& list) { void AssetsLoader::processPreloadConfig(const io::path& file) { auto root = io::read_json(file); + processPreloadList(AssetType::ANIMATION, root["animation"]); processPreloadList(AssetType::ATLAS, root["atlases"]); processPreloadList(AssetType::FONT, root["fonts"]); - processPreloadList(AssetType::SHADER, root["shaders"]); - processPreloadList(AssetType::TEXTURE, root["textures"]); - processPreloadList(AssetType::SOUND, root["sounds"]); processPreloadList(AssetType::MODEL, root["models"]); processPreloadList(AssetType::POST_EFFECT, root["post-effects"]); + processPreloadList(AssetType::SHADER, root["shaders"]); processPreloadList(AssetType::SKELETON, root["skeletons"]); + processPreloadList(AssetType::SOUND, root["sounds"]); + processPreloadList(AssetType::TEXTURE, root["textures"]); // layouts are loaded automatically } diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index 2c173fdf1..f406cef45 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -23,6 +23,7 @@ #include "graphics/core/Shader.hpp" #include "graphics/core/Texture.hpp" #include "graphics/core/TextureAnimation.hpp" +#include "logic/scripting/scripting.hpp" #include "io/io.hpp" #include "util/stringutil.hpp" @@ -44,6 +45,20 @@ static bool load_animated_texture( Atlas* dstAtlas ); +assetload::postfunc assetload::animation( + AssetsLoader&, + const ResPaths& paths, + const std::string& filename, + const std::string& name, + const std::shared_ptr& +) { + auto path = paths.find(filename + ".vca"); + if (io::is_regular_file(path)) { + scripting::load_vca_animation(path, name); + } + return [](auto&) {}; +} + assetload::postfunc assetload::texture( AssetsLoader&, const ResPaths& paths, diff --git a/src/assets/assetload_funcs.hpp b/src/assets/assetload_funcs.hpp index 085d45742..9ccc601ab 100644 --- a/src/assets/assetload_funcs.hpp +++ b/src/assets/assetload_funcs.hpp @@ -13,6 +13,13 @@ struct AssetCfg; /// @brief see AssetsLoader.h: aloader_func namespace assetload { + postfunc animation( + AssetsLoader&, + const ResPaths& paths, + const std::string& filename, + const std::string& name, + const std::shared_ptr& settings + ); postfunc texture( AssetsLoader&, const ResPaths& paths, diff --git a/src/constants.hpp b/src/constants.hpp index 5bacfe078..03900b9e3 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -75,13 +75,14 @@ inline constexpr int DEFAULT_FONT_SIZE = 16; inline constexpr int DEFAULT_PRE_RENDER_FONT_PAGES = 1; // must be at least 1 -inline const std::string SHADERS_FOLDER = "shaders"; -inline const std::string TEXTURES_FOLDER = "textures"; +inline const std::string ANIMATION_FOLDER = "animation"; inline const std::string FONTS_FOLDER = "fonts"; inline const std::string LAYOUTS_FOLDER = "layouts"; -inline const std::string SOUNDS_FOLDER = "sounds"; inline const std::string MODELS_FOLDER = "models"; -inline const std::string SKELETONS_FOLDER = "skeletons"; inline const std::string POST_EFFECTS_FOLDER = "shaders/effects"; +inline const std::string SHADERS_FOLDER = "shaders"; +inline const std::string SKELETONS_FOLDER = "skeletons"; +inline const std::string SOUNDS_FOLDER = "sounds"; +inline const std::string TEXTURES_FOLDER = "textures"; inline const std::string FONT_DEFAULT = "normal"; diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index c59dc0a9a..661bec651 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -844,6 +844,21 @@ void scripting::load_layout_script( lua::pop(L); } +void scripting::load_vca_animation( + const io::path& file, + const std::string& identifier +) { + auto L = lua::get_main_state(); + if (lua::get_from_registry(L, "internals", "load_vca_animation", true)) { + lua::pushlstring(L, file.string()); + lua::pushlstring(L, identifier); + if (lua::call(L, 2, 0)) { + lua::pop(L); + } + lua::pop(L); + } +} + void scripting::close() { lua::finalize(); content = nullptr; diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 0044213cd..e774864ef 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -251,6 +251,11 @@ namespace scripting { const std::string& fileName, UiDocScript& script ); + + void load_vca_animation( + const io::path& file, + const std::string& identifier + ); /// @brief Finalize lua state. Using scripting after will lead to Lua panic void close();