Advertisement
Akito13

OrangeTimeSystem.js Error 1

Nov 7th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.95 KB | None | 0 0
  1. /*=============================================================================
  2. * Orange - Time System
  3. * By Hudell - www.hudell.com
  4. * OrangeTimeSystem.js
  5. * Version: 2.0.1
  6. * Free for commercial and non commercial use.
  7. *=============================================================================*/
  8. /*:
  9. * @plugindesc Adds a time system to your game
  10. * @author Hudell
  11. *
  12. * @param useRealTimeStructure
  13. * @desc If true, the time "Length" (except secondLength) variables will be ignored and the plugin will use the real time structure
  14. * @default false
  15. *
  16. * @param useRealTime
  17. * @desc If true, the time will be synced with the player's computer
  18. * @default false
  19. *
  20. * @param secondLength
  21. * @desc How many real time milliseconds should an ingame second last
  22. * @default 100
  23. *
  24. * @param secondLengthVariable
  25. * @desc Load the length of the second from a variable instead of a fixed value
  26. * @default 0
  27. *
  28. * @param minuteLength
  29. * @desc How many ingame seconds should an ingame minute last
  30. * @default 60
  31. *
  32. * @param hourLength
  33. * @desc How many ingame minutes should an ingame hour last
  34. * @default 60
  35. *
  36. * @param dayLength
  37. * @desc How many ingame hours should an ingame day last
  38. * @default 24
  39. *
  40. * @param weekLength
  41. * @desc How many ingame days should an ingame week last
  42. * @default 7
  43. *
  44. * @param monthLength
  45. * @desc How many ingame days should an ingame month last
  46. * @default 31
  47. *
  48. * @param yearLength
  49. * @desc How many ingame months should an ingame year last
  50. * @default 12
  51. *
  52. * @param dayPeriod1Hour
  53. * @desc At what hour does night turn into early morning
  54. * @default 6
  55. *
  56. * @param dayPeriod2Hour
  57. * @desc At what hour does early morning turn into day
  58. * @default 9
  59. *
  60. * @param dayPeriod3Hour
  61. * @desc At what hour does day turn into evening
  62. * @default 18
  63. *
  64. * @param dayPeriod4Hour
  65. * @desc At what hour does evening turn into night
  66. * @default 20
  67. *
  68. * @param mainSwitchId
  69. * @desc The Number of the Switch used to activate the flow of time
  70. * @default 0
  71. *
  72. * @param pauseClockDuringConversations
  73. * @desc If true, it will stop the flow of time while messages are being displayed on screen.
  74. * @default true
  75. *
  76. * @param initialSecond
  77. * @desc At what second will the game start?
  78. * @default 0
  79. *
  80. * @param initialMinute
  81. * @desc At what minute will the game start?
  82. * @default 0
  83. *
  84. * @param initialHour
  85. * @desc At what hour will the game start?
  86. * @default 6
  87. *
  88. * @param initialDay
  89. * @desc At what day will the game start?
  90. * @default 1
  91. *
  92. * @param initialMonth
  93. * @desc At what month will the game start?
  94. * @default 1
  95. *
  96. * @param initialYear
  97. * @desc At what year will the game start?
  98. * @default 1
  99. *
  100. * @param weekDayOffset
  101. * @desc Change the value here to a number betwen 0 and 6 to change the week day of the first day of the firstyear
  102. * @default 0
  103. *
  104. * @param dayNames
  105. * @desc A list of all the day names, separated by comma. If empty, the day number will be used
  106. * @default Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
  107. *
  108. * @param dayShortNames
  109. * @desc A list of all the day short names, separated by comma. If empty, the day number will be used
  110. * @default Mon, Tue, Wed, Thur, Fri, Sat, Sun
  111. *
  112. * @param monthNames
  113. * @desc A list of all the month names, separated by comma. If empty, the month number will be used
  114. * @default January, February, March, April, May, June, July, August, September, October, November, December
  115. *
  116. * @param monthShortNames
  117. * @desc A list of all the month short names, separated by comma. If empty, the month number will be used
  118. * @default Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
  119. *
  120. * @param insideSwitch
  121. * @desc A switch to indicate if a map is inside a building or closed space
  122. * @default 0
  123. *
  124. * @param tilesetList
  125. * @desc You can set a list of comma separated tilesets that will always be treated as "inside", regardless of the switch value.
  126. * @default
  127. *
  128. * @help
  129. * ============================================================================
  130. * Introduction and Instructions
  131. * ============================================================================
  132. *
  133. * This plugin creates a time system for your game. Nothing will change on the
  134. * game itself, unless you use another plugin that uses this one.
  135. *
  136. * ============================================================================
  137. * Latest Version
  138. * ============================================================================
  139. *
  140. * Get the latest version of this script on http://link.hudell.com/time-system
  141. *
  142. */
  143.  
  144. var Imported = Imported || {};
  145.  
  146. if (Imported['MVCommons'] === undefined) {
  147. var MVC = {};
  148.  
  149. (function($){
  150. $.isArray = function(obj) { return Object.prototype.toString.apply(obj) === '[object Array]'; };
  151. $.shallowClone = function(obj) { var result; if ($.isArray(obj)) { return obj.slice(0); } else if (obj && !obj.prototype && (typeof obj == 'object' || obj instanceof Object)) { result = {}; for (var p in obj) { result[p] = obj[p]; } return result; } return obj; };
  152. $.defaultGetter = function(name) { return function () { return this['_' + name]; }; };
  153. $.defaultSetter = function(name) { return function (value) { var prop = '_' + name; if ((!this[prop]) || this[prop] !== value) { this[prop] = value; if (this._refresh) { this._refresh(); } } }; };
  154. $.accessor = function(value, name /* , setter, getter */) { Object.defineProperty(value, name, { get: arguments.length > 3 ? arguments[3] : $.defaultGetter(name), set: arguments.length > 2 ? arguments[2] : $.defaultSetter(name), configurable: true });};
  155. $.reader = function(obj, name /*, getter */) { Object.defineProperty(obj, name, { get: arguments.length > 2 ? arguments[2] : defaultGetter(name), configurable: true }); };
  156. })(MVC);
  157.  
  158. Number.prototype.fix = function() { return parseFloat(this.toPrecision(12)); };
  159. Number.prototype.floor = function() { return Math.floor(this.fix()); };
  160.  
  161. if (Utils.isOptionValid('test')) {
  162. console.log('MVC not found, OrangeTimeSystem will be using essentials (copied from MVC 1.2.1).');
  163. }
  164. }
  165.  
  166. if (Imported['OrangeEventManager'] === undefined) {
  167. var OrangeEventManager = {};
  168. (function($) { "use strict"; $._events = []; $.on = function(eventName, callback) { if (this._events[eventName] === undefined) this._events[eventName] = []; this._events[eventName].push(callback); }; $.un = function(eventName, callback) { if (this._events[eventName] === undefined) return; for (var i = 0; i < this._events[eventName].length; i++) { if (this._events[eventName][i] == callback) { this._events[eventName][i] = undefined; return; } } }; $.executeCallback = function(callback) { if (typeof(callback) == "function") { return callback.call(this); } if (typeof(callback) == "number") { $gameTemp.reserveCommonEvent(callback); return true; } if (typeof(callback) == "string") { if (parseInt(callback, 10) == callback.trim()) { $gameTemp.reserveCommonEvent(parseInt(callback, 10)); return true; } return eval(callback); } console.error("Unknown callback type: ", callback); return undefined; }; $.runEvent = function(eventName) { if (this._events[eventName] === undefined) return; for (var i = 0; i < this._events[eventName].length; i++) { var callback = this._events[eventName][i]; if (this.executeCallback(callback) === false) { break; } } };})(OrangeEventManager);
  169. Imported["OrangeEventManager"] = 1;
  170.  
  171. if (Utils.isOptionValid('test')) {
  172. console.log('OrangeTimeSystem will be using it\'s internal copy of OrangeEventManager 1.0.');
  173. }
  174. }
  175.  
  176. var OrangeTimeSystem = OrangeTimeSystem || MVC.shallowClone(OrangeEventManager);
  177.  
  178. var DayPeriods = {
  179. EARLY_MORNING: 1,
  180. DAY: 2,
  181. EVENING: 3,
  182. NIGHT: 4
  183. };
  184.  
  185. (function($) {
  186. "use strict";
  187.  
  188. $.Parameters = PluginManager.parameters('OrangeTimeSystem');
  189. $.Param = $.Param || {};
  190.  
  191. $.Param.useRealTime = $.Parameters["useRealTime"] == "true";
  192. $.Param.useRealTimeStructure = $.Parameters["useRealTimeStructure"] == "true";
  193.  
  194. if ($.Param.useRealTime) {
  195. $.Param.secondLength = 1000;
  196. } else {
  197. $.Param.secondLength = Number($.Parameters['secondLength'] || 100);
  198. }
  199.  
  200. if ($.Param.useRealTime || $.Param.useRealTimeStructure) {
  201. $.Param.yearLength = 12;
  202. $.Param.minuteLength = 60;
  203. $.Param.hourLength = 60;
  204. $.Param.dayLength = 24;
  205. $.Param.weekLength = 7;
  206. $.Param.monthLength = 31;
  207. } else {
  208. $.Param.yearLength = Number($.Parameters['yearLength'] || 12);
  209. $.Param.minuteLength = Number($.Parameters['minuteLength'] || 60);
  210. $.Param.hourLength = Number($.Parameters['hourLength'] || 60);
  211. $.Param.dayLength = Number($.Parameters['dayLength'] || 24);
  212. $.Param.weekLength = Number($.Parameters['weekLength'] || 7);
  213. $.Param.monthLength = Number($.Parameters['monthLength'] || 31);
  214. }
  215.  
  216. $.Param.secondLengthVariable = Number($.Parameters['secondLengthVariable'] || 0);
  217.  
  218. $.Param.initialSecond = Number($.Parameters['initialSecond'] || 0);
  219. $.Param.initialMinute = Number($.Parameters['initialMinute'] || 0);
  220. $.Param.initialHour = Number($.Parameters['initialHour'] || 6);
  221. $.Param.initialDay = Number($.Parameters['initialDay'] || 1);
  222. $.Param.initialMonth = Number($.Parameters['initialMonth'] || 1);
  223. $.Param.initialYear = Number($.Parameters['initialYear'] || 1);
  224.  
  225. $.Param.dayPeriod1Hour = Number($.Parameters['dayPeriod1Hour'] || 6);
  226. $.Param.dayPeriod2Hour = Number($.Parameters['dayPeriod2Hour'] || 9);
  227. $.Param.dayPeriod3Hour = Number($.Parameters['dayPeriod3Hour'] || 18);
  228. $.Param.dayPeriod4Hour = Number($.Parameters['dayPeriod4Hour'] || 20);
  229.  
  230. $.Param.insideSwitch = Number($.Parameters['insideSwitch'] || 0);
  231. $.Param.tilesetList = ($.Parameters["tilesetList"] || '').split(',');
  232.  
  233. for (var i = 0; i < $.Param.tilesetList.length; i++) {
  234. $.Param.tilesetList[i] = parseInt($.Param.tilesetList[i], 10);
  235. }
  236.  
  237. $.Param.weekDayOffset = Number($.Parameters['weekDayOffset'] || 1);
  238. $.Param.pauseClockDuringConversations = $.Parameters["pauseClockDuringConversations"] !== "false";
  239.  
  240. var switchId = parseInt($.Parameters['mainSwitchId'], 10);
  241.  
  242. var monthNames = ($.Parameters.monthNames || "").trim();
  243. var monthShortNames = ($.Parameters.monthShortNames || "").trim();
  244. var dayNames = ($.Parameters.dayNames || "").trim();
  245. var dayShortNames = ($.Parameters.dayShortNames || "").trim();
  246.  
  247. if (monthNames.length > 0) {
  248. $.Param.monthNames = monthNames.split(',');
  249. } else {
  250. $.Param.monthNames = [];
  251. }
  252.  
  253. if (monthShortNames.length > 0) {
  254. $.Param.monthShortNames = monthShortNames.split(',');
  255. } else {
  256. $.Param.monthShortNames = [];
  257. }
  258.  
  259. if (dayNames.length > 0) {
  260. $.Param.dayNames = dayNames.split(',');
  261. } else {
  262. $.Param.dayNames = [];
  263. }
  264.  
  265. if (dayShortNames.length > 0) {
  266. $.Param.dayShortNames = dayShortNames.split(',');
  267. } else {
  268. $.Param.dayShortNames = [];
  269. }
  270.  
  271. while ($.Param.monthNames.length < $.Param.yearLength) {
  272. $.Param.monthNames.push(($.Param.monthNames.length + 1).toString());
  273. }
  274. while ($.Param.monthShortNames.length < $.Param.yearLength) {
  275. $.Param.monthShortNames.push(($.Param.monthShortNames.length + 1).toString());
  276. }
  277. while ($.Param.dayNames.length < $.Param.weekLength) {
  278. $.Param.dayNames.push(($.Param.dayNames.length + 1).toString());
  279. }
  280. while ($.Param.dayShortNames.length < $.Param.weekLength) {
  281. $.Param.dayShortNames.push(($.Param.dayShortNames.length + 1).toString());
  282. }
  283.  
  284. if (switchId !== NaN && switchId > 0) {
  285. $.Param.mainSwitchId = switchId;
  286. } else {
  287. $.Param.mainSwitchId = undefined;
  288. }
  289.  
  290. MVC.accessor($, 'seconds');
  291. MVC.accessor($, 'minute');
  292. MVC.accessor($, 'hour');
  293. MVC.accessor($, 'day');
  294. MVC.accessor($, 'month');
  295. MVC.accessor($, 'year');
  296. MVC.accessor($, 'dayPeriod');
  297. MVC.accessor($, 'weekDay');
  298.  
  299. MVC.reader($, 'monthName', function(){
  300. return $.Param.monthNames[(this.month - 1) % $.Param.monthNames.length];
  301. });
  302.  
  303. MVC.reader($, 'monthShortName', function(){
  304. return $.Param.monthShortNames[(this.month - 1) % $.Param.monthShortNames.length];
  305. });
  306.  
  307. MVC.reader($, 'dayName', function(){
  308. return $.Param.dayNames[(this.weekDay - 1 + $.Param.dayNames.length) % $.Param.dayNames.length];
  309. });
  310.  
  311. MVC.reader($, 'dayShortName', function(){
  312. return $.Param.dayShortNames[(this.weekDay - 1 + $.Param.dayShortNames.length) % $.Param.dayShortNames.length];
  313. });
  314.  
  315. MVC.reader($, 'inside', function(){
  316. if (SceneManager._scene instanceof Scene_Map) {
  317. if ($.Param.tilesetList.length > 0) {
  318. if ($dataMap !== null) {
  319. if ($.Param.tilesetList.indexOf($dataMap.tilesetId) >= 0) {
  320. return true;
  321. }
  322. }
  323. }
  324.  
  325. if ($.Param.insideSwitch > 0) {
  326. if ($gameSwitches.value($.Param.insideSwitch)) {
  327. return true;
  328. }
  329. }
  330. }
  331.  
  332. return false;
  333. });
  334.  
  335. $.seconds = $.Param.initialSecond;
  336. $.minute = $.Param.initialMinute;
  337. $.hour = $.Param.initialHour;
  338. $.day = $.Param.initialDay;
  339. $.month = $.Param.initialMonth;
  340. $.year = $.Param.initialYear;
  341.  
  342. MVC.accessor($, 'paused', function(value) {
  343. if ($.Param.mainSwitchId !== undefined) {
  344. $gameSwitches.setValue($.Param.mainSwitchId, !value);
  345. return;
  346. }
  347.  
  348. this._paused = value;
  349. }, function() {
  350. if ($.Param.mainSwitchId !== undefined) {
  351. if ($gameSwitches === undefined || $gameSwitches === null) return true;
  352. return !$gameSwitches.value($.Param.mainSwitchId);
  353. }
  354.  
  355. if (this._paused !== undefined) {
  356. return this._paused;
  357. }
  358.  
  359. return false;
  360. });
  361.  
  362. $._timeEvents = [];
  363. $._afterTimeEvents = [];
  364. $.seconds = 0;
  365. $.minute = 0;
  366. $.hour = 0;
  367. $.day = 1;
  368. $.month = 1;
  369. $.year = 1;
  370. $.dayPeriod = 0;
  371. $.weekDay = 0;
  372.  
  373. $.runTimeChangeEvents = function(oldData) {
  374. if (oldData.seconds != this.seconds) {
  375. this._onChangeSecond();
  376. }
  377.  
  378. if (oldData.minute !== this.minute) {
  379. this._onChangeMinute();
  380. }
  381.  
  382. if (oldData.hour !== this.hour) {
  383. this._onChangeHour();
  384. }
  385.  
  386. if (oldData.day !== this.day) {
  387. this._onChangeDay();
  388. }
  389.  
  390. if (oldData.month !== this.month) {
  391. this._onChangeMonth();
  392. }
  393.  
  394. if (oldData.year !== this.year) {
  395. this._onChangeYear();
  396. }
  397.  
  398. if (oldData.dayPeriod !== this.dayPeriod) {
  399. this._onChangeDayPeriod();
  400. }
  401. };
  402.  
  403. $.convertConfigToTimestamp = function(config) {
  404. var years = config.year;
  405. var months = config.month;
  406. var days = config.day;
  407. var hours = config.hour;
  408. var minutes = config.minute;
  409. var seconds = config.seconds;
  410.  
  411. if ($.Param.useRealTime || $.Param.useRealTimeStructure) {
  412. var dateObj = new Date();
  413. dateObj.setFullYear(years);
  414. dateObj.setMonth(months - 1);
  415. dateObj.setDate(days);
  416. dateObj.setHours(hours);
  417. dateObj.setMinutes(minutes);
  418. dateObj.setSeconds(seconds);
  419.  
  420. return dateObj.getTime();
  421. } else {
  422. years -= 1;
  423. if (years > 0) {
  424. months += years * $.Param.yearLength;
  425. }
  426.  
  427. months -= 1;
  428. if (months > 0) {
  429. days += months * $.Param.monthLength;
  430. }
  431.  
  432. days -= 1;
  433. if (days > 0) {
  434. hours += days * $.Param.dayLength;
  435. }
  436.  
  437. if (hours > 0) {
  438. minutes += hours * $.Param.hourLength;
  439. }
  440.  
  441. if (minutes > 0) {
  442. seconds += minutes * $.Param.minuteLength;
  443. }
  444.  
  445. return seconds;
  446. }
  447. };
  448.  
  449. $.convertTimestampToConfig = function(timestamp) {
  450. if ($.Param.useRealTime || $.Param.useRealTimeStructure) {
  451. var dateObj = new Date(timestamp);
  452.  
  453. return {
  454. seconds : dateObj.getSeconds(),
  455. minute : dateObj.getMinutes(),
  456. hour : dateObj.getHours(),
  457. day : dateObj.getDate(),
  458. month : dateObj.getMonth() + 1,
  459. year : dateObj.getFullYear()
  460. };
  461. } else {
  462. var seconds = timestamp;
  463. var minutes = 0;
  464. var hours = 0;
  465. var days = 0;
  466. var months = 0;
  467. var years = 0;
  468.  
  469. minutes = (seconds / $.Param.minuteLength).floor();
  470. seconds -= (minutes * $.Param.minuteLength);
  471.  
  472. hours = (minutes / $.Param.hourLength).floor();
  473. minutes -= (hours * $.Param.hourLength);
  474.  
  475. days = (hours / $.Param.dayLength).floor();
  476. hours -= (days * $.Param.dayLength);
  477.  
  478. months = (days / $.Param.monthLength).floor();
  479. days -= (months * $.Param.monthLength);
  480.  
  481. years = (months / $.Param.yearLength).floor();
  482. months -= (years * $.Param.yearLength);
  483.  
  484. return {
  485. seconds : seconds,
  486. minute : minutes,
  487. hour : hours,
  488. day : days + 1,
  489. month : months + 1,
  490. year : years + 1
  491. };
  492. }
  493. };
  494.  
  495. // Returns the difference in number of seconds
  496. $.compareTimestamps = function(timestamp1, timestamp2) {
  497. if (typeof timestamp1 == "object") {
  498. timestamp1 = $.convertConfigToTimestamp(timestamp1);
  499. }
  500.  
  501. if (typeof timestamp2 == "object") {
  502. timestamp2 = $.convertConfigToTimestamp(timestamp2);
  503. }
  504.  
  505. var diff = timestamp2 - timestamp1;
  506.  
  507. if ($.Param.useRealTime || $.Param.useRealTimeStructure) {
  508. return (diff / 1000).floor();
  509. } else {
  510. return diff;
  511. }
  512. };
  513.  
  514. $.validateDateTimeValues = function(date) {
  515. if ($.Param.useRealTime || $.Param.useRealTimeStructure) return;
  516.  
  517. while (date.seconds >= $.Param.minuteLength) {
  518. date.minute += 1;
  519. date.seconds -= $.Param.minuteLength;
  520. }
  521.  
  522. while (date.minute >= $.Param.hourLength) {
  523. date.hour += 1;
  524. date.minute -= $.Param.hourLength;
  525. }
  526.  
  527. while (date.hour >= $.Param.dayLength) {
  528. date.day += 1;
  529. date.hour -= $.Param.dayLength;
  530. }
  531.  
  532. while (date.day > $.Param.monthLength) {
  533. date.month += 1;
  534. date.day -= $.Param.monthLength;
  535. }
  536.  
  537. while (date.month > $.Param.yearLength) {
  538. date.year += 1;
  539. date.month -= $.Param.yearLength;
  540. }
  541. };
  542.  
  543. $.updateTime = function(runEvents) {
  544. if (runEvents === undefined) runEvents = true;
  545.  
  546. var oldData = $.getDateTime();
  547.  
  548. if ($.Param.useRealTimeStructure) {
  549. $.applyRealTimeLogic();
  550. }
  551.  
  552. var date = {
  553. seconds : this.seconds,
  554. minute : this.minute,
  555. hour : this.hour,
  556. day : this.day,
  557. month : this.month,
  558. year : this.year
  559. };
  560.  
  561. this.validateDateTimeValues(date);
  562.  
  563. this.seconds = date.seconds;
  564. this.minute = date.minute;
  565. this.hour = date.hour;
  566. this.day = date.day;
  567. this.month = date.month;
  568. this.year = date.year;
  569.  
  570. this.updateDayPeriod();
  571.  
  572. if (runEvents) {
  573. this.runTimeChangeEvents(oldData);
  574. }
  575.  
  576. // Calculate week day
  577. if (!$.Param.useRealTime && !$.Param.useRealTimeStructure) {
  578. var previousYears = this.year - 1;
  579. var previousMonths = previousYears * $.Param.yearLength;
  580. var numMonths = previousMonths + this.month - 1;
  581. var numDays = numMonths * $.Param.monthLength + this.day;
  582. this.weekDay = numDays % $.Param.weekLength + $.Param.weekDayOffset;
  583. }
  584.  
  585. this._onUpdateTime();
  586. };
  587.  
  588. $.updateDayPeriodForDate = function(date) {
  589. // Calculate day period
  590. if (date.hour < $.Param.dayPeriod1Hour) {
  591. date.dayPeriod = 4;
  592. } else if (date.hour < $.Param.dayPeriod2Hour) {
  593. date.dayPeriod = 1;
  594. } else if (date.hour < $.Param.dayPeriod3Hour) {
  595. date.dayPeriod = 2;
  596. } else if (date.hour < $.Param.dayPeriod4Hour) {
  597. date.dayPeriod = 3;
  598. } else {
  599. date.dayPeriod = 4;
  600. }
  601. };
  602.  
  603. $.updateDayPeriod = function() {
  604. this.updateDayPeriodForDate(this);
  605. };
  606.  
  607. $.isEarlyMorning = function() {
  608. return this.dayPeriod == DayPeriods.EARLY_MORNING;
  609. };
  610.  
  611. $.isMidday = function() {
  612. return this.dayPeriod == DayPeriods.DAY;
  613. };
  614.  
  615. $.isEvening = function() {
  616. return this.dayPeriod == DayPeriods.EVENING;
  617. };
  618.  
  619. $.isNight = function() {
  620. return this.dayPeriod == DayPeriods.NIGHT;
  621. };
  622.  
  623. $.isEnabled = function() {
  624. return (this._intervalHandler !== undefined);
  625. };
  626.  
  627. $.enableTime = function() {
  628. if (this._intervalHandler !== undefined) return;
  629.  
  630. var length = $.Param.secondLength;
  631. if ($.Param.useRealTime) {
  632. length = 1000;
  633. } else if ($.Param.secondLengthVariable > 0) {
  634. if ($gameVariables !== null) {
  635. if ($gameVariables.value($.Param.secondLengthVariable) > 0) {
  636. length = $gameVariables.value($.Param.secondLengthVariable);
  637. }
  638. }
  639. }
  640.  
  641. this._intervalHandler = setInterval(function() {
  642. $.progressTime();
  643. }, length);
  644. };
  645.  
  646. $.refreshTimeSystem = function() {
  647. $.disableTime();
  648. $.enableTime();
  649. };
  650.  
  651. $.disableTime = function() {
  652. if (this._intervalHandler === undefined) return;
  653.  
  654. clearInterval(this._intervalHandler);
  655. this._intervalHandler = undefined;
  656. };
  657.  
  658. $.applyRealTimeLogic = function(){
  659. var date = new Date();
  660.  
  661. date.setFullYear(this.year);
  662. date.setMonth(this.month - 1);
  663. date.setDate(this.day);
  664. date.setHours(this.hour);
  665. date.setMinutes(this.minute);
  666. date.setSeconds(this.seconds);
  667.  
  668. this.seconds = date.getSeconds();
  669. this.minute = date.getMinutes();
  670. this.hour = date.getHours();
  671. this.day = date.getDate();
  672. this.month = date.getMonth() + 1;
  673. this.year = date.getFullYear();
  674. this.weekDay = date.getDay();
  675. };
  676.  
  677. $.loadRealTime = function() {
  678. var date = new Date();
  679. var oldData = this.getDateTime();
  680. var anyChanged = false;
  681.  
  682. $.seconds = date.getSeconds();
  683. $.minute = date.getMinutes();
  684. $.hour = date.getHours();
  685. $.day = date.getDate();
  686. $.month = date.getMonth() + 1;
  687. $.year = date.getFullYear();
  688. $.weekDay = date.getDay();
  689. $.updateDayPeriod();
  690.  
  691. if ($.seconds != oldData.seconds) {
  692. $._onChangeSecond();
  693. }
  694.  
  695. if ($.minute != oldData.minute) {
  696. $._onChangeMinute();
  697. }
  698.  
  699. if ($.hour != oldData.hour) {
  700. $._onChangeHour();
  701. }
  702.  
  703. if ($.day != oldData.day) {
  704. $._onChangeDay();
  705. }
  706.  
  707. if ($.month != oldData.month) {
  708. $._onChangeMonth();
  709. }
  710.  
  711. if ($.year != oldData.year) {
  712. $._onChangeYear();
  713. }
  714. };
  715.  
  716. $.setTime = function(seconds, minute, hour, day, month, year) {
  717. var oldData = this.getDateTime();
  718.  
  719. if (seconds !== undefined) {
  720. this.seconds = seconds;
  721. }
  722.  
  723. if (minute !== undefined) {
  724. this.minute = minute;
  725. }
  726.  
  727. if (hour !== undefined) {
  728. this.hour = hour;
  729. }
  730.  
  731. if (day !== undefined) {
  732. this.day = day;
  733. }
  734.  
  735. if (month !== undefined) {
  736. this.month = month;
  737. }
  738.  
  739. if (year !== undefined) {
  740. this.year = year;
  741. }
  742.  
  743. $.updateTime(false);
  744. $.runTimeChangeEvents(oldData);
  745. };
  746.  
  747. $.addSeconds = function(seconds) {
  748. $.addTime({seconds : seconds});
  749. };
  750.  
  751. $.addMinutes = function(minutes) {
  752. $.addTime({minutes : minutes});
  753. };
  754.  
  755. $.addHours = function(hours) {
  756. $.addTime({hours : hours});
  757. };
  758.  
  759. $.addDays = function(days) {
  760. $.addTime({days : days});
  761. };
  762.  
  763. $.addMonths = function(months) {
  764. $.addTime({months : months});
  765. };
  766.  
  767. $.addYears = function(years) {
  768. $.addTime({years : years});
  769. };
  770.  
  771. $.addTime = function(timeData) {
  772. $.passTime(timeData.seconds, timeData.minutes, timeData.hours, timeData.days, timeData.months, timeData.years);
  773. };
  774.  
  775. $.passTime = function(seconds, minutes, hours, days, months, years) {
  776. var oldData = this.getDateTime();
  777.  
  778. this.seconds += Number(seconds || 0);
  779. this.minute += Number(minutes || 0);
  780. this.hour += Number(hours || 0);
  781. this.day += Number(days || 0);
  782. this.month += Number(months || 0);
  783. this.year += Number(years || 0);
  784.  
  785. $.updateTime(false);
  786. $.runTimeChangeEvents(oldData);
  787. };
  788.  
  789. $.isInternallyPaused = function() {
  790. if ($.Param.pauseClockDuringConversations === true) {
  791. if (SceneManager._scene instanceof Scene_Map) {
  792. if ($gameMessage.isBusy()) {
  793. return true;
  794. }
  795. }
  796. }
  797.  
  798. return false;
  799. };
  800.  
  801. $.progressTime = function() {
  802. if (this.paused) return;
  803. if (this.isInternallyPaused()) return;
  804.  
  805. if ($.Param.useRealTime) {
  806. $.loadRealTime();
  807. $._onUpdateTime();
  808. } else if (SceneManager._scene instanceof Scene_Map) {
  809. $.seconds += 1;
  810.  
  811. $.updateTime();
  812. $._onChangeSecond();
  813. }
  814. };
  815.  
  816. $._onChangeSecond = function() {
  817. this.runEvent('changeSecond');
  818. };
  819.  
  820. $._onChangeMinute = function() {
  821. this.runEvent('changeMinute');
  822. };
  823.  
  824. $._onChangeHour = function() {
  825. this.runEvent('changeHour');
  826. };
  827.  
  828. $._onChangeDayPeriod = function() {
  829. this.runEvent('changeDayPeriod');
  830. };
  831.  
  832. $._onChangeDay = function() {
  833. this.runEvent('changeDay');
  834. };
  835.  
  836. $._onChangeMonth = function() {
  837. this.runEvent('changeMonth');
  838. };
  839.  
  840. $._onChangeYear = function() {
  841. this.runEvent('changeYear');
  842. };
  843.  
  844. $.onTime = function(callback, hour, minute, second, autoRemove) {
  845. return this.onDateTime(callback, 0, 0, 0, hour, minute, second, autoRemove);
  846. };
  847.  
  848. $.onDate = function(callback, day, month, year, autoRemove) {
  849. return this.onDateTime(callback, day, month, year, 0, 0, 0, autoRemove);
  850. };
  851.  
  852. $.onDateTime = function(callback, day, month, year, hour, minute, second, autoRemove) {
  853. var config = {
  854. day: day,
  855. month: month,
  856. year: year,
  857. hour: hour,
  858. minute: minute,
  859. second: second,
  860. callback: callback,
  861. autoRemove : autoRemove,
  862. after : false
  863. };
  864.  
  865. if (autoRemove === undefined) {
  866. autoRemove = false;
  867. }
  868.  
  869. return $.registerTimeEvent(config);
  870. };
  871.  
  872. $.onWeekDay = function(callback, weekDay) {
  873. return this.registerTimeEvent({
  874. weekDay: weekDay,
  875. callback: callback
  876. });
  877. };
  878.  
  879. $.atDate = $.onDate;
  880. $.atTime = $.onTime;
  881. $.atDateTime = $.onDateTime;
  882. $.atWeekDay = $.onWeekDay;
  883.  
  884. $.registerAfterTimeEvent = function(config) {
  885. this._afterTimeEvents.push(config);
  886. return this._afterTimeEvents.indexOf(config);
  887. };
  888.  
  889. $.runInDateTime = function(callback, years, months, days, hours, minutes, seconds, autoRemove) {
  890. var newDate = $.getDateTime();
  891.  
  892. newDate.year += Number(years || 0);
  893. newDate.month += Number(months || 0);
  894. newDate.day += Number(days || 0);
  895. newDate.hour += Number(hours || 0);
  896. newDate.minute += Number(minutes ||0);
  897. newDate.seconds += Number(seconds || 0);
  898.  
  899. this.validateDateTimeValues(newDate);
  900.  
  901. if (autoRemove === undefined) {
  902. autoRemove = true;
  903. }
  904.  
  905. var config = {
  906. callback : callback,
  907. year : newDate.year,
  908. month : newDate.month,
  909. day : newDate.day,
  910. hour : newDate.hour,
  911. minute : newDate.minute,
  912. seconds : newDate.seconds,
  913. autoRemove : autoRemove,
  914. after : true
  915. };
  916.  
  917. var key = undefined;
  918.  
  919. key = $.registerAfterTimeEvent(config);
  920. config.key = key;
  921. return key;
  922. };
  923.  
  924. $.runInHours = function(callback, hours, minutes, seconds) {
  925. return $.runInDateTime(callback, 0, 0, 0, hours, minutes, seconds);
  926. };
  927.  
  928. $.runInDays = function(callback, days) {
  929. return $.runInDateTime(callback, 0, 0, days);
  930. };
  931.  
  932. $.runInMonths = function(callback, months) {
  933. return $.runInDateTime(callback, 0, months);
  934. };
  935.  
  936. $.runInYears = function(callback, years) {
  937. return $.runInDateTime(callback, years);
  938. };
  939.  
  940. $.runInMinutes = function(callback, minutes) {
  941. return $.runInHours(callback, 0, minutes);
  942. };
  943.  
  944. $.runInSeconds = function(callback, seconds) {
  945. return $.runInHours(callback, 0, 0, seconds);
  946. };
  947.  
  948. $.registerTimeEvent = function(config) {
  949. this._timeEvents.push(config);
  950. return this._timeEvents.indexOf(config);
  951. };
  952.  
  953. $.checkIfEventShouldRun = function(config) {
  954. if (config.day !== undefined && config.day != this.day) return false;
  955. if (config.month !== undefined && config.month != this.month) return false;
  956. if (config.year !== undefined && config.year != this.year) return false;
  957. if (config.hour !== undefined && config.hour != this.hour) return false;
  958. if (config.minute !== undefined && config.minute != this.minute) return false;
  959. if (config.second !== undefined && config.second != this.seconds) return false;
  960. if (config.weekDay !== undefined && config.weekDay != this.weekDay) return false;
  961.  
  962. return true;
  963. };
  964.  
  965. $.checkEventsToRun = function(eventList, after) {
  966. var config = undefined;
  967. var i;
  968. var keysToRemove = [];
  969.  
  970. var currentTimestamp = this.convertConfigToTimestamp(this.getDateTime());
  971. if ($.Param.useRealTime || $.Param.useRealTimeStructure) {
  972. currentTimestamp = (currentTimestamp / 1000).floor();
  973. }
  974.  
  975. for (i = 0; i < eventList.length; i++) {
  976. config = eventList[i];
  977.  
  978. if (config.callback === undefined) continue;
  979.  
  980. if (after) {
  981. if (config.seconds === undefined) {
  982. config.seconds = config.second;
  983. }
  984.  
  985. var timestamp = this.convertConfigToTimestamp(config);
  986. if ($.Param.useRealTime || $.Param.useRealTimeStructure) {
  987. timestamp = (timestamp / 1000).floor();
  988. }
  989.  
  990. if (timestamp > currentTimestamp) {
  991. continue;
  992. }
  993. } else {
  994. if (!this.checkIfEventShouldRun(config)) {
  995. continue;
  996. }
  997. }
  998.  
  999. this.executeCallback(config.callback);
  1000. if (config.autoRemove === true) {
  1001. config.callback = undefined;
  1002. keysToRemove.push(config.key);
  1003. }
  1004. }
  1005.  
  1006. for (var key in keysToRemove) {
  1007. delete eventList[key];
  1008. }
  1009. };
  1010.  
  1011. $.checkIfConfigHasCallback = function(config) {
  1012. return config.callback !== undefined;
  1013. };
  1014.  
  1015. $._onUpdateTime = function() {
  1016. $.checkEventsToRun(this._timeEvents, false);
  1017. $.checkEventsToRun(this._afterTimeEvents, true);
  1018.  
  1019. this._timeEvents = this._timeEvents.filter($.checkIfConfigHasCallback);
  1020. this._afterTimeEvents = this._afterTimeEvents.filter($.checkIfConfigHasCallback);
  1021. };
  1022.  
  1023. $.getDateTime = function() {
  1024. return {
  1025. hour: $.hour,
  1026. minute: $.minute,
  1027. seconds: $.seconds,
  1028. day: $.day,
  1029. month: $.month,
  1030. year: $.year,
  1031. dayPeriod: $.dayPeriod,
  1032. weekDay: $.weekDay,
  1033. paused: $.paused
  1034. };
  1035. };
  1036.  
  1037. $.getTomorrow = function(){
  1038. var dateObj = $.getDateTime();
  1039.  
  1040. dateObj.day += 1;
  1041. $.validateDateTimeValues(dateObj);
  1042.  
  1043. return dateObj;
  1044. };
  1045.  
  1046. $.setDateTime = function(dateTime) {
  1047. this.hour = dateTime.hour || 0;
  1048. this.minute = dateTime.minute || 0;
  1049. this.seconds = dateTime.seconds || 0;
  1050. this.day = dateTime.day || 1;
  1051. this.month = dateTime.month || 1;
  1052. this.year = dateTime.year || 1;
  1053. this.dayPeriod = dateTime.dayPeriod || DayPeriods.EARLY_MORNING;
  1054. this.weekDay = dateTime.weekDay || 0;
  1055.  
  1056. if (dateTime.paused !== undefined) {
  1057. this.paused = dateTime.paused;
  1058. } else {
  1059. this.paused = false;
  1060. }
  1061. };
  1062.  
  1063. $.getCallbacksFromList = function(eventList) {
  1064. var callbackList = [];
  1065.  
  1066. for (var key in eventList) {
  1067. if (eventList[key].callback === undefined) continue;
  1068.  
  1069. //can't save functions
  1070. if (typeof(eventList[key].callback) == "function") continue;
  1071.  
  1072. callbackList.push(MVC.shallowClone(eventList[key]));
  1073. }
  1074.  
  1075. return callbackList;
  1076. };
  1077.  
  1078. $.getCallbacks = function() {
  1079. var callbackList = {};
  1080.  
  1081. callbackList.after = this.getCallbacksFromList(this._afterTimeEvents);
  1082. callbackList.normal = this.getCallbacksFromList(this._timeEvents);
  1083.  
  1084. return callbackList;
  1085. };
  1086.  
  1087. $.setCallbacksToList = function(callbackList) {
  1088. var eventList = [];
  1089.  
  1090. for (var i = 0; i < callbackList.length; i++) {
  1091. var config = MVC.shallowClone(callbackList[i]);
  1092.  
  1093. eventList.push(config);
  1094. config.key = eventList.indexOf(config);
  1095. }
  1096.  
  1097. return eventList;
  1098. };
  1099.  
  1100. $.setCallbacks = function(callbackList) {
  1101. if (callbackList.after !== undefined) {
  1102. this._afterTimeEvents = this.setCallbacksToList(callbackList.after);
  1103. }
  1104.  
  1105. if (callbackList.normal !== undefined) {
  1106. this._timeEvents = this.setCallbacksToList(callbackList.normal);
  1107. }
  1108. };
  1109.  
  1110. $.checkRunInCommands = function(eventId, args) {
  1111. if (args.length < 6) return;
  1112.  
  1113. var value = parseInt(args[4], 10);
  1114.  
  1115. switch (args[5].toUpperCase()) {
  1116. case 'MINUTES' :
  1117. $.runInMinutes(eventId, value);
  1118. break;
  1119. case 'SECONDS' :
  1120. $.runInSeconds(eventId, value);
  1121. break;
  1122. case 'HOURS' :
  1123. $.runInHours(eventId, value);
  1124. break;
  1125. case 'DAYS' :
  1126. $.runInDays(eventId, value);
  1127. break;
  1128. case 'MONTHS' :
  1129. $.runInMonths(eventId, value);
  1130. break;
  1131. case 'YEARS' :
  1132. $.runInYears(eventId, value);
  1133. break;
  1134. default:
  1135. return;
  1136. }
  1137. };
  1138.  
  1139. $.checkRunOnCommands = function(eventId, args) {
  1140. // #ToDo: Plugin Commands for ON events (onDateTime)
  1141.  
  1142. };
  1143.  
  1144. $.checkRunEveryCommands = function(eventId, args) {
  1145. if (args.length < 5) return;
  1146.  
  1147. switch (args[4].toUpperCase()) {
  1148. case 'HOUR' :
  1149. $.on('changeHour', eventId);
  1150. break;
  1151. case 'MINUTE' :
  1152. $.on('changeMinute', eventId);
  1153. break;
  1154. case 'SECOND' :
  1155. $.on('changeSecond', eventId);
  1156. break;
  1157. case 'DAY' :
  1158. $.on('changeDay', eventId);
  1159. break;
  1160. case 'MONTH' :
  1161. $.on('changeMonth', eventId);
  1162. break;
  1163. case 'YEAR' :
  1164. $.on('changeYear', eventId);
  1165. break;
  1166. case 'PERIOD' :
  1167. $.on('changeDayPeriod', eventId);
  1168. break;
  1169. default :
  1170. break;
  1171. }
  1172. };
  1173.  
  1174. $.checkRunCommands = function(command, args) {
  1175. if (args.length < 4) return;
  1176.  
  1177. if (command.toUpperCase() != 'RUN') return;
  1178. if (args[0].toUpperCase() != 'COMMON') return;
  1179. if (args[1].toUpperCase() != 'EVENT') return;
  1180.  
  1181. var eventId = parseInt(args[2], 10);
  1182. if (eventId <= 0) return;
  1183.  
  1184. if (args[3].toUpperCase() == 'ON') {
  1185. this.checkRunOnCommands(eventId, args);
  1186. } else if (args[3].toUpperCase() == 'IN') {
  1187. this.checkRunInCommands(eventId, args);
  1188. } else if (args[3].toUpperCase() == 'EVERY') {
  1189. this.checkRunEveryCommands(eventId, args);
  1190. }
  1191. };
  1192.  
  1193. $.checkSystemCommands = function(command, args) {
  1194. if (command.toUpperCase() == 'RESTART' || command.toUpperCase() == 'REFRESH') {
  1195. if (args.length > 0 && args[0].toUpperCase() == 'TIME') {
  1196. $.refreshTimeSystem();
  1197. }
  1198. }
  1199. };
  1200.  
  1201. var oldDataManager_makeSaveContents = DataManager.makeSaveContents;
  1202. DataManager.makeSaveContents = function() {
  1203. var contents = oldDataManager_makeSaveContents.call(this);
  1204.  
  1205. contents.orangeDateTime = $.getDateTime();
  1206. contents.orangeTimeSystemCallbacks = $.getCallbacks();
  1207.  
  1208. return contents;
  1209. };
  1210.  
  1211. var oldDataManager_extractSaveContents = DataManager.extractSaveContents;
  1212. DataManager.extractSaveContents = function(contents) {
  1213. oldDataManager_extractSaveContents.call(this, contents);
  1214.  
  1215. if (contents.orangeDateTime !== undefined) {
  1216. $.setDateTime(contents.orangeDateTime);
  1217. }
  1218.  
  1219. if (contents.orangeTimeSystemCallbacks !== undefined) {
  1220. $.setCallbacks(contents.orangeTimeSystemCallbacks);
  1221. }
  1222. };
  1223.  
  1224. var oldDataManager_setupNewGame = DataManager.setupNewGame;
  1225. DataManager.setupNewGame = function() {
  1226. oldDataManager_setupNewGame.call(this);
  1227. $.setDateTime({
  1228. seconds : $.Param.initialSecond,
  1229. minute : $.Param.initialMinute,
  1230. hour : $.Param.initialHour,
  1231. day : $.Param.initialDay,
  1232. month : $.Param.initialMonth,
  1233. year : $.Param.initialYear
  1234. });
  1235. $.refreshTimeSystem();
  1236. };
  1237.  
  1238. var oldGameInterpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  1239. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  1240. oldGameInterpreter_pluginCommand.call(this, command, args);
  1241.  
  1242. $.checkRunCommands(command, args);
  1243. $.checkSystemCommands(command, args);
  1244. };
  1245.  
  1246. $.enableTime();
  1247. })(OrangeTimeSystem);
  1248.  
  1249. Imported.OrangeTimeSystem = 2.0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement