projet Web 3Mic
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { generatePlayground } from '/modules/playground.mjs'
  2. import { levelsBlueprint } from '/modules/levels.mjs'
  3. import { MoveDirection } from '/modules/enums.mjs'
  4. import { fillLevelsSelection, selectLevel, LevelManager } from '/modules/levelSelection.mjs'
  5. import { Timer } from '/modules/timer.mjs'
  6. import { TutorialControler } from '/modules/tutorialControler.mjs'
  7. import { Scoreboard, Gamer } from '/modules/scoreboard.mjs'
  8. // Get the canvas and the context
  9. let canvas = document.getElementById('canvas');
  10. let difficultySlider = document.getElementById('difficulty-slider');
  11. let mouseOnCanvas = false;
  12. canvas.addEventListener("mouseenter",() => {
  13. mouseOnCanvas = true;
  14. difficultySlider.disabled = true;
  15. });
  16. canvas.addEventListener("mouseleave",() => {
  17. mouseOnCanvas = false;
  18. difficultySlider.disabled = false;
  19. });
  20. let ctx = canvas.getContext('2d');
  21. // Create the game state
  22. // The game state is a global variable that contains all the information about the game
  23. let gameState = {
  24. playground: generatePlayground(levelsBlueprint[0].structure, canvas.width, canvas.height),
  25. width: canvas.width,
  26. height: canvas.height,
  27. timer: new Timer(1500, () => {
  28. alert("Les vaches mangent le foin");
  29. }),
  30. playable: false,
  31. levelManager: new LevelManager( () => {
  32. alert("Toutes les bottes sont rangées");
  33. gameState.timer.stop();
  34. } ),
  35. levelId: 0,
  36. scoreboard: new Scoreboard(),
  37. tutorial: new TutorialControler(),
  38. };
  39. fillLevelsSelection(gameState, ctx);
  40. window.ctx = ctx
  41. window.addEventListener("keydown", (event) => {
  42. if (!event.defaultPrevented && mouseOnCanvas) {
  43. if (gameState.playable) {
  44. // If the game is playable, the keyboard is used to move the player
  45. switch (event.key) {
  46. case "ArrowDown":
  47. gameState.playground.move(MoveDirection.Down);
  48. break;
  49. case "ArrowUp":
  50. gameState.playground.move(MoveDirection.Up);
  51. break;
  52. case "ArrowRight":
  53. gameState.playground.move(MoveDirection.Right);
  54. break;
  55. case "ArrowLeft":
  56. gameState.playground.move(MoveDirection.Left);
  57. break;
  58. default:
  59. return;
  60. break;
  61. }
  62. gameState.playground.draw(ctx, canvas.width, canvas.height);
  63. // If the game is solved, the next level is loaded
  64. if (gameState.playground.isSolved()) {
  65. gameState.levelManager.next(ctx, gameState);
  66. }
  67. } else {
  68. // If the game is not playable, the tutorial is displayed
  69. switch (event.key) {
  70. case "ArrowRight":
  71. case " ":
  72. case "Enter":
  73. gameState.tutorial.next();
  74. if (gameState.tutorial.isFinished()) {
  75. gameState.playable = true;
  76. gameState.timer.start();
  77. }
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. }
  84. });
  85. // Add event listener to the input field to update the name
  86. let inputName = document.getElementById("name");
  87. inputName.addEventListener("input", (event) => {
  88. inputName.setAttribute('value', event.target.value);
  89. });
  90. // Add event listener to the submit button to update the name
  91. let submitButton = document.getElementById("submit-name");
  92. submitButton.addEventListener("click", () => {
  93. let value = inputName.value;
  94. gamestate.scoreboard.updatedName(value);
  95. });
  96. // Draw the playground
  97. window.gamestate = gameState;
  98. gameState.playground.draw(ctx);