game.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 Game management
  29. */
  30. Wolf.Game = (function() {
  31. Wolf.setConsts({
  32. BUTTON_ATTACK : 1,
  33. BUTTON_USE : 2,
  34. BUTTON_ANY : 128, // any key whatsoever
  35. BASEMOVE : 35,
  36. RUNMOVE : 70,
  37. MOVESCALE : 150,
  38. BACKMOVESCALE : 100,
  39. MAXMOUSETURN : 10,
  40. TURNANGLESCALE : 300,
  41. MOUSEDEADBAND : 0.2,
  42. gd_baby : 0,
  43. gd_easy : 1,
  44. gd_medium : 2,
  45. gd_hard : 3
  46. });
  47. var rendering = false,
  48. playing = false,
  49. fsInit = false,
  50. hndRender = 0,
  51. hndCycle = 0,
  52. hndFps = 0,
  53. lastFPSTime = 0,
  54. lastFrame = 0,
  55. frameNum = 0,
  56. cycleNum = 0,
  57. mouseEnabled = false,
  58. paused = false,
  59. intermissionAnim = 0,
  60. currentGame = null,
  61. levelMusic,
  62. processAI = true,
  63. keyInputActive = false,
  64. preloadTextures = {},
  65. preloadSprites = {},
  66. controls = {
  67. up : ["UP"],
  68. left : ["LEFT"],
  69. down : ["DOWN"],
  70. right : ["RIGHT"],
  71. run : ["SHIFT"],
  72. attack : ["X"],
  73. use : ["SPACE"],
  74. strafe : ["Z"],
  75. weapon1 : ["1"],
  76. weapon2 : ["2"],
  77. weapon3 : ["3"],
  78. weapon4 : ["4"]
  79. },
  80. ticsPerSecond = 70,
  81. lastTimeCount = 0;
  82. /**
  83. * @description Build the movement, angles, and buttons for a frame of action:
  84. * Player.angle
  85. * Player.cmd.buttons
  86. * Player.cmd.forwardMove
  87. * Player.cmd.sideMove
  88. * @private
  89. * @param {object} player The player object.
  90. * @param {number} tics The number of tics since last frame.
  91. */
  92. function updatePlayerControls(player, tics) {
  93. var moveValue,
  94. running = false,
  95. strafing = false,
  96. leftKey = false,
  97. rightKey = false,
  98. downKey = false,
  99. upKey = false,
  100. changeWeapon = -1,
  101. mouseMovement,
  102. mouseCoords;
  103. player.cmd.buttons = 0;
  104. player.cmd.forwardMove = 0;
  105. player.cmd.sideMove = 0;
  106. leftKey = Wolf.Input.checkKeys(controls.left);
  107. rightKey = Wolf.Input.checkKeys(controls.right);
  108. downKey = Wolf.Input.checkKeys(controls.down);
  109. upKey = Wolf.Input.checkKeys(controls.up);
  110. running = Wolf.Input.checkKeys(controls.run);
  111. strafing = Wolf.Input.checkKeys(controls.strafe);
  112. moveValue = (running ? Wolf.RUNMOVE : Wolf.BASEMOVE);
  113. if (Wolf.Input.checkKeys(controls.attack) || (mouseEnabled && Wolf.Input.leftMouseDown())) {
  114. player.cmd.buttons |= Wolf.BUTTON_ATTACK;
  115. }
  116. if (mouseEnabled && Wolf.Input.rightMouseDown()) {
  117. if (mouseCoords = Wolf.Input.getMouseCoords()) {
  118. player.cmd.forwardMove += - (mouseCoords.y < 0 ? Wolf.MOVESCALE : Wolf.BACKMOVESCALE) * moveValue * mouseCoords.y;
  119. }
  120. } else if (!(upKey && downKey)) {
  121. if (upKey) {
  122. player.cmd.forwardMove += moveValue * Wolf.MOVESCALE;
  123. }
  124. if (downKey) {
  125. player.cmd.forwardMove += -moveValue * Wolf.BACKMOVESCALE;
  126. }
  127. }
  128. if (mouseEnabled && Wolf.Input.isPointerLocked()) {
  129. mouseMovement = Wolf.Input.getMouseMovement();
  130. player.angle -= (mouseMovement.x * Wolf.TURNANGLESCALE * tics)>>0;
  131. } else {
  132. if (leftKey) {
  133. if (strafing) {
  134. player.cmd.sideMove += -moveValue * Wolf.MOVESCALE;
  135. } else {
  136. player.angle += Wolf.TURNANGLESCALE * tics;
  137. }
  138. }
  139. if (rightKey) {
  140. if (strafing) {
  141. player.cmd.sideMove += moveValue * Wolf.MOVESCALE;
  142. } else {
  143. player.angle -= Wolf.TURNANGLESCALE * tics;
  144. }
  145. }
  146. if (mouseEnabled && (mouseCoords = Wolf.Input.getMouseCoords())) {
  147. if (Math.abs(mouseCoords.x) > Wolf.MOUSEDEADBAND) {
  148. player.angle -= (Wolf.TURNANGLESCALE * tics * (mouseCoords.x + (mouseCoords.x < 0 ? 1 : -1) * Wolf.MOUSEDEADBAND))>>0;
  149. }
  150. }
  151. }
  152. // change weapon?
  153. if (Wolf.Input.checkKeys(controls.weapon1) && player.items & Wolf.ITEM_WEAPON_1) {
  154. changeWeapon = Wolf.WEAPON_KNIFE;
  155. } else if (Wolf.Input.checkKeys(controls.weapon2) && player.items & Wolf.ITEM_WEAPON_2 && player.ammo[Wolf.AMMO_BULLETS]) {
  156. changeWeapon = Wolf.WEAPON_PISTOL;
  157. } else if (Wolf.Input.checkKeys(controls.weapon3) && player.items & Wolf.ITEM_WEAPON_3 && player.ammo[Wolf.AMMO_BULLETS]) {
  158. changeWeapon = Wolf.WEAPON_AUTO;
  159. } else if (Wolf.Input.checkKeys(controls.weapon4) && player.items & Wolf.ITEM_WEAPON_4 && player.ammo[Wolf.AMMO_BULLETS]) {
  160. changeWeapon = Wolf.WEAPON_CHAIN;
  161. }
  162. if (changeWeapon > -1) {
  163. player.previousWeapon = Wolf.WEAPON_KNIFE;
  164. player.weapon = player.pendingWeapon = changeWeapon;
  165. }
  166. if (Wolf.Input.checkKeys(controls.use)) {
  167. player.cmd.buttons |= Wolf.BUTTON_USE;
  168. }
  169. }
  170. /**
  171. * @description Initiate the game cycle to process player and world logic
  172. * @private
  173. * @param {object} game The game object
  174. */
  175. function startGameCycle(game) {
  176. var deathTics = 0,
  177. deathTicsMax = ticsPerSecond * 2;
  178. // cancel existing game cycle
  179. if (hndCycle) {
  180. clearTimeout(hndCycle);
  181. hndCycle = 0;
  182. }
  183. function nextCycle() {
  184. if (!playing) {
  185. return;
  186. }
  187. hndCycle = setTimeout(nextCycle, 1000 / 30);
  188. cycleNum++;
  189. if (paused) {
  190. return;
  191. }
  192. var player = game.player,
  193. level = game.level,
  194. lives, score,
  195. tics = calcTics();
  196. if (player.playstate != Wolf.ex_dead) {
  197. updatePlayerControls(player, tics);
  198. player.angle = Wolf.Math.normalizeAngle(player.angle);
  199. Wolf.Player.process(game, player, tics);
  200. if (processAI) {
  201. Wolf.Actors.process(game, tics);
  202. }
  203. Wolf.PushWall.process(level, tics);
  204. Wolf.Doors.process(level, player, tics);
  205. } else {
  206. if (died(game, tics)) {
  207. deathTics += tics;
  208. if (deathTics >= deathTicsMax) {
  209. deathTics = 0;
  210. $("#game .renderer .death").css("display", "none");
  211. if (game.player.lives > 0) {
  212. lives = game.player.lives;
  213. score = game.player.startScore;
  214. game.level = Wolf.Level.reload(level);
  215. Wolf.Level.scanInfoPlane(game.level, game.skill); // Spawn items/guards
  216. game.player = Wolf.Player.spawn(game.level.spawn, game.level, game.skill);
  217. game.player.lives = lives - 1;
  218. game.player.score = score;
  219. game.player.startScore = score;
  220. game.level.state.startTime = (new Date).getTime();
  221. game.level.state.elapsedTime = 0;
  222. } else {
  223. gameOver(game);
  224. return;
  225. }
  226. } else {
  227. $("#game .renderer .death").css({
  228. display : "block",
  229. backgroundColor : "rgba(255,0,0," + (deathTics / deathTicsMax) + ")"
  230. });
  231. }
  232. }
  233. }
  234. Wolf.Sprites.clean(level);
  235. updateHUD(game, tics);
  236. }
  237. lastTimeCount = (new Date).getTime();
  238. nextCycle();
  239. }
  240. function died(game, tics) {
  241. var fangle,
  242. dx, dy,
  243. iangle, curangle,
  244. clockwise,
  245. counter,
  246. change,
  247. player = game.player,
  248. killer = player.lastAttacker;
  249. //gamestate.weapon = -1; // take away weapon
  250. //SD_PlaySound (PLAYERDEATHSND);
  251. // swing around to face attacker
  252. dx = killer.x - player.position.x;
  253. dy = player.position.y - killer.y;
  254. fangle = -Math.atan2(dy,dx); // returns -pi to pi
  255. if (fangle < 0) {
  256. fangle = Math.PI * 2 + fangle;
  257. }
  258. iangle = Math.round(fangle / (Math.PI * 2) * Wolf.ANGLES);
  259. curangle = Wolf.FINE2DEG(player.angle);
  260. if (curangle > iangle) {
  261. counter = curangle - iangle;
  262. clockwise = Wolf.ANGLES - curangle + iangle;
  263. } else {
  264. clockwise = iangle - curangle;
  265. counter = curangle + Wolf.ANGLES - iangle;
  266. }
  267. if (clockwise < counter) {
  268. // rotate clockwise
  269. if (curangle > iangle) {
  270. curangle -= Wolf.ANGLES;
  271. }
  272. if (curangle == iangle) {
  273. return true;
  274. } else {
  275. change = tics * Wolf.DEATHROTATE;
  276. if (curangle + change > iangle) {
  277. change = iangle - curangle;
  278. }
  279. curangle += change;
  280. if (curangle >= Wolf.ANGLES) {
  281. curangle -= Wolf.ANGLES;
  282. }
  283. player.angle = Wolf.DEG2FINE(curangle);
  284. }
  285. } else {
  286. // rotate counterclockwise
  287. if (curangle < iangle) {
  288. curangle += Wolf.ANGLES;
  289. }
  290. if (curangle == iangle) {
  291. return true;
  292. } else {
  293. change = -tics * Wolf.DEATHROTATE;
  294. if (curangle + change < iangle) {
  295. change = iangle - curangle;
  296. }
  297. curangle += change;
  298. if (curangle < 0) {
  299. curangle += Wolf.ANGLES;
  300. }
  301. player.angle = Wolf.DEG2FINE(curangle);
  302. }
  303. }
  304. return false;
  305. }
  306. /**
  307. * @description Game over. No more lives.
  308. * @private
  309. * @param {object} game The game object
  310. */
  311. function gameOver(game) {
  312. playing = false;
  313. rendering = false;
  314. $("#game .renderer").hide();
  315. $("#game .fps").hide();
  316. $("#game .gameover").show();
  317. endGame();
  318. function exit() {
  319. $(document).off("keydown", progress);
  320. $("#game").fadeOut(null, function() {
  321. $("#game .gameover").hide();
  322. Wolf.Menu.show();
  323. });
  324. }
  325. function progress(e) {
  326. if (!$("#game .gameover").is(":visible")) {
  327. exit();
  328. return;
  329. }
  330. if (e.keyCode == 13 || e.keyCode == 32) {
  331. exit();
  332. }
  333. }
  334. $(document).on("keydown", progress);
  335. }
  336. function victory(game) {
  337. if (game.player.playstate == Wolf.ex_victory) {
  338. return;
  339. }
  340. keyInputActive = false;
  341. Wolf.log("Victory!");
  342. $("#game .renderer .player-weapon").hide();
  343. Wolf.Actors.spawnBJVictory(game.player, game.level, game.skill);
  344. game.player.playstate = Wolf.ex_victory;
  345. }
  346. function endEpisode(game) {
  347. Wolf.Game.startIntermission(game);
  348. }
  349. /**
  350. * @description Calculate the number of tics since last time calcTics() was called.
  351. * Accumulates fractions.
  352. * @private
  353. * @returns {number} The number of tics
  354. */
  355. function calcTics() {
  356. var now = (new Date).getTime(),
  357. delta = (now - lastTimeCount) / 1000,
  358. tics = Math.floor(ticsPerSecond * delta);
  359. lastTimeCount += (tics * 1000 / ticsPerSecond) >> 0;
  360. return tics;
  361. }
  362. /**
  363. * @description Update HUD stats
  364. * @private
  365. * @param {string} name The name/class of the player stat (health, ammo, etc.)
  366. * @param {number} value The new value
  367. */
  368. function updateStat(name, value) {
  369. var numdivs = $("#game .hud ." + name + " .number");
  370. for (var i=numdivs.length-1;i>=0;i--) {
  371. if (value == 0 && i < numdivs.length-1) {
  372. numdivs[i].style.backgroundPosition = 16 + "px 0";
  373. } else {
  374. numdivs[i].style.backgroundPosition = - (16 * (value % 10)) + "px 0";
  375. value = (value / 10) >> 0;
  376. }
  377. }
  378. }
  379. /**
  380. * @description Update the HUD
  381. * @private
  382. * @param {object} game The game object
  383. */
  384. function updateHUD(game, tics) {
  385. var player = game.player,
  386. frame = player.weapon * 4 + player.weaponFrame;
  387. if (player.playstate == Wolf.ex_dead || player.playstate == Wolf.ex_victory) {
  388. $("#game .renderer .player-weapon").css("display", "none");
  389. } else {
  390. $("#game .renderer .player-weapon").css({
  391. display : "block",
  392. backgroundPosition : - (frame * Wolf.HUD_WEAPON_WIDTH) + "px 0"
  393. });
  394. }
  395. $("#game .hud .weapon").css({
  396. backgroundPosition : - (player.weapon * 96) + "px 0"
  397. });
  398. $("#game .hud .key1").css({
  399. display : (player.items & Wolf.ITEM_KEY_1) ? "block" : "none"
  400. });
  401. $("#game .hud .key2").css({
  402. display : (player.items & Wolf.ITEM_KEY_2) ? "block" : "none"
  403. });
  404. updateStat("ammo", player.ammo[Wolf.AMMO_BULLETS]);
  405. updateStat("health", player.health);
  406. updateStat("lives", player.lives);
  407. updateStat("score", player.score);
  408. updateStat("floor", game.levelNum+1);
  409. drawFace(player, tics);
  410. }
  411. /**
  412. * @description Update the game display
  413. * @private
  414. * @param {object} game The game object
  415. */
  416. function updateScreen(game) {
  417. var player = game.player,
  418. level = game.level,
  419. viewport = {
  420. x : player.position.x,
  421. y : player.position.y,
  422. angle : player.angle
  423. };
  424. var res = Wolf.Raycaster.traceRays(viewport, level);
  425. Wolf.Renderer.clear();
  426. Wolf.Renderer.draw(viewport, level, res.tracers, res.visibleTiles);
  427. }
  428. /**
  429. * @description Update BJ face pic
  430. * @private
  431. * @param {object} player
  432. * @param {number} tics
  433. */
  434. function drawFace(player, tics) {
  435. var pic;
  436. // decide on the face
  437. player.faceCount += tics;
  438. if (player.faceGotGun && player.faceCount > 0) {
  439. // gotgun will set facecount to a negative number initially, go back
  440. // to normal face with random look after expired.
  441. player.faceGotGun = false;
  442. }
  443. if (player.faceCount > Wolf.Random.rnd()) {
  444. player.faceGotGun = player.faceOuch = false;
  445. player.faceFrame = Wolf.Random.rnd() >> 6;
  446. if( player.faceFrame == 3) {
  447. player.faceFrame = 0;
  448. }
  449. player.faceCount = 0;
  450. }
  451. if (player.health) {
  452. if (player.faceGotGun) {
  453. pic = 22;
  454. } else {
  455. var h = player.health;
  456. if (h > 100) {
  457. h = 100;
  458. }
  459. if (h < 0) {
  460. h = 0;
  461. }
  462. pic = (3*((100-h)/16)>>0) + player.faceFrame;
  463. //gsh
  464. if ((player.flags & Wolf.FL_GODMODE)) {
  465. pic = 23 + player.faceFrame;
  466. }
  467. }
  468. } else {
  469. pic = 21;
  470. }
  471. $("#game .hud .bj").css({
  472. backgroundPosition : - (pic * Wolf.HUD_FACE_WIDTH) + "px 0"
  473. });
  474. }
  475. /**
  476. * @description Update the FPS counter
  477. * @private
  478. */
  479. function updateFPS() {
  480. var now = (new Date).getTime(),
  481. dt = (now - lastFPSTime) / 1000,
  482. frames = frameNum - lastFrame;
  483. lastFPSTime = now;
  484. lastFrame = frameNum;
  485. $("#game .fps").html((frames / dt).toFixed(2));
  486. }
  487. /**
  488. * @description Initiate the rendering cycle
  489. * @private
  490. * @param {object} game The game object
  491. */
  492. function startRenderCycle(game) {
  493. // cancel existing render cycle
  494. if (hndRender) {
  495. cancelAnimationFrame(hndRender);
  496. hndRender = 0;
  497. }
  498. /*
  499. if (!hndFps) {
  500. hndFps = setInterval(updateFPS, 1000);
  501. }
  502. $("#game .fps").show();
  503. */
  504. Wolf.Renderer.init();
  505. $("#game .renderer").show();
  506. function nextFrame() {
  507. if (!rendering) {
  508. return;
  509. }
  510. if (!paused) {
  511. updateScreen(game);
  512. }
  513. hndRender = requestAnimationFrame(nextFrame);
  514. frameNum++;
  515. }
  516. rendering = true;
  517. nextFrame();
  518. }
  519. /**
  520. * @description Start playing the specified level of the specified episode.
  521. * @memberOf Wolf.Game
  522. * @param {object} game The game object.
  523. * @param {number} episodeNum The episode number.
  524. * @param {number} levelNum The level number.
  525. */
  526. function startLevel(game, episodeNum, levelNum) {
  527. if (!Wolf.Episodes[episodeNum].enabled) {
  528. return;
  529. }
  530. playing = false;
  531. rendering = false;
  532. game.episodeNum = episodeNum;
  533. game.levelNum = levelNum;
  534. var episode = Wolf.Episodes[game.episodeNum];
  535. Wolf.Level.load(episode.levels[game.levelNum].file, function(error, level) {
  536. if (error) {
  537. throw error;
  538. }
  539. $("#game .renderer .floor").css({
  540. "background-color" : "rgb("
  541. + level.floor[0] + ","
  542. + level.floor[1] + ","
  543. + level.floor[2] + ")"
  544. });
  545. $("#game .renderer .ceiling").css({
  546. "background-color" : "rgb("
  547. + level.ceiling[0] + ","
  548. + level.ceiling[1] + ","
  549. + level.ceiling[2] + ")"
  550. });
  551. game.level = level;
  552. levelMusic = level.music;
  553. Wolf.Level.scanInfoPlane(level, game.skill); // Spawn items/guards
  554. /*
  555. game.player.position.x = 1944862;
  556. game.player.position.y = 2156427;
  557. game.player.angle = 8507;
  558. */
  559. $("#game .loading").show();
  560. preloadLevelAssets(level, function() {
  561. Wolf.Sound.startMusic(level.music);
  562. game.player = Wolf.Player.spawn(level.spawn, level, game.skill, game.player);
  563. game.player.startScore = game.player.score;
  564. level.state.startTime = (new Date).getTime();
  565. level.state.elapsedTime = 0;
  566. playing = true;
  567. startGameCycle(game);
  568. startRenderCycle(game);
  569. Wolf.Input.reset();
  570. Wolf.Input.lockPointer();
  571. $("#game .loading").hide();
  572. $("#game").focus();
  573. $("#game .renderer .player-weapon").show();
  574. keyInputActive = true;
  575. });
  576. });
  577. }
  578. /**
  579. * @description Preload the music and textures for the specified level
  580. * @private
  581. * @param {object} level The level object
  582. * @param {function} callback Called when all files have loaded.
  583. */
  584. function preloadLevelAssets(level, callback) {
  585. var files = [],
  586. tx, ty, texture, x, y, f, i, numFiles,
  587. texturePath = "art/walls-shaded/" + Wolf.TEXTURERESOLUTION + "/",
  588. spritePath = "art/sprites/" + Wolf.TEXTURERESOLUTION + "/";
  589. function addTexture(texture) {
  590. if (texture > 0) {
  591. if (texture % 2 == 0) {
  592. texture--;
  593. }
  594. f = texturePath + "w_" + texture + ".png";
  595. if (!preloadTextures[f]) {
  596. files.push(f);
  597. preloadTextures[f] = true;
  598. }
  599. }
  600. }
  601. for (x=0;x<64;++x) {
  602. for (y=0;y<64;++y) {
  603. addTexture(level.wallTexX[x][y]);
  604. addTexture(level.wallTexY[x][y]);
  605. }
  606. }
  607. // static sprites
  608. f = spritePath + "002_053.png";
  609. if (!preloadSprites[f]) {
  610. files.push(f);
  611. preloadSprites[f] = true
  612. }
  613. /*
  614. for (i=0;i<level.state.guards.length;++i) {
  615. texture = level.state.guards[i].sprite;
  616. if (texture) {
  617. f = spritePath + Wolf.Sprites.getTexture(texture).sheet;
  618. if (!preloadSprites[f]) {
  619. files.push(f);
  620. preloadSprites[f] = true;
  621. }
  622. }
  623. }
  624. */
  625. for (i=0;i<files.length;++i) {
  626. files[i] = "preload!timeout=5!" + files[i];
  627. }
  628. if (files.length) {
  629. Modernizr.load({
  630. load : files,
  631. complete : callback
  632. });
  633. } else {
  634. callback();
  635. }
  636. }
  637. /**
  638. * @description Start a new game with the specified skill level.
  639. * @memberOf Wolf.Game
  640. * @param {number} skill The difficulty level.
  641. */
  642. function startGame(skill) {
  643. if (isPlaying()) {
  644. endGame();
  645. levelMusic = null;
  646. Wolf.Sound.stopAllSounds();
  647. }
  648. $("#game .renderer .death").hide();
  649. $("#game .renderer .damage-flash").hide();
  650. $("#game .renderer .bonus-flash").hide();
  651. $("#game").show();
  652. var game = {
  653. episode : -1,
  654. level : -1,
  655. skill : skill,
  656. killRatios : [],
  657. secretRatios : [],
  658. treasureRatios : [],
  659. totalTime : 0
  660. };
  661. currentGame = game; // for debugging only
  662. return game;
  663. }
  664. function endGame() {
  665. // cancel game cycle
  666. if (hndCycle) {
  667. clearTimeout(hndCycle);
  668. hndCycle = 0;
  669. }
  670. // cancel render cycle
  671. if (hndRender) {
  672. cancelAnimationFrame(hndRender);
  673. hndRender = 0;
  674. }
  675. playing = false;
  676. rendering = false;
  677. Wolf.Renderer.reset();
  678. if (paused) {
  679. togglePause();
  680. }
  681. }
  682. function startVictoryText(game) {
  683. endGame();
  684. $("#game").fadeOut(null, function() {
  685. var name = "victory" + (game.episodeNum+1),
  686. num = (game.episodeNum == 2) ? 1 : 2;
  687. Wolf.Menu.showText(name, num, function() {
  688. Wolf.Menu.show("main");
  689. });
  690. });
  691. }
  692. /**
  693. * @description Start the post-level intermission.
  694. * @memberOf Wolf.Game
  695. * @param {object} game The game object.
  696. */
  697. function startIntermission(game, delay) {
  698. var episode = Wolf.Episodes[game.episodeNum],
  699. parTime = episode.levels[game.levelNum].partime * 60,
  700. bonus = 0,
  701. parBonusAmount = 500,
  702. ratioBonusAmount = 10000,
  703. levelState = game.level.state,
  704. killRatio = levelState.totalMonsters ? ((levelState.killedMonsters / levelState.totalMonsters * 100) >> 0) : 0,
  705. secretRatio = levelState.totalSecrets ? ((levelState.foundSecrets / levelState.totalSecrets * 100) >> 0) : 0,
  706. treasureRatio = levelState.totalTreasure ? ((levelState.foundTreasure / levelState.totalTreasure * 100) >> 0) : 0,
  707. time = levelState.elapsedTime + ((new Date).getTime() - levelState.startTime),
  708. totalTime, i,
  709. avgKill = 0, avgSecret = 0, avgTreasure = 0;
  710. playing = false;
  711. Wolf.Sound.startMusic("music/URAHERO.ogg");
  712. $("#game .renderer").hide();
  713. $("#game .fps").hide();
  714. $("#game .intermission .digit").hide();
  715. $("#game .intermission").show();
  716. $("#game .intermission .background").hide();
  717. $("#game .intermission .background-secret").hide();
  718. $("#game .intermission .background-victory").hide();
  719. $("#game .intermission .stat").hide();
  720. $("#game .intermission .victory-stat").hide();
  721. $("#game .intermission .bj").hide();
  722. // 99 mins max
  723. time = Math.min(99*60, Math.round(time / 1000));
  724. killRatio = Math.min(killRatio, 100);
  725. secretRatio = Math.min(secretRatio, 100);
  726. treasureRatio = Math.min(treasureRatio, 100);
  727. game.killRatios.push(killRatio);
  728. game.secretRatios.push(secretRatio);
  729. game.treasureRatios.push(treasureRatio);
  730. game.totalTime += time;
  731. // secret level
  732. if (game.levelNum == 9) {
  733. $("#game .intermission .background-secret").show();
  734. $("#game .intermission .bj").show();
  735. bonus = 15000;
  736. // boss level
  737. } else if (game.levelNum == 8) {
  738. $("#game .intermission .background-victory").show();
  739. $("#game .intermission .victory-stat").show();
  740. totalTime = Math.min(99*60, game.totalTime);
  741. for (i=0;i<game.killRatios.length;i++) {
  742. avgKill += game.killRatios[i];
  743. }
  744. for (i=0;i<game.secretRatios.length;i++) {
  745. avgSecret += game.secretRatios[i];
  746. }
  747. for (i=0;i<game.treasureRatios.length;i++) {
  748. avgTreasure += game.treasureRatios[i];
  749. }
  750. avgKill = Math.round(avgKill / game.killRatios.length);
  751. avgSecret = Math.round(avgSecret / game.secretRatios.length);
  752. avgTreasure = Math.round(avgTreasure / game.treasureRatios.length);
  753. setIntermissionNumber("total-time-minutes", (totalTime / 60) >> 0, true);
  754. setIntermissionNumber("total-time-seconds", ((totalTime / 60) % 1) * 60, true);
  755. setIntermissionNumber("avg-kill-ratio", avgKill, false);
  756. setIntermissionNumber("avg-secret-ratio", avgSecret, false);
  757. setIntermissionNumber("avg-treasure-ratio", avgTreasure, false);
  758. // regular level
  759. } else {
  760. $("#game .intermission .background").show();
  761. $("#game .intermission .bj").show();
  762. $("#game .intermission .stat").show();
  763. if (parTime && parTime > time) {
  764. bonus += (parTime - time) * parBonusAmount;
  765. }
  766. if (killRatio == 100) {
  767. bonus += ratioBonusAmount;
  768. }
  769. if (secretRatio == 100) {
  770. bonus += ratioBonusAmount;
  771. }
  772. if (treasureRatio == 100) {
  773. bonus += ratioBonusAmount;
  774. }
  775. time = time / 60;
  776. parTime = parTime / 60;
  777. setIntermissionNumber("floor", game.levelNum + 1, false);
  778. setIntermissionNumber("bonus", bonus, false);
  779. setIntermissionNumber("time-minutes", time >> 0, true);
  780. setIntermissionNumber("time-seconds", (time % 1) * 60, true);
  781. setIntermissionNumber("par-minutes", parTime >> 0, true);
  782. setIntermissionNumber("par-seconds", (parTime % 1) * 60, true);
  783. setIntermissionNumber("kill-ratio", killRatio, false);
  784. setIntermissionNumber("secret-ratio", secretRatio, false);
  785. setIntermissionNumber("treasure-ratio", treasureRatio, false);
  786. }
  787. function anim() {
  788. var now = (new Date).getTime(),
  789. bjFrame = Math.floor(now / 500) % 2;
  790. $("#game .intermission .bj").css({
  791. backgroundPosition : - (162 * bjFrame) + "px 0px"
  792. });
  793. intermissionAnim = requestAnimationFrame(anim);
  794. }
  795. if (game.levelNum != 8) {
  796. if (!intermissionAnim) {
  797. anim();
  798. }
  799. }
  800. function exitIntermission() {
  801. if (intermissionAnim) {
  802. cancelAnimationFrame(intermissionAnim);
  803. intermissionAnim = 0;
  804. }
  805. $(document).off("keydown", progress);
  806. $("#game .intermission").hide();
  807. }
  808. function progress(e) {
  809. var nextLevel;
  810. if (!$("#game .intermission").is(":visible")) {
  811. exitIntermission();
  812. return;
  813. }
  814. if (e.keyCode == 13 || e.keyCode == 32) {
  815. exitIntermission();
  816. if (game.player.playstate == Wolf.ex_secretlevel) {
  817. nextLevel = 9;
  818. } else {
  819. if (game.levelNum == 8) { // Level was boss level - end of episode.
  820. $("#game").fadeOut(1000, function() {
  821. startVictoryText(game);
  822. });
  823. return;
  824. } else if (game.levelNum == 9) { // Level was secret level - go to previous level + 1
  825. // yuck
  826. switch (game.episodeNum) {
  827. case 0: nextLevel = 1; break;
  828. case 1: nextLevel = 1; break;
  829. case 2: nextLevel = 7; break;
  830. case 3: nextLevel = 3; break;
  831. case 4: nextLevel = 4; break;
  832. case 5: nextLevel = 3; break;
  833. default: nextLevel = game.levelNum + 1; break;
  834. }
  835. } else {
  836. nextLevel = game.levelNum + 1;
  837. }
  838. }
  839. Wolf.Player.givePoints(game.player, bonus);
  840. startLevel(game, game.episodeNum, nextLevel);
  841. }
  842. }
  843. $(document).on("keydown", progress);
  844. }
  845. /**
  846. * @description Update an intermission screen stat.
  847. * @private
  848. * @param {object} name The name (and CSS class) of the stat
  849. * @param {number} value The value.
  850. * @param {boolean} zeros If true, leading zeros are displayed.
  851. */
  852. function setIntermissionNumber(name, value, zeros) {
  853. var digits = $("#game .intermission ." + name + " .digit"),
  854. i, digit, v;
  855. for (i=0;i<10;i++) {
  856. digits.removeClass("num-" + i);
  857. }
  858. value = value >> 0;
  859. v = value;
  860. for (i=0;i<digits.length;i++) {
  861. digit = v % 10;
  862. if (v > 0 || zeros || (value == 0 && i == 0)) {
  863. digits.eq(digits.length - 1 - i).addClass("num-" + digit);
  864. }
  865. v = (v / 10) >> 0;
  866. }
  867. digits.show();
  868. }
  869. /**
  870. * @description Start red damage flash.
  871. * @memberOf Wolf.Game
  872. */
  873. function startDamageFlash() {
  874. $("#game .renderer .damage-flash").show().fadeOut(300);
  875. }
  876. /**
  877. * @description Start bonus flash.
  878. * @memberOf Wolf.Game
  879. */
  880. function startBonusFlash() {
  881. $("#game .renderer .bonus-flash").show().fadeOut(300);
  882. }
  883. /**
  884. * @description Show a notification.
  885. * @memberOf Wolf.Game
  886. * @param {string} text The notification
  887. */
  888. function notify(text) {
  889. Wolf.log(text);
  890. }
  891. /**
  892. * @description Query fullscreen.
  893. * @memberOf Wolf.Game
  894. * @returns boolean True if browser is in fullscreen mode, otherwise false.
  895. */
  896. function isFullscreen() {
  897. if ("webkitIsFullScreen" in document) {
  898. return document.webkitIsFullScreen;
  899. } else if ("mozFullScreen" in document) {
  900. return document.mozFullScreen;
  901. } else if ($("#main").data("scale") > 1) {
  902. return true;
  903. }
  904. return false;
  905. }
  906. /**
  907. * @description Fullscreen event handler.
  908. * @private
  909. * @param {object} e Event object.
  910. */
  911. function fullscreenChange(e) {
  912. if (isFullscreen()) {
  913. enterFullscreen();
  914. } else {
  915. exitFullscreen();
  916. }
  917. }
  918. /**
  919. * @description Toggle the fullscreen state
  920. * @private
  921. */
  922. function toggleFullscreen() {
  923. var main = $("#main")[0],
  924. ret = false;
  925. if (isFullscreen()) {
  926. if (document.exitFullscreen) {
  927. document.exitFullscreen();
  928. return true;
  929. } else if (document.webkitCancelFullscreen) {
  930. document.webkitCancelFullscreen();
  931. return true;
  932. } else if (document.mozCancelFullscreen) {
  933. document.mozCancelFullscreen();
  934. return true;
  935. }
  936. } else {
  937. if (main.requestFullScreenWithKeys) {
  938. main.requestFullScreenWithKeys();
  939. return true;
  940. } else if (main.requestFullScreen) {
  941. main.requestFullScreen(true);
  942. return true;
  943. } else if (main.webkitRequestFullScreen) {
  944. main.webkitRequestFullScreen(true);
  945. return true;
  946. } else if (document.body.mozRequestFullScreenWithKeys) {
  947. document.body.mozRequestFullScreenWithKeys();
  948. return true;
  949. } else if (document.body.mozRequestFullScreen) {
  950. document.body.mozRequestFullScreen();
  951. return true;
  952. }
  953. }
  954. return false;
  955. }
  956. /**
  957. * @description Scale the game to fit fullscreen mode
  958. * @private
  959. */
  960. function enterFullscreen() {
  961. var ratio = window.innerWidth / 640,
  962. sliceZoom = Math.floor(Wolf.SLICE_WIDTH * ratio),
  963. zoom = sliceZoom / Wolf.SLICE_WIDTH,
  964. transform = "scale(" + zoom + ")";
  965. $("#main").css({
  966. "transform" : transform,
  967. "-webkit-transform" : transform,
  968. "-moz-transform" : transform,
  969. "-ms-transform" : transform,
  970. "-o-transform" : transform
  971. }).data("scale", zoom);
  972. }
  973. /**
  974. * @description Scale the game to fit windowed mode
  975. * @private
  976. */
  977. function exitFullscreen() {
  978. $("#main").css({
  979. "transform" : "",
  980. "-webkit-transform" : "",
  981. "-moz-transform" : "",
  982. "-ms-transform" : "",
  983. "-o-transform" : ""
  984. }).data("scale", 1);
  985. }
  986. /**
  987. * @description Initialize the game module
  988. * @memberOf Wolf.Game
  989. */
  990. function init() {
  991. $(document)
  992. .on("mozfullscreenchange", fullscreenChange)
  993. .on("webkitfullscreenchange", fullscreenChange)
  994. .on("fullscreenchange", fullscreenChange);
  995. Wolf.Input.bindKey("F11", function(e) {
  996. if (!keyInputActive) {
  997. return;
  998. }
  999. if (toggleFullscreen()) {
  1000. e.preventDefault();
  1001. } else {
  1002. if (isFullscreen()) {
  1003. exitFullscreen();
  1004. } else {
  1005. enterFullscreen();
  1006. }
  1007. }
  1008. });
  1009. Wolf.Input.bindKey("P", function(e) {
  1010. if (!keyInputActive) {
  1011. return;
  1012. }
  1013. togglePause();
  1014. });
  1015. Wolf.Input.bindKey("ESC", function(e) {
  1016. if (!keyInputActive) {
  1017. return;
  1018. }
  1019. exitToMenu();
  1020. });
  1021. if (!isFullscreen() && (window.fullScreen || (window.innerWidth == screen.width && window.innerHeight == screen.height))) {
  1022. toggleFullscreen();
  1023. }
  1024. }
  1025. /**
  1026. * @description Exit to main menu
  1027. * @memberOf Wolf.Game
  1028. */
  1029. function exitToMenu() {
  1030. if (!paused) {
  1031. togglePause();
  1032. }
  1033. $("#game").hide();
  1034. keyInputActive = false;
  1035. Wolf.Menu.show("main");
  1036. }
  1037. /**
  1038. * @description Resume game after coming from menu
  1039. * @memberOf Wolf.Game
  1040. */
  1041. function resume() {
  1042. $("#game").show();
  1043. if (paused) {
  1044. togglePause();
  1045. }
  1046. keyInputActive = true;
  1047. if (levelMusic) {
  1048. Wolf.Sound.startMusic(levelMusic);
  1049. }
  1050. }
  1051. /**
  1052. * @description Query the game state
  1053. * @memberOf Wolf.Game
  1054. * @returns {boolean} True if the is currently playing
  1055. */
  1056. function isPlaying() {
  1057. return playing;
  1058. }
  1059. /**
  1060. * @description Toggle the pause state.
  1061. * @private
  1062. */
  1063. function togglePause() {
  1064. paused = !paused;
  1065. if (paused) {
  1066. Wolf.Sound.pauseMusic(true);
  1067. } else {
  1068. Wolf.Sound.pauseMusic(false);
  1069. lastTimeCount = (new Date).getTime();
  1070. }
  1071. $("#game .renderer div.pause.overlay").toggle(paused);
  1072. }
  1073. function enableMouse(enable) {
  1074. mouseEnabled = enable;
  1075. }
  1076. function isMouseEnabled() {
  1077. return mouseEnabled;
  1078. }
  1079. function getControls() {
  1080. var c = {};
  1081. for (var a in controls) {
  1082. if (controls.hasOwnProperty(a)) {
  1083. c[a] = controls[a];
  1084. }
  1085. }
  1086. return c;
  1087. }
  1088. function bindControl(action, keys) {
  1089. controls[action] = keys;
  1090. }
  1091. /*
  1092. function dump() {
  1093. console.log(currentGame);
  1094. window.open("data:text/plain," + JSON.stringify(currentGame), "dump");
  1095. }
  1096. function debugGodMode(enable) {
  1097. if (currentGame && currentGame.player) {
  1098. if (enable) {
  1099. currentGame.player.flags |= Wolf.FL_GODMODE;
  1100. } else {
  1101. currentGame.player.flags &= ~Wolf.FL_GODMODE;
  1102. }
  1103. Wolf.log("God mode " + (enable ? "enabled" : "disabled"));
  1104. }
  1105. }
  1106. function debugNoTarget(enable) {
  1107. if (currentGame && currentGame.player) {
  1108. if (enable) {
  1109. currentGame.player.flags |= Wolf.FL_NOTARGET;
  1110. } else {
  1111. currentGame.player.flags &= ~Wolf.FL_NOTARGET;
  1112. }
  1113. Wolf.log("No target " + (enable ? "enabled" : "disabled"));
  1114. }
  1115. }
  1116. function debugVictory() {
  1117. if (currentGame && currentGame.player) {
  1118. Wolf.log("Winning!");
  1119. Wolf.Game.startIntermission(currentGame);
  1120. }
  1121. }
  1122. function debugEndEpisode() {
  1123. if (currentGame && currentGame.player) {
  1124. victory(currentGame);
  1125. }
  1126. }
  1127. function debugToggleAI(enable) {
  1128. processAI = !!enable;
  1129. }
  1130. function debugGiveAll() {
  1131. if (currentGame && currentGame.player) {
  1132. Wolf.Player.givePoints(currentGame.player, 10000);
  1133. Wolf.Player.giveHealth(currentGame.player, 100, 100);
  1134. Wolf.Player.giveKey(currentGame.player, 0);
  1135. Wolf.Player.giveKey(currentGame.player, 1);
  1136. Wolf.Player.giveWeapon(currentGame.player, 2);
  1137. Wolf.Player.giveWeapon(currentGame.player, 3);
  1138. Wolf.Player.giveAmmo(currentGame.player, Wolf.AMMO_BULLETS, 99);
  1139. Wolf.log("Giving keys, weapons, ammo, health and 10000 points");
  1140. }
  1141. }
  1142. */
  1143. return {
  1144. startGame : startGame,
  1145. startLevel : startLevel,
  1146. startIntermission : startIntermission,
  1147. startDamageFlash : startDamageFlash,
  1148. startBonusFlash : startBonusFlash,
  1149. enableMouse : enableMouse,
  1150. isMouseEnabled : isMouseEnabled,
  1151. isPlaying : isPlaying,
  1152. notify : notify,
  1153. isFullscreen : isFullscreen,
  1154. init : init,
  1155. getControls : getControls,
  1156. bindControl : bindControl,
  1157. resume : resume,
  1158. victory : victory,
  1159. endEpisode : endEpisode
  1160. /*
  1161. dump : dump,
  1162. debugGodMode : debugGodMode,
  1163. debugNoTarget : debugNoTarget,
  1164. debugToggleAI : debugToggleAI,
  1165. debugGiveAll : debugGiveAll,
  1166. debugVictory : debugVictory,
  1167. debugEndEpisode : debugEndEpisode
  1168. */
  1169. };
  1170. })();