78 lines
1.3 KiB
Plaintext
78 lines
1.3 KiB
Plaintext
// Core 层 - 数学工具
|
|
// 提供数学计算的工具函数
|
|
|
|
// 常量
|
|
const PI <- 3.14159265359;
|
|
const DEG_TO_RAD <- PI / 180.0;
|
|
const RAD_TO_DEG <- 180.0 / PI;
|
|
|
|
// 角度转弧度
|
|
function degToRad(deg) {
|
|
return deg * DEG_TO_RAD;
|
|
}
|
|
|
|
// 弧度转角度
|
|
function radToDeg(rad) {
|
|
return rad * RAD_TO_DEG;
|
|
}
|
|
|
|
// 限制数值在范围内
|
|
function clamp(value, min, max) {
|
|
if (value < min) return min;
|
|
if (value > max) return max;
|
|
return value;
|
|
}
|
|
|
|
// 线性插值
|
|
function lerp(a, b, t) {
|
|
return a + (b - a) * t;
|
|
}
|
|
|
|
// 获取符号
|
|
function sign(x) {
|
|
if (x > 0) return 1;
|
|
if (x < 0) return -1;
|
|
return 0;
|
|
}
|
|
|
|
// 向量2D类
|
|
Vector2 <- class {
|
|
x = 0.0;
|
|
y = 0.0;
|
|
|
|
constructor(_x = 0.0, _y = 0.0) {
|
|
x = _x;
|
|
y = _y;
|
|
}
|
|
|
|
function length() {
|
|
return sqrt(x * x + y * y);
|
|
}
|
|
|
|
function normalize() {
|
|
local len = length();
|
|
if (len > 0) {
|
|
return Vector2(x / len, y / len);
|
|
}
|
|
return Vector2(0, 0);
|
|
}
|
|
|
|
function add(v) {
|
|
return Vector2(x + v.x, y + v.y);
|
|
}
|
|
|
|
function sub(v) {
|
|
return Vector2(x - v.x, y - v.y);
|
|
}
|
|
|
|
function mul(s) {
|
|
return Vector2(x * s, y * s);
|
|
}
|
|
|
|
function toString() {
|
|
return "Vector2(" + x + ", " + y + ")";
|
|
}
|
|
};
|
|
|
|
print("Math utilities loaded");
|