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

[转载发布] Spawn.js, a simple event spawner

[复制链接]
累计送礼:
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

    灌水之王

    发表于 3 天前 | 显示全部楼层 |阅读模式
    Introduction:
    This plugin provide several functions to spawn or copy events.

    spawnEvent(which,tag, x, y,d)
    Spawn an event at location x, y
    Parameters:
    which: the event template, see 'Event template' below
    tag: if a dead event contains this tag string in its note, it will be revived instead, and 'which' will be ignored unless the dead event is not found. By default, an event is dead if its _erased property is true. If tag is false, the function will not try to find a dead event and always spawn a new one. If tag is a function, it will be used to search for a dead entity using its own rule. The function must have a parameter which is the event to be tested, if the entity is dead the funcion should return true, otherwise it returns false.
    x, y: location of the spawned event. Optional.
    d: direction of the spawned event. Optional.

    replaceEvent(e1,e2)
    Replace event e1 with e2. An event using 'Event template' e2 will be spawned and replace event e1.

    Game_Event.setPreserved(preserved)
    Set if the event is preserved. A preserved event will not reset if the player leaves current map and returns.
    Parameter preserved should be a boolean value.

    Game_Event.isPreserved()
    Check if the event is preserved.

    Game_Event.reload()
    Reload current event. If the event is foreign, it will use it's original position in the foreign map. If the event has been replaced, the original event will be loaded instead. Notice this is a 'had reset', so everything including self switches will be reset.

    $E(which)
    Return an event, parameter 'which' should be in the format of 'Event template', see below.

    $spr(which)
    Return an event's sprite, parameter 'which' should be in the format of 'Event template', see below. This is not part of the spawn functions, but just in case the sprite is changed for some reason.

    Event template:
    Any event from the map editor can be used as template to spawn.
    When using as a parameter, it can be any of the format below:

    • Event id: it should be an integer.
    • Event name: a string indicating the name of the event, if multiple events have the same name, the first one will be used.
    • Event tag: if name is not found, the function will try to find the string inside the note property. A tag can be any string, but it is recommended to use html like tags.
    • Event object: You can provide an event object to copy from
    • Object with an eventId method. Usually it means an Game_Interpreter object, so you can use 'this' as parameter in an event command script.
    • Event from another map: This is only used by spawnEvent function. An event from a foreign map will be spawned in current map, and can be preserved if you use setPreserved to change it's preserved property. In this case, the parameter should be a string in the form of 'event@map'. 'event' can be either id, name or tag. 'map' can be either map id, filename, or map name. For example, '1@Map001', 'cat@1', '<dog>@village_of_fire'...
    Source code:
    Spoiler: source
                    JavaScript:       
    1. //=============================================================================
    2. // Spawn.js
    3. //=============================================================================
    4. /*:
    5. * @plugindesc Spawn or copy events from current and other maps
    6. * @author utunnels
    7. *
    8. * @help This plugin provide several functions to spawn or copy events.
    9. spawnEvent(which,tag, x, y,d)
    10. Spawn an event at location x, y
    11. Parameters:
    12. which: the event template, see 'Event template' below
    13. tag: if a dead event contains this tag string in its note,
    14. it will be revived instead, and 'which' will be ignored unless
    15. the dead event is not found. By default, an event is dead if
    16. its _erased property is true. If tag is false, the function will
    17. not try to find a dead event and always spawn a new one.
    18. If tag is a function, it will be used to search for a dead entity
    19. using its own rule. The function must have a parameter which is
    20. the event to be tested, if the entity is dead the funcion should
    21. return true, otherwise it returns false.
    22. x, y: location of the spawned event.
    23. d: direction of the spawned event.
    24. replaceEvent(e1,e2)
    25. Replace event e1 with e2. An event using 'Event template' e2 will
    26. be spawned and replace event e1.
    27. Game_Event.setPreserved(preserved)
    28. Set if the event is preserved. A preserved event will not reset
    29. if the player leaves current map and returns.
    30. Parameter preserved should be a boolean value.
    31. Game_Event.isPreserved()
    32. Check if the event is preserved.
    33. Game_Event.reload()
    34. Reload current event. If the event is foreign, it will use it's
    35. original position in the foreign map. If the event has been replaced,
    36. the original event will be loaded instead. Notice this is a 'had reset',
    37. so everything including self switches will be reset.
    38. $E(which)
    39. Return an event, parameter 'which' should be in the format of
    40. 'Event template', see below.
    41. $spr(which)
    42. Return an event's sprite, parameter 'which' should be in the
    43. format of 'Event template', see below.
    44. Event template:
    45. Any event from the map editor can be used as template to spawn.
    46. When using as a parameter, it can be any of the format below:
    47. 1. Event id: it should be an integer.
    48. 2. Event name: a string indicating the name of the event,
    49. if multiple events have the same name, the first one will be used.
    50. 3. Event tag: if name is not found, the function will try to find
    51. the string inside the note property. A tag can be any string, but
    52. it is recommended to use html like tags.
    53. 4. Event object: You can provide an event object to copy from
    54. 5. Object with an eventId method. Usually it means an Game_Interpreter,
    55. So you can use 'this' as parameter in an event command script.
    56. 6. Event from another map: This is only used by spawnEvent function.
    57. An event from a foreign map will be spawned in current map, and can be
    58. preserved if you use setPreserved to change it's preserved property.
    59. In this case, the parameter should be a string in the form of 'event@map'.
    60. 'event' can be either id, name or tag. 'map' can be either map id, filename,
    61. or map name. For example, '1@Map001', 'cat@1', '<dog>@village_of_fire'...
    62. */
    63. Game_Event.prototype.originalEventId = function() {
    64.     return this._originalEventId||this._eventId;
    65. };
    66. Game_Event.prototype.event = function() {
    67.     var e = $dataMap.events[this.originalEventId()];
    68.     if(!e){
    69.       if(this._foreignMapId){
    70.         e=this.reloadForeignEvent();
    71.       }
    72.     }
    73.     return e;
    74. };
    75. Game_Event.prototype.reloadForeignEvent=function(){
    76.   var fmapid = this._foreignMapId,feid=this._foreignEventId,slot=this.originalEventId();
    77.   var map = loadMapJSONSync(fmapid);
    78.   var e = map.events[feid]
    79.   e = JSON.parse(JSON.stringify(e));
    80.   $dataMap.events[slot] = e;
    81.   return e;
    82. };
    83. Game_Event.prototype.reload = function() {
    84.   var thismap = loadMapJSONSync($gameMap.mapId());
    85.   var eid = this.eventId();
    86.   var oid = this.originalEventId();
    87.   if(!thismap.events[eid]){
    88.     if(!$dataMap.events[oid]){
    89.       this.reloadForeignEvent();
    90.     }
    91.     replaceEvent(this,oid,true);
    92.   }else{
    93.     replaceEvent(this,eid,true);
    94.   }
    95.   $gameSelfSwitches.setValue([$gameMap.mapId(),eid,'A'],false);
    96.   $gameSelfSwitches.setValue([$gameMap.mapId(),eid,'B'],false);
    97.   $gameSelfSwitches.setValue([$gameMap.mapId(),eid,'C'],false);
    98.   $gameSelfSwitches.setValue([$gameMap.mapId(),eid,'D'],false);
    99. };
    100. $mapCache = {};
    101. //Load a file from data folder, synchronous
    102. //Replace this with node fs version if necessary
    103. function loadDataJSONSync(filename){
    104.   var src = filename+'.json';
    105.   var xhr = new XMLHttpRequest(), result=null;
    106.   xhr.open('GET', 'data/' + src, false); xhr.overrideMimeType('application/json');
    107.   xhr.onload = function() {
    108.     if (xhr.status == 200) {
    109.       result = JSON.parse(xhr.responseText);
    110.     } else {
    111.       console.log('xhr status: ' + xhr.status + ', ' + filename);
    112.     }
    113.   };
    114.   xhr.send();
    115.   return result;
    116. }
    117. function loadMapJSONSync(mapId){
    118.   var filename;
    119.   if(/Map[0-9][0-9][0-9]/.test(mapId)){//Map117
    120.     filename = mapId;
    121.   }else if(!isNaN(mapId)){//117
    122.     filename = 'Map%1'.format(mapId.padZero(3));
    123.   }else{//map name
    124.     var mi = $dataMapInfos.findIndex(function(m){return m&&m.name==mapId;});
    125.     if(mi>0){
    126.       filename = 'Map%1'.format(mi.padZero(3));
    127.     }else{
    128.       return null;
    129.     }
    130.   }
    131.   var map = $mapCache[filename];
    132.   if(!map){
    133.     var map = loadDataJSONSync(filename);
    134.     if(!map) return null;
    135.     $mapCache[filename] = map;
    136.     map.id = Number(filename.substring(3));
    137.   }
    138.   return map;
    139. }
    140. function readForeignEventInfo(which){
    141.   var ns = which.split('@');
    142.   var map = loadMapJSONSync(ns[1]);
    143.   fmapid = map.id;
    144.   var e = map.events[ns[0]]||map.events.find(function(e){return e&&e.name==ns[0];})||map.events.find(function(e){return e&&e.note.contains(ns[0]);})||null;
    145.   e.mapId = fmapid;
    146.   return e;
    147. }
    148. function copyEvent(which,newonly){
    149.   var fmapid,feid;
    150.   if(typeof which=='string'&&which.contains('@')){
    151.     var e = readForeignEventInfo(which);
    152.     var slot;
    153.     if(e){
    154.       if(e.mapId!=$gameMap.mapId()){
    155.         fmapid = e.mapId;
    156.         feid = e.id;
    157.         var fe = $gameMap._events.find(function(e){return e&&e._foreignMapId==fmapid&&e._foreignEventId==e.id;});
    158.         if(fe) slot = fe.originalEventId();
    159.         else{
    160.           slot = $gameMap._events.length;
    161.           e = JSON.parse(JSON.stringify(e));
    162.           $dataMap.events[slot] = e;
    163.         }
    164.         which = e.id = slot;
    165.       }else{
    166.         which = e.id;
    167.       }
    168.     }else{
    169.       throw "copyEvent: event not found - " + which;
    170.     }
    171.   }
    172.   if(typeof which!='number'){
    173.     var we = $E(which);
    174.     if(we){
    175.       which = we.originalEventId();
    176.     }else{
    177.       we = $dataMap.events.find(function(e){return e&&e.name==which;})||$dataMap.events.find(function(e){return e&&e.note.contains(which);});
    178.       if(!we){
    179.         throw 'copyEvent: event not found - ' + which;
    180.       }
    181.       which = we.id;
    182.     }
    183.   }
    184.   var freeid = $gameMap._events.findIndex(function(ev,i){return i>0&&!ev;});
    185.   freeid = freeid>0?freeid:$gameMap._events.length;
    186.   var event = new Game_Event($gameMap._mapId, which);
    187.   event._foreignMapId = fmapid||0;
    188.   event._foreignEventId = feid||0;
    189.   event._eventId = freeid;
    190.   event._originalEventId = which;
    191.   if(newonly) return event;
    192.   $gameMap._events[freeid]=event;
    193.   event.refresh();
    194.   var spr = new Sprite_Character(event);
    195.   SceneManager._scene._spriteset._characterSprites.push(spr);
    196.   SceneManager._scene._spriteset._tilemap.addChild(spr);
    197.   return event;
    198. }
    199. function spawnEvent(which,tag, x, y, d){
    200.   var es = null;
    201.   if(tag===false){
    202.     // do not revive 'dead' events, always copy
    203.   }else{
    204.     //search for a dead event and revive it
    205.     if(typeof tag=='function'){
    206.       //tag as searh function
    207.       es = $gameMap.events().find(tag);
    208.     }else{
    209.       if(tag){
    210.         //find a dead event with the specific tag
    211.         es = $gameMap._events.find(function(e){return e&&e._erased&&e.event().note.contains(tag);});
    212.       }
    213.       if(!es){
    214.         //find a dead event with the same copy
    215.         if(typeof which=='string'&&which.indexOf('@')>0){
    216.         }else{
    217.           var ee = $E(which);
    218.           es = $gameMap._events.find(function(e){return e&&e._erased&&e.originalEventId()==ee.originalEventId();});
    219.         }
    220.       }
    221.     }
    222.   }
    223.   if(es) {
    224.     es._erased=false;
    225.     es._moveRoute = null;
    226.     delete es._isPreserved;
    227.     for(var k in $gameSelfSwitches._data){
    228.       var kk,A,B,C,D; eval('kk=['+k+']');
    229.       if(kk[0]==$gameMap.mapId()&&kk[1]==es.eventId()){
    230.         delete $gameSelfSwitches._data[k];
    231.       }
    232.     }
    233.     //need to reset sprite if you have played with it
    234.     //delete es.shiftY;
    235.     //$spr(es).scale.set(1,1);
    236.     //$spr(es).rotation=0;
    237.     //$spr(es).anchor.set(0.5,1);
    238.     es.refresh();
    239.   }else{
    240.     es = copyEvent(which);
    241.   }
    242.   if(typeof x!='undefined'){
    243.     es._x = es._realX = x;
    244.   }
    245.   if(typeof y!='undefined'){
    246.     es._y = es._realY = y;
    247.   }
    248.   if(typeof d!='undefined'){
    249.     es._direction = d;
    250.   }
    251.   return es;
    252. }
    253. function replaceEvent(e1, e2, reset){
    254.   e1 = $E(e1);
    255.   e2 = copyEvent(e2, true);
    256.   e2._eventId = e1._eventId;
    257.   if(!reset){
    258.     e2._x = e1._x;
    259.     e2._y = e1._y;
    260.     e2._direction=e1._direction;
    261.     e2._realX = e1._realX;
    262.     e2._realY = e1._realY;
    263.     e2._opacity = e1._opacity;
    264.     e2._blendMode = e1._blendMode;
    265.     e2._pattern = e1._blendMode;
    266.     e2._transparent = e1._transparent;
    267.     e2._bushDepth = e1._bushDepth;
    268.     e2._animationId = e1._animationId;
    269.     e2._balloonId = e1._balloonId;
    270.     e2._animationPlaying = e1._animationPlaying;
    271.     e2._balloonPlaying = e1._balloonPlaying;
    272.     e2._animationCount = e1._animationCount;
    273.     e2._stopCount = e1._stopCount;
    274.     e2._jumpCount = e1._jumpCount;
    275.     e2._jumpPeak = e1._jumpPeak;
    276.     e2._movementSuccess = e1._movementSuccess;
    277.   }
    278.   for(var k in e1) delete e1[k];
    279.   Object.assign(e1,e2);
    280.   //e1.setupPage();
    281.   e1.refresh();
    282. }
    283. //preserve event when map changes
    284. Game_Event.prototype.setPreserved = function(p) {
    285.     this._isPreserved = p;
    286. };
    287. Game_Event.prototype.isPreserved = function() {
    288.     return this._isPreserved;
    289. };
    290. var gp_performTransfer = Game_Player.prototype.performTransfer;
    291. Game_Player.prototype.performTransfer = function() {
    292.   if (this.isTransferring()) {
    293.     $gameSystem.savePreservedEvents();
    294.   }
    295.   gp_performTransfer.call(this);
    296. };
    297. Game_System.prototype.savePreservedEvents = function(){
    298.   this._preservedEvents = this._preservedEvents||[];
    299.   var mid = $gameMap.mapId();
    300.   this._preservedEvents[mid] = [];
    301.   $gameMap.events().forEach(function(e){
    302.     if(e._isPreserved){
    303.       $gameSystem._preservedEvents[mid].push(e);
    304.     }
    305.   });
    306. };
    307. var gm_setupEvents = Game_Map.prototype.setupEvents;
    308. Game_Map.prototype.setupEvents = function() {
    309.     gm_setupEvents.call(this);
    310.     //copy preserved events back
    311.     if($gameSystem._preservedEvents[this._mapId]){
    312.       $gameSystem._preservedEvents[this._mapId].forEach((function(e){
    313.         this._events[e._eventId] = e;
    314.       }).bind(this));
    315.     }
    316.     this.refreshTileEvents();
    317. };
    318. //////////////////////////////////////////////////////////////////////
    319. function $E(n){
    320.   if(n instanceof Game_Event) return n;
    321.   if(typeof n.eventId=='function') n = n.eventId();
    322.   return $gameMap._events[n]||$gameMap._events.find(function(e){return e&&e.event().name==n;})||$gameMap._events.find(function(e){return e&&e.event().note.contains(n);})||null;
    323. }
    324. function $spr(c){
    325.   if(c instanceof Game_Character == false){
    326.     c = $E(c);
    327.   }
    328.   if(!c) return null;
    329.   return SceneManager._scene._spriteset._characterSprites.find(function(s){return s._character==c;});
    330. }
    331. ///////////////////////////////event layer/////////////////////////////
    332. function makeEventLayer(e){
    333.   e = $E(e);
    334.   var spr = $spr(e);
    335.   if(spr._madeEventLayerSprite) return;
    336.   e.screenX = function(){
    337.     var tw = $gameMap.tileWidth();
    338.     return Math.round(this.scrolledX() * tw+(this.layerX||0));
    339.   };
    340.   e.screenY = function() {
    341.       var th = $gameMap.tileHeight();
    342.       return Math.round(this.scrolledY() * th - this.jumpHeight() + (this.layerY||0));
    343.   };
    344.   e.screenZ = function() {
    345.       return this.z==undefined?(this._priorityType * 2 + 1):this.z;
    346.   };
    347.   spr._madeEventLayerSprite = true;
    348.   spr.anchor.set(0,0);
    349.   spr.updateCharacterFrame = function(){
    350.     var c = this._character;
    351.     if(c.frameX!=undefined){
    352.       this.setFrame(c.frameX,c.frameY,c.frameWidth,c.frameHeight);
    353.     }else{
    354.       this.setFrame(0,0,this.bitmap.width,this.bitmap.height);
    355.     }
    356.   };
    357. }
    358. ////////////////////////////////////video player///////////////////////////////////////////
    359. function makeEventVideo(e,name,x,y,w,h){
    360.   if(e.video){
    361.     e.video.pause();
    362.   }
    363.   //makeEventLayer(e);
    364.   var spr = $spr(e);
    365.   spr.updateCharacterFrame();
    366.   x = x||0;
    367.   y=y||0;
    368.   w=w||spr.width;
    369.   h=h||spr.height;
    370.   var vp = 'movies/';
    371.   var v = document.createElement('video');
    372.   var s1 = document.createElement('source');
    373.   s1.type = 'video/webm';
    374.   s1.src = vp + name + '.webm';
    375.   var s2 = document.createElement('source');
    376.   s2.type = 'video/mp4';
    377.   s2.src = vp + name + '.mp4';
    378.   v.autoplay=false;
    379.   v.append(s1,s2);
    380.   v.autoplay=false;
    381.   v.preload=true;
    382.   v.loop=true;
    383.   v.play();
    384.   var bitmap = new Bitmap(w,h);
    385.   var sspr = new Sprite(bitmap);
    386.   sspr.video = v;
    387.   spr.x = x;
    388.   spr.y = y;
    389.   sspr.anchor = spr.anchor;
    390.   spr.removeChildren();
    391.   spr.addChild(sspr);
    392.   sspr.update = function(){
    393.     this.bitmap.canvas.getContext('2d').drawImage(this.video,0,0,this.width,this.height);
    394.     this.bitmap._setDirty();
    395.     Sprite.prototype.update.call(this);
    396.   }
    397.   e.video = v;
    398. }
    复制代码


    Terms of use:
    You can use it in any project, credit is not required.

    Download link and sample:
    See the attachment. It provides a sample project which uses Spawn.js


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

    本帖子中包含更多资源

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

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

    使用道具 举报

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

    本版积分规则

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

    幸运抽奖

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

    立即查看

    聊天机器人
    Loading...

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

    GMT+8, 2026-8-1 02:35 , Processed in 0.085523 second(s), 53 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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