areas.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * ===========================================================================
  3. *
  4. * Wolf3D Browser Version GPL Source Code
  5. * Copyright (C) 2012 id Software LLC, a ZeniMax Media company.
  6. *
  7. * This file is part of the Wolf3D Browser Version GPL Source Code ("Wolf3D Browser Source Code").
  8. *
  9. * Wolf3D Browser Source Code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Wolf3D Browser Source Code is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License version 2
  20. * along with Wolf3D Browser Source Code. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * If you have questions concerning this license, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  23. *
  24. * ===========================================================================
  25. */
  26. /**
  27. * @namespace
  28. * @description Area management
  29. */
  30. Wolf.Areas = (function() {
  31. /*
  32. Notes:
  33. Open doors connect two areas, so sounds will travel between them and sight
  34. will be checked when the player is in a connected area.
  35. Areaconnect is incremented/decremented by each door. If >0 they connect.
  36. Every time a door opens or closes the areabyplayer matrix gets recalculated.
  37. An area is true if it connects with the player's current spor.
  38. */
  39. /**
  40. * @description Initialize areas
  41. * @memberOf Wolf.Areas
  42. * @param {object} levelState The level state object
  43. * @param {number} areanumber Initial area
  44. */
  45. function init(level, areanumber) {
  46. level.state.areaconnect = [];
  47. level.state.areabyplayer = [];
  48. for (var i=0;i<Wolf.NUMAREAS;i++) {
  49. level.state.areaconnect[i] = [];
  50. for (var j=0;j<Wolf.NUMAREAS;j++) {
  51. level.state.areaconnect[i][j] = 0;
  52. }
  53. level.state.areabyplayer[i] = false;
  54. }
  55. level.state.areabyplayer[areanumber] = true;
  56. }
  57. /**
  58. * @private
  59. * @description Scans outward from playerarea, marking all connected areas.
  60. * @param {object} level The level object
  61. * @param {number} areanumber Area
  62. */
  63. function recursiveConnect(level, areanumber) {
  64. for (var i = 0;i < Wolf.NUMAREAS; ++i) {
  65. if (level.state.areaconnect[areanumber][i] && !level.state.areabyplayer[i]) {
  66. level.state.areabyplayer[i] = true;
  67. recursiveConnect(level, i);
  68. }
  69. }
  70. }
  71. /**
  72. * @description Connect area.
  73. * @memberOf Wolf.Areas
  74. * @param {object} level The level object
  75. * @param {number} areanumber New area
  76. */
  77. function connect(level, areanumber) {
  78. var i, c = 0;
  79. if (areanumber >= Wolf.NUMAREAS) {
  80. throw new Error("areanumber >= Wolf.NUMAREAS");
  81. }
  82. level.state.areabyplayer = [];
  83. level.state.areabyplayer[areanumber] = true;
  84. recursiveConnect(level, areanumber);
  85. for (i = 0; i < Wolf.NUMAREAS; i++) {
  86. if (level.state.areabyplayer[i]) {
  87. c++;
  88. }
  89. }
  90. }
  91. /**
  92. * @description Join ares
  93. * @memberOf Wolf.Areas
  94. * @param {object} level The level object
  95. * @param {number} area1 Area 1
  96. * @param {number} area2 Area 2
  97. */
  98. function join(level, area1, area2) {
  99. if (area1 < 0 || area1 >= Wolf.NUMAREAS) {
  100. throw new Error("area1 < 0 || area1 >= Wolf.NUMAREAS");
  101. }
  102. if (area2 < 0 || area2 >= Wolf.NUMAREAS) {
  103. throw new Error("area2 < 0 || area2 >= Wolf.NUMAREAS");
  104. }
  105. level.state.areaconnect[area1][area2]++;
  106. level.state.areaconnect[area2][area1]++;
  107. }
  108. /**
  109. * @description Disconnect ares
  110. * @memberOf Wolf.Areas
  111. * @param {object} level The level object
  112. * @param {number} area1 Area 1
  113. * @param {number} area2 Area 2
  114. */
  115. function disconnect(level, area1, area2) {
  116. if (area1 < 0 || area1 >= Wolf.NUMAREAS) {
  117. throw new Error("area1 < 0 || area1 >= Wolf.NUMAREAS");
  118. }
  119. if (area2 < 0 || area2 >= Wolf.NUMAREAS) {
  120. throw new Error("area2 < 0 || area2 >= Wolf.NUMAREAS");
  121. }
  122. level.state.areaconnect[area1][area2]--;
  123. level.state.areaconnect[area2][area1]--;
  124. }
  125. return {
  126. init : init,
  127. connect : connect,
  128. join : join,
  129. disconnect : disconnect
  130. };
  131. })();