Advertisement
Lyson

Sensor SelfSwitch.js

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