【神秘脚本】弹幕科技-001
分享一个弹幕系统黑科技具体能干嘛,应用场景是啥 一两句话也解释不明白
建议复制代码问AI
RUBY 代码
//=============================================================================
// RPG Maker Plugin - Formula Interpreter
// FormulaInterpreter.js
//=============================================================================
/*:
* @target MZ
* @plugindesc 公式解析器
* @author Limpid
* * @help
* * 【语法说明】
* v / V : 变量 (小写初始化获取,大写实时获取)
* s / S : 开关 (1/0)
* r/ R: 随机数 (0 到 n)
* g / G : 金钱
* i / I : 物品数量
* l / L : 角色等级
* p / P : 角色特定属性 (如 P)
* ^prop^ : 动态引用宿主对象的属性 (如 ^x^, ^target.hp^)
* @^prop^ : 静态引用宿主对象的属性 (初始化时固定)
* * 内置函数: a(x)[绝对值], s(max, i)[正弦], c(max, i)[余弦]
*/
var Imported = Imported || {};
Imported.FormulaInterpreter = true;
function FormulaInterpreter(){
throw new Error("This is a static class");
}
/**
* 内部数学方法映射 (内联化处理)
*/
FormulaInterpreter.methods = {
a: "Math.abs",
s: "(max, i) => Math.sin((Math.PI / 2) / (Number(max) || 1) * (Number(i) || 0))",
c: "(max, i) => Math.cos((Math.PI / 2) / (Number(max) || 1) * (Number(i) || 0))"
};
/**
* 语法解析注册表
*/
FormulaInterpreter.registry = {
static: {
'v': (id) => $gameVariables.value(Number(id)),
's': (id) => $gameSwitches.value(Number(id)) ? 1 : 0,
'r': (n) => Math.random() * Number(n),
'g': () => $gameParty.gold(),
'i': (id) => $gameParty.numItems($dataItems),
'l': (id) => $gameActors.actor(Number(id))?.level || 0,
'p': (id_attr) => {
const = id_attr.split('.');
return$gameActors.actor(Number(id))?. || 0;
}
},
dynamic: {
'V': (id) => `$gameVariables.value(${id})`,
'S': (id) => `($gameSwitches.value(${id}) ? 1 : 0)`,
'R': (id) => `(Math.random() * ${id})`,
'G': () => `$gameParty.gold()`,
'I': (id) => `$gameParty.numItems($dataItems[${id}])`,
'L': (id) => `$gameActors.actor(${id})?.level || 0`,
'P': (id_attr) => {
const = id_attr.split('.');
return`$gameActors.actor(${id})?.${attr} || 0`;
}
}
};
FormulaInterpreter._compilePool = new Map();
/**
* 创建响应式参数对象
* @param{Object} config - 公式配置库,如 { x: "^t^ * 2"}
* @param{Object} origin - 宿主对象
*/
FormulaInterpreter.create = function (config, origin){
if(!origin.params) origin.params = {};
if(!origin._fCache) origin._fCache = {};
if(!origin._fFrame) origin._fFrame = {};
for(let key in config){
const formula = config;
const compiled = this.compile(formula, origin);
if(compiled.type === 'dynamic'){
const func = compiled.func;
Object.defineProperty(origin.params, key, {
get: function (){
const now = Graphics.frameCount;
if(origin._fFrame === now)return origin._fCache;
const val = func(origin) ?? 0;
origin._fCache = val;
origin._fFrame = now;
return val;
},
enumerable: true,
configurable: true
});
}else{
origin.params = compiled.value;
}
}
};
/**
* 核心编译逻辑
*/
FormulaInterpreter.compile = function (formula, origin){
if(typeof formula !== 'string')return{ type: 'static', value: Number(formula) || 0};
if(this._compilePool.has(formula))return this._compilePool.get(formula);
let f = formula;
f = f.replace(/@\^([\w.]+)\^/g, (_, p) => {
returnp.split('.').reduce((acc, k) => (acc && acc !== undefined ? acc : 0), origin);
});
let last;
const staticRegex = /()\[([^\[\]]+)\]/g;
do{
last = f;
f = f.replace(staticRegex, (match, type, content) => {
const handler = this.registry.static;
return(handler && !match.includes('(')) ? handler(content) : match;
});
staticRegex.lastIndex = 0;
}while(last !== f);
const isDynamic = /\^|\[/.test(f);
const jsSyntax = this._toJsSyntax(f);
let result;
if(!isDynamic){
try {
const val = new Function(`return (${jsSyntax})`)();
result = { type: 'static', value: Number(val) || 0};
}catch(e){
result = { type: 'static', value: 0};
}
}else{
result = {
type: 'dynamic',
func: new Function('origin', `return (${jsSyntax});`)
};
}
this._compilePool.set(formula, result);
return result;
};
/**
* 语法转换
*/
FormulaInterpreter._toJsSyntax = function (f){
let js = f;
js = js.replace(/\^([\w.]+)\^/g, (_, p) => {
const chain = p.split('.').join('?.');
return`(origin?.${chain} ?? 0)`;
});
const dynamicRegex = /(+)\[([^\[\]]+)\]/g;
while(dynamicRegex.test(js)){
js = js.replace(dynamicRegex, (match, type, content) => {
const handler = this.registry.dynamic;
return handler ? handler(content) : match;
});
dynamicRegex.lastIndex = 0;
}
js = js.replace(/(\w*)\(([^)]*)\)/gi, (m, name, args) => {
const method = this.methods;
if(!method)return m;
return name.toLowerCase() === 'a' ? `Math.abs(${args})` : `(${method})(${args})`;
});
return js;
};
//=============================================================================
// 示例注入:Scene_Base
//=============================================================================
const _Scene_Base_initialize = Scene_Base.prototype.initialize;
Scene_Base.prototype.initialize = function(){
_Scene_Base_initialize.call(this);
// 示例数据结构
let data = {
x1: "^t^+R", // 随 startX, t 和 变量1 实时变化
y1: "R", // 每次访问 y1 都会获得一个新的随机数
size1: "r", // 仅在初始化时生成一个随机数,之后固定
color1: "'#1e90ff'" // 静态字符串
};
// 假设宿主有一些基础属性
this.startX = 100;
this.t = 0;
FormulaInterpreter.create(data, this);
};
const _Scene_Base_update = Scene_Base.prototype.update;
Scene_Base.prototype.update = function(){
_Scene_Base_update.call(this);
this.t++;
if(this.params){
console.log("Current Y1:", this.params.x1);
}
};
本帖来自P1论坛作者糜腥珊瑚态耄耋,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=498852若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
页:
[1]