Advertisement
Guest User

Chapter 2: Tank Game Course

a guest
Apr 30th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var TankGame = (function() {
  2.   var TARGET_WIDTH = 801; // Desired width
  3.   var TARGET_HEIGHT = 601;
  4.   var WIDTH; // Actual width of game, scaled to fit screen
  5.   var HEIGHT;
  6.   var GUI_HEIGHT = 150;
  7.   var DEBUG = false;
  8.   // WASD
  9.   var P1_UP = 87;
  10.   var P1_DOWN = 83;
  11.   var P1_LEFT = 65;
  12.   var P1_RIGHT = 68;
  13.   var P1_FIRE = 49;
  14.  
  15.   // Arrow keys
  16.   var P2_UP = 38;
  17.   var P2_DOWN = 40;
  18.   var P2_LEFT = 37;
  19.   var P2_RIGHT = 39;
  20.   var P2_FIRE = 189;
  21.  
  22.   // Other settings
  23.   var TANK_SIZE = 15;
  24.   var TANK_SPEED = 1;
  25.   var TANK_TURN_SPEED = 5;
  26.   var WALL_WIDTH = 2;
  27.   var CELL_SIZE = 50;
  28.   var RESET_COUNTER_MAX = 200;
  29.   var EPSILON = 0.001;
  30.  
  31.  
  32.   var MAX_DIST_FOR_COLLISIONS = 2; // (this is multiplied by CELL_SIZE)
  33.   var TANK_P1, TANK_P2;
  34.   var CELLS_X, CELLS_Y;
  35.   var CANVAS, CTX, KEYSTATE, GAME_OBJECTS;
  36.   var PRERENDERED_CANVAS, PRERENDERED_CTX, PRERENDERED_REDRAW_NEEDED;
  37.   var GUI_REDRAW_NEEDED;
  38.   var END_ROUND = false;
  39.   var RESET_COUNTER;
  40.  
  41.   var P1 = 1;
  42.   var P2 = 2;
  43.   var P1_SCORE = 0;
  44.   var P2_SCORE = 0;
  45.  
  46.  
  47.  
  48.  
  49.   /*
  50.   ===============================================================================
  51.   -------------------------------------CLASSES-----------------------------------
  52.   ===============================================================================
  53.   */
  54.  
  55.   function deg2rad(degrees) {
  56.     return degrees * (Math.PI/180);
  57.   }
  58.  
  59.   class Vector2d {
  60.     constructor(x, y) {
  61.       if (typeof x == 'undefined' || typeof  y == 'undefined') {
  62.         throw "Invalid arguments";
  63.       }
  64.       this.x = x;
  65.       this.y = y;
  66.     }
  67.  
  68.     rotate(radians) {
  69.       // Rotates coordinates counterclockwise
  70.       //radians=-radians
  71.       if (radians != 0) {
  72.         var x = this.x;
  73.         var y = this.y;
  74.         this.x = x * Math.cos(radians) - y * Math.sin(radians);
  75.         this.y = x * Math.sin(radians) + y * Math.cos(radians);
  76.       }
  77.       return this;
  78.     }
  79.  
  80.     add(vector) {
  81.       if (vector instanceof Vector2d) {
  82.         this.x += vector.x;
  83.         this.y += vector.y;
  84.       }
  85.       else throw "Invalid argument";
  86.       return this;
  87.     }
  88.  
  89.     subtract(vector) {
  90.       if (vector instanceof Vector2d) {
  91.         this.x -= vector.x;
  92.         this.y -= vector.y;
  93.       }
  94.       else throw "Invalid argument";
  95.       return this;
  96.     }
  97.  
  98.     multiply(value) {
  99.       if (isNaN(value) === false) {
  100.         this.x *= value;
  101.         this.y *= value;
  102.       }
  103.       else throw "Invalid argument";
  104.       return this;
  105.     }
  106.  
  107.     get_dot_product(other) {
  108.       if (other instanceof Vector2d) return this.x*other.x + this.y*other.y;
  109.       else throw "Invalid argument";
  110.     }
  111.  
  112.     get_unit_vector() {
  113.       var c = this.get_magnitude();
  114.       var unit_vec = new Vector2d(this.x/c, this.y/c);
  115.       return unit_vec;
  116.     }
  117.  
  118.     get_right_normal() {
  119.       return new Vector2d(this.y, -this.x);
  120.     }
  121.  
  122.     get_magnitude() {
  123.       return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
  124.     }
  125.  
  126.     get_magnitude_squared() {
  127.       return Math.pow(this.x, 2) + Math.pow(this.y, 2);
  128.     }
  129.  
  130.     get_inverted() {
  131.       return new Vector2d(-this.x, -this.y);
  132.     }
  133.  
  134.     clone() {
  135.       return new Vector2d(this.x, this.y);
  136.     }
  137.  
  138.     reflect(vector) {
  139.       if (vector instanceof Vector2d) {
  140.         var vec2 = vector.clone(); // Avoid modifying given vector
  141.         vec2.multiply(vec2.get_dot_product(this));
  142.         vec2.multiply(2);
  143.         this.subtract(vec2);
  144.         return this;
  145.       }
  146.       else throw "Invalid argument";
  147.     }
  148.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement