index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import * as wasm from "fourteen-screws";
  2. import { Cluiche } from "fourteen-screws";
  3. let demo_level = require('./demo-level');
  4. let textures = demo_level.textures;
  5. const cluiche = Cluiche.new();
  6. cluiche.load_textures(textures);
  7. let canvas = document.getElementById("canvas");
  8. let context = canvas.getContext("2d", { willReadFrequently: true });
  9. let keystate = {}
  10. document.addEventListener('keydown', (event) => { keystate[event.code] = true; }, false);
  11. document.addEventListener('keyup', (event) => { keystate[event.code] = false; }, false);
  12. let joystick = new JoyStick('joystick', { "internalFillColor": "#CCCCCC", "internalStrokeColor": "#333333", "externalStrokeColor": "#333333" }, function(stickData) {
  13. keystate["joystick"] = stickData.cardinalDirection;
  14. console.log(keystate["joystick"]);
  15. });
  16. const fps = new class {
  17. constructor() {
  18. this.fps = document.getElementById("fps");
  19. this.frames = [];
  20. this.lastFrameTimestamp = performance.now();
  21. }
  22. render () {
  23. // Convert the delta time since the last frame render into a measure
  24. // of frames per second
  25. const now = performance.now();
  26. const delta = now - this.lastFrameTimeStamp;
  27. this.lastFrameTimeStamp = now;
  28. const fps = 1 / delta * 1000;
  29. // Save only the latest 100 timings.
  30. this.frames.push(fps);
  31. if (this.frames.length > 100) {
  32. this.frames.shift();
  33. }
  34. // find the max, min, and mean of our 100 latest timings
  35. let min = Infinity;
  36. let max = -Infinity;
  37. let sum = 0;
  38. for (let i = 0; i < this.frames.length; i++) {
  39. sum += this.frames[i];
  40. min = Math.min(this.frames[i], min);
  41. max = Math.max(this.frames[i], max);
  42. }
  43. let mean = sum / this.frames.length;
  44. this.fps.textContent = `
  45. Frames per Second:
  46. latest = ${Math.round(fps)} |
  47. avg of last 100 = ${Math.round(mean)} |
  48. min of last 100 = ${Math.round(min)} |
  49. max of last 100 = ${Math.round(max)}
  50. `.trim();
  51. }
  52. }
  53. function render() {
  54. context.clearRect(0, 0, 320, 200);
  55. var image = context.getImageData(0, 0, 320, 200);
  56. cluiche.render(image.data);
  57. context.putImageData(image, 0, 0);
  58. }
  59. function events() {
  60. if (keystate['KeyW'] || keystate['ArrowUp'] || [ "N", "NW", "NE" ].includes(keystate['joystick'])) {
  61. cluiche.player_forward();
  62. }
  63. if (keystate['KeyS'] || keystate['ArrowDown'] || [ "S", "SW", "SE" ].includes(keystate['joystick'])) {
  64. cluiche.player_back();
  65. }
  66. if (keystate['KeyA']) {
  67. cluiche.player_strafe_left();
  68. }
  69. if (keystate['KeyD']) {
  70. cluiche.player_strafe_right();
  71. }
  72. if (keystate['ArrowLeft'] || [ "W", "SW", "NW" ].includes(keystate['joystick'])) {
  73. cluiche.player_turn_left();
  74. }
  75. if (keystate['ArrowRight'] || [ "E", "SE", "NE" ].includes(keystate['joystick'])) {
  76. cluiche.player_turn_right();
  77. }
  78. }
  79. function tick() {
  80. fps.render();
  81. events();
  82. render();
  83. requestAnimationFrame(tick);
  84. }
  85. requestAnimationFrame(tick);