Files
yangxiangyuan 6dc79a2c12 feat: 新增多项功能并添加新工具
- 私人剪贴板新增限时直通URL生成功能
- doc_cloud_keeper新增标题复制按钮
- 新增贪蛇出动游戏工具
- 新增OSS中转直链工具
- 添加批量抽帧测试脚本
2026-08-11 10:22:31 +08:00

70 lines
2.4 KiB
JavaScript

/* 贪蛇出动 - 通用工具函数 */
(function () {
'use strict';
window.GS = window.GS || {};
const U = {};
GS.U = U;
U.rand = (a, b) => a + Math.random() * (b - a);
U.randInt = (a, b) => Math.floor(U.rand(a, b + 1));
U.pick = arr => arr[Math.floor(Math.random() * arr.length)];
U.clamp = (v, a, b) => Math.max(a, Math.min(b, v));
U.dist2 = (x1, y1, x2, y2) => { const dx = x2 - x1, dy = y2 - y1; return dx * dx + dy * dy; };
U.dist = (x1, y1, x2, y2) => Math.sqrt(U.dist2(x1, y1, x2, y2));
// 角度归一化到 [-PI, PI]
U.normAngle = a => {
while (a > Math.PI) a -= Math.PI * 2;
while (a < -Math.PI) a += Math.PI * 2;
return a;
};
// 数字格式化:>=1e6 显示为 "1,194k",否则千分位
U.fmt = n => {
n = Math.floor(n);
if (n >= 1e6) return Math.floor(n / 1000).toLocaleString('en-US') + 'k';
return n.toLocaleString('en-US');
};
// 北京时间日期串 YYYY-MM-DD(今日最佳用)
U.todayBJ = () => {
const d = new Date(Date.now() + (8 * 60 + new Date().getTimezoneOffset()) * 60000);
const m = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return d.getFullYear() + '-' + m + '-' + day;
};
U.nowStr = () => {
const d = new Date();
const p = x => String(x).padStart(2, '0');
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate()) + ' ' +
p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds());
};
// 简单空间哈希网格(金币拾取/碰撞加速)
class Grid {
constructor(cell) { this.cell = cell; this.m = new Map(); }
clear() { this.m.clear(); }
_key(cx, cy) { return cx + ',' + cy; }
insert(obj) {
const cx = Math.floor(obj.x / this.cell), cy = Math.floor(obj.y / this.cell);
const k = this._key(cx, cy);
let arr = this.m.get(k);
if (!arr) { arr = []; this.m.set(k, arr); }
arr.push(obj);
}
query(x, y, r) {
const out = [];
const c0 = Math.floor((x - r) / this.cell), c1 = Math.floor((x + r) / this.cell);
const r0 = Math.floor((y - r) / this.cell), r1 = Math.floor((y + r) / this.cell);
for (let cx = c0; cx <= c1; cx++) for (let cy = r0; cy <= r1; cy++) {
const arr = this.m.get(this._key(cx, cy));
if (arr) for (const o of arr) out.push(o);
}
return out;
}
}
U.Grid = Grid;
})();