扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 98|回复: 0

[转载发布] 【神秘脚本】弹幕科技-001

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    3 天前
  • 签到天数: 193 天

    连续签到: 1 天

    [LV.7]常住居民III

    2606

    主题

    790

    回帖

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    17633
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    21059

    灌水之王

    发表于 昨天 08:34 | 显示全部楼层 |阅读模式
    分享一个弹幕系统黑科技
    具体能干嘛,应用场景是啥 一两句话也解释不明白
    建议复制代码问AI

    RUBY 代码
    1. //=============================================================================
    2. // RPG Maker Plugin - Formula Interpreter
    3. // FormulaInterpreter.js
    4. //=============================================================================
    5. /*:
    6. * @target MZ
    7. * @plugindesc 公式解析器
    8. * @author Limpid
    9. * * @help
    10. * * 【语法说明】
    11. * v[id] / V[id] : 变量 (小写初始化获取,大写实时获取)
    12. * s[id] / S[id] : 开关 (1/0)
    13. * r[n]  / R[n]  : 随机数 (0 到 n)
    14. * g / G         : 金钱
    15. * i[id] / I[id] : 物品数量
    16. * l[id] / L[id] : 角色等级
    17. * p[id.attr] / P[id.attr] : 角色特定属性 (如 P[1.hp])
    18. * ^prop^        : 动态引用宿主对象的属性 (如 ^x^, ^target.hp^)
    19. * @^prop^       : 静态引用宿主对象的属性 (初始化时固定)
    20. * * 内置函数: a(x)[绝对值], s(max, i)[正弦], c(max, i)[余弦]
    21. */
    22. var Imported = Imported || {};
    23. Imported.FormulaInterpreter = true;
    24. function FormulaInterpreter(){
    25.     throw new Error("This is a static class");
    26. }
    27. /**
    28. * 内部数学方法映射 (内联化处理)
    29. */
    30. FormulaInterpreter.methods = {
    31.     a: "Math.abs",
    32.     s: "(max, i) => Math.sin((Math.PI / 2) / (Number(max) || 1) * (Number(i) || 0))",
    33.     c: "(max, i) => Math.cos((Math.PI / 2) / (Number(max) || 1) * (Number(i) || 0))"
    34. };
    35. /**
    36. * 语法解析注册表
    37. */
    38. FormulaInterpreter.registry = {
    39.     static: {
    40.         'v': (id) => $gameVariables.value(Number(id)),
    41.         's': (id) => $gameSwitches.value(Number(id)) ? 1 : 0,
    42.         'r': (n) => Math.random() * Number(n),
    43.         'g': () => $gameParty.gold(),
    44.         'i': (id) => $gameParty.numItems($dataItems[Number(id)]),
    45.         'l': (id) => $gameActors.actor(Number(id))?.level || 0,
    46.         'p': (id_attr) => {
    47.             const [id, attr] = id_attr.split('.');
    48.             return$gameActors.actor(Number(id))?.[attr] || 0;
    49.         }
    50.     },
    51.     dynamic: {
    52.         'V': (id) => `$gameVariables.value(${id})`,
    53.         'S': (id) => `($gameSwitches.value(${id}) ? 1 : 0)`,
    54.         'R': (id) => `(Math.random() * ${id})`,
    55.         'G': () => `$gameParty.gold()`,
    56.         'I': (id) => `$gameParty.numItems($dataItems[${id}])`,
    57.         'L': (id) => `$gameActors.actor(${id})?.level || 0`,
    58.         'P': (id_attr) => {
    59.             const [id, attr] = id_attr.split('.');
    60.             return`$gameActors.actor(${id})?.${attr} || 0`;
    61.         }
    62.     }
    63. };
    64. FormulaInterpreter._compilePool = new Map();
    65. /**
    66. * 创建响应式参数对象
    67. * @param{Object} config - 公式配置库,如 { x: "^t^ * 2"}
    68. * @param{Object} origin - 宿主对象
    69. */
    70. FormulaInterpreter.create = function (config, origin){
    71.     if(!origin.params) origin.params = {};
    72.     if(!origin._fCache) origin._fCache = {};
    73.     if(!origin._fFrame) origin._fFrame = {};
    74.     for(let key in config){
    75.         const formula = config[key];
    76.         const compiled = this.compile(formula, origin);
    77.         if(compiled.type === 'dynamic'){
    78.             const func = compiled.func;
    79.             Object.defineProperty(origin.params, key, {
    80.                 get: function (){
    81.                     const now = Graphics.frameCount;
    82.                     if(origin._fFrame[key] === now)return origin._fCache[key];
    83.                     const val = func(origin) ?? 0;
    84.                     origin._fCache[key] = val;
    85.                     origin._fFrame[key] = now;
    86.                     return val;
    87.                 },
    88.                 enumerable: true,
    89.                 configurable: true
    90.             });
    91.         }else{
    92.             origin.params[key] = compiled.value;
    93.         }
    94.     }
    95. };
    96. /**
    97. * 核心编译逻辑
    98. */
    99. FormulaInterpreter.compile = function (formula, origin){
    100.     if(typeof formula !== 'string')return{ type: 'static', value: Number(formula) || 0};
    101.     if(this._compilePool.has(formula))return this._compilePool.get(formula);
    102.     let f = formula;
    103.     f = f.replace(/@\^([\w.]+)\^/g, (_, p) => {
    104.         returnp.split('.').reduce((acc, k) => (acc && acc[k] !== undefined ? acc[k] : 0), origin);
    105.     });
    106.     let last;
    107.     const staticRegex = /([a-z])\[([^\[\]]+)\]/g;
    108.     do{
    109.         last = f;
    110.         f = f.replace(staticRegex, (match, type, content) => {
    111.             const handler = this.registry.static[type];
    112.             return(handler && !match.includes('(')) ? handler(content) : match;
    113.         });
    114.         staticRegex.lastIndex = 0;
    115.     }while(last !== f);
    116.     const isDynamic = /\^|[A-Z]\[/.test(f);
    117.     const jsSyntax = this._toJsSyntax(f);
    118.     let result;
    119.     if(!isDynamic){
    120.         try {
    121.             const val = new Function(`return (${jsSyntax})`)();
    122.             result = { type: 'static', value: Number(val) || 0};
    123.         }catch(e){
    124.             result = { type: 'static', value: 0};
    125.         }
    126.     }else{
    127.         result = {
    128.             type: 'dynamic',
    129.             func: new Function('origin', `return (${jsSyntax});`)
    130.         };
    131.     }
    132.     this._compilePool.set(formula, result);
    133.     return result;
    134. };
    135. /**
    136. * 语法转换
    137. */
    138. FormulaInterpreter._toJsSyntax = function (f){
    139.     let js = f;
    140.     js = js.replace(/\^([\w.]+)\^/g, (_, p) => {
    141.         const chain = p.split('.').join('?.');
    142.         return`(origin?.${chain} ?? 0)`;
    143.     });
    144.     const dynamicRegex = /([A-Z]+)\[([^\[\]]+)\]/g;
    145.     while(dynamicRegex.test(js)){
    146.         js = js.replace(dynamicRegex, (match, type, content) => {
    147.             const handler = this.registry.dynamic[type];
    148.             return handler ? handler(content) : match;
    149.         });
    150.         dynamicRegex.lastIndex = 0;
    151.     }
    152.     js = js.replace(/([a-z]\w*)\(([^)]*)\)/gi, (m, name, args) => {
    153.         const method = this.methods[name.toLowerCase()];
    154.         if(!method)return m;
    155.         return name.toLowerCase() === 'a' ? `Math.abs(${args})` : `(${method})(${args})`;
    156.     });
    157.     return js;
    158. };
    159. //=============================================================================
    160. // 示例注入:Scene_Base
    161. //=============================================================================
    162. const _Scene_Base_initialize = Scene_Base.prototype.initialize;
    163. Scene_Base.prototype.initialize = function(){
    164.     _Scene_Base_initialize.call(this);
    165.     // 示例数据结构
    166.     let data = {
    167.         x1: "^t^+R[100]", // 随 startX, t 和 变量1 实时变化
    168.         y1: "R[100]",                         // 每次访问 y1 都会获得一个新的随机数
    169.         size1: "r[100]",                      // 仅在初始化时生成一个随机数,之后固定
    170.         color1: "'#1e90ff'"                   // 静态字符串
    171.     };
    172.     // 假设宿主有一些基础属性
    173.     this.startX = 100;
    174.     this.t = 0;
    175.     FormulaInterpreter.create(data, this);
    176. };
    177. const _Scene_Base_update = Scene_Base.prototype.update;
    178. Scene_Base.prototype.update = function(){
    179.     _Scene_Base_update.call(this);
    180.     this.t++;
    181.     if(this.params){
    182.         console.log("Current Y1:", this.params.x1);
    183.     }
    184. };
    复制代码


                本帖来自P1论坛作者糜腥珊瑚态耄耋,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=498852  若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    x
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-4-24 13:50 , Processed in 0.087007 second(s), 52 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表