weapon.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Weapons
  29. */
  30. Wolf.Weapon = (function() {
  31. function fireHit(game, player) {
  32. var level = game.level,
  33. closest,
  34. dist,
  35. d1,
  36. n,
  37. shotDist,
  38. damage,
  39. guard;
  40. Wolf.Sound.startSound(null, null, 0, Wolf.CHAN_WEAPON, "lsfx/023.wav", 1, Wolf.ATTN_NORM, 0);
  41. // actually fire
  42. dist = 0x7fffffff;
  43. closest = null;
  44. for (n=0; n<level.state.numGuards; ++n) {
  45. guard = level.state.guards[n];
  46. if (guard.flags & Wolf.FL_SHOOTABLE) { // && Guards[n].flags&FL_VISABLE
  47. shotDist = Wolf.Math.point2LineDist(guard.x - player.position.x, guard.y - player.position.y, player.angle);
  48. if (shotDist > (2 * Wolf.TILEGLOBAL / 3)) {
  49. continue; // miss
  50. }
  51. d1 = Wolf.Math.lineLen2Point(guard.x - player.position.x, guard.y - player.position.y, player.angle);
  52. if (d1 < 0 || d1 > dist) {
  53. continue;
  54. }
  55. if (!Wolf.Level.checkLine(guard.x, guard.y, player.position.x, player.position.y, level)) {
  56. continue; // obscured
  57. }
  58. dist = d1;
  59. closest = guard;
  60. }
  61. }
  62. if (!closest || dist > Wolf.TILE2POS(1)) {
  63. return; // missed if further than 1.5 tiles
  64. }
  65. damage = Wolf.Random.rnd() >> 4;
  66. Wolf.ActorAI.damageActor(closest, game, player, damage); // hit something
  67. }
  68. function fireLead(game, player) {
  69. var level = game.level,
  70. closest,
  71. damage,
  72. dx, dy, dist,
  73. d1, shotDist, n,
  74. guard;
  75. switch (player.weapon) {
  76. case Wolf.WEAPON_PISTOL:
  77. Wolf.Sound.startSound(null, null, 0, Wolf.CHAN_WEAPON, "sfx/012.wav", 1, Wolf.ATTN_NORM, 0);
  78. break;
  79. case Wolf.WEAPON_AUTO:
  80. Wolf.Sound.startSound(null, null, 0, Wolf.CHAN_WEAPON, "sfx/011.wav", 1, Wolf.ATTN_NORM, 0);
  81. break;
  82. case Wolf.WEAPON_CHAIN:
  83. Wolf.Sound.startSound(null, null, 0, Wolf.CHAN_WEAPON, "sfx/013.wav", 1, Wolf.ATTN_NORM, 0);
  84. break;
  85. }
  86. player.madenoise = true;
  87. dist = 0x7fffffff;
  88. closest = null;
  89. for (n=0;n < level.state.numGuards; ++n) {
  90. guard = level.state.guards[n];
  91. if (guard.flags & Wolf.FL_SHOOTABLE ) { // && Guards[n].flags&FL_VISABLE
  92. shotDist = Wolf.Math.point2LineDist(guard.x - player.position.x, guard.y - player.position.y, player.angle);
  93. if (shotDist > (2 * Wolf.TILEGLOBAL / 3)) {
  94. continue; // miss
  95. }
  96. d1 = Wolf.Math.lineLen2Point(guard.x - player.position.x, guard.y - player.position.y, player.angle);
  97. if (d1 < 0 || d1 > dist) {
  98. continue;
  99. }
  100. if (!Wolf.Level.checkLine(guard.x, guard.y, player.position.x, player.position.y, level)) {
  101. continue; // obscured
  102. }
  103. dist = d1;
  104. closest = guard;
  105. }
  106. }
  107. if (!closest) { // missed
  108. var tracePoint = {
  109. angle : Wolf.Math.normalizeAngle(player.angle - Wolf.DEG2FINE(2) + (Math.random() * 0x10000) % (Wolf.DEG2FINE(4))),
  110. x : player.position.x,
  111. y : player.position.y,
  112. flags : Wolf.TRACE_BULLET
  113. }
  114. Wolf.Raycaster.trace(level, null, tracePoint);
  115. if (tracePoint.flags & Wolf.TRACE_HIT_DOOR) {
  116. Wolf.Sound.startSound(null, null, 0, Wolf.CHAN_AUTO, "lsfx/028.wav", 1, Wolf.ATTN_NORM, 0);
  117. }
  118. return;
  119. }
  120. // hit something
  121. dx = Math.abs(closest.tile.x - player.tile.x);
  122. dy = Math.abs(closest.tile.y - player.tile.y);
  123. dist = Math.max(dx, dy);
  124. if (dist < 2) {
  125. damage = Wolf.Random.rnd() / 4;
  126. } else if (dist < 4) {
  127. damage = Wolf.Random.rnd() / 6;
  128. } else {
  129. if (Wolf.Random.rnd() / 12 < dist) {
  130. return; // missed
  131. }
  132. damage = Wolf.Random.rnd() / 6;
  133. }
  134. Wolf.ActorAI.damageActor(closest, game, player, damage);
  135. }
  136. return {
  137. fireHit : fireHit,
  138. fireLead : fireLead
  139. };
  140. })();