Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(effect LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(effect STATIC
|
||||
src/time_event.cpp
|
||||
src/effect_data.cpp
|
||||
src/effect_instance.cpp
|
||||
src/flying_data.cpp
|
||||
src/flying_instance.cpp
|
||||
src/effect_manager.cpp
|
||||
)
|
||||
|
||||
target_include_directories(effect PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
add_library(effect::effect ALIAS effect)
|
||||
|
||||
option(EFFECT_BUILD_TESTS "Build effect SDK tests" ON)
|
||||
if(EFFECT_BUILD_TESTS)
|
||||
add_executable(effect_test tests/effect_test.cpp)
|
||||
target_link_libraries(effect_test PRIVATE effect)
|
||||
add_test(NAME effect_test COMMAND effect_test)
|
||||
endif()
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "effect/types.h"
|
||||
#include "effect/time_event.h"
|
||||
#include "effect/effect_data.h"
|
||||
#include "effect/effect_instance.h"
|
||||
#include "effect/flying_data.h"
|
||||
#include "effect/flying_instance.h"
|
||||
#include "effect/effect_manager.h"
|
||||
@@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include "effect/types.h"
|
||||
#include "effect/time_event.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
|
||||
namespace effect {
|
||||
|
||||
struct EmitterProperty {
|
||||
uint32_t max_emission_count = 1;
|
||||
float cycle_length = 0.5f;
|
||||
bool cycle_loop_enable = true;
|
||||
int loop_count = 0;
|
||||
|
||||
uint8_t emitter_shape = 0; // 0: Point, 1: Ellipse, 2: Box
|
||||
uint8_t emitter_advanced_type = 0;
|
||||
bool emit_from_edge_flag = false;
|
||||
float emitting_radius = 0.0f;
|
||||
Vec3 emitting_direction{0.0f, 0.0f, 0.0f};
|
||||
|
||||
std::vector<TimeEvent> time_emitting_velocity;
|
||||
std::vector<TimeEvent> time_emission_count_per_second;
|
||||
std::vector<TimeEvent> time_life_time;
|
||||
std::vector<TimeEvent> time_size_x;
|
||||
std::vector<TimeEvent> time_size_y;
|
||||
std::vector<TimeEvent> time_emitting_dir_x;
|
||||
std::vector<TimeEvent> time_emitting_dir_y;
|
||||
std::vector<TimeEvent> time_emitting_dir_z;
|
||||
std::vector<TimeEvent> time_emitting_angular_velocity;
|
||||
};
|
||||
|
||||
struct ParticleProperty {
|
||||
uint8_t src_blend_type = 5; // D3DBLEND_SRCALPHA
|
||||
uint8_t dest_blend_type = 2; // D3DBLEND_ONE (Additive) or 6 (D3DBLEND_INVSRCALPHA)
|
||||
uint8_t color_op_type = 4; // D3DTOP_MODULATE
|
||||
|
||||
uint8_t billboard_type = 1; // 0: None, 1: Camera, 2: Y-axis, 3: Flat
|
||||
uint8_t rotation_type = 0;
|
||||
float rotation_speed = 0.0f;
|
||||
int rotation_random_begin = 0;
|
||||
int rotation_random_end = 0;
|
||||
|
||||
bool attach_enable = false;
|
||||
bool stretch_enable = false;
|
||||
|
||||
uint8_t tex_ani_type = 0;
|
||||
float tex_ani_delay = 0.03f;
|
||||
bool tex_ani_random_start_frame = false;
|
||||
|
||||
std::vector<TimeEvent> time_gravity;
|
||||
std::vector<TimeEvent> time_air_resistance;
|
||||
std::vector<TimeEvent> time_scale_x;
|
||||
std::vector<TimeEvent> time_scale_y;
|
||||
std::vector<TimeEvent> time_color_r;
|
||||
std::vector<TimeEvent> time_color_g;
|
||||
std::vector<TimeEvent> time_color_b;
|
||||
std::vector<TimeEvent> time_alpha;
|
||||
std::vector<TimeEvent> time_rotation;
|
||||
|
||||
std::vector<std::string> texture_files;
|
||||
};
|
||||
|
||||
struct ParticleElement {
|
||||
float start_time = 0.0f;
|
||||
Vec3 position{0.0f, 0.0f, 0.0f};
|
||||
EmitterProperty emitter;
|
||||
ParticleProperty particle;
|
||||
};
|
||||
|
||||
class EffectData {
|
||||
public:
|
||||
EffectData() = default;
|
||||
|
||||
bool load_from_string(const std::string& script_content);
|
||||
bool load_file(const std::filesystem::path& filepath);
|
||||
|
||||
const std::string& get_filename() const { return m_filename; }
|
||||
float get_bounding_sphere_radius() const { return m_bounding_sphere_radius; }
|
||||
const Vec3& get_bounding_sphere_position() const { return m_bounding_sphere_pos; }
|
||||
const std::vector<ParticleElement>& get_particles() const { return m_particles; }
|
||||
|
||||
private:
|
||||
std::string m_filename;
|
||||
float m_bounding_sphere_radius = 50.0f;
|
||||
Vec3 m_bounding_sphere_pos{0.0f, 0.0f, 0.0f};
|
||||
std::vector<ParticleElement> m_particles;
|
||||
};
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include "effect/effect_data.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace effect {
|
||||
|
||||
struct ParticleState {
|
||||
Vec3 pos{0.0f, 0.0f, 0.0f};
|
||||
Vec3 vel{0.0f, 0.0f, 0.0f};
|
||||
float life = 0.0f;
|
||||
float max_life = 1.0f;
|
||||
float rot = 0.0f;
|
||||
float rot_speed = 0.0f;
|
||||
float base_size_x = 1.0f;
|
||||
float base_size_y = 1.0f;
|
||||
uint8_t frame_index = 0;
|
||||
};
|
||||
|
||||
struct EmitterInstance {
|
||||
float local_time = 0.0f;
|
||||
uint32_t emitted_count = 0;
|
||||
float emission_accumulator = 0.0f;
|
||||
std::vector<ParticleState> particles;
|
||||
};
|
||||
|
||||
class EffectInstance {
|
||||
public:
|
||||
explicit EffectInstance(std::shared_ptr<EffectData> data);
|
||||
~EffectInstance() = default;
|
||||
|
||||
void set_position(float x, float y, float z) { m_position = {x, y, z}; }
|
||||
void set_position(const Vec3& p) { m_position = p; }
|
||||
const Vec3& get_position() const { return m_position; }
|
||||
|
||||
void set_world_matrix(const Mat4& mat) {
|
||||
m_world_matrix = mat;
|
||||
m_has_custom_matrix = true;
|
||||
}
|
||||
|
||||
void update(float dt);
|
||||
void build_draw_batches(const Vec3& camera_pos,
|
||||
const Vec3& camera_dir,
|
||||
const Vec3& camera_up,
|
||||
std::vector<ParticleDrawBatch>& out_batches);
|
||||
|
||||
bool is_alive() const { return m_is_alive; }
|
||||
void kill() { m_is_alive = false; }
|
||||
size_t get_active_particle_count() const;
|
||||
|
||||
std::shared_ptr<EffectData> get_data() const { return m_data; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<EffectData> m_data;
|
||||
bool m_is_alive = true;
|
||||
float m_age = 0.0f;
|
||||
Vec3 m_position{0.0f, 0.0f, 0.0f};
|
||||
Mat4 m_world_matrix = Mat4::identity();
|
||||
bool m_has_custom_matrix = false;
|
||||
|
||||
std::vector<EmitterInstance> m_emitters;
|
||||
};
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "effect/effect_data.h"
|
||||
#include "effect/effect_instance.h"
|
||||
#include "effect/flying_data.h"
|
||||
#include "effect/flying_instance.h"
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace effect {
|
||||
|
||||
class EffectManager {
|
||||
public:
|
||||
EffectManager() = default;
|
||||
~EffectManager() = default;
|
||||
|
||||
std::shared_ptr<EffectData> get_or_load_effect(const std::string& filepath);
|
||||
std::shared_ptr<FlyingData> get_or_load_flying(const std::string& filepath);
|
||||
|
||||
std::shared_ptr<EffectInstance> spawn_effect(const std::string& filepath, const Vec3& pos);
|
||||
std::shared_ptr<FlyingInstance> spawn_flying(const std::string& filepath, const Vec3& start, const Vec3& target);
|
||||
|
||||
void update(float dt);
|
||||
void build_draw_batches(const Vec3& camera_pos,
|
||||
const Vec3& camera_dir,
|
||||
const Vec3& camera_up,
|
||||
std::vector<ParticleDrawBatch>& out_batches);
|
||||
|
||||
size_t get_active_effects_count() const { return m_effects.size(); }
|
||||
size_t get_active_flying_count() const { return m_flying.size(); }
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, std::shared_ptr<EffectData>> m_effect_cache;
|
||||
std::unordered_map<std::string, std::shared_ptr<FlyingData>> m_flying_cache;
|
||||
|
||||
std::vector<std::shared_ptr<EffectInstance>> m_effects;
|
||||
std::vector<std::shared_ptr<FlyingInstance>> m_flying;
|
||||
};
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include "effect/types.h"
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
namespace effect {
|
||||
|
||||
struct FlyingAttachData {
|
||||
int type = 1;
|
||||
int fly_type = 1;
|
||||
std::string attach_file;
|
||||
bool has_tail = false;
|
||||
uint32_t tail_color = 0xFFFFFFFF;
|
||||
float tail_length = 0.2f;
|
||||
float tail_size = 8.0f;
|
||||
bool rect_shape = false;
|
||||
};
|
||||
|
||||
class FlyingData {
|
||||
public:
|
||||
FlyingData() = default;
|
||||
|
||||
bool load_from_string(const std::string& content);
|
||||
bool load_file(const std::filesystem::path& filepath);
|
||||
|
||||
float initial_velocity = 2000.0f;
|
||||
float gravity = 0.0f;
|
||||
Vec3 acceleration{0.0f, 0.0f, 0.0f};
|
||||
float bomb_range = 10.0f;
|
||||
std::string bomb_effect;
|
||||
bool homing_flag = false;
|
||||
float homing_start_time = 0.0f;
|
||||
float homing_max_angle = 3.0f;
|
||||
float range = 10000.0f;
|
||||
|
||||
FlyingAttachData attach;
|
||||
};
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include "effect/flying_data.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
namespace effect {
|
||||
|
||||
struct TrailPoint {
|
||||
Vec3 pos;
|
||||
float time = 0.0f;
|
||||
};
|
||||
|
||||
class FlyingInstance {
|
||||
public:
|
||||
FlyingInstance(std::shared_ptr<FlyingData> data, const Vec3& start_pos, const Vec3& target_pos);
|
||||
~FlyingInstance() = default;
|
||||
|
||||
bool update(float dt);
|
||||
|
||||
bool is_alive() const { return m_is_alive; }
|
||||
void kill() { m_is_alive = false; }
|
||||
|
||||
const Vec3& get_position() const { return m_position; }
|
||||
const Vec3& get_velocity() const { return m_velocity; }
|
||||
const Vec3& get_target() const { return m_target; }
|
||||
const std::vector<TrailPoint>& get_trail() const { return m_trail; }
|
||||
|
||||
void set_hit_callback(std::function<void(const Vec3&)> cb) {
|
||||
m_hit_cb = std::move(cb);
|
||||
}
|
||||
|
||||
std::shared_ptr<FlyingData> get_data() const { return m_data; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<FlyingData> m_data;
|
||||
bool m_is_alive = true;
|
||||
float m_age = 0.0f;
|
||||
float m_distance_traveled = 0.0f;
|
||||
|
||||
Vec3 m_position{0.0f, 0.0f, 0.0f};
|
||||
Vec3 m_velocity{0.0f, 0.0f, 0.0f};
|
||||
Vec3 m_target{0.0f, 0.0f, 0.0f};
|
||||
|
||||
std::vector<TrailPoint> m_trail;
|
||||
std::function<void(const Vec3&)> m_hit_cb;
|
||||
};
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
namespace effect {
|
||||
|
||||
struct TimeEvent {
|
||||
float time = 0.0f;
|
||||
float value = 0.0f;
|
||||
};
|
||||
|
||||
float interpolate_time_event(const std::vector<TimeEvent>& events, float time, float default_val = 0.0f);
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
|
||||
namespace effect {
|
||||
|
||||
struct Vec3 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
|
||||
constexpr Vec3() = default;
|
||||
constexpr Vec3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
|
||||
|
||||
constexpr Vec3 operator+(const Vec3& o) const { return {x + o.x, y + o.y, z + o.z}; }
|
||||
constexpr Vec3 operator-(const Vec3& o) const { return {x - o.x, y - o.y, z - o.z}; }
|
||||
constexpr Vec3 operator*(float s) const { return {x * s, y * s, z * s}; }
|
||||
constexpr Vec3 operator/(float s) const { return {x / s, y / s, z / s}; }
|
||||
|
||||
Vec3& operator+=(const Vec3& o) { x += o.x; y += o.y; z += o.z; return *this; }
|
||||
Vec3& operator-=(const Vec3& o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
|
||||
Vec3& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
|
||||
|
||||
float length_sq() const { return x * x + y * y + z * z; }
|
||||
float length() const { return std::sqrt(length_sq()); }
|
||||
|
||||
Vec3 normalized() const {
|
||||
float l = length();
|
||||
if (l < 1e-6f) return {0.0f, 0.0f, 0.0f};
|
||||
return {x / l, y / l, z / l};
|
||||
}
|
||||
|
||||
static float dot(const Vec3& a, const Vec3& b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
static Vec3 cross(const Vec3& a, const Vec3& b) {
|
||||
return {
|
||||
a.y * b.z - a.z * b.y,
|
||||
a.z * b.x - a.x * b.z,
|
||||
a.x * b.y - a.y * b.x
|
||||
};
|
||||
}
|
||||
|
||||
static Vec3 lerp(const Vec3& a, const Vec3& b, float t) {
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
};
|
||||
|
||||
struct Mat4 {
|
||||
// Column-major 4x4 matrix
|
||||
std::array<float, 16> m = {
|
||||
1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
static Mat4 identity() {
|
||||
return Mat4{};
|
||||
}
|
||||
|
||||
static Mat4 translation(float tx, float ty, float tz) {
|
||||
Mat4 res;
|
||||
res.m[12] = tx;
|
||||
res.m[13] = ty;
|
||||
res.m[14] = tz;
|
||||
return res;
|
||||
}
|
||||
|
||||
Vec3 transform_point(const Vec3& p) const {
|
||||
return {
|
||||
m[0] * p.x + m[4] * p.y + m[8] * p.z + m[12],
|
||||
m[1] * p.x + m[5] * p.y + m[9] * p.z + m[13],
|
||||
m[2] * p.x + m[6] * p.y + m[10] * p.z + m[14]
|
||||
};
|
||||
}
|
||||
|
||||
Vec3 transform_vector(const Vec3& v) const {
|
||||
return {
|
||||
m[0] * v.x + m[4] * v.y + m[8] * v.z,
|
||||
m[1] * v.x + m[5] * v.y + m[9] * v.z,
|
||||
m[2] * v.x + m[6] * v.y + m[10] * v.z
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
struct Color4 {
|
||||
float r = 1.0f;
|
||||
float g = 1.0f;
|
||||
float b = 1.0f;
|
||||
float a = 1.0f;
|
||||
};
|
||||
|
||||
struct ParticleVertex {
|
||||
Vec3 pos;
|
||||
Color4 color;
|
||||
float u = 0.0f;
|
||||
float v = 0.0f;
|
||||
};
|
||||
|
||||
struct ParticleDrawBatch {
|
||||
std::string texture;
|
||||
uint8_t src_blend = 5; // D3DBLEND_SRCALPHA
|
||||
uint8_t dest_blend = 2; // D3DBLEND_ONE (Additive)
|
||||
uint8_t color_op = 4; // D3DTOP_MODULATE
|
||||
std::vector<ParticleVertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
};
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,284 @@
|
||||
#include "effect/effect_data.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace effect {
|
||||
|
||||
namespace {
|
||||
|
||||
float to_float(const std::string& s) {
|
||||
return std::strtof(s.c_str(), nullptr);
|
||||
}
|
||||
|
||||
int to_int(const std::string& s) {
|
||||
return std::atoi(s.c_str());
|
||||
}
|
||||
|
||||
std::string to_lower(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
|
||||
return std::tolower(c);
|
||||
});
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<std::string> tokenize(const std::string& content) {
|
||||
std::vector<std::string> tokens;
|
||||
size_t i = 0;
|
||||
size_t n = content.size();
|
||||
|
||||
while (i < n) {
|
||||
while (i < n && (content[i] == ' ' || content[i] == '\t' || content[i] == '\r' || content[i] == '\n')) {
|
||||
++i;
|
||||
}
|
||||
if (i >= n) break;
|
||||
|
||||
// Comments
|
||||
if (i + 1 < n && content[i] == '/' && content[i + 1] == '/') {
|
||||
while (i < n && content[i] != '\n') ++i;
|
||||
continue;
|
||||
}
|
||||
if (content[i] == '#') {
|
||||
while (i < n && content[i] != '\n') ++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Bracket tokens
|
||||
if (content[i] == '{' || content[i] == '}') {
|
||||
tokens.push_back(std::string(1, content[i]));
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Quoted string
|
||||
if (content[i] == '"') {
|
||||
++i;
|
||||
size_t start = i;
|
||||
while (i < n && content[i] != '"') ++i;
|
||||
tokens.push_back(content.substr(start, i - start));
|
||||
if (i < n && content[i] == '"') ++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Normal word
|
||||
size_t start = i;
|
||||
while (i < n && content[i] != ' ' && content[i] != '\t' && content[i] != '\r' && content[i] != '\n' &&
|
||||
content[i] != '{' && content[i] != '}' && content[i] != '"') {
|
||||
++i;
|
||||
}
|
||||
tokens.push_back(content.substr(start, i - start));
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
bool EffectData::load_file(const std::filesystem::path& filepath) {
|
||||
std::ifstream file(filepath, std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
std::stringstream buf;
|
||||
buf << file.rdbuf();
|
||||
m_filename = filepath.string();
|
||||
return load_from_string(buf.str());
|
||||
}
|
||||
|
||||
bool EffectData::load_from_string(const std::string& content) {
|
||||
m_particles.clear();
|
||||
auto tokens = tokenize(content);
|
||||
size_t idx = 0;
|
||||
size_t total = tokens.size();
|
||||
|
||||
auto read_time_events = [&](std::vector<TimeEvent>& out) {
|
||||
if (idx < total && tokens[idx] == "{") {
|
||||
++idx;
|
||||
while (idx < total && tokens[idx] != "}") {
|
||||
float t = to_float(tokens[idx++]);
|
||||
if (idx < total && tokens[idx] != "}") {
|
||||
float v = to_float(tokens[idx++]);
|
||||
out.push_back({t, v});
|
||||
}
|
||||
}
|
||||
if (idx < total && tokens[idx] == "}") ++idx;
|
||||
}
|
||||
};
|
||||
|
||||
while (idx < total) {
|
||||
std::string token = to_lower(tokens[idx]);
|
||||
|
||||
if (token == "boundingsphereradius" && idx + 1 < total) {
|
||||
++idx;
|
||||
m_bounding_sphere_radius = to_float(tokens[idx++]);
|
||||
} else if (token == "boundingsphereposition" && idx + 3 < total) {
|
||||
++idx;
|
||||
m_bounding_sphere_pos.x = to_float(tokens[idx++]);
|
||||
m_bounding_sphere_pos.y = to_float(tokens[idx++]);
|
||||
m_bounding_sphere_pos.z = to_float(tokens[idx++]);
|
||||
} else if (token == "group" && idx + 1 < total && to_lower(tokens[idx + 1]) == "particle") {
|
||||
idx += 2;
|
||||
if (idx < total && tokens[idx] == "{") ++idx;
|
||||
|
||||
ParticleElement pe;
|
||||
int depth = 1;
|
||||
|
||||
while (idx < total && depth > 0) {
|
||||
std::string p_tok = to_lower(tokens[idx]);
|
||||
|
||||
if (tokens[idx] == "{") {
|
||||
++depth;
|
||||
++idx;
|
||||
continue;
|
||||
}
|
||||
if (tokens[idx] == "}") {
|
||||
--depth;
|
||||
++idx;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (p_tok == "starttime" && idx + 1 < total) {
|
||||
++idx;
|
||||
pe.start_time = to_float(tokens[idx++]);
|
||||
} else if (p_tok == "position" && idx + 3 < total) {
|
||||
++idx;
|
||||
pe.position.x = to_float(tokens[idx++]);
|
||||
pe.position.y = to_float(tokens[idx++]);
|
||||
pe.position.z = to_float(tokens[idx++]);
|
||||
} else if (p_tok == "list" && idx + 1 < total && to_lower(tokens[idx + 1]) == "timeeventposition") {
|
||||
idx += 2;
|
||||
if (idx < total && tokens[idx] == "{") {
|
||||
++idx;
|
||||
while (idx < total && tokens[idx] != "}") {
|
||||
// Can contain moving type and coordinates
|
||||
float t = to_float(tokens[idx++]);
|
||||
(void)t;
|
||||
if (idx < total && tokens[idx] != "}") {
|
||||
std::string mov_type = tokens[idx++];
|
||||
(void)mov_type;
|
||||
if (idx + 2 < total && tokens[idx] != "}") {
|
||||
pe.position.x = to_float(tokens[idx++]);
|
||||
pe.position.y = to_float(tokens[idx++]);
|
||||
pe.position.z = to_float(tokens[idx++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (idx < total && tokens[idx] == "}") ++idx;
|
||||
}
|
||||
} else if (p_tok == "group" && idx + 1 < total && to_lower(tokens[idx + 1]) == "emitterproperty") {
|
||||
idx += 2;
|
||||
if (idx < total && tokens[idx] == "{") ++idx;
|
||||
int edepth = 1;
|
||||
while (idx < total && edepth > 0) {
|
||||
std::string e_tok = to_lower(tokens[idx]);
|
||||
if (tokens[idx] == "{") { ++edepth; ++idx; continue; }
|
||||
if (tokens[idx] == "}") { --edepth; ++idx; continue; }
|
||||
|
||||
if (e_tok == "maxemissioncount" && idx + 1 < total) {
|
||||
++idx; pe.emitter.max_emission_count = to_int(tokens[idx++]);
|
||||
} else if (e_tok == "cyclelength" && idx + 1 < total) {
|
||||
++idx; pe.emitter.cycle_length = to_float(tokens[idx++]);
|
||||
} else if (e_tok == "cycleloopenable" && idx + 1 < total) {
|
||||
++idx; pe.emitter.cycle_loop_enable = (to_int(tokens[idx++]) != 0);
|
||||
} else if (e_tok == "loopcount" && idx + 1 < total) {
|
||||
++idx; pe.emitter.loop_count = to_int(tokens[idx++]);
|
||||
} else if (e_tok == "emittershape" && idx + 1 < total) {
|
||||
++idx; pe.emitter.emitter_shape = static_cast<uint8_t>(to_int(tokens[idx++]));
|
||||
} else if (e_tok == "emittingdirection" && idx + 3 < total) {
|
||||
++idx;
|
||||
pe.emitter.emitting_direction.x = to_float(tokens[idx++]);
|
||||
pe.emitter.emitting_direction.y = to_float(tokens[idx++]);
|
||||
pe.emitter.emitting_direction.z = to_float(tokens[idx++]);
|
||||
} else if (e_tok == "list" && idx + 1 < total) {
|
||||
std::string list_name = to_lower(tokens[idx + 1]);
|
||||
idx += 2;
|
||||
if (list_name == "timeeventemittingvelocity") read_time_events(pe.emitter.time_emitting_velocity);
|
||||
else if (list_name == "timeeventemissioncountpersecond") read_time_events(pe.emitter.time_emission_count_per_second);
|
||||
else if (list_name == "timeeventlifetime") read_time_events(pe.emitter.time_life_time);
|
||||
else if (list_name == "timeeventsizex") read_time_events(pe.emitter.time_size_x);
|
||||
else if (list_name == "timeeventsizey") read_time_events(pe.emitter.time_size_y);
|
||||
else if (list_name == "timeeventemittingdirectionx") read_time_events(pe.emitter.time_emitting_dir_x);
|
||||
else if (list_name == "timeeventemittingdirectiony") read_time_events(pe.emitter.time_emitting_dir_y);
|
||||
else if (list_name == "timeeventemittingdirectionz") read_time_events(pe.emitter.time_emitting_dir_z);
|
||||
else if (list_name == "timeeventemittingangularvelocity") read_time_events(pe.emitter.time_emitting_angular_velocity);
|
||||
else {
|
||||
// Skip unknown list
|
||||
if (idx < total && tokens[idx] == "{") {
|
||||
++idx;
|
||||
while (idx < total && tokens[idx] != "}") ++idx;
|
||||
if (idx < total && tokens[idx] == "}") ++idx;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
} else if (p_tok == "group" && idx + 1 < total && to_lower(tokens[idx + 1]) == "particleproperty") {
|
||||
idx += 2;
|
||||
if (idx < total && tokens[idx] == "{") ++idx;
|
||||
int pdepth = 1;
|
||||
while (idx < total && pdepth > 0) {
|
||||
std::string pp_tok = to_lower(tokens[idx]);
|
||||
if (tokens[idx] == "{") { ++pdepth; ++idx; continue; }
|
||||
if (tokens[idx] == "}") { --pdepth; ++idx; continue; }
|
||||
|
||||
if (pp_tok == "srcblendtype" && idx + 1 < total) {
|
||||
++idx; pe.particle.src_blend_type = static_cast<uint8_t>(to_int(tokens[idx++]));
|
||||
} else if (pp_tok == "destblendtype" && idx + 1 < total) {
|
||||
++idx; pe.particle.dest_blend_type = static_cast<uint8_t>(to_int(tokens[idx++]));
|
||||
} else if (pp_tok == "coloroperationtype" && idx + 1 < total) {
|
||||
++idx; pe.particle.color_op_type = static_cast<uint8_t>(to_int(tokens[idx++]));
|
||||
} else if (pp_tok == "billboardtype" && idx + 1 < total) {
|
||||
++idx; pe.particle.billboard_type = static_cast<uint8_t>(to_int(tokens[idx++]));
|
||||
} else if (pp_tok == "rotationtype" && idx + 1 < total) {
|
||||
++idx; pe.particle.rotation_type = static_cast<uint8_t>(to_int(tokens[idx++]));
|
||||
} else if (pp_tok == "rotationspeed" && idx + 1 < total) {
|
||||
++idx; pe.particle.rotation_speed = to_float(tokens[idx++]);
|
||||
} else if (pp_tok == "attachenable" && idx + 1 < total) {
|
||||
++idx; pe.particle.attach_enable = (to_int(tokens[idx++]) != 0);
|
||||
} else if (pp_tok == "stretchenable" && idx + 1 < total) {
|
||||
++idx; pe.particle.stretch_enable = (to_int(tokens[idx++]) != 0);
|
||||
} else if (pp_tok == "list" && idx + 1 < total) {
|
||||
std::string list_name = to_lower(tokens[idx + 1]);
|
||||
idx += 2;
|
||||
if (list_name == "timeeventgravity") read_time_events(pe.particle.time_gravity);
|
||||
else if (list_name == "timeeventairresistance") read_time_events(pe.particle.time_air_resistance);
|
||||
else if (list_name == "timeeventscalex") read_time_events(pe.particle.time_scale_x);
|
||||
else if (list_name == "timeeventscaley") read_time_events(pe.particle.time_scale_y);
|
||||
else if (list_name == "timeeventcolorred") read_time_events(pe.particle.time_color_r);
|
||||
else if (list_name == "timeeventcolorgreen") read_time_events(pe.particle.time_color_g);
|
||||
else if (list_name == "timeeventcolorblue") read_time_events(pe.particle.time_color_b);
|
||||
else if (list_name == "timeeventalpha") read_time_events(pe.particle.time_alpha);
|
||||
else if (list_name == "timeeventrotation") read_time_events(pe.particle.time_rotation);
|
||||
else if (list_name == "texturefiles") {
|
||||
if (idx < total && tokens[idx] == "{") {
|
||||
++idx;
|
||||
while (idx < total && tokens[idx] != "}") {
|
||||
pe.particle.texture_files.push_back(tokens[idx++]);
|
||||
}
|
||||
if (idx < total && tokens[idx] == "}") ++idx;
|
||||
}
|
||||
} else {
|
||||
if (idx < total && tokens[idx] == "{") {
|
||||
++idx;
|
||||
while (idx < total && tokens[idx] != "}") ++idx;
|
||||
if (idx < total && tokens[idx] == "}") ++idx;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
m_particles.push_back(std::move(pe));
|
||||
} else {
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
|
||||
return !m_particles.empty() || m_bounding_sphere_radius > 0.0f;
|
||||
}
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,214 @@
|
||||
#include "effect/effect_instance.h"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
namespace effect {
|
||||
|
||||
EffectInstance::EffectInstance(std::shared_ptr<EffectData> data)
|
||||
: m_data(std::move(data)) {
|
||||
if (m_data) {
|
||||
m_emitters.resize(m_data->get_particles().size());
|
||||
}
|
||||
}
|
||||
|
||||
size_t EffectInstance::get_active_particle_count() const {
|
||||
size_t count = 0;
|
||||
for (const auto& emitter : m_emitters) {
|
||||
count += emitter.particles.size();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void EffectInstance::update(float dt) {
|
||||
if (!m_is_alive || !m_data) return;
|
||||
|
||||
m_age += dt;
|
||||
const auto& particles_def = m_data->get_particles();
|
||||
bool any_active = false;
|
||||
|
||||
for (size_t i = 0; i < particles_def.size(); ++i) {
|
||||
const auto& p_def = particles_def[i];
|
||||
auto& emitter_inst = m_emitters[i];
|
||||
|
||||
if (m_age < p_def.start_time) {
|
||||
any_active = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
emitter_inst.local_time += dt;
|
||||
float cycle_len = p_def.emitter.cycle_length > 0.0f ? p_def.emitter.cycle_length : 1.0f;
|
||||
float current_cycle_time = emitter_inst.local_time;
|
||||
|
||||
if (p_def.emitter.cycle_loop_enable) {
|
||||
current_cycle_time = std::fmod(emitter_inst.local_time, cycle_len);
|
||||
} else if (emitter_inst.local_time > cycle_len) {
|
||||
// cycle finished
|
||||
}
|
||||
|
||||
// 1. Spawning new particles
|
||||
float emission_rate = interpolate_time_event(
|
||||
p_def.emitter.time_emission_count_per_second, current_cycle_time, 10.0f
|
||||
);
|
||||
|
||||
bool can_emit = p_def.emitter.cycle_loop_enable || (emitter_inst.local_time <= cycle_len);
|
||||
if (p_def.emitter.max_emission_count > 0 &&
|
||||
emitter_inst.emitted_count >= p_def.emitter.max_emission_count &&
|
||||
!p_def.emitter.cycle_loop_enable) {
|
||||
can_emit = false;
|
||||
}
|
||||
|
||||
if (can_emit) {
|
||||
emitter_inst.emission_accumulator += emission_rate * dt;
|
||||
while (emitter_inst.emission_accumulator >= 1.0f) {
|
||||
emitter_inst.emission_accumulator -= 1.0f;
|
||||
if (p_def.emitter.max_emission_count > 0 &&
|
||||
emitter_inst.emitted_count >= p_def.emitter.max_emission_count &&
|
||||
!p_def.emitter.cycle_loop_enable) {
|
||||
break;
|
||||
}
|
||||
|
||||
ParticleState p;
|
||||
p.pos = p_def.position;
|
||||
if (!m_has_custom_matrix) {
|
||||
p.pos += m_position;
|
||||
} else {
|
||||
p.pos = m_world_matrix.transform_point(p.pos);
|
||||
}
|
||||
|
||||
float vel_mag = interpolate_time_event(p_def.emitter.time_emitting_velocity, current_cycle_time, 50.0f);
|
||||
Vec3 dir = p_def.emitter.emitting_direction;
|
||||
if (dir.length_sq() < 1e-4f) {
|
||||
dir = {0.0f, 0.0f, 1.0f};
|
||||
} else {
|
||||
dir = dir.normalized();
|
||||
}
|
||||
|
||||
p.vel = dir * vel_mag;
|
||||
p.max_life = interpolate_time_event(p_def.emitter.time_life_time, current_cycle_time, 1.5f);
|
||||
if (p.max_life <= 0.0f) p.max_life = 0.1f;
|
||||
p.life = 0.0f;
|
||||
p.base_size_x = interpolate_time_event(p_def.emitter.time_size_x, current_cycle_time, 32.0f);
|
||||
p.base_size_y = interpolate_time_event(p_def.emitter.time_size_y, current_cycle_time, 32.0f);
|
||||
p.rot_speed = p_def.particle.rotation_speed;
|
||||
|
||||
emitter_inst.particles.push_back(p);
|
||||
emitter_inst.emitted_count++;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Update active particles
|
||||
for (auto it = emitter_inst.particles.begin(); it != emitter_inst.particles.end();) {
|
||||
it->life += dt;
|
||||
if (it->life >= it->max_life) {
|
||||
it = emitter_inst.particles.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
float norm_life = it->life / it->max_life;
|
||||
float grav = interpolate_time_event(p_def.particle.time_gravity, norm_life, 0.0f);
|
||||
float air_res = interpolate_time_event(p_def.particle.time_air_resistance, norm_life, 0.0f);
|
||||
|
||||
it->vel.z -= grav * dt;
|
||||
if (air_res > 0.0f) {
|
||||
float drag = std::max(0.0f, 1.0f - air_res * dt);
|
||||
it->vel *= drag;
|
||||
}
|
||||
|
||||
it->pos += it->vel * dt;
|
||||
it->rot += it->rot_speed * dt;
|
||||
++it;
|
||||
}
|
||||
|
||||
if (!emitter_inst.particles.empty() || can_emit) {
|
||||
any_active = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!any_active) {
|
||||
m_is_alive = false;
|
||||
}
|
||||
}
|
||||
|
||||
void EffectInstance::build_draw_batches(const Vec3& camera_pos,
|
||||
const Vec3& camera_dir,
|
||||
const Vec3& camera_up,
|
||||
std::vector<ParticleDrawBatch>& out_batches) {
|
||||
if (!m_data) return;
|
||||
|
||||
Vec3 cam_fwd = camera_dir.normalized();
|
||||
Vec3 cam_up = camera_up.normalized();
|
||||
Vec3 cam_right = Vec3::cross(cam_fwd, cam_up).normalized();
|
||||
|
||||
const auto& particles_def = m_data->get_particles();
|
||||
|
||||
for (size_t i = 0; i < particles_def.size(); ++i) {
|
||||
const auto& p_def = particles_def[i];
|
||||
const auto& emitter_inst = m_emitters[i];
|
||||
if (emitter_inst.particles.empty()) continue;
|
||||
|
||||
ParticleDrawBatch batch;
|
||||
if (!p_def.particle.texture_files.empty()) {
|
||||
batch.texture = p_def.particle.texture_files[0];
|
||||
}
|
||||
batch.src_blend = p_def.particle.src_blend_type;
|
||||
batch.dest_blend = p_def.particle.dest_blend_type;
|
||||
batch.color_op = p_def.particle.color_op_type;
|
||||
|
||||
for (const auto& p : emitter_inst.particles) {
|
||||
float norm_life = p.life / p.max_life;
|
||||
|
||||
float scale_x = interpolate_time_event(p_def.particle.time_scale_x, norm_life, 1.0f);
|
||||
float scale_y = interpolate_time_event(p_def.particle.time_scale_y, norm_life, 1.0f);
|
||||
float half_w = (p.base_size_x * scale_x) * 0.5f;
|
||||
float half_h = (p.base_size_y * scale_y) * 0.5f;
|
||||
|
||||
Color4 col;
|
||||
col.r = interpolate_time_event(p_def.particle.time_color_r, norm_life, 1.0f);
|
||||
col.g = interpolate_time_event(p_def.particle.time_color_g, norm_life, 1.0f);
|
||||
col.b = interpolate_time_event(p_def.particle.time_color_b, norm_life, 1.0f);
|
||||
col.a = interpolate_time_event(p_def.particle.time_alpha, norm_life, 1.0f);
|
||||
|
||||
Vec3 right_vec = cam_right;
|
||||
Vec3 up_vec = cam_up;
|
||||
|
||||
if (p_def.particle.billboard_type == 2) {
|
||||
// Cylindrical Y/Z billboard
|
||||
Vec3 to_cam = (camera_pos - p.pos).normalized();
|
||||
to_cam.z = 0.0f;
|
||||
to_cam = to_cam.normalized();
|
||||
right_vec = Vec3::cross({0.0f, 0.0f, 1.0f}, to_cam).normalized();
|
||||
up_vec = {0.0f, 0.0f, 1.0f};
|
||||
} else if (p_def.particle.billboard_type == 3) {
|
||||
// Flat horizontal billboard
|
||||
right_vec = {1.0f, 0.0f, 0.0f};
|
||||
up_vec = {0.0f, 1.0f, 0.0f};
|
||||
}
|
||||
|
||||
uint32_t base_idx = static_cast<uint32_t>(batch.vertices.size());
|
||||
|
||||
// 4 Quad vertices
|
||||
Vec3 p0 = p.pos - right_vec * half_w - up_vec * half_h;
|
||||
Vec3 p1 = p.pos + right_vec * half_w - up_vec * half_h;
|
||||
Vec3 p2 = p.pos + right_vec * half_w + up_vec * half_h;
|
||||
Vec3 p3 = p.pos - right_vec * half_w + up_vec * half_h;
|
||||
|
||||
batch.vertices.push_back({p0, col, 0.0f, 0.0f});
|
||||
batch.vertices.push_back({p1, col, 1.0f, 0.0f});
|
||||
batch.vertices.push_back({p2, col, 1.0f, 1.0f});
|
||||
batch.vertices.push_back({p3, col, 0.0f, 1.0f});
|
||||
|
||||
batch.indices.push_back(base_idx + 0);
|
||||
batch.indices.push_back(base_idx + 1);
|
||||
batch.indices.push_back(base_idx + 2);
|
||||
batch.indices.push_back(base_idx + 0);
|
||||
batch.indices.push_back(base_idx + 2);
|
||||
batch.indices.push_back(base_idx + 3);
|
||||
}
|
||||
|
||||
if (!batch.vertices.empty()) {
|
||||
out_batches.push_back(std::move(batch));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "effect/effect_manager.h"
|
||||
|
||||
namespace effect {
|
||||
|
||||
void EffectManager::clear() {
|
||||
m_effects.clear();
|
||||
m_flying.clear();
|
||||
m_effect_cache.clear();
|
||||
m_flying_cache.clear();
|
||||
}
|
||||
|
||||
std::shared_ptr<EffectData> EffectManager::get_or_load_effect(const std::string& filepath) {
|
||||
auto it = m_effect_cache.find(filepath);
|
||||
if (it != m_effect_cache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
auto data = std::make_shared<EffectData>();
|
||||
if (data->load_file(filepath)) {
|
||||
m_effect_cache[filepath] = data;
|
||||
return data;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<FlyingData> EffectManager::get_or_load_flying(const std::string& filepath) {
|
||||
auto it = m_flying_cache.find(filepath);
|
||||
if (it != m_flying_cache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
auto data = std::make_shared<FlyingData>();
|
||||
if (data->load_file(filepath)) {
|
||||
m_flying_cache[filepath] = data;
|
||||
return data;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<EffectInstance> EffectManager::spawn_effect(const std::string& filepath, const Vec3& pos) {
|
||||
auto data = get_or_load_effect(filepath);
|
||||
if (!data) return nullptr;
|
||||
|
||||
auto inst = std::make_shared<EffectInstance>(data);
|
||||
inst->set_position(pos);
|
||||
m_effects.push_back(inst);
|
||||
return inst;
|
||||
}
|
||||
|
||||
std::shared_ptr<FlyingInstance> EffectManager::spawn_flying(const std::string& filepath, const Vec3& start, const Vec3& target) {
|
||||
auto data = get_or_load_flying(filepath);
|
||||
if (!data) return nullptr;
|
||||
|
||||
auto inst = std::make_shared<FlyingInstance>(data, start, target);
|
||||
m_flying.push_back(inst);
|
||||
return inst;
|
||||
}
|
||||
|
||||
void EffectManager::update(float dt) {
|
||||
for (auto it = m_effects.begin(); it != m_effects.end();) {
|
||||
(*it)->update(dt);
|
||||
if (!(*it)->is_alive()) {
|
||||
it = m_effects.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto it = m_flying.begin(); it != m_flying.end();) {
|
||||
bool alive = (*it)->update(dt);
|
||||
if (!alive || !(*it)->is_alive()) {
|
||||
it = m_flying.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EffectManager::build_draw_batches(const Vec3& camera_pos,
|
||||
const Vec3& camera_dir,
|
||||
const Vec3& camera_up,
|
||||
std::vector<ParticleDrawBatch>& out_batches) {
|
||||
for (auto& eff : m_effects) {
|
||||
eff->build_draw_batches(camera_pos, camera_dir, camera_up, out_batches);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,142 @@
|
||||
#include "effect/flying_data.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace effect {
|
||||
|
||||
namespace {
|
||||
|
||||
float to_float(const std::string& s) {
|
||||
return std::strtof(s.c_str(), nullptr);
|
||||
}
|
||||
|
||||
int to_int(const std::string& s) {
|
||||
return std::atoi(s.c_str());
|
||||
}
|
||||
|
||||
std::string to_lower(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
|
||||
return std::tolower(c);
|
||||
});
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<std::string> tokenize(const std::string& content) {
|
||||
std::vector<std::string> tokens;
|
||||
size_t i = 0;
|
||||
size_t n = content.size();
|
||||
|
||||
while (i < n) {
|
||||
while (i < n && (content[i] == ' ' || content[i] == '\t' || content[i] == '\r' || content[i] == '\n')) {
|
||||
++i;
|
||||
}
|
||||
if (i >= n) break;
|
||||
|
||||
if (i + 1 < n && content[i] == '/' && content[i + 1] == '/') {
|
||||
while (i < n && content[i] != '\n') ++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (content[i] == '{' || content[i] == '}') {
|
||||
tokens.push_back(std::string(1, content[i]));
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (content[i] == '"') {
|
||||
++i;
|
||||
size_t start = i;
|
||||
while (i < n && content[i] != '"') ++i;
|
||||
tokens.push_back(content.substr(start, i - start));
|
||||
if (i < n && content[i] == '"') ++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t start = i;
|
||||
while (i < n && content[i] != ' ' && content[i] != '\t' && content[i] != '\r' && content[i] != '\n' &&
|
||||
content[i] != '{' && content[i] != '}' && content[i] != '"') {
|
||||
++i;
|
||||
}
|
||||
tokens.push_back(content.substr(start, i - start));
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
bool FlyingData::load_file(const std::filesystem::path& filepath) {
|
||||
std::ifstream file(filepath, std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
std::stringstream buf;
|
||||
buf << file.rdbuf();
|
||||
return load_from_string(buf.str());
|
||||
}
|
||||
|
||||
bool FlyingData::load_from_string(const std::string& content) {
|
||||
auto tokens = tokenize(content);
|
||||
size_t idx = 0;
|
||||
size_t total = tokens.size();
|
||||
|
||||
while (idx < total) {
|
||||
std::string tok = to_lower(tokens[idx]);
|
||||
|
||||
if (tok == "initialvelocity" && idx + 1 < total) {
|
||||
++idx; initial_velocity = to_float(tokens[idx++]);
|
||||
} else if (tok == "gravity" && idx + 1 < total) {
|
||||
++idx; gravity = to_float(tokens[idx++]);
|
||||
} else if (tok == "bombrange" && idx + 1 < total) {
|
||||
++idx; bomb_range = to_float(tokens[idx++]);
|
||||
} else if (tok == "bombeffect" && idx + 1 < total) {
|
||||
++idx; bomb_effect = tokens[idx++];
|
||||
} else if (tok == "homingflag" && idx + 1 < total) {
|
||||
++idx; homing_flag = (to_int(tokens[idx++]) != 0);
|
||||
} else if (tok == "homingstarttime" && idx + 1 < total) {
|
||||
++idx; homing_start_time = to_float(tokens[idx++]);
|
||||
} else if (tok == "homingmaxangle" && idx + 1 < total) {
|
||||
++idx; homing_max_angle = to_float(tokens[idx++]);
|
||||
} else if (tok == "range" && idx + 1 < total) {
|
||||
++idx; range = to_float(tokens[idx++]);
|
||||
} else if (tok == "acceleration" && idx + 3 < total) {
|
||||
++idx;
|
||||
acceleration.x = to_float(tokens[idx++]);
|
||||
acceleration.y = to_float(tokens[idx++]);
|
||||
acceleration.z = to_float(tokens[idx++]);
|
||||
} else if (tok == "group" && idx + 1 < total && to_lower(tokens[idx + 1]) == "attachdata") {
|
||||
idx += 2;
|
||||
if (idx < total && tokens[idx] == "{") ++idx;
|
||||
int depth = 1;
|
||||
while (idx < total && depth > 0) {
|
||||
std::string a_tok = to_lower(tokens[idx]);
|
||||
if (tokens[idx] == "{") { ++depth; ++idx; continue; }
|
||||
if (tokens[idx] == "}") { --depth; ++idx; continue; }
|
||||
|
||||
if (a_tok == "type" && idx + 1 < total) {
|
||||
++idx; attach.type = to_int(tokens[idx++]);
|
||||
} else if (a_tok == "flytype" && idx + 1 < total) {
|
||||
++idx; attach.fly_type = to_int(tokens[idx++]);
|
||||
} else if (a_tok == "attachfile" && idx + 1 < total) {
|
||||
++idx; attach.attach_file = tokens[idx++];
|
||||
} else if (a_tok == "tailflag" && idx + 1 < total) {
|
||||
++idx; attach.has_tail = (to_int(tokens[idx++]) != 0);
|
||||
} else if (a_tok == "taillength" && idx + 1 < total) {
|
||||
++idx; attach.tail_length = to_float(tokens[idx++]);
|
||||
} else if (a_tok == "tailsize" && idx + 1 < total) {
|
||||
++idx; attach.tail_size = to_float(tokens[idx++]);
|
||||
} else if (a_tok == "tailshaperect" && idx + 1 < total) {
|
||||
++idx; attach.rect_shape = (to_int(tokens[idx++]) != 0);
|
||||
} else {
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "effect/flying_instance.h"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
namespace effect {
|
||||
|
||||
FlyingInstance::FlyingInstance(std::shared_ptr<FlyingData> data, const Vec3& start_pos, const Vec3& target_pos)
|
||||
: m_data(std::move(data)), m_position(start_pos), m_target(target_pos) {
|
||||
Vec3 dir = (m_target - m_position).normalized();
|
||||
float speed = m_data ? m_data->initial_velocity : 2000.0f;
|
||||
m_velocity = dir * speed;
|
||||
m_trail.push_back({m_position, 0.0f});
|
||||
}
|
||||
|
||||
bool FlyingInstance::update(float dt) {
|
||||
if (!m_is_alive || !m_data) return false;
|
||||
|
||||
m_age += dt;
|
||||
|
||||
// Check hit condition
|
||||
Vec3 to_target = m_target - m_position;
|
||||
float dist = to_target.length();
|
||||
if (dist <= m_data->bomb_range) {
|
||||
m_is_alive = false;
|
||||
if (m_hit_cb) {
|
||||
m_hit_cb(m_position);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Homing steering
|
||||
if (m_data->homing_flag && m_age >= m_data->homing_start_time && dist > 1e-4f) {
|
||||
Vec3 desired_dir = to_target.normalized();
|
||||
float current_speed = m_velocity.length();
|
||||
if (current_speed > 1e-4f) {
|
||||
Vec3 current_dir = m_velocity / current_speed;
|
||||
float dot_val = std::clamp(Vec3::dot(current_dir, desired_dir), -1.0f, 1.0f);
|
||||
float angle = std::acos(dot_val);
|
||||
|
||||
// Maximum turn angle this frame (homing_max_angle in radians or degrees)
|
||||
float max_turn = m_data->homing_max_angle * dt;
|
||||
if (angle <= max_turn) {
|
||||
current_dir = desired_dir;
|
||||
} else {
|
||||
// Slerp towards desired direction
|
||||
float t = max_turn / angle;
|
||||
current_dir = Vec3::lerp(current_dir, desired_dir, t).normalized();
|
||||
}
|
||||
m_velocity = current_dir * current_speed;
|
||||
}
|
||||
}
|
||||
|
||||
// Ballistic physics: acceleration & gravity
|
||||
m_velocity += m_data->acceleration * dt;
|
||||
m_velocity.z -= m_data->gravity * dt;
|
||||
|
||||
Vec3 step = m_velocity * dt;
|
||||
m_position += step;
|
||||
m_distance_traveled += step.length();
|
||||
|
||||
// Check range limit
|
||||
if (m_distance_traveled >= m_data->range) {
|
||||
m_is_alive = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update trail
|
||||
if (m_data->attach.has_tail) {
|
||||
m_trail.push_back({m_position, m_age});
|
||||
float max_tail_time = m_data->attach.tail_length > 0.0f ? m_data->attach.tail_length : 0.2f;
|
||||
while (!m_trail.empty() && (m_age - m_trail.front().time) > max_tail_time) {
|
||||
m_trail.erase(m_trail.begin());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "effect/time_event.h"
|
||||
|
||||
namespace effect {
|
||||
|
||||
float interpolate_time_event(const std::vector<TimeEvent>& events, float time, float default_val) {
|
||||
if (events.empty()) return default_val;
|
||||
if (events.size() == 1 || time <= events.front().time) return events.front().value;
|
||||
if (time >= events.back().time) return events.back().value;
|
||||
|
||||
for (size_t i = 0; i + 1 < events.size(); ++i) {
|
||||
if (time >= events[i].time && time <= events[i + 1].time) {
|
||||
float dt = events[i + 1].time - events[i].time;
|
||||
if (dt < 1e-6f) return events[i].value;
|
||||
float t = (time - events[i].time) / dt;
|
||||
return events[i].value + t * (events[i + 1].value - events[i].value);
|
||||
}
|
||||
}
|
||||
return events.back().value;
|
||||
}
|
||||
|
||||
} // namespace effect
|
||||
@@ -0,0 +1,251 @@
|
||||
#include "effect/effect.h"
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static int g_failures = 0;
|
||||
#define TEST_CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL [" << __FILE__ << ":" << __LINE__ << "]: " #cond << std::endl; \
|
||||
++g_failures; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
void test_math_and_time_events() {
|
||||
std::cout << "[Test] Math & TimeEvent interpolation..." << std::endl;
|
||||
|
||||
effect::Vec3 v1{1.0f, 2.0f, 3.0f};
|
||||
effect::Vec3 v2{4.0f, 5.0f, 6.0f};
|
||||
effect::Vec3 v3 = v1 + v2;
|
||||
TEST_CHECK(v3.x == 5.0f && v3.y == 7.0f && v3.z == 9.0f);
|
||||
|
||||
float d = effect::Vec3::dot({1, 0, 0}, {0, 1, 0});
|
||||
TEST_CHECK(std::abs(d) < 1e-5f);
|
||||
|
||||
effect::Vec3 c = effect::Vec3::cross({1, 0, 0}, {0, 1, 0});
|
||||
TEST_CHECK(std::abs(c.x) < 1e-5f && std::abs(c.y) < 1e-5f && std::abs(c.z - 1.0f) < 1e-5f);
|
||||
|
||||
std::vector<effect::TimeEvent> events = {
|
||||
{0.0f, 10.0f},
|
||||
{1.0f, 20.0f},
|
||||
{3.0f, 40.0f}
|
||||
};
|
||||
|
||||
TEST_CHECK(effect::interpolate_time_event(events, -0.5f) == 10.0f);
|
||||
TEST_CHECK(effect::interpolate_time_event(events, 0.0f) == 10.0f);
|
||||
TEST_CHECK(effect::interpolate_time_event(events, 0.5f) == 15.0f);
|
||||
TEST_CHECK(effect::interpolate_time_event(events, 1.0f) == 20.0f);
|
||||
TEST_CHECK(effect::interpolate_time_event(events, 2.0f) == 30.0f);
|
||||
TEST_CHECK(effect::interpolate_time_event(events, 3.0f) == 40.0f);
|
||||
TEST_CHECK(effect::interpolate_time_event(events, 5.0f) == 40.0f);
|
||||
}
|
||||
|
||||
void test_effect_data_and_simulation() {
|
||||
std::cout << "[Test] Effect script parsing & particle simulation..." << std::endl;
|
||||
|
||||
std::string script = R"(
|
||||
BoundingSphereRadius 120.000000
|
||||
BoundingSpherePosition 10.000000 20.000000 30.000000
|
||||
|
||||
Group Particle
|
||||
{
|
||||
StartTime 0.000000
|
||||
Position 0.000000 0.000000 50.000000
|
||||
|
||||
Group EmitterProperty
|
||||
{
|
||||
MaxEmissionCount 10
|
||||
CycleLength 1.000000
|
||||
CycleLoopEnable 1
|
||||
EmittingDirection 0.000000 0.000000 1.000000
|
||||
|
||||
List TimeEventEmittingVelocity
|
||||
{
|
||||
0.000000 100.000000
|
||||
}
|
||||
List TimeEventEmissionCountPerSecond
|
||||
{
|
||||
0.000000 50.000000
|
||||
}
|
||||
List TimeEventLifeTime
|
||||
{
|
||||
0.000000 1.000000
|
||||
}
|
||||
List TimeEventSizeX
|
||||
{
|
||||
0.000000 20.000000
|
||||
}
|
||||
List TimeEventSizeY
|
||||
{
|
||||
0.000000 20.000000
|
||||
}
|
||||
}
|
||||
|
||||
Group ParticleProperty
|
||||
{
|
||||
SrcBlendType 5
|
||||
DestBlendType 2
|
||||
BillboardType 1
|
||||
RotationSpeed 2.000000
|
||||
|
||||
List TimeEventGravity
|
||||
{
|
||||
0.000000 9.800000
|
||||
}
|
||||
List TimeEventAlpha
|
||||
{
|
||||
0.000000 0.000000
|
||||
0.500000 1.000000
|
||||
1.000000 0.000000
|
||||
}
|
||||
List TextureFiles
|
||||
{
|
||||
"d:/ymir work/effect/flare.dds"
|
||||
}
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
auto eff_data = std::make_shared<effect::EffectData>();
|
||||
TEST_CHECK(eff_data->load_from_string(script));
|
||||
TEST_CHECK(eff_data->get_bounding_sphere_radius() == 120.0f);
|
||||
TEST_CHECK(eff_data->get_bounding_sphere_position().x == 10.0f);
|
||||
TEST_CHECK(eff_data->get_particles().size() == 1);
|
||||
|
||||
const auto& p0 = eff_data->get_particles()[0];
|
||||
TEST_CHECK(p0.particle.texture_files.size() == 1);
|
||||
TEST_CHECK(p0.particle.texture_files[0] == "d:/ymir work/effect/flare.dds");
|
||||
TEST_CHECK(p0.particle.billboard_type == 1);
|
||||
TEST_CHECK(p0.particle.rotation_speed == 2.0f);
|
||||
|
||||
// Test simulation
|
||||
effect::EffectInstance instance(eff_data);
|
||||
instance.set_position(0.0f, 0.0f, 0.0f);
|
||||
TEST_CHECK(instance.is_alive());
|
||||
TEST_CHECK(instance.get_active_particle_count() == 0);
|
||||
|
||||
// Step simulation by 0.1s -> should emit particles
|
||||
instance.update(0.1f);
|
||||
TEST_CHECK(instance.get_active_particle_count() > 0);
|
||||
|
||||
// Verify drawing commands / billboards
|
||||
std::vector<effect::ParticleDrawBatch> batches;
|
||||
instance.build_draw_batches({0.0f, -100.0f, 50.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, batches);
|
||||
TEST_CHECK(!batches.empty());
|
||||
if (!batches.empty()) {
|
||||
TEST_CHECK(batches[0].texture == "d:/ymir work/effect/flare.dds");
|
||||
TEST_CHECK(batches[0].vertices.size() >= 4);
|
||||
TEST_CHECK(batches[0].indices.size() >= 6);
|
||||
}
|
||||
}
|
||||
|
||||
void test_flying_homing_missile() {
|
||||
std::cout << "[Test] Flying homing missile physics & hit..." << std::endl;
|
||||
|
||||
std::string fly_script = R"(
|
||||
InitialVelocity 1000.000000
|
||||
Gravity 0.000000
|
||||
BombRange 15.000000
|
||||
HomingFlag 1
|
||||
HomingMaxAngle 10.000000
|
||||
Range 5000.000000
|
||||
Group AttachData
|
||||
{
|
||||
Type 1
|
||||
AttachFile "arrow.mse"
|
||||
TailFlag 1
|
||||
TailLength 0.200000
|
||||
}
|
||||
)";
|
||||
|
||||
auto fly_data = std::make_shared<effect::FlyingData>();
|
||||
TEST_CHECK(fly_data->load_from_string(fly_script));
|
||||
TEST_CHECK(fly_data->initial_velocity == 1000.0f);
|
||||
TEST_CHECK(fly_data->bomb_range == 15.0f);
|
||||
TEST_CHECK(fly_data->homing_flag == true);
|
||||
TEST_CHECK(fly_data->attach.has_tail == true);
|
||||
|
||||
effect::Vec3 start{0.0f, 0.0f, 0.0f};
|
||||
effect::Vec3 target{100.0f, 20.0f, 0.0f};
|
||||
|
||||
bool hit_called = false;
|
||||
effect::Vec3 hit_location;
|
||||
|
||||
effect::FlyingInstance fly_inst(fly_data, start, target);
|
||||
fly_inst.set_hit_callback([&](const effect::Vec3& loc) {
|
||||
hit_called = true;
|
||||
hit_location = loc;
|
||||
});
|
||||
|
||||
// Step physics towards target
|
||||
float dt = 0.02f;
|
||||
for (int step = 0; step < 50; ++step) {
|
||||
if (!fly_inst.is_alive()) break;
|
||||
fly_inst.update(dt);
|
||||
}
|
||||
|
||||
TEST_CHECK(hit_called);
|
||||
TEST_CHECK((hit_location - target).length() <= fly_data->bomb_range);
|
||||
}
|
||||
|
||||
void test_real_effect_files_if_present() {
|
||||
std::cout << "[Test] Real effect files check..." << std::endl;
|
||||
std::vector<std::string> mse_paths = {
|
||||
"metin2-client/build/zh-locale/locale-src/zh/effect/gm.mse",
|
||||
"../../metin2-client/build/zh-locale/locale-src/zh/effect/gm.mse",
|
||||
"ava2-client/ymir work/pc/warrior/effect/geom_4_badak.mse",
|
||||
"../../ava2-client/ymir work/pc/warrior/effect/geom_4_badak.mse"
|
||||
};
|
||||
|
||||
for (const auto& path : mse_paths) {
|
||||
if (fs::exists(path)) {
|
||||
effect::EffectData data;
|
||||
bool ok = data.load_file(path);
|
||||
TEST_CHECK(ok);
|
||||
if (ok) {
|
||||
std::cout << " -> Loaded real MSE (" << path << "), particles count: "
|
||||
<< data.get_particles().size() << std::endl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> msf_paths = {
|
||||
"ava2-client/ymir work/pc/warrior/effect/geompung.msf",
|
||||
"../../ava2-client/ymir work/pc/warrior/effect/geompung.msf"
|
||||
};
|
||||
|
||||
for (const auto& path : msf_paths) {
|
||||
if (fs::exists(path)) {
|
||||
effect::FlyingData data;
|
||||
bool ok = data.load_file(path);
|
||||
TEST_CHECK(ok);
|
||||
if (ok) {
|
||||
std::cout << " -> Loaded real MSF (" << path << "), init velocity: "
|
||||
<< data.initial_velocity << ", homing: "
|
||||
<< data.homing_flag << std::endl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "=== Running effect Unit Tests ===" << std::endl;
|
||||
test_math_and_time_events();
|
||||
test_effect_data_and_simulation();
|
||||
test_flying_homing_missile();
|
||||
test_real_effect_files_if_present();
|
||||
|
||||
if (g_failures == 0) {
|
||||
std::cout << "=== ALL effect TESTS PASSED ===" << std::endl;
|
||||
return 0;
|
||||
} else {
|
||||
std::cerr << "=== " << g_failures << " effect TESTS FAILED ===" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user