pushwall.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Push wall management
  29. */
  30. Wolf.PushWall = (function() {
  31. var PWall = {};
  32. reset();
  33. function reset() {
  34. PWall.active = false;
  35. PWall.tilesMoved = 0;
  36. PWall.pointsMoved = 0;
  37. PWall.dir = 0;
  38. PWall.x = 0;
  39. PWall.y = 0;
  40. PWall.dx = 0;
  41. PWall.dy = 0;
  42. PWall.texX = 0;
  43. PWall.texY = 0;
  44. }
  45. function push(level, x, y, dir) {
  46. var dx, dy;
  47. if (PWall.active) {
  48. return false; // another PWall is moving [only one at a time!]
  49. }
  50. dx = Wolf.Math.dx4dir[dir];
  51. dy = Wolf.Math.dy4dir[dir];
  52. if (level.tileMap[x + dx][y + dy] & (Wolf.SOLID_TILE | Wolf.DOOR_TILE)) {
  53. // noway (smth is blocking)
  54. return true;
  55. }
  56. // remove secret flag & make everything needed when pushwall used!
  57. level.tileMap[x][y] &= (~Wolf.SECRET_TILE);
  58. level.tileMap[x][y] &= (~Wolf.WALL_TILE);
  59. level.tileMap[x][y] |= Wolf.PUSHWALL_TILE;
  60. if (++level.state.foundSecrets == level.state.totalSecrets) {
  61. Wolf.Game.notify("You found the last secret!");
  62. } else {
  63. Wolf.Game.notify("You found a secret!");
  64. }
  65. Wolf.Sound.startSound(null, null, 1, Wolf.CHAN_AUTO, "sfx/034.wav", 1, Wolf.ATTN_STATIC, 0);
  66. // good way to avoid stuckness; [un]comment one more down!
  67. // it makes a tile behind pushwall unpassable
  68. level.tileMap[x + dx][y + dy] |= Wolf.PUSHWALL_TILE;
  69. level.wallTexX[x + dx][y + dy] = level.wallTexX[x][y];
  70. level.wallTexY[x + dx][y + dy] = level.wallTexY[x][y];
  71. // write down PWall info
  72. PWall.active = true;
  73. PWall.tilesMoved = PWall.pointsMoved = 0;
  74. PWall.dir = dir;
  75. PWall.x = x;
  76. PWall.y = y;
  77. PWall.dx = dx;
  78. PWall.dy = dy;
  79. PWall.texX = level.wallTexX[x][y];
  80. PWall.texY = level.wallTexY[x][y];
  81. return true;
  82. }
  83. function process(level, tics) {
  84. if (!PWall.active) {
  85. return; // no active PWall to work with
  86. }
  87. PWall.pointsMoved += tics;
  88. if (PWall.pointsMoved < 128) {
  89. return;
  90. }
  91. PWall.pointsMoved -= 128;
  92. PWall.tilesMoved++;
  93. // Free tile
  94. level.tileMap[PWall.x][PWall.y] &= (~Wolf.PUSHWALL_TILE);
  95. // Occupy new tile
  96. PWall.x += PWall.dx;
  97. PWall.y += PWall.dy;
  98. // Shall we move further?
  99. if (level.tileMap[PWall.x + PWall.dx][PWall.y + PWall.dy] & (Wolf.SOLID_TILE | Wolf.DOOR_TILE | Wolf.ACTOR_TILE | Wolf.POWERUP_TILE) || PWall.tilesMoved == 3) {
  100. level.tileMap[PWall.x][PWall.y] &= (~Wolf.PUSHWALL_TILE); // wall now
  101. level.tileMap[PWall.x][PWall.y] |= Wolf.WALL_TILE; // wall now
  102. level.wallTexX[PWall.x][PWall.y] = PWall.texX;
  103. level.wallTexY[PWall.x][PWall.y] = PWall.texY;
  104. PWall.active = false; // Free Push Wall
  105. } else {
  106. level.tileMap[PWall.x + PWall.dx][PWall.y + PWall.dy] |= Wolf.PUSHWALL_TILE;
  107. // Not sure if this is right but it fixed an issue with the pushwall texture changing mid-slide.
  108. level.wallTexX[PWall.x + PWall.dx][PWall.y + PWall.dy] = PWall.texX;
  109. level.wallTexY[PWall.x + PWall.dx][PWall.y + PWall.dy] = PWall.texY;
  110. }
  111. }
  112. function get() {
  113. return PWall;
  114. }
  115. return {
  116. reset : reset,
  117. process : process,
  118. push : push,
  119. get : get
  120. };
  121. })();