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

[转载发布] Player Position History Plugin v1.04

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

    连续签到: 2 天

    [LV.7]常住居民III

    5778

    主题

    864

    回帖

    3万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

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

    灌水之王

    发表于 昨天 16:31 | 显示全部楼层 |阅读模式
    Transition History v1.04

    Author: Michael Morris @Blue Booth Studios


             

    Introduction
            A number of my scripts require knowledge of the previous (or "calling") player position.  For example, a player walks onto a save point, and the game loads a new map with a campsite.  At some point, the player opts to leave the campsite, and needs to appear at the position he/she occupied before entering the campsite.  On the World Map, this information is tracked so that if a player decides not to use the World Map (cancels out of it), the player will appear at their position prior to entering said map.  This same information can easily be used for mini-games, etc.  By itself, it isn't hard to do (assign 3 or 4 variables).  But I ran into a more complicated use case when considering daisy-chaining world maps.


            For example, let's say the player enters your world map.  You might have one world map for the Dark Forest zone.  The player chooses the first node in that world map, which leads out of the Dark Forest zone, and into the Open Plains zone (if this still is not clear, consider how world maps are handled in games like Super Mario World or Mario RPG).  If the player selects cancel while in the Open Plains zone (the second world map called), the player will transition back to the Dark Forest zone world map, at the first node.  But the original player position (that is, the player position before the Dark Forest zone) has been lost!  In order to get around this, this plugin allows you to track a variable size array of previous locations.  And while this has been created with a specific use in mind, the coding allows for any use of it you can think of.  I know you guys will come up with some other incredible ways to use it, and I look forward to seeing them!


    Features
            - Can 'save' and 'load' a position from $gameVariables specified in parameter settings (nothing that can't be done by eventing alone, these two functions are for convenience).


            - Factors out the 'save' and 'load' from the World Map script to keep responsibilities separate.


            - Can set the total size of the transition history array in parameters.


            - Array uses push and pop so the most recent transition is always at the end of the array (use getHead to access).  If no elements in array, uses the saved position variables.


            - If the array grows beyond it's specified maximum size, it uses shift to replace the oldest element.


            - Added storeCurrent to make storing current player position less cumbersome.


    How to Use
            - Copy script into your game js/plugins directory.


            - Call script plugin commands within your own events or plugins.


    Requirements


            This script has no external requirements.


    Demo


            No demo provided - this script doesn't produce visible effects by itself.  It needs to be used in conjunction with another script like the World Map.


    Script

    Spoiler//=============================================================================
    // Bluebooth Plugins - Transition History
    // BBS_TransitionHistory.js
    //=============================================================================

    //=============================================================================
    /*:
    * @plugindesc v1.04 Stores a list of previous player transition positions.
    * @author Michael Morris
    *
    * @param History Length
    * @desc How many transitions should be remembered?  If the number of transitions is
    * larger than the history length size than the oldest transition will be removed from
    * the list.
    * @default 8
    *
    * @param Last Map Id
    * @desc Choose the variable ID that stores the player's last map ID.
    * @default 1
    *
    * @param Last Player Map X
    * @desc Choose the variable ID that stores the player's last map X.
    * -1 do not remember last Player X.
    * @default 2
    *  
    * @param Last Player Map Y
    * @desc Choose the variable ID that stores the player's last map Y.
    * -1 do not remember last Player Y.
    * @default 3
    *
    * @param Last Player Facing Direction
    * @desc Choose the variable ID that stores the player's last facing direction.
    * -1 do not remember last facing direction.
    * @default 4
    *
    * @param Next Map Id
    * @desc Choose the variable ID that stores the player's next map ID.
    * @default 5
    *
    * @param Next Player Map X
    * @desc Choose the variable ID that stores the player's next map X.
    * -1 do not remember last Player X.
    * @default 6
    *  
    * @param Next Player Map Y
    * @desc Choose the variable ID that stores the player's next map Y.
    * -1 do not remember last Player Y.
    * @default 7
    *
    * @param Next Player Facing Direction
    * @desc Choose the variable ID that stores the player's next facing direction.
    * -1 do not remember last facing direction.
    * @default 8
    *
    * @param Debug Mode
    * @desc Enable to activate console variable logging.  Use for debugging odd behaviour.
    * true to enable console variable logging.
    * @default false
    *  
    * @help
    * ============================================================================
    * Introduction
    * ============================================================================
    *
    * To be used in more complicated scripts, this tracks a list of last player map positions
    * before transition.  It is intended to be used for things like daisy chaining world maps.
    * If the player transfers back to the previous map, then use pop.  Should not be used for
    * data you want to be saved to disk.  This is for the current session only.
    *
    * ============================================================================
    * Plugin Commands
    * ============================================================================
    *
    * Use the following plugin commands to manipulate world map nodes.
    *
    * Plugin Commands:
    *          
    *         TransitionHistory init                                                 # Initialize Transition History.
    *         TransitionHistory saveMapPos mapId, x, y, dir        # Stores in $gameVariables map Id, player coordinates, and facing direction.
    *   TransitionHistory setNext mapId, x, y, dir                # Stores in next $gameVariables map Id, player coordinates, and facing direction.
    *         TransitionHistory storeCurrent                                         # Stores current map Id, player coordinates, and facing direction.
    *         TransitionHistory store mapId, x, y, dir                # Stores map Id, player coordinates, and facing direction.
    *   TransitionHistory getHead                                                # Gets the head element from the history array.  Sadly not sexual.
    *         TransitionHistory pop                                                         # Removes most recent map Id, player coordinate, and facing direction.
    *         TransitionHistory clear                                                # Frees all memory allocated by Transition History.
    *
    * ============================================================================
    * Versions
    * ============================================================================
    * 1.04 - Fixed bad logic error where changing maps would break transition history.  Ouch.
    * 1.03 - Added store current function as store ... was cumbersome.
    * 1.02 - If history array is empty, getHead will return saved map pos (see: saveMapPos).  Can't pop if history array empty.
    * 1.01 - Plugin finished.
    *
    */
    //=============================================================================

    //=============================================================================
    var Imported = Imported || {} ;
    var BBS = BBS || {};
    Imported.TransitionHistory = 1;
    BBS.TransitionHistory = BBS.TransitionHistory || {};

    (function() {

            //=============================================================================
            // Parameter Variables
            //=============================================================================
            var parameters = PluginManager.parameters('BBS_TransitionHistory');
            var historyMaxSize          = Number(parameters['History Length'] || '8');
            var lastMapVar                  = Number(parameters['Last Map Id'] || '1');
            var lastXVar                  = Number(parameters['Last Player Map X'] || '2');
            var lastYVar                  = Number(parameters['Last Player Map Y'] || '3');
            var lastFDirVar                  = Number(parameters['Last Player Facing Direction'] || '4');
            var nextMapVar                  = Number(parameters['Next Map Id'] || '5');
            var nextXVar                  = Number(parameters['Next Player Map X'] || '6');
            var nextYVar                  = Number(parameters['Next Player Map Y'] || '7');
            var nextFDirVar                  = Number(parameters['Next Player Facing Direction'] || '8');
            var debugging                  = eval(String(parameters['Debug Mode'] || 'false'));

            //=============================================================================
            // Game_Interpreter
            //=============================================================================
        var BBS_TH_Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
        Game_Interpreter.prototype.pluginCommand = function(command, args) {
            if (command === 'TransitionHistory') {
                switch (args[0]) {
                            case 'init':
                                    $gameTemp.THhistoryInit();
                                    break;
                            case 'saveMapPos':
                                    $gameTemp.THsaveMapPos();
                                    break;
                            case 'setNext':
                                    $gameTemp.THsetNext(Number(args[1]), Number(args[2]), Number(args[3]), Number(args[4]));
                                    break;
                            case 'storeCurrent':
                                    $gameTemp.THstoreCurrent();
                                    break;
                            case 'store':
                                    $gameTemp.THstoreMapPos(Number(args[1]), Number(args[2]), Number(args[3]), Number(args[4]));
                                    break;
                            case 'getHead':
                                    $gameTemp.THgetHead();
                                    break;
                            case 'pop':
                                    $gameTemp.THpopMapPos();
                                    break;
                            case 'clear':
                                    $gameTemp.THclearHistory();
                                    break;
                            };
            }
                    else {
                            BBS_TH_Game_Interpreter_pluginCommand.call(this, command, args);
                    }
        };

            //=============================================================================
            // LastPosition Class
            //=============================================================================       
            function LastPosition(mapId, x, y, faceDir) {
                    this.mapId = mapId;
                    this.x = x;
                    this.y = y;
                    this.faceDir = faceDir;
            };

            //=============================================================================
            // Game Temp
            //=============================================================================
            Game_Temp.prototype.THhistoryInit = function() {
                    this._history = new Array();
            };

            Game_Temp.prototype.THsaveMapPos = function() {
                    $gameVariables.setValue(lastMapVar, $gameMap.mapId());
                    $gameVariables.setValue(lastXVar, 0);
                    $gameVariables.setValue(lastYVar, 0);
                    $gameVariables.setValue(lastFDirVar, 2);

                    // Override defaults with values, if they were provided.
                    if (lastXVar > 0) {
                            $gameVariables.setValue(lastXVar, $gamePlayer.x);
                    }

                    if (lastYVar > 0) {
                            $gameVariables.setValue(lastYVar, $gamePlayer.y);
                    }

                    if (lastFDirVar > 0) {
                            $gameVariables.setValue(lastFDirVar, $gamePlayer.direction());
                    }

                    if (debugging) {
                            console.log("Saved: ");
                            console.log($gameMap.mapId());
                            console.log($gamePlayer.x);
                            console.log($gamePlayer.y);
                            console.log($gamePlayer.direction());
                    }               

            };

            Game_Temp.prototype.THstoreMapPos = function(mapId, x, y, faceDir) {
                    if (this._history === undefined) this.THhistoryInit();

                    if (this._historySize >= historyMaxSize) {
                            this._historySize = this._historySize - 1;
                            this._history.shift();
                    }

                    var newItem = new LastPosition(mapId, x, y, faceDir);
                    this._historySize = this._history.push(newItem);

                    if (debugging) {
                            console.log("Pushed: ");
                            console.log(mapId);
                            console.log(x);
                            console.log(y);
                            console.log(faceDir);
                    }
            };

            Game_Temp.prototype.THstoreCurrent = function() {
                    if ($gameMap === undefined) return;
                    if ($gamePlayer === undefined) return;

                    this.THstoreMapPos($gameMap._mapId, $gamePlayer.x, $gamePlayer.y, $gamePlayer._direction);

                    if (debugging) {
                            console.log("Pushed: ");
                            console.log($gameMap._mapId);
                            console.log($gamePlayer.x);
                            console.log($gamePlayer.y);
                            console.log($gamePlayer._direction);
                    }
            };

            Game_Temp.prototype.THpopMapPos = function() {
                    if (this._historySize > 0) {
                            this._historySize = this._historySize - 1;
                            this._history.pop();
                    }
            };

            Game_Temp.prototype.THgetHead = function() {
                    if (this._historySize > 0) {
                            var headIndex = this._historySize - 1;

                            var nextMapId = this._history[headIndex].mapId;
                            var nextX = this._history[headIndex].x;
                            var nextY = this._history[headIndex].y;
                            var nextFD = this._history[headIndex].faceDir;
                    }
                    else {
                            // If no entries in history, revert to saved co-ordinates.
                            var nextMapId = $gameVariables.value(lastMapVar);
                            var nextX = $gameVariables.value(lastXVar);
                            var nextY = $gameVariables.value(lastYVar);
                            var nextFD = $gameVariables.value(lastFDirVar);               
                    }

                    $gameVariables.setValue(nextMapVar, nextMapId);
                    $gameVariables.setValue(nextXVar, 0);
                    $gameVariables.setValue(nextYVar, 0);
                    $gameVariables.setValue(nextFDirVar, 2);

                    // Override defaults with values, if they were provided.
                    if (nextXVar > 0) {
                            $gameVariables.setValue(nextXVar, nextX);
                    }

                    if (nextYVar > 0) {
                            $gameVariables.setValue(nextYVar, nextY);
                    }

                    if (nextFDirVar > 0) {
                            $gameVariables.setValue(nextFDirVar, nextFD);
                    }

                    if (debugging) {
                            console.log("Dest: ");
                            console.log(nextMapId);
                            console.log(nextX);
                            console.log(nextY);
                            console.log(nextFD);
                    }
            };

            Game_Temp.prototype.THsetNext = function(mapId, x, y, faceDir) {       
                    $gameVariables.setValue(nextMapVar, mapId);
                    $gameVariables.setValue(nextXVar, 0);
                    $gameVariables.setValue(nextYVar, 0);
                    $gameVariables.setValue(nextFDirVar, 2);

                    // Override defaults with values, if they were provided.
                    if (nextXVar > 0) {
                            $gameVariables.setValue(nextXVar, x);
                    }

                    if (nextYVar > 0) {
                            $gameVariables.setValue(nextYVar, y);
                    }

                    if (nextFDirVar > 0) {
                            $gameVariables.setValue(nextFDirVar, faceDir);
                    }

            };

            Game_Temp.prototype.THclearHistory = function() {
                    while (this._historySize > 0) {
                            this._historySize = this._historySize - 1;
                            this._history.pop();
                    }
            }

    })(BBS.TransitionHistory);
    //=============================================================================
    // End of File
    //=============================================================================






    Change Log


    1.04


              - Fixed bad logic error where changing maps would break transition history.  Ouch.
    1.03


              - Added store current function as store ... was cumbersome.
    1.02


              - If history array is empty, getHead will return saved map pos (see: saveMapPos).  Can't pop if history array empty.
    1.01


              - Plugin finished.


    Known Bugs / TODO


            Suggestions, bug reports, and feature requests are welcomed!


    Compatibility Issues


            None known.  Compatible with Klaus Map Overlays, Terrax Lighting System, and most (if not all) of Yanfly's plugins.


    FAQ


            Q: The demo won't open, how to open it?
            A: Download Winrar and try again.


    Credit and Thanks
            - Micheal Morris @Blue Booth Studios
            - So much credit to Tsukihime and all of those who have been supportive of my scripts and put up with my incessant barrage of questions.


    Author's Notes
            Free for non-commercial usage as long as credit is given, contact me for commercial use.


    View attachment BBS_TransitionHistory.js


    本贴来自国际rpgmaker官方论坛作者:bluebooth处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/player-position-history-plugin-v1-04.56249/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

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

    本版积分规则

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

    幸运抽奖

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

    立即查看

    聊天机器人
    Loading...

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

    GMT+8, 2026-7-26 04:51 , Processed in 0.067793 second(s), 55 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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