Advertisement
Lyson

Sensor SelfSwitch 3.3.3.js

Mar 20th, 2016
1,651
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.3.3
  5. //=========================================================
  6.  
  7. var Imported = Imported || {};
  8. Imported.LSensor = true;
  9.  
  10. var Lyson = Lyson || {};
  11. Lyson.Sensor = Lyson.Sensor || {};
  12. LS = Lyson.Sensor;
  13.  
  14. /*:
  15.  * @author Lyson
  16.  * @plugindesc Allows events to flip a self switch when a
  17.  * player is in range.
  18.  * <Sensor SelfSwitch>
  19.  *
  20.  * @param Event Self Switch
  21.  * @desc The self switch for event tags.
  22.  * A, B, C, or D
  23.  * @default D
  24.  *
  25.  * @param Comment Self Switch
  26.  * @desc The self switch for comment tags.
  27.  * A, B, C, or D
  28.  * @default D
  29.  *
  30.  * @param Blackout Region
  31.  * @desc The region number that will stop the selfswitch.
  32.  * Number between 1 and 255
  33.  * @default
  34.  *
  35.  * @param Blackout Variable
  36.  * @desc Set Blackout Region to be the a variable number.
  37.  * NO - false, YES - true
  38.  * @default false
  39.  *
  40.  * @param Blackout Switch
  41.  * @desc Set Blackout Region to trigger by a switch (set below).
  42.  * NO - false, YES - true
  43.  * @default false
  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 tag sensor zone.
  67.  *
  68.  * Comment tags only provide 1 way switching, they will NOT turn off the Self
  69.  * Switch when the player leaves the comment tag sensor zone.
  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. All commas are optional.
  89.  *    
  90.  * Placing an exclamation mark "!" in front of any number in the following tags
  91.  * will use the value of the variable with the ID of that number.
  92.  * Ex: <Sensor: !3> will use the value of variable 3.
  93.  *
  94.  *    <Sensor: x>
  95.  *
  96.  * Where x is the number of tiles away that the selfswitch will be triggered.
  97.  *
  98.  *
  99.  *    <SensorLV: x>
  100.  *
  101.  * This makes it so that the selfswitch will only be triggered in a straight
  102.  * line in the direction the event is facing.
  103.  * Where x is the number of tiles away that the selfswitch will be triggered.
  104.  *
  105.  *    <SensorCV: x>
  106.  *
  107.  * This makes it so that the selfswitch will be triggered in a cone, the
  108.  * direction the event is looking.
  109.  * Where x is the number of tiles away the cone extends.
  110.  *
  111.  *    <SensorRV: x, y>
  112.  *
  113.  * This makes it so that the selfswitch will only be triggered in a
  114.  * rectangular line in the direction the event is facing.
  115.  * Where x is the number of tiles to both sides of the event's looking
  116.  * direction, and y is the number of tiles away that the selfswitch will be
  117.  * triggered.
  118.  *
  119.  *   <SensorCV: x L>
  120.  *   <SensorRV: x y left>
  121.  *   <SensorLV: x left up>
  122.  *
  123.  * Sensor direction can be set using d, l, r, u, down, left, right, up. This is
  124.  * not case sensitive. The direction set is in relation to the event the tag is
  125.  * on. x and/or y are defined as established in above tags. This works for any
  126.  * sensor type, except the basic sensor.
  127.  * You may put any combinations of directions in the tag. They may be separated
  128.  * by spaces, but this is not required
  129.  *
  130.  *
  131.  * -----------------------------------------------------------------------------
  132.  * Plugin Commands
  133.  * -----------------------------------------------------------------------------
  134.  *
  135.  * Change your sensors on the fly! Use Plugin Commands to alter any aspect of
  136.  * sensors on your event. You could even ADD or REMOVE sensors to/from an event.
  137.  *
  138.  * LSensor event property value
  139.  * LSensor page property value
  140.  *
  141.  * Okay, this is the Plugin command LSensor is required, as is either event or
  142.  * page. If you don't have at least one property and value after these, why are
  143.  * you using the plugin command??
  144.  *
  145.  * You can have any number of properties and values in the Plugin command, but
  146.  * only 1 event or page identifier.
  147.  * The event identifier is equivilant to using/modifying the event note tag.
  148.  * The page identifier is equivilant to using/modifying a comment tag.
  149.  *
  150.  * LSensor off
  151.  * LSensor event off
  152.  * LSensor page off
  153.  *
  154.  * On/Off commands turn all, event level, or comment level sensors on or off
  155.  * respectively
  156.  *
  157.  * Here is the list and limits of the properties:
  158.  *
  159.  * Id       x
  160.  * Type     b, cv, lv, rv, null
  161.  * Range    x (any number)
  162.  * RangeVar x (any number)
  163.  * Width    x (any number)
  164.  * WidthVar x (any number)
  165.  * Dir      d, l, r, u, down, left, right, up
  166.  * ThisDir  true
  167.  *
  168.  * Id x
  169.  *    The Id of the event to be affected, this is not needed if the change is
  170.  *    to happen on the current event. Do NOT include preceding 0's.
  171.  * Type b, cv, lv, rv, null
  172.  *    These are basic, coneview, lineview, rectangleview, the shorthand MUST be
  173.  *    used. Width/WidthVar will be ignored on b, cv, and lv types. The command
  174.  *    null will prevent the sensor from working.
  175.  * Range x (any number)
  176.  *    Know that if you set this and then RangeVar, Range will be overwritten.
  177.  * RangeVar x (any number)
  178.  *    This is the variable number whose value will be used for the sensor Range.
  179.  * Width x (any number)
  180.  *    Know that if you set this and then WidthVar, Width will be overwritten.
  181.  * WidthVar x (any number)
  182.  *    This is the variable number whose value will be used for the sensor Width.
  183.  * Dir d, l, r, u, down, left, right, up
  184.  *    Shorthand or full word can be used with Dir. This will set the direction
  185.  *    that the sensor will use. Multiple directions can be used, a space is
  186.  *    required between each direction.
  187.  * ThisDir true
  188.  *    This will set the sensor to use the direction of the event itself.
  189.  *    Do NOT use both this and the Dir commands. This coudl easily lead to
  190.  *    errors.
  191.  *
  192.  * ## A note about page sensor changes. The changes will only occur for the
  193.  *    current page, and will not persist through page changes, even if the page
  194.  *    that was changed comes back up. The plugin will use the original tag, if
  195.  *    there was one, to make a sensor on page changes.
  196.  *
  197.  * -----------------------------------------------------------------------------
  198.  * Special Thanks to Gilles, Estriole and YOU!
  199.  * -----------------------------------------------------------------------------
  200.  * =============================================================================
  201.  */
  202.  
  203. // Make the plugin name independent.
  204. Lyson.Parameters = $plugins.filter(function (plugin) { return plugin.description.indexOf('<Sensor SelfSwitch>') != -1; })[0].parameters;
  205. Lyson.Param = Lyson.Param || {};
  206.  
  207. // Plugin Parameters
  208. Lyson.Param.SelfSwitch = String(Lyson.Parameters['Event Self Switch']);
  209. Lyson.Param.CommentSwitch = String(Lyson.Parameters['Comment Self Switch']);
  210. Lyson.Param.RegionBlock = Number(Lyson.Parameters['Blackout Region']);
  211. Lyson.Param.blockVariable = String(Lyson.Parameters['Blackout Variable']);
  212. Lyson.Param.blockSwitch = String(Lyson.Parameters['Blackout Switch']);
  213. Lyson.Param.blockSwitchNum = Number(Lyson.Parameters['Blackout Switch Number']);
  214.  
  215. // Event Container to keep sensors persistent
  216. LS.events = LS.events || {};
  217.  
  218. // Global Variables for switching sensors off and on
  219. LS.Sensors = true;
  220. LS.EventSensors = true;
  221. LS.PageSensors = true;
  222.  
  223. // Scene_Map alias to inject sensor building
  224. LS.Scene_Map_start = Scene_Map.prototype.start;
  225. Scene_Map.prototype.start = function () {
  226.     if (!LS.events[$gameMap._mapId]) {
  227.         LS.events[$gameMap._mapId] = LS.events[$gameMap._mapId] || {};
  228.         $gameMap.events().forEach(function (event) {
  229.             LS.processSensorTags.call(event);
  230.         });
  231.     } else {
  232.         console.log('no');
  233.         $gameMap.events().forEach(function (event) {
  234.             LS.recoverSensorEvents.call(event);
  235.         });
  236.     }
  237.     LS.Scene_Map_start.call(this);
  238. }
  239.  
  240. // Plugin Command
  241. LS.Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  242. Game_Interpreter.prototype.pluginCommand = function (command, args) {
  243.     if (command.toLowerCase() === 'lsensor') {
  244.         for (var i = 0; i < args.length; i++) {
  245.         }
  246.         try {
  247.             if (args[0].toLowerCase() === 'event') { LS.eventCommand.call(this, args); };
  248.             if (args[0].toLowerCase() === 'page') { LS.pageCommand.call(this, args); };
  249.             if (args[0].toLowerCase() === 'off') { LS.Sensors = false; };
  250.             if (args[0].toLowerCase() === 'on') { LS.Sensors = true; };
  251.         }
  252.         catch (e) { //Stop game processing and throw error if the plugin command is incorrect.
  253.             var event = $gameMap._events[this._eventId];
  254.             console.error("Error reading Plugin Command for Sensor Selfswitch.\nPlease check Plugin Command on Event: " + event.event().name + ", EventID: " + this._eventId);
  255.             console.error(e.stack);
  256.             Graphics.printError("Event Sensor Plugin Command Invalid", "EventID: " + this.event().id + ", EventName: " + event.name);
  257.             AudioManager.stopAll();
  258.             SceneManager.stop();
  259.         }
  260.     }
  261.     LS.Game_Interpreter_pluginCommand.call(this, command, args);
  262. }
  263.  
  264. // Handle Plugin Command for events.
  265. LS.eventCommand = function (args) {
  266.     if (args[1].toLowerCase() === 'off') { LS.EventSensors = false; };
  267.     if (args[1].toLowerCase() === 'on') { LS.EventSensors = true; };
  268.     var obj;
  269.     var i;
  270.     if (args[1].toLowerCase() === 'id') {
  271.         obj = $gameMap._events[parseInt(args[2])].event();
  272.         i = 3;
  273.     } else {
  274.         obj = $gameMap._events[this._eventId].event();
  275.         i = 1;
  276.     };
  277.     for (; i < args.length; i += 2) {
  278.         var value = i + 1;
  279.         switch (args[i].toLowerCase()) {
  280.             case 'type':
  281.                 LS.setType(obj, args[value]);
  282.                 console.log("I Ran");
  283.                 break;
  284.             case 'range':
  285.                 LS.setRangeVar(obj, null)
  286.                 LS.setRange(obj, parseInt(args[value]));
  287.                 break;
  288.             case 'rangevar':
  289.                 LS.setRangeVar(obj, parseInt(args[value]));
  290.                 break;
  291.             case 'width':
  292.                 LS.setWidthVar(obj, null);
  293.                 LS.setWidth(obj, parseInt(args[value]));
  294.                 break;
  295.             case 'widthvar':
  296.                 LS.setWidthVar(obj, parseInt(args[value]));
  297.                 break;
  298.             case 'dir':
  299.                 var dirs = [];
  300.                 var filter = /([dlru])?(?:own|eft|p|ight)*/i
  301.                 for (var d = i + 1; d < args.length; d++) {
  302.                     if (args[d].match(filter)[1]) {
  303.                         dirs.push(args[d].match(filter)[1]);
  304.                         i = d - 1;
  305.                     } else {
  306.                         break;
  307.                     };
  308.                 }
  309.                 LS.setThisDir(obj, false);
  310.                 LS.setDir(obj, dirs);
  311.                 break;
  312.             case 'thisdir':
  313.                 LS.setThisDir(obj, !!args[value]);
  314.                 break;
  315.         }
  316.     }
  317.     if (args[1].toLowerCase() === 'id') {
  318.         LS.saveSensorProperties.call($gameMap._events[parseInt(args[2])]);
  319.     } else {
  320.         LS.saveSensorProperties.call($gameMap._events[this._eventId]);
  321.     };
  322. };
  323.  
  324.  
  325. // Handle Plugin Command for pages.
  326. LS.pageCommand = function (args) {
  327.     if (args[1].toLowerCase() === 'off') { LS.PageSensors = false; };
  328.     if (args[1].toLowerCase() === 'on') { LS.PageSensors = true; };
  329.     var obj;
  330.     var i;
  331.     if (args[1].toLowerCase() === 'id') {
  332.         obj = $gameMap._events[parseInt(args[2])].page();
  333.         i = 3;
  334.     } else {
  335.         obj = $gameMap._events[this._eventId].page();
  336.         i = 1;
  337.     };
  338.     for (; i < args.length; i += 2) {
  339.         console.log(args[i] + " " + args[i + 1])
  340.         var value = i + 1;
  341.         switch (args[i].toLowerCase()) {
  342.             case 'type':
  343.                 LS.setType(obj, args[value]);
  344.                 break;
  345.             case 'range':
  346.                 LS.setRangeVar(obj, null)
  347.                 LS.setRange(obj, parseInt(args[value]));
  348.                 break;
  349.             case 'rangevar':
  350.                 LS.setRangeVar(obj, parseInt(args[value]));
  351.                 break;
  352.             case 'width':
  353.                 LS.setWidthVar(obj, null);
  354.                 LS.setWidth(obj, parseInt(args[value]));
  355.                 break;
  356.             case 'widthvar':
  357.                 LS.setWidthVar(obj, parseInt(args[value]));
  358.                 break;
  359.             case 'dir':
  360.                 var dirs = [];
  361.                 var filter = /([dlru])?(?:own|eft|p|ight)*/i
  362.                 for (var d = i + 1; d < args.length; d++) {
  363.                     if (args[d].match(filter)[1]) {
  364.                         dirs.push(args[d].match(filter)[1]);
  365.                         i = d - 1;
  366.                     } else {
  367.                         break;
  368.                     };
  369.                 }
  370.                 LS.setThisDir(obj, false);
  371.                 LS.setDir(obj, args[value]);
  372.                 break;
  373.             case 'thisdir':
  374.                 LS.setThisDir(obj, !!args[value]);
  375.                 break;
  376.         }
  377.     }
  378.     if (args[1].toLowerCase() === 'id') {
  379.         LS.saveSensorProperties.call($gameMap._events[parseInt(args[2])]);
  380.     } else {
  381.         LS.saveSensorProperties.call($gameMap._events[this._eventId]);
  382.     };
  383. };
  384.  
  385.  
  386. // Clears the sensor values for a page so that they wont carry over if there is nothing overwriting them.
  387. LS.Game_Event_clearPageSettings = Game_Event.prototype.clearPageSettings;
  388. Game_Event.prototype.clearPageSettings = function () {
  389.     LS.Game_Event_clearPageSettings.call(this);
  390.     var page = this.page();
  391.     this._sensorType = null;
  392.     this._sensorDirection = null;
  393.     this._sensorThisDir = null;
  394.     this._sensorRange = 0;
  395.     this._sensorRangeVar = -1;
  396.     this._sensorWidth = 0;
  397.     this._sensorWidthVar = -1;
  398. };
  399.  
  400. // Alias Game_Event.prototype.setupPageSettings to check tags, and apply properties.
  401. LS.Game_Event_setupPageSettings = Game_Event.prototype.setupPageSettings;
  402. Game_Event.prototype.setupPageSettings = function () {
  403.     if (!this.page()) { return LS.Game_Event_setupPageSettings.call(this); };
  404.     LS.Game_Event_setupPageSettings.call(this);
  405.     LS.processSensorTags.call(this, true);
  406. };
  407.  
  408. //Alias Game_Event.prototype.update to add sensor checking.
  409. LS.Game_Event_update = Game_Event.prototype.update;
  410. Game_Event.prototype.update = function () {
  411.     LS.Game_Event_update.call(this);
  412.     if (LS.Sensors) { this.updateSensor(); };
  413. };
  414.  
  415. // Update function to check sensor zones.
  416. Game_Event.prototype.updateSensor = function () {
  417.     if (this._erased) return;
  418.     if (!this.page()) return;
  419.     if (!this.event()._sensorType && !this.page()._sensorType) return;
  420.     if (this.event()._sensorRange <= 0 && this.page()._sensorRange <= 0) return;
  421.     LS.checkSensorZone.call(this);
  422. };
  423.  
  424. // Sensor Property gets
  425. LS.getType = function (obj) {
  426.     return obj._sensorType;
  427. }
  428. LS.getRange = function (obj) {
  429.     LS.setRange(obj);
  430.     return obj._sensorRange;
  431. }
  432. LS.getWidth = function (obj) {
  433.     LS.setWidth(obj);
  434.     return obj._sensorWidth;
  435. }
  436. LS.getRangeVar = function (obj) {
  437.     return obj._sensorRangeVar;
  438. }
  439. LS.getWidthVar = function (obj) {
  440.     return obj._sensorWidthVar;
  441. }
  442. LS.getDir = function (obj) {
  443.     LS.setDir.call(this, obj);
  444.     return obj._sensorDirection;
  445. }
  446. LS.getThisDir = function (obj) {
  447.     return obj._sensorThisDir;
  448. }
  449.  
  450. // Sensor Property sets
  451. LS.setType = function (obj, args) {
  452.     if (typeof args === 'undefined') { args = 'b' };
  453.     obj._sensorType = args;
  454. }
  455. LS.setRange = function (obj, args) {
  456.     if (typeof args === 'undefined') { args = obj._sensorRange; }
  457.     if (obj._sensorRangeVar) { obj._sensorRange = $gameVariables.value(LS.getRangeVar(obj)); } else { obj._sensorRange = args; };
  458. }
  459. LS.setWidth = function (obj, args) {
  460.     if (typeof args === 'undefined') args = obj._sensorWidth;
  461.     if (obj._sensorWidthVar) { obj._sensorWidth = $gameVariables.value(LS.getWidthVar(obj)); } else { obj._sensorWidth = args; };
  462. }
  463. LS.setRangeVar = function (obj, args) {
  464.     obj._sensorRangeVar = args;
  465. }
  466. LS.setWidthVar = function (obj, args) {
  467.     obj._sensorWidthVar = args;
  468. }
  469. LS.setDir = function (obj, args) {
  470.     if (typeof args === 'undefined') { obj._sensorDirection = obj._sensorDirection } else { obj._sensorDirection = LS.makeDir(args); };
  471.     if (LS.getThisDir(obj)) { obj._sensorDirection = this.direction(); };
  472. }
  473. LS.setThisDir = function (obj, args) {
  474.     obj._sensorThisDir = args;
  475. }
  476.  
  477.  
  478. // Define RegEx to be matched, sort whether its an event's note or event page's comment being checked.
  479. LS.processSensorTags = function (pageChange) {
  480.     if (!$dataMap) return;
  481.     if (!LS.events[this._mapId]) return;
  482.  
  483.     var tag = /<(?:SENSOR)(LV||CV||RV)?:\s(!?)(\d+)(?:(?:,?\s)(!?)(\d+))?(?:(?:,?\s)([dlru])?(?:own|eft|p|ight)*\s?([dlru])?(?:own|eft|p|ight)*\s?([dlru])?(?:own|eft|p|ight)*\s?([dlru])?(?:own|eft|p|ight)*)>/i;
  484.     // Prevent changing the event properties every page change
  485.     if (!pageChange) { LS.processEventTag.call(this, tag); };
  486.     LS.processCommentTag.call(this, tag);
  487. };
  488.  
  489. // Save event sensor
  490. LS.saveSensorProperties = function () {
  491.     var map = $gameMap._mapId;
  492.     var ev = this._eventId;
  493.     var obj;
  494.     if (this.event()._sensorType || this.page()._sensorType) {
  495.         obj = this.event();
  496.         var newEvent = {
  497.             type: LS.getType(obj),
  498.             range: LS.getRange(obj),
  499.             width: LS.getWidth(obj),
  500.             rangeVar: LS.getRangeVar(obj),
  501.             widthVar: LS.getWidthVar(obj),
  502.             dir: LS.getDir.call(this, obj),
  503.             thisDir: LS.getThisDir(obj)
  504.         };
  505.         if (LS.events[map][ev] === newEvent) { return; };
  506.         LS.events[$gameMap._mapId][this._eventId] = newEvent;
  507.     }
  508.     if (!this.page()) return;
  509.     if (this.page()._sensorType) {
  510.         obj = this.page();
  511.         var newEvent = {
  512.             index: this._pageIndex,
  513.             type: LS.getType(obj),
  514.             range: LS.getRange(obj),
  515.             width: LS.getWidth(obj),
  516.             rangeVar: LS.getRangeVar(obj),
  517.             widthVar: LS.getWidthVar(obj),
  518.             dir: LS.getDir.call(this, obj),
  519.             thisDir: LS.getThisDir(obj)
  520.         };
  521.         if (LS.events[map][ev].currentPage === newEvent) { return; };
  522.         LS.events[$gameMap._mapId][this._eventId].currentPage = newEvent;
  523.     }
  524. }
  525.  
  526. // Recover event sensor
  527. LS.recoverSensorEvents = function () {
  528.     if (!LS.events[$gameMap._mapId]) return;
  529.     var obj;
  530.     for (var ev in LS.events[$gameMap._mapId]) {
  531.         //console.log(ev);
  532.         if (this._eventId == ev) {
  533.             sensor = LS.events[$gameMap._mapId][ev];
  534.             obj = this.event()
  535.             obj._sensorType = sensor.type;
  536.             obj._sensorRange = sensor.range;
  537.             obj._sensorWidth = sensor.width;
  538.             obj._sensorRangeVar = sensor.rangeVar;
  539.             obj._sensorWidthVar = sensor.widthVar;
  540.             obj._sensorDirection = sensor.dir;
  541.             obj._sensorThisDir = sensor.thisDir;
  542.             if (!sensor.currentPage) break;
  543.             obj = this.page()
  544.             if (this._pageIndex == sensor.currentPage.index) {
  545.                 obj._sensorType = sensor.currentPage.type;
  546.                 obj._sensorRange = sensor.currentPage.range;
  547.                 obj._sensorWidth = sensor.currentPage.width;
  548.                 obj._sensorRangeVar = sensor.currentPage.rangeVar;
  549.                 obj._sensorWidthVar = sensor.currentPage.widthVar;
  550.                 obj._sensorDirection = sensor.currentPage.dir;
  551.                 obj._sensorThisDir = sensor.currentPage.thisDir;
  552.             }
  553.         }
  554.     }
  555. }
  556.  
  557. // Check RegEx against note entries on an Event
  558. LS.processEventTag = function (tag) {
  559.     var event = this.event();
  560.     if (event.note) {
  561.         var note = event.note.split(/(?:>)[ ]/);
  562.         for (var i = 0; i < note.length; i++) {
  563.             if (note[i].match(tag)) {
  564.                 LS.setupSensorSettings.call(this, note[i].match(tag), false)
  565.             }
  566.         }
  567.     }
  568. };
  569.  
  570. // Check the RegEx against comment entries on an event page.
  571. LS.processCommentTag = function (tag) {
  572.     if (!this.page()) return;
  573.     var note;
  574.     var list = this.page().list;
  575.     for (var i = 0; i < list.length; i++) {
  576.         if (list[i].code === 108 || list[i].code === 408) {
  577.             note = list[i].parameters[0];
  578.             if (note.match(tag)) {
  579.                 LS.setupSensorSettings.call(this, note.match(tag), true)
  580.             }
  581.         }
  582.     }
  583. };
  584.  
  585. //Sets sensor properties to either the event object or the event's page object.
  586. LS.setupSensorSettings = function (match, commTag) {
  587.     var object;
  588.     if (commTag) { object = this.page() } else { object = this.event(); }; //Set var object to page or event, to place the properties on the proper object.
  589.     try {
  590.         //Set range property that all sensor types, except RV use.
  591.         if (match[2]) { LS.setRangeVar(object, parseInt(match[3])); LS.setRange(object); } else { LS.setRange(object, parseInt(match[3])); };
  592.         if (match[1]) {//Matches LV, CV, or RV following Sensor in the tag, indicating sensor type.
  593.             //Set the direction of the sensor zone if indicated on tag.
  594.             var dirs = [match[6], match[7], match[8], match[9]].join('').split('');
  595.             if (dirs) { LS.setDir.call(this, object, dirs) } else { LS.setThisDir(object, true); LS.setDir.call(this, object); }
  596.             switch (match[1].toLowerCase()) {//Set properties based on the sensor type.
  597.                 case "lv": //Set properties specific to Line View sensors
  598.                     LS.setWidth(object, 0);
  599.                     LS.setType(object, 'lv');
  600.                     break;
  601.                 case "rv": //Set properties specific to Rectangle View sensors & overwrite _sensorRange/_sensorRangeVar properties.
  602.                     LS.setType(object, 'rv');
  603.                     if (match[2]) { LS.setWidthVar(object, parseInt(match[3])); LS.setWidth(object); } else { LS.setWidth(object, parseInt(match[3])); };
  604.                     if (match[4]) { LS.setRangeVar(object, parseInt(match[5])); LS.setRange(object); } else { LS.setRange(object, parseInt(match[5])); };
  605.                     break;
  606.                 case "cv": //Set properties specific to Cone View sensors
  607.                     LS.setType(object, 'cv');
  608.                     break;
  609.                 default:
  610.                     break;
  611.             }
  612.         } else {//No LV, CV, RV means its a basic sensor.
  613.             LS.setType(object, 'b');
  614.         }
  615.     } catch (e) { //Stop game processing and throw error if the tag is incorrect.
  616.         console.error("Error reading Sensor SelfSwitch Tag.\nPlease check Sensor tag on Event: " + this.event().name + ", EventID: " + this.event().id);
  617.         console.error(e.stack);
  618.         Graphics.printError("Event Sensor Tag Invalid", "EventID: " + this.event().id + ", EventName: " + this.event().name);
  619.         AudioManager.stopAll();
  620.         SceneManager.stop();
  621.     }
  622.  
  623.     if (object._sensorType) { LS.events[$gameMap._mapId][this._eventId] = { currentPage: {} }; };
  624.     LS.saveSensorProperties.call(this);
  625. };
  626.  
  627. //Process the tag direction indicator.
  628. LS.makeDir = function (dirs) {
  629.     var directions = [];
  630.     for (var i = 0; i < dirs.length; i++) {
  631.         var dir = dirs[i];
  632.         switch (dir.toLowerCase()) {
  633.             case "d":
  634.                 directions.push(2);
  635.                 break;
  636.             case "l":
  637.                 directions.push(4);
  638.                 break;
  639.             case "r":
  640.                 directions.push(6);
  641.                 break;
  642.             case "u":
  643.                 directions.push(8);
  644.                 break;
  645.             default:
  646.                 break;
  647.         }
  648.     }
  649.     return directions;
  650. };
  651.  
  652. // Uses the sensor type to check if player is in the sensor zone.
  653. LS.checkSensorZone = function () {
  654.     var object = [this.page(), this.event()];
  655.     for (var i = 0; i < object.length; i++) {
  656.         if (object[i]._sensorType) {
  657.             LS.checkChanges.call(this, object[i]);
  658.             if (i === 0 && !LS.PageSensors) return;
  659.             if (i === 1 && !LS.EventSensors) return;
  660.             switch (object[i]._sensorType) {
  661.                 case "lv":
  662.                     LS.setWidth(object, 0);
  663.                 case "rv":
  664.                     LS.rectSensor.call(this, object[i]);
  665.                     break;
  666.                 case "cv":
  667.                     LS.coneSensor.call(this, object[i]);
  668.                     break;
  669.                 case "b":
  670.                     LS.basicSensor.call(this, object[i]);
  671.                     break;
  672.                 default:
  673.                     break;
  674.             }
  675.         }
  676.     }
  677. };
  678.  
  679. // Update direction when using event's direction, and variable based range and width.
  680. LS.checkChanges = function (object) {
  681.     LS.getRange(object);
  682.     LS.getWidth(object);
  683.     LS.getDir.call(this, object);
  684.     return;
  685. }
  686.  
  687. // Check player position against basic circle sensor of event.
  688. LS.basicSensor = function (object) {
  689.     var selfs = LS.selfSwitch;
  690.     if (object == this.page()) { selfs = LS.selfSwitchComment; };
  691.     if (LS.regionBlock()) { selfs.call(this, false); return; };
  692.  
  693.     var inRange = Math.abs(this.deltaXFrom($gamePlayer.x));
  694.     inRange += Math.abs(this.deltaYFrom($gamePlayer.y));
  695.  
  696.     if (inRange <= LS.getRange(object)) {
  697.         selfs.call(this, true);
  698.     } else {
  699.         selfs.call(this, false);
  700.     };
  701.  
  702. };
  703.  
  704. // Check player position against Rectangle View and Line View sensor of event.
  705. LS.rectSensor = function (object) {
  706.     //Set selfs to the appropriate selfswitch switching function.
  707.     var selfs = LS.selfSwitch;
  708.     if (object == this.page()) { selfs = LS.selfSwitchComment; };
  709.     // Checks blackout region function.
  710.     if (LS.regionBlock()) { selfs.call(this, false); return; };
  711.     // Provides 2 way switching for event note sensors.
  712.     var trigger = false;
  713.  
  714.     var absDeltaY = Math.abs(this.deltaYFrom($gamePlayer.y));
  715.     var absDeltaX = Math.abs(this.deltaXFrom($gamePlayer.x));
  716.     var sensor = LS.getRange(object);
  717.     var sensorNeg = 0 - sensor;
  718.     var sWidth = LS.getWidth(object);
  719.     if (object._sensorType === 'lv') { sWidth = 0 };
  720.  
  721.     var dir = LS.getDir.call(this, object);
  722.     for (var d = 0; d < dir.length; d++) {
  723.         if ((dir[d] === 2 || dir[d] === 8)) {//if sensor direction is up or down
  724.             //Positive value if player is to the down of the event, Negative value if the player is to the up of the event.
  725.             var inFront = this.deltaYFrom($gamePlayer.y);
  726.             if (absDeltaX <= sWidth) {
  727.                 if (dir[d] === 8) {//if sensor direction is up, check player distance against sensor range.
  728.                     if (inFront <= sensor && inFront > 0) trigger = true;
  729.                 } else {//else sensor direction down, check player distance against sensor range.
  730.                     if (inFront >= sensorNeg && inFront < 0) trigger = true;
  731.                 };
  732.             };
  733.         } else {//else sensor direction is left or right
  734.             //Positive value if player is to the left of the event, Negative value if the player is to the right of the event.
  735.             var inFront = this.deltaXFrom($gamePlayer.x);
  736.             if (absDeltaY <= sWidth) {
  737.                 if (dir[d] === 4) {//if sensor direction is left, check player distance against sensor range.
  738.                     if (inFront <= sensor && inFront > 0) trigger = true;
  739.                 } else {//else sensor direction is right, check player distance against sensor range.
  740.                     if (inFront >= sensorNeg && inFront < 0) trigger = true;
  741.                 };
  742.             };
  743.         };
  744.     }
  745.     //if (inFront = 0) trigger = false;
  746.     selfs.call(this, trigger);
  747. };
  748.  
  749. // Check player position against Cone View sensor of event.
  750. LS.coneSensor = function (object) {
  751.     //Set selfs to the appropriate selfswitch switching function.
  752.     var selfs = LS.selfSwitch;
  753.     if (object == this.page()) { selfs = LS.selfSwitchComment; };
  754.     // Checks blackout region function.
  755.     if (LS.regionBlock()) { selfs.call(this, false); return; };
  756.     // Provides 2 way switching for event note sensors.
  757.     var trigger = false;
  758.  
  759.     // The distance between the event and the player, on the X axis as absDeltaX, on the Y axis as absDeltaY.
  760.     var absDeltaY = Math.abs(this.deltaYFrom($gamePlayer.y));
  761.     var absDeltaX = Math.abs(this.deltaXFrom($gamePlayer.x));
  762.     //Local variables for the page/event's sensor range and the negative of that range
  763.     var sensor = object._sensorRange;
  764.     var sensorNeg = 0 - sensor;
  765.  
  766.     // Math for cone shape
  767.     var coneL = this.deltaXFrom($gamePlayer.x) - absDeltaY;
  768.     var coneR = this.deltaXFrom($gamePlayer.x) + absDeltaY;
  769.     var coneU = this.deltaYFrom($gamePlayer.y) - absDeltaX;
  770.     var coneD = this.deltaYFrom($gamePlayer.y) + absDeltaX;
  771.  
  772.     var dir = LS.getDir.call(this, object);
  773.    
  774.     for (var d = 0; d < dir.length; d++) {
  775.         if ((dir[d] === 2 || dir[d] === 8)) {
  776.             //Positive value if player is to the down of the event, Negative value if the player is to the up of the event.
  777.             var inFront = this.deltaYFrom($gamePlayer.y);
  778.             if (coneU >= 0 && dir[d] === 8) { //If player is in cone shape upwards and the sensor zone is directed up, check player distance against sensor range.
  779.                 if (inFront <= sensor && inFront > 0) trigger = true;
  780.             } else if (coneD <= 0 && dir[d] === 2) {//If player is in cone shape downwards and the sensor zone is directed down, check player distance against sensor range.
  781.                 if (inFront >= sensorNeg && inFront < 0) trigger = true;
  782.             };
  783.         } else {
  784.             //Positive value if player is to the left of the event, Negative value if the player is to the right of the event.
  785.             var inFront = this.deltaXFrom($gamePlayer.x);
  786.             if (coneL >= 0 && dir[d] === 4) {//If player is in cone shape left and the sensor zone is directed left, check player distance against sensor range.
  787.                 if (inFront <= sensor && inFront > 0) trigger = true;
  788.             } else if (coneR <= 0 && dir[d] === 6) {//If player is in cone shape right and the sensor zone is directed right, check player distance against sensor range.
  789.                 if (inFront >= sensorNeg && inFront < 0) trigger = true;
  790.             };
  791.         };
  792.     }
  793.     selfs.call(this, trigger);
  794. };
  795.  
  796. // Returns true or false depending on if player is on a blackout region that meets all the switch/variable conditions if any exist.
  797. LS.regionBlock = function () {
  798.     if (Lyson.Param.RegionBlock === 0) return false;
  799.     var blockedRegion;
  800.     if (Lyson.Param.blockVariable === 'true') { blockedRegion = $gameVariables.value(Lyson.Param.RegionBlock) } else { blockedRegion = Lyson.Param.RegionBlock };
  801.  
  802.     if (Lyson.Param.blockSwitch === 'true') {
  803.         if ($gameSwitches.value(Lyson.Param.blockSwitchNum) !== true) { return false; };
  804.     };
  805.  
  806.     if ($gamePlayer.regionId() === blockedRegion) { return true; } else { return false; };
  807. };
  808.  
  809. // Change the event note selfswitch.
  810. LS.selfSwitch = function (selfs) {
  811.     $gameSelfSwitches.setValue([this._mapId, this._eventId, Lyson.Param.SelfSwitch], selfs);
  812. };
  813.  
  814. // Change the comment tag selfswitch.
  815. LS.selfSwitchComment = function (selfs) {
  816.     $gameSelfSwitches.setValue([this._mapId, this._eventId, Lyson.Param.CommentSwitch], selfs);
  817. };
  818.  
  819. // End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement