Advertisement
orzecho1994

game.js

Jun 3rd, 2020
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Level {
  2.        
  3.     wall;
  4.     width;
  5.     height;
  6.     gravity;
  7.    
  8.         constructor(wallWidth, wallHeight, canvasWidth, canvasHeight, map) {
  9.            
  10.             //rozmiar sciany
  11.             this.wall = { width: wallWidth, height: wallHeight };
  12.            
  13.             //rozmiar poziomu
  14.             this.width = canvasWidth / wallWidth;
  15.             this.height = canvasHeight / wallHeight;
  16.            
  17.             //rodzaje scian
  18.             this.walls = [
  19.                 {id: 'background', colour: '#808080', solid: 0 },
  20.                 {id: 'wall', colour: '#333333', solid: 1 },
  21.                 {id: 'win', colour: '#00ff00', solid: 0 },
  22.                 {id: 'lose', colour: '#ff0000', solid: 0 }
  23.             ];
  24.            
  25.             //mapa
  26.             this.mapx=map;
  27.             this.gravity = wallHeight / 25;
  28.            
  29.         }
  30.    
  31.         set wall(size) {
  32.             var canvasWidth = this.wall.width * this.width;
  33.             var canvasHeight = this.wall.height * this.height;
  34.            
  35.             this.wall = { width: size.width, height: size.height };
  36.            
  37.             this.width = canvasWidth / size.width;
  38.             this.height = canvasHeight / size.height;
  39.         }
  40.    
  41.         get wall() {
  42.             return this.wall;
  43.         }
  44.    
  45.    
  46.         set mapx(map) {
  47.            
  48.             this.checkMap(map);
  49.             this.map=map;
  50.            
  51.            
  52.         }
  53.    
  54.         get mapx() {
  55.             this.checkMap(this.map);
  56.             return this.map;
  57.         }
  58.    
  59.    
  60.         drawWall(x1, y1, x2, y2, colour, context) {
  61.             context.beginPath();
  62.             context.strokeStyle = colour;
  63.             context.rect( x1, y1, x2-x1, y2-y1 );
  64.             context.fillStyle = colour;
  65.             context.fill();
  66.         }
  67.        
  68.    
  69.        
  70.         drawMap(context) {
  71.             for (var x=0; x<level.width; x++) {
  72.                 for (var y=0; y<level.height; y++) {
  73.                     this.drawWall(
  74.                         x*this.wall.width,
  75.                         y*this.wall.height,
  76.                         (x+1)*this.wall.width,
  77.                         (y+1)*this.wall.height,
  78.                         this.walls[this.mapx[y][x]].colour,
  79.                         context
  80.                     );
  81.                 }
  82.             }
  83.         }
  84.    
  85.         checkMap (map) {
  86.             //alert("checkMap initialazed.");
  87.            
  88.             var minWidth = map[0].length;
  89.             var minHeight = map.length;
  90.            
  91.             for (var y=0; y<this.height; y++) {
  92.                
  93.                 if(map[y].length<minWidth) minWidth = map[y].length;
  94.                 for (var x=0; x<map[y].length; x++) {
  95.                     if (map[y][x]>this.walls.length-1) {
  96.                         alert("illegal wall at "+y+" "+x+" !");
  97.                         return false;
  98.                     }
  99.                 }
  100.             }
  101.            
  102.             if(minWidth == this.width && minHeight == this.height) {
  103.                 //alert("checkMap successful!");
  104.                 return true;
  105.                 }
  106.             else {
  107.                 alert("checkMap failed!");
  108.                 return false;
  109.                 }
  110.         }
  111.    
  112.    
  113.         checkWall (wall) {
  114.            
  115.         }
  116.     };
  117.  
  118. class Player {
  119.    
  120.  
  121.     colour;
  122.     level;
  123.     position;
  124.     r;
  125.     velocity;
  126.     speed;
  127.     moving;
  128.     gravity;
  129.     fallingCounter;
  130.     onground;
  131.    
  132.     constructor(colour, level) {
  133.            
  134.             //rozmiar sciany
  135.             this.level = level;
  136.            
  137.             this.colour = colour;
  138.            
  139.             this.position = {
  140.                 x: level.wall.width * (1 + 0.5),
  141.                 y: level.wall.width * (21 + 0.5)
  142.             };
  143.             this.r = (level.wall.height / 2);
  144.             //var colour = '#0000ff';
  145.             this.velocity = {
  146.                 x: 0,
  147.                 y: 0
  148.             };
  149.        
  150.             this.speed = {
  151.                 x: level.wall.width / 5,
  152.                 y: level.wall.height / 4.5
  153.             };
  154.        
  155.             this.moving = {
  156.                 left: false,
  157.                 right: false,
  158.                 up: false
  159.             }
  160.             this.gravity = level.gravity;
  161.             this.fallingCounter = 1;
  162.             this.onground = false;
  163.        
  164.         }
  165.    
  166.     drawPlayer(context) {
  167.        
  168.         context.beginPath();
  169.         context.arc(this.position.x, this.position.y, this.r, 0, 2 * Math.PI);
  170.         context.fillStyle = this.colour;
  171.         context.fill();
  172.        
  173.         /*
  174.         this.velocity = {
  175.                 x: 0,
  176.                 y: 0
  177.             };
  178.             */
  179.     }
  180.    
  181.    
  182.     move(context) {
  183.        
  184.         var location = {
  185.             centerX: Math.floor(this.position.x / this.level.wall.width),
  186.             centerY: Math.floor(this.position.y / this.level.wall.height),
  187.             left: Math.floor((this.position.x - this.r - 0.01)/this.level.wall.width),
  188.             right: Math.floor((this.position.x + this.r + 0.01)/this.level.wall.width),
  189.             top: Math.floor((this.position.y - this.r - 0.01)/this.level.wall.height),
  190.             bottom: Math.floor((this.position.y + this.r + 0.01)/this.level.wall.height),
  191.         };
  192.        
  193.         var walls = {
  194.             center: this.level.walls[this.level.map[location.centerY][location.centerX]],
  195.             left: this.level.walls[this.level.map[location.centerY][location.left]],
  196.             right: this.level.walls[this.level.map[location.centerY][location.right]],
  197.             top: this.level.walls[this.level.map[location.top][location.centerX]],
  198.             bottom: this.level.walls[this.level.map[location.bottom][location.centerX]],
  199.             topleft: this.level.walls[this.level.map[location.top][location.left]],
  200.             topright: this.level.walls[this.level.map[location.top][location.right]],
  201.             bottomleft: this.level.walls[this.level.map[location.bottom][location.left]],
  202.             bottomright: this.level.walls[this.level.map[location.bottom][location.right]],
  203.         };
  204.        
  205.         var oldPosition = this.position;
  206.        
  207.         this.position = {
  208.             x: this.position.x + this.velocity.x,
  209.             y: this.position.y + this.velocity.y + this.gravity * this.fallingCounter
  210.         };
  211.        
  212.         this.fallingCounter += 0.15;
  213.        
  214.         this.onground = false;
  215.         this.checkSolidWalls(walls, location, oldPosition);
  216.         this.checkWinWalls(walls);
  217.         this.checkLoseWalls(walls);
  218.        
  219.        
  220.         this.drawPlayer(context);
  221.        
  222.        
  223.        
  224.     }
  225.    
  226.    
  227.     checkSolidWalls(walls, location, oldPosition) {
  228.        
  229.        
  230.        
  231.         if(walls.left.solid == 1) {
  232.             if(this.position.x < oldPosition.x)
  233.                 this.position.x = (location.left +1) * this.level.wall.width + this.r;
  234.         }
  235.        
  236.         if(walls.right.solid == 1) {
  237.             if(this.position.x > oldPosition.x)
  238.                 this.position.x = (location.right - 1) * this.level.wall.width + this.r;
  239.         }
  240.        
  241.         if(walls.top.solid == 1) {
  242.             if(this.position.y < oldPosition.y)
  243.                 this.position.y = (location.top +1) * this.level.wall.width + this.r;
  244.         }
  245.        
  246.         if(walls.bottom.solid == 1) {
  247.             if(this.position.y > oldPosition.y){
  248.                 this.position.y = (location.bottom -1) * this.level.wall.width + this.r;
  249.                 this.fallingCounter = 1;
  250.                 this.onground = true;
  251.             }    
  252.         }
  253.        
  254.         if(walls.topleft.solid == 1) {
  255.             if(this.position.y < oldPosition.y && this.position.x < oldPosition.x) {
  256.                 this.position.y = (location.top +1) * this.level.wall.width + this.r;
  257.                 this.position.x = (location.left +1) * this.level.wall.width + this.r;
  258.             }
  259.         }
  260.            
  261.         if(walls.topright.solid == 1) {
  262.             if(this.position.y < oldPosition.y && this.position.x > oldPosition.x) {
  263.                 this.position.y = (location.top +1) * this.level.wall.width + this.r;
  264.                 this.position.x = (location.right - 1) * this.level.wall.width + this.r;
  265.             }
  266.         }
  267.        
  268.         if(walls.bottomleft.solid == 1) {
  269.             if(this.position.y > oldPosition.y && this.position.x < oldPosition.x) {
  270.                 this.position.y = (location.bottom -1) * this.level.wall.width + this.r;
  271.                 this.position.x = (location.left +1) * this.level.wall.width + this.r;
  272.             }
  273.         }
  274.        
  275.         if(walls.bottomright.solid == 1) {
  276.             if(this.position.y > oldPosition.y && this.position.x > oldPosition.x) {
  277.                 this.position.y = (location.bottom -1) * this.level.wall.width + this.r;
  278.                 this.position.x = (location.right - 1) * this.level.wall.width + this.r;
  279.             }
  280.         }
  281.                
  282.        
  283.     }
  284.    
  285.     checkWinWalls(walls) {
  286.         if(walls.center.id == 'win') {
  287.                 alert('  WIN! :D  ');
  288.                 this.position = {
  289.                     x: this.level.wall.width * (1 + 0.5),
  290.                     y: this.level.wall.height * (1 + 0.5)
  291.                 };
  292.             }
  293.     }
  294.    
  295.     checkLoseWalls(walls) {
  296.         if(walls.center.id == 'lose') {
  297.                 alert('  GAME OVER  ');
  298.                 this.position = {
  299.                     x: this.level.wall.width * (1 + 0.5),
  300.                     y: this.level.wall.height * (1 + 0.5)
  301.                 };
  302.             }
  303.     }
  304.    
  305.    
  306.     startMoving(key, context) {
  307.        
  308.        
  309.      
  310.         switch(key) {
  311.             case 37:
  312.                 this.moving.left = true;
  313.                 break;
  314.             case 39:
  315.                 this.moving.right = true;
  316.                 break;
  317.             case 38:
  318.                 if(this.onground==true)
  319.                 this.moving.up = true;
  320.                 break;
  321.         };
  322.        
  323.         //if(!this.moving.up) this.fallingCounter = 1;
  324.        
  325.         this.setMoving();
  326.        
  327.         //this.move(context);
  328.     }
  329.    
  330.     stopMoving(key,context) {
  331.        
  332.         if(this.moving.up) this.fallingCounter = 0;
  333.        
  334.         switch(key) {
  335.             case 37:
  336.                 this.moving.left = false;
  337.                 break;
  338.             case 39:
  339.                 this.moving.right = false;
  340.                 break;
  341.             case 38:
  342.                 this.moving.up = false;
  343.                 break;
  344.         };
  345.         this.setMoving();
  346.        
  347.         //this.move(context);
  348.     }
  349.    
  350.    
  351.     setMoving() {
  352.          
  353.         this.velocity.x=0;
  354.         this.velocity.y=0;
  355.        
  356.         if(this.moving.right==true)
  357.             this.velocity.x += this.speed.x;
  358.        
  359.         if(this.moving.left==true)
  360.             this.velocity.x -= this.speed.x;
  361.        
  362.    
  363.         if(this.moving.up==true && this.onground==true)
  364.             this.velocity.y -= this.speed.y*2;
  365.     }
  366.    
  367. }
  368.  
  369.  
  370. document.addEventListener('DOMContentLoaded', function (event) {
  371.    
  372.     //zapisanie do zmiennej elementu canvas
  373.     var canvas = document.getElementById('canvas');
  374.     //zapisanie do zmienne zawartosci elementu canvas
  375.     var context = canvas.getContext('2d');
  376.            
  377.     //ustawienie wymiarow elementu canvas
  378.     canvas.width = 800;
  379.     canvas.height = 600;
  380.    
  381.     //ustawienie koloru wypelnieniaB1CFFF
  382.     context.fillStyle = '#333333';
  383.     //narysowanie prostokata o wymiarach elementu canvas
  384.     context.fillRect(0, 0, canvas.width, canvas.height);
  385.    
  386.    
  387.     console.log('DONE');
  388.     var sizeofwall = 25;
  389.     // rozmiar Å›ciany
  390.     var wall = { width: sizeofwall, height: sizeofwall };
  391.    
  392.     // rozmiar poziomu
  393.     var width = canvas.width / sizeofwall;
  394.     var height = canvas.height / sizeofwall;
  395.    
  396.     //rodzaje Å›cian
  397.     var walls = [
  398.         {id: 'background', colour: '#333333', solid: 0 },
  399.         {id: 'wall', colour: '#a0a0a0', solid: 1 },
  400.         {id: 'win', colour: '#00ff00', solid: 0 },
  401.         {id: 'lose', colour: '#ff0000', solid: 0 }
  402.     ];
  403.    
  404.     // mapa
  405.     /*
  406.     var map = [
  407.         [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ],
  408.         [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1 ],
  409.         [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1 ],
  410.         [ 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  411.         [ 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  412.         [ 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ,1, 1, 1, 1, 1, 1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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  423.         [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  424.         [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ],
  425.         [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  426.         [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  427.         [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1 ],
  428.         [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  429.         [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
  430.         [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
  431.     ];
  432.     */
  433.      var map = [
  434.         [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 ],
  455.         [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 ],
  456.         [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2, 2, 2, 2, 1 ],
  457.         [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
  458.     ];
  459.    
  460.     level = new Level(sizeofwall,sizeofwall, canvas.width, canvas.height, map);
  461.     level.drawMap(context);
  462.    
  463.     player = new Player('#0000ff', level);
  464.     player.drawPlayer(context);
  465.    
  466.     document.addEventListener('keydown', function(event) {
  467.         //alert("wcisnieto "+event.keyCode);
  468.         player.startMoving(event.keyCode, context);
  469.        
  470.     });
  471.    
  472.     document.addEventListener('keyup', function(event) {
  473.         //alert("wcisnieto "+event.keyCode);
  474.         player.stopMoving(event.keyCode, context);
  475.        
  476.     });
  477.  
  478.    
  479.     function drawGame() {
  480.         context.clearRect(0, 0, canvas.Width, canvas.height);
  481.         level.drawMap(context);
  482.         player.move(context);
  483.     }
  484.    
  485.     setInterval(drawGame, 1000/25);
  486.    
  487.    
  488. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement