Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. "use strict";
  2. //--------------------------------------------------------------------------------------------------------------------
  3. //------ Variables, Constants and Objects
  4. //--------------------------------------------------------------------------------------------------------------------
  5. const SimonSaysSheet = {
  6. Background: {x: 0, y: 0, w: 720, h: 720, count: 2, speed: 0, alpha: 1},
  7. ButtonYellow: {x: 0, y: 720, w: 314, h: 314, count: 2, speed: 0, alpha: 1, pos: {x: 29, y: 377, r1: 100, r2: 333}},
  8. ButtonBlue: {x: 0, y: 1034, w: 314, h: 314, count: 2, speed: 0, alpha: 1, pos: {x: 377, y: 377, r1: 100, r2: 333}},
  9. ButtonRed: {x: 0, y: 1348, w: 314, h: 314, count: 2, speed: 0, alpha: 1, pos: {x: 377, y: 29, r1: 100, r2: 333}},
  10. ButtonGreen: {x: 0, y: 1662, w: 314, h: 314, count: 2, speed: 0, alpha: 1, pos: {x: 29, y: 29, r1: 100, r2: 333}},
  11. ButtonStartEnd: {x: 0, y: 1976, w: 360, h: 360, count: 2, speed: 0, alpha: 1, pos: {x: 180, y: 180, r1: 1, r2: 180}
  12. }
  13. };
  14.  
  15. const Position = {x: 0, y: 0};
  16. const centerGame = {x: SimonSaysSheet.Background.w/2, y: SimonSaysSheet.Background.h/2};
  17. const cvs = document.getElementById("cvs");
  18. const ctx = cvs.getContext("2d");
  19. const imgSheet = new Image();
  20. const mousePos = Object.create(Position);
  21. let background = null;
  22. const buttons = [];
  23.  
  24.  
  25. //--------------------------------------------------------------------------------------------------------------------
  26. //------ Classes
  27. //--------------------------------------------------------------------------------------------------------------------
  28.  
  29. function TBackground() {
  30. const pos = Object.create(Position);
  31. const sp = new TSprite(imgSheet, SimonSaysSheet.Background, pos);
  32.  
  33. this.draw = function () {
  34. sp.draw();
  35. }
  36. }
  37.  
  38. function TButton(aSpi) {
  39. const pos = aSpi.pos;
  40. const sp = new TSprite(imgSheet, aSpi, pos);
  41.  
  42. this.draw = function () {
  43. sp.draw();
  44. }
  45.  
  46. }
  47.  
  48. //--------------------------------------------------------------------------------------------------------------------
  49. //------ Function and Events
  50. //--------------------------------------------------------------------------------------------------------------------
  51. function isMouseInsideButton(aBounds,r1,r2){
  52. let isOver = !((mousePos.x < aBounds.left) || (mousePos.y < aBounds.top) || (mousePos.x > aBounds.right) || (mousePos.y > aBounds.bottom));
  53. if (isOver) {
  54. const hyp = Math.pow(mousePos.x - centerGame.x, 2) + Math.pow(mousePos.y - centerGame.y, 2);
  55. isOver = (hyp > r1) && (hyp < r2);
  56. }
  57. return isOver;
  58. }
  59.  
  60. function setMousePos(aEvent) {
  61. const bounds = cvs.getBoundingClientRect();
  62. mousePos.x = aEvent.clientX - bounds.left;
  63. mousePos.y = aEvent.clientY - bounds.top;
  64. }
  65.  
  66. function cvsMouseMove(aEvent) {
  67. setMousePos(aEvent);
  68. // Mouse move over canvas
  69. }
  70.  
  71. function cvsMouseDown() {
  72. // Mouse button down in canvas
  73. }
  74.  
  75. function cvsMouseUp() {
  76. // Mouse button up in canvas
  77. }
  78.  
  79. function loadGame(){
  80. // Sprite sheet is loaded and game can start!
  81. background = new TBackground();
  82. buttons.push(
  83. new TButton(SimonSaysSheet.ButtonYellow),
  84. new TButton(SimonSaysSheet.ButtonBlue),
  85. new TButton(SimonSaysSheet.ButtonRed),
  86. new TButton(SimonSaysSheet.ButtonGreen)
  87. );
  88. drawGame();
  89. }
  90.  
  91. function drawGame(){
  92. ctx.clearRect(0,0,cvs.width, cvs.height);
  93. background.draw();
  94. for(let i = 0; i < buttons.length; i++){
  95. buttons[i].draw();
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102. //--------------------------------------------------------------------------------------------------------------------
  103. //------ Main Code
  104. //--------------------------------------------------------------------------------------------------------------------
  105. imgSheet.addEventListener("load", loadGame);
  106. imgSheet.src = "SimonSaysSheet.png";
  107. cvs.width = SimonSaysSheet.Background.w;
  108. cvs.height = SimonSaysSheet.Background.h;
  109. cvs.addEventListener("mousemove", cvsMouseMove);
  110. cvs.addEventListener("mousedown", cvsMouseDown);
  111. cvs.addEventListener("mouseup", cvsMouseUp);
  112. document.addEventListener('contextmenu', aEvent => aEvent.preventDefault());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement