Advertisement
Lyson

Sensor SelfSwitch.js

Nov 3rd, 2015
489
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: 3.1.1
  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.  * <Sensor SelfSwitch>
  18.  *
  19.  * @param Event Self Switch
  20.  * @desc The self switch for event tags.
  21.  * A, B, C, or D
  22.  * @default D
  23.  *
  24.  * @param Comment Self Switch
  25.  * @desc The self switch for comment tags.
  26.  * A, B, C, or D
  27.  * @default D
  28.  *
  29.  * @param Blackout Region
  30.  * @desc The region number that will stop the selfswitch.
  31.  * Number between 1 and 255
  32.  * @default
  33.  *
  34.  * @param Blackout Variable
  35.  * @desc Set Blackout Region to be the a variable number.
  36.  * NO - false, YES - true
  37.  * @default false
  38.  *
  39.  * @param Blackout Switch
  40.  * @desc Set Blackout Region to trigger by a switch (set below).
  41.  * NO - false, YES - true
  42.  * @default false
  43.  *
  44.  *
  45.  * @param Blackout Switch Number
  46.  * @desc Set the number of the switch to trigger the Blackout Region.
  47.  * This will not be used unless Blackout Switch is set to YES.
  48.  * @default
  49.  *
  50.  * @help
  51.  * =============================================================================
  52.  * What does it do?
  53.  * =============================================================================
  54.  *
  55.  * This plugin activates a self switch when a player is in a certain proximity
  56.  * of an event. It uses a notetag in the note of the event to set the proximity.
  57.  *
  58.  * =============================================================================
  59.  * Usage
  60.  * =============================================================================
  61.  *
  62.  * In the plugin parameters, set the self switch to be triggered on the event
  63.  * when a player enters the range. Options are A, B, C, and D. Default is D.
  64.  *
  65.  * Event tags provide 2 way functionality, they will turn off the switch when
  66.  * the player leaves the event's range.
  67.  *
  68.  * Comment tags only provide 1 way switching, they will NOT turn off the Self
  69.  * Switch when the player leaves the event's range.
  70.  *
  71.  * The value of Blackout Region is the number of the region where you want the
  72.  * sensor to be ineffectual.
  73.  * When Blackout Variable is true, Blackout Region is the variable number whose
  74.  * value contains the desired region number, anything other than a number set
  75.  * to the variable will most likely cause errors.
  76.  *
  77.  * When Blackout Switch is true and the switch (set in Blackout Switch Number)
  78.  * is off, the region established for Blackout Region will not alter the
  79.  * funcitons of the sensor. When it is ON the established region will prevent
  80.  * triggering the self switch.
  81.  *
  82.  *
  83.  * -----------------------------------------------------------------------------
  84.  * Notetags
  85.  * -----------------------------------------------------------------------------
  86.  *
  87.  * These notetags go in the note box for the event or in a comment on an event
  88.  * page.
  89.  *
  90.  *    <Sensor: x>
  91.  *
  92.  * Where x is the number of tiles away that the selfswitch will be triggered.
  93.  *
  94.  *
  95.  *    <SensorLV: x>
  96.  *
  97.  * This makes it so that the selfswitch will only be triggered in a straight
  98.  * line in the direction the event is facing.
  99.  * Where x is the number of tiles away that the selfswitch will be triggered.
  100.  *
  101.  *    <SensorVar: x>
  102.  *
  103.  * This makes it so that the selfswitch range is set to the value of variable
  104.  * x.
  105.  * Where x is the number of tiles away that the selfswitch will be triggered.
  106.  *
  107.  *    <SensorLVar: x>
  108.  *
  109.  * This makes it so that the selfswitch will only be triggered in a straight
  110.  * line in the direction the event is facing, and the range is set by
  111.  * variable x.
  112.  * Where x is the number of tiles away that the selfswitch will be triggered.
  113.  *
  114.  *    <SensorCV: x>
  115.  *
  116.  * This makes it so that the selfswitch will be triggered in a cone, the
  117.  * direction the event is looking.
  118.  * Where x is the number of tiles away the cone extends.
  119.  *
  120.  *    <SensorCVar: x>
  121.  *
  122.  * This makes it so that the selfswitch will be triggered in a cone, the
  123.  * direction the event is looking and the range is set by variable x.
  124.  * Where x is the number of tiles away the cone extends.
  125.  *
  126.  *    <SensorRV: x, y>
  127.  *
  128.  * This makes it so that the selfswitch will only be triggered in a
  129.  * rectangular line in the direction the event is facing.
  130.  * Where x is the number of tiles to both sides of the event's looking
  131.  * direction, and y is the number of tiles away that the selfswitch will be
  132.  * triggered.
  133.  *
  134.  *    <SensorRVar: x, y>
  135.  *
  136.  * This makes it so that the selfswitch will only be triggered in a
  137.  * rectangular line in the direction the event is facing.
  138.  * Where x is the number of variable whose value is the number of tiles to
  139.  * both sides of the event's looking direction, and y is the number of
  140.  * variable whose value is the number of tiles away that the selfswitch will be
  141.  * triggered.
  142.  * Additionally, putting a ! in front of either number uses the number given
  143.  * instead of a variable.
  144.  *
  145.  * -----------------------------------------------------------------------------
  146.  * Special Thanks to Gilles!
  147.  * ----------------------------------------------------------------------------
  148.  * =============================================================================
  149.  */
  150.  
  151.  
  152. Lyson.Parameters = $plugins.filter(function (plugin) { return plugin.description.indexOf('<Sensor SelfSwitch>') != -1; })[0].parameters;
  153. Lyson.Param = Lyson.Param || {};
  154.  
  155. Lyson.Param.SelfSwitch = String(Lyson.Parameters['Event Self Switch']);
  156. Lyson.Param.CommentSwitch = String(Lyson.Parameters['Comment Self Switch']);
  157. Lyson.Param.RegionBlock = Number(Lyson.Parameters['Blackout Region']);
  158. Lyson.Param.blockVariable = String(Lyson.Parameters['Blackout Variable']);
  159. Lyson.Param.blockSwitch = String(Lyson.Parameters['Blackout Switch']);
  160. Lyson.Param.blockSwitchNum = Number(Lyson.Parameters['Blackout Switch Number']);
  161. Lyson.Sensor.playerMoved = false;
  162.  
  163. Lyson.Sensor.Game_Event_setupPage = Game_Event.prototype.setupPage;
  164. Game_Event.prototype.setupPage = function () {
  165.     Lyson.Sensor.Game_Event_setupPage.call(this);
  166.     this.setupSensor();
  167. };
  168.  
  169. Game_Event.prototype.setupSensor = function () {
  170.     this._sensorRange = 0;
  171.     this._sensorWidth = 0;
  172. };
  173.  
  174. Lyson.Sensor.Game_Event_update = Game_Event.prototype.update;
  175. Game_Event.prototype.update = function () {
  176.     Lyson.Sensor.Game_Event_update.call(this);
  177.     this.updateSensor();
  178. };
  179.  
  180. Game_Event.prototype.updateSensor = function () {
  181.     if (this._erased) return;
  182.     if (!this._sensorRange) { this._sensorRange = 0 };
  183.     if (!this._sensorWidth) { this._sensorWidth = 0 };
  184.     if (this._sensorRange < 0) return;
  185.     Lyson.Sensor.playerMoved = $gamePlayer.isMoving();
  186.     if (!Lyson.Sensor.playerMoved) return;
  187.     Lyson.Sensor.processEventNotetags.call(this);
  188.     Lyson.Sensor.proccessPageComments.call(this);
  189. };
  190.  
  191. Lyson.Sensor.processEventNotetags = function () {
  192.     if (!$dataMap) {
  193.         return;
  194.     }
  195.     var event = this.event();
  196.     if (event.note) {
  197.         var note1 = /<(?:SENSOR):[ ](\d+)>/i;
  198.         var note2 = /<(?:SENSORVAR):[ ](\d+)>/i;
  199.         var note3 = /<(?:SENSORLV):[ ](\d+)>/i;
  200.         var note4 = /<(?:SENSORLVAR):[ ](\d+)>/i;
  201.         var note5 = /<(?:SENSORCV):[ ](\d+)>/i;
  202.         var note6 = /<(?:SENSORCVAR):[ ](\d+)>/i;
  203.         var note7 = /<(?:SENSORRV):[ ](\d+),[ ](\d+)>/i;
  204.         var note8 = /<(?:SENSORRVAR):[ ](!?)(\d+),[ ](!?)(\d+)>/i;
  205.  
  206.         var notedata = event.note.split(/(?:>)[ ]/);
  207.         for (var i = 0; i < notedata.length; i++) {
  208.             var tag = notedata[i];
  209.             if (tag.match(note1)) {
  210.                 this._sensorRange = parseInt(RegExp.$1);
  211.                 Lyson.Sensor.basicSensor.call(this);
  212.                 return;
  213.             }
  214.             if (tag.match(note2)) {
  215.                 this._sensorRange = $gameVariables.value(parseInt(RegExp.$1));
  216.                 Lyson.Sensor.basicSensor.call(this);
  217.                 return;
  218.             }
  219.             if (tag.match(note3)) {
  220.                 this._sensorWidth = 0;
  221.                 this._sensorRange = parseInt(RegExp.$1);
  222.                 Lyson.Sensor.rectSensor.call(this);
  223.                 return;
  224.             }
  225.             if (tag.match(note4)) {
  226.                 this._sensorWidth = 0;
  227.                 this._sensorRange = $gameVariables.value(parseInt(RegExp.$1));
  228.                 Lyson.Sensor.rectSensor.call(this);
  229.                 return;
  230.             }
  231.             if (tag.match(note5)) {
  232.                 this._sensorRange = parseInt(RegExp.$1);
  233.                 Lyson.Sensor.coneSensor.call(this);
  234.                 return;
  235.             }
  236.             if (tag.match(note6)) {
  237.                 this._sensorRange = $gameVariables.value(parseInt(RegExp.$1));
  238.                 Lyson.Sensor.coneSensor.call(this);
  239.                 return;
  240.             }
  241.             if (tag.match(note7)) {
  242.                 this._sensorWidth = parseInt(RegExp.$1);
  243.                 this._sensorRange = parseInt(RegExp.$2);
  244.                 Lyson.Sensor.rectSensor.call(this);
  245.                 return;
  246.             }
  247.             if (tag.match(note8)) {
  248.                 this._sensorWidth = $gameVariables.value(parseInt(RegExp.$2));
  249.                 if (RegExp.$1 === "!") { this._sensorWidth = parseInt(RegExp.$2); }
  250.                 this._sensorRange = $gameVariables.value(parseInt(RegExp.$4));
  251.                 if (RegExp.$3 === "!") { this._sensorRange = parseInt(RegExp.$4); }
  252.                 Lyson.Sensor.rectSensor.call(this);
  253.                 return;
  254.             }
  255.         }
  256.     }
  257. };
  258.  
  259. Lyson.Sensor.proccessPageComments = function () {
  260.     if (!$dataMap) {
  261.         return;
  262.     }
  263.  
  264.     var list;
  265.     if (!this.page()) { list = this.event().pages[0]; } else { list = this.page().list; };
  266.  
  267.     var note1 = /<(?:SENSOR):[ ](\d+)>/i;
  268.     var note2 = /<(?:SENSORVAR):[ ](\d+)>/i;
  269.     var note3 = /<(?:SENSORLV):[ ](\d+)>/i;
  270.     var note4 = /<(?:SENSORLVAR):[ ](\d+)>/i;
  271.     var note5 = /<(?:SENSORCV):[ ](\d+)>/i;
  272.     var note6 = /<(?:SENSORCVAR):[ ](\d+)>/i;
  273.     var note7 = /<(?:SENSORRV):[ ](\d+),[ ](\d+)>/i;
  274.     var note8 = /<(?:SENSORRVAR):[ ](!?)(\d+),[ ](!?)(\d+)>/i;
  275.  
  276.     var tag;
  277.     for (var i = 0; i < list.length; i++) {
  278.         if (list[i].code === 108 || list[i].code === 408) {
  279.             tag = list[i].parameters[0];
  280.                 if (tag.match(note1)) {
  281.                     this._sensorRange = parseInt(RegExp.$1);
  282.                     Lyson.Sensor.basicSensor.call(this, 'comm');
  283.                     return;
  284.                 }
  285.                 if (tag.match(note2)) {
  286.                     this._sensorRange = $gameVariables.value(parseInt(RegExp.$1));
  287.                     Lyson.Sensor.basicSensor.call(this, 'comm');
  288.                     return;
  289.                 }
  290.                 if (tag.match(note3)) {
  291.                     this._sensorWidth = 0;
  292.                     this._sensorRange = parseInt(RegExp.$1);
  293.                     Lyson.Sensor.rectSensor.call(this, 'comm');
  294.                     return;
  295.                 }
  296.                 if (tag.match(note4)) {
  297.                     this._sensorWidth = 0;
  298.                     this._sensorRange = $gameVariables.value(parseInt(RegExp.$1));
  299.                     Lyson.Sensor.rectSensor.call(this, 'comm');
  300.                     return;
  301.                 }
  302.                 if (tag.match(note5)) {
  303.                     this._sensorRange = parseInt(RegExp.$1);
  304.                     Lyson.Sensor.coneSensor.call(this, 'comm');
  305.                     return;
  306.                 }
  307.                 if (tag.match(note6)) {
  308.                     this._sensorRange = $gameVariables.value(parseInt(RegExp.$1));
  309.                     Lyson.Sensor.coneSensor.call(this, 'comm');
  310.                     return;
  311.                 }
  312.                 if (tag.match(note7)) {
  313.                     this._sensorWidth = parseInt(RegExp.$1);
  314.                     this._sensorRange = parseInt(RegExp.$2);
  315.                     Lyson.Sensor.rectSensor.call(this, 'comm');
  316.                     return;
  317.                 }
  318.                 if (tag.match(note8)) {
  319.                     this._sensorWidth = $gameVariables.value(parseInt(RegExp.$2));
  320.                     if (RegExp.$1 === "!") { this._sensorWidth = parseInt(RegExp.$2); }
  321.                     this._sensorRange = $gameVariables.value(parseInt(RegExp.$4));
  322.                     if (RegExp.$3 === "!") { this._sensorRange = parseInt(RegExp.$4); }
  323.                     Lyson.Sensor.rectSensor.call(this, 'comm');
  324.                     return;
  325.                 }
  326.         }
  327.     }
  328. };
  329.  
  330. Lyson.Sensor.basicSensor = function (c) {
  331.     var selfs = Lyson.Sensor.selfSwitch;
  332.     if (c === 'comm') var selfs = Lyson.Sensor.selfSwitchC;
  333.     if (Lyson.Sensor.regionBlock()) { selfs.call(this, false); return; };
  334.  
  335.     var inRange = Math.abs(this.deltaXFrom($gamePlayer.x));
  336.     inRange += Math.abs(this.deltaYFrom($gamePlayer.y));
  337.  
  338.     if (inRange <= this._sensorRange) {
  339.         selfs.call(this, true);
  340.     } else {
  341.         selfs.call(this, false);
  342.     };
  343.  
  344. };
  345.  
  346. Lyson.Sensor.rectSensor = function (c) {
  347.     var selfs = Lyson.Sensor.selfSwitch;
  348.     if (c === 'comm') var selfs = Lyson.Sensor.selfSwitchC;
  349.     if (Lyson.Sensor.regionBlock()) { selfs.call(this, false); return; };
  350.     var trigger = false;
  351.  
  352.     var absY = Math.abs(this.deltaYFrom($gamePlayer.y));
  353.     var absX = Math.abs(this.deltaXFrom($gamePlayer.x));
  354.     var sensor = this._sensorRange;
  355.     var sWidth = this._sensorWidth;
  356.     var sensorNeg = 0 - sensor;
  357.     var dir = this.direction();
  358.  
  359.     if ((dir === 2 || dir === 8)) {
  360.         var inFront = this.deltaYFrom($gamePlayer.y);
  361.         if (absX <= sWidth) {
  362.             if (dir === 8) {
  363.                 if (inFront <= sensor && inFront > 0) trigger = true;
  364.             } else {
  365.                 if (inFront >= sensorNeg && inFront < 0) trigger = true;
  366.             };
  367.         };
  368.     } else {
  369.         var inFront = this.deltaXFrom($gamePlayer.x);
  370.         if (absY <= sWidth) {
  371.             if (dir === 4) {
  372.                 if (inFront <= sensor && inFront > 0) trigger = true;
  373.             } else {
  374.                 if (inFront >= sensorNeg && inFront < 0) trigger = true;
  375.             };
  376.         };
  377.     };
  378.     selfs.call(this, trigger);
  379. };
  380.  
  381. Lyson.Sensor.coneSensor = function (c) {
  382.     var selfs = Lyson.Sensor.selfSwitch;
  383.     if (c === 'comm') { var selfs = Lyson.Sensor.selfSwitchC }
  384.     if (Lyson.Sensor.regionBlock()) { selfs.call(this, false); return; };
  385.     var trigger = false;
  386.  
  387.     var absY = Math.abs(this.deltaYFrom($gamePlayer.y));
  388.     var absX = Math.abs(this.deltaXFrom($gamePlayer.x));
  389.     var sensor = this._sensorRange;
  390.     var sensorNeg = 0 - sensor;
  391.     var coneL = this.deltaXFrom($gamePlayer.x) - absY;
  392.     var coneR = this.deltaXFrom($gamePlayer.x) + absY;
  393.     var coneU = this.deltaYFrom($gamePlayer.y) - absX;
  394.     var coneD = this.deltaYFrom($gamePlayer.y) + absX;
  395.     var dir = this.direction();
  396.     if ((dir === 2 || dir === 8)) {
  397.         var inFront = this.deltaYFrom($gamePlayer.y);
  398.         if (coneU >= 0 && dir === 8) {
  399.             if (inFront <= sensor && inFront > 0) trigger = true;
  400.         } else if (coneD <= 0 && dir === 2) {
  401.             if (inFront >= sensorNeg && inFront < 0) trigger = true;
  402.         };
  403.     } else {
  404.         var inFront = this.deltaXFrom($gamePlayer.x);
  405.         if (coneL >= 0 && dir === 4) {
  406.             if (inFront <= sensor && inFront > 0) trigger = true;
  407.         } else if (coneR <= 0 && dir === 6) {
  408.             if (inFront >= sensorNeg && inFront < 0) trigger = true;
  409.         };
  410.     };
  411.     selfs.call(this, trigger);
  412. };
  413.  
  414. Lyson.Sensor.regionBlock = function () {
  415.     var blockedRegion;
  416.     if (Lyson.Param.RegionBlock > 0) { blockedRegion = Lyson.Param.RegionBlock };
  417.     if (Lyson.Param.blockVariable === 'true') { blockedRegion = $gameVariables.value(Lyson.Param.RegionBlock) };
  418.  
  419.     if (Lyson.Param.blockSwitch === 'true') {
  420.         if ($gameSwitches.value(Lyson.Param.blockSwitchNum) !== true) { return false; };
  421.     };
  422.  
  423.     if ($gamePlayer.regionId() === blockedRegion) { return true; } else { return false; };
  424. };
  425.  
  426. Lyson.Sensor.selfSwitch = function (selfs) {
  427.     $gameSelfSwitches.setValue([this._mapId, this._eventId, Lyson.Param.SelfSwitch], selfs);
  428. };
  429.  
  430. Lyson.Sensor.selfSwitchC = function (selfs) {
  431.     $gameSelfSwitches.setValue([this._mapId, this._eventId, Lyson.Param.CommentSwitch], selfs);
  432. };
  433.  
  434. // End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement