Advertisement
Lyson

Sensor SelfSwitch.js

Oct 31st, 2015
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=========================================================
  2. // Sensor SelfSwitch
  3. // Sensor SelfSwitch.js
  4. // Version: 1.2
  5. //=========================================================
  6.  
  7. var Imported = Imported || {};
  8. Imported.LSensor = true;
  9.  
  10. var Lyson = Lyson || {};
  11. Lyson.Sensor = Lyson.Sensor || {};
  12.  
  13. /*:
  14.  * @author Lyson
  15.  * @plugindesc Allows events to flip a self switch when a
  16.  * player is in range.
  17.  *
  18.  * @param Self Switch
  19.  * @desc The self switch to flip when the player is in range.
  20.  * A, B, C, or D
  21.  * @default D
  22.  *
  23.  * @help
  24.  * =============================================================================
  25.  * What does it do?
  26.  * =============================================================================
  27.  *
  28.  * This plugin activates a self switch when a player is in a certain proximity
  29.  * of an event. It uses a notetag in the note of the event to set the proximity.
  30.  *
  31.  * =============================================================================
  32.  * Usage
  33.  * =============================================================================
  34.  *
  35.  * In the plugin parameters, set the self switch to be triggered on the event
  36.  * when a player enters the range. Options are A, B, C, and D. Default is D.
  37.  *
  38.  * Event tags provide 2 way functionality, they will turn off the switch when
  39.  * the player leaves the event's range.
  40.  *
  41.  * Comment tags only provide 1 way switching, they will NOT turn off the Self
  42.  * Switch when the player leaves the event's range.
  43.  *
  44.  * -----------------------------------------------------------------------------
  45.  * Notetags
  46.  * -----------------------------------------------------------------------------
  47.  *
  48.  * These notetags go in the note box for the event or in a comment on an event
  49.  * page.
  50.  *
  51.  *    <Sensor: x>
  52.  *
  53.  * Where x is the number of tiles away that the selfswitch will be triggered.
  54.  *
  55.  * ----------------------------------------------------------------------------
  56.  * =============================================================================
  57.  */
  58.  
  59. Lyson.Parameters = $plugins.filter(function (plugin) { return plugin.description.indexOf('<Sensor SelfSwitch>') != -1; })[0].parameters;
  60. Lyson.Param = Lyson.Param || {};
  61.  
  62. Lyson.Param.SelfSwitch = String(Lyson.Parameters['Self Switch']);
  63.  
  64. Lyson.Sensor.playerMoved = false;
  65.  
  66. Lyson.Sensor.Game_Event_setupPage = Game_Event.prototype.setupPage;
  67. Game_Event.prototype.setupPage = function () {
  68.     Lyson.Sensor.Game_Event_setupPage.call(this);
  69.     this.setupSensor();
  70. };
  71.  
  72.  
  73.  
  74. Game_Event.prototype.setupSensor = function () {
  75.     this._sensorRange = 0;
  76. };
  77.  
  78. Lyson.Sensor.Game_Event_update = Game_Event.prototype.update;
  79. Game_Event.prototype.update = function () {
  80.     Lyson.Sensor.Game_Event_update.call(this);
  81.     this.updateSensor();
  82. };
  83.  
  84. Game_Event.prototype.updateSensor = function () {
  85.     if (this._erased) return;
  86.     if (!this._sensorRange) { this._sensorRange = 0 };
  87.     if (this._sensorRange < 0) return;
  88.     Lyson.Sensor.playerMoved = $gamePlayer.isMoving();
  89.     if (!Lyson.Sensor.playerMoved) { return; }
  90.     Lyson.Sensor.processEventNotetags.call(this);
  91. };
  92.  
  93. Lyson.Sensor.processEventNotetags = function () {
  94.     if (!$dataMap) {
  95.         return;
  96.     }
  97.     var event = this.event();
  98.     if (event.note) {
  99.         var note1 = /<(?:SENSOR):[ ](\d+)>/i;
  100.  
  101.         var notedata = event.note.split(/(?:>)[ ]/);
  102.         for (var i = 0; i < notedata.length; i++) {
  103.             var tag = notedata[i];
  104.             if (tag.match(note1)) {
  105.                 this._sensorRange = parseInt(RegExp.$1);
  106.                 Lyson.Sensor.basicSensor.call(this);
  107.             }
  108.         }
  109.     }
  110. };
  111.  
  112. Lyson.Sensor.basicSensor = function (c) {
  113.     var selfs = Lyson.Sensor.selfSwitch;
  114.  
  115.     var inRange = Math.abs(this.deltaXFrom($gamePlayer.x));
  116.     inRange += Math.abs(this.deltaYFrom($gamePlayer.y));
  117.  
  118.     if (inRange <= this._sensorRange) {
  119.         selfs.call(this, true);
  120.     } else {
  121.         selfs.call(this, false);
  122.     };
  123.  
  124. };
  125.  
  126. Lyson.Sensor.selfSwitch = function (selfs) {
  127.     $gameSelfSwitches.setValue([this._mapId, this._eventId, Lyson.Param.SelfSwitch], selfs)
  128. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement