设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 101|回复: 0

[转载发布] Event Manager

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    7959

    主题

    864

    回帖

    3万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

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

    灌水之王

    发表于 4 天前 | 显示全部楼层 |阅读模式
    This is a javascript plugin that will not do anything on it's own, but can be useful for other scripters. This script essentially implements an observer pattern into RPG Maker. Event systems help modularize your code by sending messages between pieces of code that do not directly know about each other. They allow things to respond to a change in a state without constantly monitoring it in an update loop. You can read more about it here:
    Observer pattern - Wikipedia



                                            en.wikipedia.org                               




    This script hasn't been fully optimized and I'm sure there are some things that may not be fully standardized with RPG Makers coding style. It was made in a couple of hours. On every triple a project I've worked on so far, each engine has had one of these, so I wanted to make my own for my internal project. Hopefully this comes in handy for you!

    This was made with RPG Maker MV in mind. I have not tested this with RPG Maker MZ, but I don't see much of a reason why this wouldn't work with MZ as well.

    I made a small project with a couple of use-case examples! Nothing mind-blowing, but hopefully will provide you with a few ideas to get some ideas across for how it can be used! You can find that project here.

    Terms of Use: Free to use or modify for free or commercial projects. My only request is that you don't republish this (or your edits) and claim it as your own. If you want to give credit (not required), please give to Nathan Pringle and specify it was this script in particular.

                    JavaScript:       
    1. //=============================================================================
    2. // EventManager
    3. // by Nathan Pringle
    4. // Date: 2021-10-11
    5. //=============================================================================
    6. /*:
    7. * @plugindesc Adds event support for internal scripts.
    8. * @author Nathan Pringle
    9. *
    10. * @help
    11. * -----------------------------------------------------------------------------
    12. * Event systems help modularize your code by sending messages between pieces of
    13. * code that do not directly know about each other. They allow things to respond
    14. * to a change in a state without constantly monitoring it in an update loop. This is
    15. * generally known as an "Observer pattern". You can read more about it here:
    16. * https://en.wikipedia.org/wiki/Observer_pattern
    17. *
    18. * Usage: Free to use or modify for free or commercial projects. My only request
    19. * is that you don't republish this (or your edits) and claim it as your own. If
    20. * you want to give credit (not required), please give to Nathan Pringle and
    21. * specify it was this script in particular.
    22. *
    23. * -----------------------------------------------------------------------------
    24. * Methods Available:
    25. * -----------------------------------------------------------------------------
    26. * EventManager.StartListening ( string eventName, string methodName, method callbackMethod )
    27. *
    28. * string eventName: The event you want to listen to. When this event is invoked, the callbackMethod will be called.
    29. * string methodName: The string name of the method. This is simply used for lookup. When you stop listening, you will need to provide this string again.
    30. * method methodCallback: The method to call when the event is invoked.
    31. *
    32. * -----------------------------------------------------------------------------
    33. * EventManager.StopListening ( string eventName, string methodName )
    34. *
    35. * string eventName: The name of the event to stop listening to.
    36. * string methodName: The string name of the method. This is the same methodName that you used to start listening.
    37. *
    38. * -----------------------------------------------------------------------------
    39. * EventManager.Invoke ( string eventName )
    40. *
    41. * string eventName: This will call the event. Will call all methods that are listening for this event.
    42. *
    43. * -----------------------------------------------------------------------------
    44. * EventManager.InvokeWithParameter ( string eventName, var parameter )
    45. *
    46. * string eventName: This will call the event. Will call all methods that are listening for this event.
    47. * var parameter: Will pass a single parameter to all methods being invoked.
    48. *
    49. * -----------------------------------------------------------------------------
    50. * EventManager.InvokeAfterTime ( string eventName, float time )
    51. *
    52. * string eventName: same as above.
    53. * float time: how long it will take to call this event, in seconds
    54. *
    55. * -----------------------------------------------------------------------------
    56. * EventManager.InvokeWhenConditionIsTrue ( string eventName, method condition )
    57. *
    58. * string eventName: same as above.
    59. * method condition: a method that should only return true or false. Will be checked every frame. When the condition returns true, the event will be invoked once.
    60. *
    61. */
    62. NP_EventManager_DataManager_createGameObjects = DataManager.createGameObjects;
    63. DataManager.createGameObjects = function() {
    64.     NP_EventManager_DataManager_createGameObjects.call(this);
    65.     EventManager.setup();
    66. };
    67. NP_EventManager_Graphics_render = Graphics.render;
    68. Graphics.render = function(stage) {
    69.     NP_EventManager_Graphics_render.call(this, stage);
    70.     EventManager.Tick();
    71. }
    72. function EventManager() {
    73.     throw new Error('This is a static class');
    74. }
    75. /// Summary: Sets up the EventManager. Should only be called once.
    76. /// Returns: N/A
    77. EventManager.setup = function() {
    78.     if (!this._isSetup) {
    79.         this._isSetup = true;
    80.         this._eventDictionary = [];
    81.         this._eventsToCallAfterTime = [];
    82.         this._eventsToCallWhenCondition = [];
    83.         this._tickTime = 1.0 / 60.0;
    84.     }
    85. };
    86. /// Summary: This will call the event. Will call all methods that are listening for this event.
    87. /// Argument - eventName: The name of the event to invoke.
    88. /// Returns: N/A
    89. EventManager.Invoke = function(eventName) {
    90.     var eventExists = false;
    91.     var eventIndex = -1;
    92.     for (var i = 0; i < this._eventDictionary.length && !eventExists; ++i) {
    93.         var eventEntry = this._eventDictionary[i];
    94.         if (eventEntry.eventName == eventName) {
    95.             eventExists = true;
    96.             eventIndex = i;
    97.         }
    98.     }
    99.   
    100.     if (eventExists) {
    101.         var eventDetails = this._eventDictionary[eventIndex];
    102.         for (var i = 0; i < eventDetails["methods"].length; ++i) {
    103.             eventDetails["methods"][i]["methodCallback"].call(this);
    104.         }
    105.     }
    106. }
    107. /// Summary: This will call the event, but also pass a single parameter to all methods being invoked. Will call all methods that are listening for this event.
    108. /// Argument - eventName: The name of the event to invoke.
    109. /// Argument - parameter: The data to pass to the methods.
    110. /// Returns: N/A
    111. EventManager.InvokeWithParameter = function (eventName, parameter) {
    112.     var eventExists = false;
    113.     var eventIndex = -1;
    114.     for (var i = 0; i < this._eventDictionary.length && !eventExists; ++i) {
    115.         var eventEntry = this._eventDictionary[i];
    116.         if (eventEntry.eventName == eventName) {
    117.             eventExists = true;
    118.             eventIndex = i;
    119.         }
    120.     }
    121.     if (eventExists) {
    122.         var eventDetails = this._eventDictionary[eventIndex];
    123.         for (var i = 0; i < eventDetails["methods"].length; ++i) {
    124.             eventDetails["methods"][i]["methodCallback"].call(this, parameter);
    125.         }
    126.     }
    127. }
    128. /// Summary: This will call the event as invoke, but will do so after a certain amount of time.
    129. /// Argument - eventName: The name of the event to invoke.
    130. /// Argument - time: The amount of time in seconds.
    131. /// Returns: N/A
    132. EventManager.InvokeAfterTime = function (eventName, time) {
    133.     var entry = {};
    134.     entry["eventName"] = eventName;
    135.     entry["time"] = time;
    136.     this._eventsToCallAfterTime.push(entry);
    137. }
    138. /// Summary: This will call the event as invoke, but will do so only when a certain condition is true.
    139. /// Argument - eventName: The name of the event to invoke.
    140. /// Argument - condition: a method that should return either true or false. It will be checked every frame. When it returns true, the event will be invoked. Will only be invoked once.
    141. /// Returns: N/A
    142. EventManager.InvokeWhenConditionIsTrue = function (eventName, condition) {
    143.     var result = condition.call(this);
    144.     var entry = {};
    145.     entry["eventName"] = eventName;
    146.     entry["condition"] = condition;
    147.     this._eventsToCallWhenCondition.push(entry);
    148. }
    149. /// Summary: A single tick. Should not be called outside of Window.update
    150. /// Returns: N/A
    151. EventManager.Tick = function() {
    152.     for (var i = 0; i < this._eventsToCallAfterTime.length; ++i) {
    153.         this._eventsToCallAfterTime[i]["time"] -= this._tickTime;
    154.         if (this._eventsToCallAfterTime[i]["time"] <= 0.0) {
    155.             EventManager.Invoke(this._eventsToCallAfterTime[i]["eventName"]);
    156.             this._eventsToCallAfterTime.splice(i, 1);
    157.             --i;
    158.         }
    159.     }
    160.     for (var i = 0; i < this._eventsToCallWhenCondition.length; ++i) {
    161.         var result = this._eventsToCallWhenCondition[i]["condition"].call(this);
    162.         if (result == true) {
    163.             var eventName = this._eventsToCallWhenCondition[i]["eventName"];
    164.             this._eventsToCallWhenCondition.splice(i, 1);
    165.             --i;
    166.             EventManager.Invoke(eventName);
    167.         }
    168.     }
    169. }
    170. /// Summary: Starts listening for a specific event. When a piece of code calls the invoke method, this method will be called.
    171. /// Argument - eventName: The name of the event to listen to.
    172. /// Argument - methodName: The string name of the method. This is simply used for lookup. When you stop listening, you will need to provide this string again.
    173. /// Argument - methodCallback: The method to call when the event is invoked.
    174. /// Returns: N/A
    175. EventManager.StartListening = function(eventName, methodName, methodCallback) {
    176.     var eventExists = false;
    177.     var eventIndex = -1;
    178.     for (var i = 0; i < this._eventDictionary.length && !eventExists; ++i) {
    179.         var eventEntry = this._eventDictionary[i];
    180.         if (eventEntry.eventName == eventName) {
    181.             eventExists = true;
    182.             eventIndex = i;
    183.         }
    184.     }
    185.     if (!eventExists) {
    186.         eventIndex = this._eventDictionary.length;
    187.         var entry = {};
    188.         entry["eventName"] = eventName;
    189.         entry["methods"] = [];
    190.         this._eventDictionary.push(entry);
    191.     }
    192.     for (var i = 0; i < this._eventDictionary[eventIndex]["methods"].length; ++i) {
    193.         if (this._eventDictionary[eventIndex]["methods"][i]["methodName"] == methodName) {
    194.             throw new Error('Trying to add a method that already exists.');
    195.         }
    196.     }
    197.     var methodEntry = {};
    198.     methodEntry["methodName"] = methodName;
    199.     methodEntry["methodCallback"] = methodCallback = methodCallback;
    200.     this._eventDictionary[eventIndex]["methods"].push(methodEntry);
    201. }
    202. /// Summary: Stops listening for a specific event. The method will no longer be called when the event is invoked.
    203. /// Argument - eventName: The name of the event to stop listening to.
    204. /// Argument - methodName: The string name of the method. This is the same methodName that you used to start listening.
    205. /// Returns: N/A
    206. EventManager.StopListening = function(eventName, methodName) {
    207.     var eventExists = false;
    208.     var eventIndex = -1;
    209.     for (var i = 0; i < this._eventDictionary.length && !eventExists; ++i) {
    210.         var eventEntry = this._eventDictionary[i];
    211.         if (eventEntry.eventName == eventName) {
    212.             eventExists = true;
    213.             eventIndex = i;
    214.         }
    215.     }
    216.     if (!eventExists) {
    217.         throw new Error('Trying to remove a method that does not exist.');
    218.     }
    219.     var methodsArrayLength = this._eventDictionary[eventIndex]["methods"].length;
    220.     var methodIndex = -1;
    221.     for (var i = 0; i < this._eventDictionary[eventIndex]["methods"].length && methodIndex == -1; ++i) {
    222.         if (this._eventDictionary[eventIndex]["methods"][i]["methodName"] == methodName) {
    223.             methodIndex = i;
    224.         }
    225.     }
    226.     if (methodIndex == -1) {
    227.         throw new Error('Trying to remove a method that does not exist.');
    228.     }
    229.     if (methodsArrayLength == 1) {
    230.         this._eventDictionary.splice(eventIndex, 1);
    231.     }
    232.     else {
    233.         this._eventDictionary[eventIndex]["methods"].splice(methodIndex, 1);
    234.     }
    235. }
    复制代码




    本贴来自国际rpgmaker官方论坛作者:nathanlink169处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/event-manager.141246/

    本帖子中包含更多资源

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

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

    使用道具 举报

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

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

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

    立即查看

    聊天机器人
    Loading...

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

    GMT+8, 2026-8-1 07:38 , Processed in 0.063850 second(s), 53 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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