Advertisement
ezmash

Character Anchors (MV)

Feb 13th, 2019
1,722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * Character Anchors by Shaz
  3.  * Shaz_CharacterAnchors.js
  4.  * Ver 1.00 2019.02.10
  5.  *
  6.  * @plugindesc Adjust anchor positions to change alignment with tiles.
  7.  * @author Shaz
  8.  *
  9.  * @help This plugin allows you to adjust a character's sprite x and y anchors
  10.  * to change the way it lines up with other tiles on the map.
  11.  * This plugin has no plugin commands.
  12.  *
  13.  *
  14.  * BACKGROUND:
  15.  * By default, character sprites are lined up on the map so they are
  16.  * centered horizontally on a tile, and vertically so the bottoms match.
  17.  * The default character sprite anchors to achieve this are 0.5 for
  18.  * x/horizontal (the middle) and 1 for y/vertical (the bottom).
  19.  *
  20.  * This plugin changes the anchor points of the sprite (0, 0 is top left; 1, 1
  21.  * is bottom right), but not the point on the map to which it's anchored
  22.  * (whatever anchor point you specify will be lined up to the center of the
  23.  * tile horizontally, and the bottom of the tile vertically).
  24.  *
  25.  *
  26.  * USAGE:
  27.  * Place the following into the note box on an event or as a comment on an
  28.  * event page to set an event's sprite anchor point:
  29.  * <anchorX: n>
  30.  * <anchorY: n>
  31.  *
  32.  * where n is any integer or floating point number (including negative numbers)
  33.  *
  34.  * In an event's note box, use only one or both tags:
  35.  * <anchorX: n><anchorY: n>
  36.  *
  37.  * In an event page's comment, use only one or both tags, in a single
  38.  * comment on the same line or on separate lines, or in two comments:
  39.  *
  40.  * Comment: <anchorX: n><anchorY: n>
  41.  *
  42.  * Comment: <anchorX: n>
  43.  *          <anchorY: n>
  44.  *
  45.  * Comment: <anchorX: n>
  46.  * Comment: <anchorY: n>
  47.  *
  48.  * If you have the same tag (anchorX or anchorY) defined several times on a
  49.  * single event page, the last one will be used.
  50.  *
  51.  *
  52.  * SCRIPT CALL:
  53.  * If needed, you can adjust a character's anchor dynamically (without defining
  54.  * it on the event) with the following script calls:
  55.  *     $gameMap.event(id).setAnchorX(n)
  56.  *     $gameMap.event(id).setAnchorY(n)
  57.  *
  58.  * You can even change the player's sprite anchor:
  59.  *     $gamePlayer.setAnchorX(n)
  60.  *     $gamePlayer.setAnchorY(n)
  61.  *
  62.  * Or a follower's sprite anchor:
  63.  *     $gamePlayer.followers()[0].setAnchorX(n)
  64.  *     $gamePlayer.followers()[0].setAnchorY(n)
  65.  *
  66.  * or inside a move route on the player or an event:
  67.  *     Script: this.setAnchorX(n)
  68.  *     Script: this.setAnchorY(n)
  69.  *
  70.  * though setting anchors via script call may not be permanent (may revert
  71.  * when the map is reloaded or you return to the map from a menu).
  72.  *
  73.  *
  74.  * EXPERIMENT:
  75.  * Add an event to your map, play the game, and open the dev tools (F8)
  76.  * In the console tab, type the following, where id is your event id with no
  77.  * leading zeros:
  78.  *     evt = $gameMap.event(id)
  79.  * Then experiment with the anchors to see how the sprite moves:
  80.  *     evt.setAnchorX(n)
  81.  *     evt.setAnchorY(n)
  82.  */
  83.  
  84. var Imported = Imported || {};
  85. Imported.Shaz_CharacterAnchors = true;
  86.  
  87. var Shaz = Shaz || {};
  88. Shaz.CA = Shaz.CA || {};
  89. Shaz.CA.Version = 1.00;
  90.  
  91. (function() {
  92.     var _Shaz_CA_Game_CharacterBase_initMembers = Game_CharacterBase.prototype.initMembers;
  93.     Game_CharacterBase.prototype.initMembers = function() {
  94.         _Shaz_CA_Game_CharacterBase_initMembers.call(this);
  95.         this.resetAnchors();
  96.     };
  97.  
  98.     Game_CharacterBase.prototype.resetAnchors = function() {
  99.         this._anchorX = 0.5;
  100.         this._anchorY = 1;
  101.     };
  102.  
  103.     Game_CharacterBase.prototype.setAnchorX = function(anchorX) {
  104.         this._anchorX = anchorX;
  105.     };
  106.  
  107.     Game_CharacterBase.prototype.setAnchorY = function(anchorY) {
  108.         this._anchorY = anchorY;
  109.     };
  110.  
  111.     Game_CharacterBase.prototype.anchorX = function() {
  112.         return this._anchorX;
  113.     };
  114.  
  115.     Game_CharacterBase.prototype.anchorY = function() {
  116.         return this._anchorY;
  117.     };
  118.  
  119.     var _Shaz_CA_Game_Event_clearPageSettings = Game_Event.prototype.clearPageSettings;
  120.     Game_Event.prototype.clearPageSettings = function() {
  121.         _Shaz_CA_Game_Event_clearPageSettings.call(this);
  122.         this.resetAnchors();
  123.     };
  124.  
  125.     var _Shaz_CA_Game_Event_setupPageSettings = Game_Event.prototype.setupPageSettings;
  126.     Game_Event.prototype.setupPageSettings = function() {
  127.         _Shaz_CA_Game_Event_setupPageSettings.call(this);
  128.         if (this.event().meta.anchorX) {
  129.             this.setAnchorX(parseFloat(this.event().meta.anchorX));
  130.         }
  131.  
  132.         if (this.event().meta.anchorY) {
  133.             this.setAnchorY(parseFloat(this.event().meta.anchorY));
  134.         }
  135.  
  136.         if (this.page() && this.list()) {
  137.             this.list().filter(function(cmd) {
  138.                 if (cmd.code === 108 || cmd.code === 408) {
  139.                     if (cmd.parameters[0].match(/<anchorX:\s*(-?[\d\.]+)>/i)) {
  140.                         this.setAnchorX(parseFloat(RegExp.$1));
  141.                     }
  142.                     if (cmd.parameters[0].match(/<anchorY:\s*(-?[\d\.]+)>/i)) {
  143.                         this.setAnchorY(parseFloat(RegExp.$1));
  144.                     }
  145.                 }
  146.             }.bind(this));
  147.         }
  148.     };
  149.  
  150.     _Shaz_Sprite_Character_updatePosition = Sprite_Character.prototype.updatePosition;
  151.     Sprite_Character.prototype.updatePosition = function() {
  152.         _Shaz_Sprite_Character_updatePosition.call(this);
  153.         this.updateAnchors();
  154.     };
  155.  
  156.     Sprite_Character.prototype.updateAnchors = function() {
  157.         this.anchor.x = this._character.anchorX();
  158.         this.anchor.y = this._character.anchorY();
  159.     };
  160.  
  161. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement