Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var TankGame = (function() {
- var TARGET_WIDTH = 801; // Desired width
- var TARGET_HEIGHT = 601;
- var WIDTH; // Actual width of game, scaled to fit screen
- var HEIGHT;
- var GUI_HEIGHT = 150;
- var DEBUG = false;
- // WASD
- var P1_UP = 87;
- var P1_DOWN = 83;
- var P1_LEFT = 65;
- var P1_RIGHT = 68;
- var P1_FIRE = 49;
- // Arrow keys
- var P2_UP = 38;
- var P2_DOWN = 40;
- var P2_LEFT = 37;
- var P2_RIGHT = 39;
- var P2_FIRE = 189;
- // Other settings
- var TANK_SIZE = 15;
- var TANK_SPEED = 1;
- var TANK_TURN_SPEED = 5;
- var WALL_WIDTH = 2;
- var CELL_SIZE = 50;
- var RESET_COUNTER_MAX = 200;
- var EPSILON = 0.001;
- var MAX_DIST_FOR_COLLISIONS = 2; // (this is multiplied by CELL_SIZE)
- var TANK_P1, TANK_P2;
- var CELLS_X, CELLS_Y;
- var CANVAS, CTX, KEYSTATE, GAME_OBJECTS;
- var PRERENDERED_CANVAS, PRERENDERED_CTX, PRERENDERED_REDRAW_NEEDED;
- var GUI_REDRAW_NEEDED;
- var END_ROUND = false;
- var RESET_COUNTER;
- var P1 = 1;
- var P2 = 2;
- var P1_SCORE = 0;
- var P2_SCORE = 0;
- /*
- ===============================================================================
- -------------------------------------CLASSES-----------------------------------
- ===============================================================================
- */
- function deg2rad(degrees) {
- return degrees * (Math.PI/180);
- }
- class Vector2d {
- constructor(x, y) {
- if (typeof x == 'undefined' || typeof y == 'undefined') {
- throw "Invalid arguments";
- }
- this.x = x;
- this.y = y;
- }
- rotate(radians) {
- // Rotates coordinates counterclockwise
- //radians=-radians
- if (radians != 0) {
- var x = this.x;
- var y = this.y;
- this.x = x * Math.cos(radians) - y * Math.sin(radians);
- this.y = x * Math.sin(radians) + y * Math.cos(radians);
- }
- return this;
- }
- add(vector) {
- if (vector instanceof Vector2d) {
- this.x += vector.x;
- this.y += vector.y;
- }
- else throw "Invalid argument";
- return this;
- }
- subtract(vector) {
- if (vector instanceof Vector2d) {
- this.x -= vector.x;
- this.y -= vector.y;
- }
- else throw "Invalid argument";
- return this;
- }
- multiply(value) {
- if (isNaN(value) === false) {
- this.x *= value;
- this.y *= value;
- }
- else throw "Invalid argument";
- return this;
- }
- get_dot_product(other) {
- if (other instanceof Vector2d) return this.x*other.x + this.y*other.y;
- else throw "Invalid argument";
- }
- get_unit_vector() {
- var c = this.get_magnitude();
- var unit_vec = new Vector2d(this.x/c, this.y/c);
- return unit_vec;
- }
- get_right_normal() {
- return new Vector2d(this.y, -this.x);
- }
- get_magnitude() {
- return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
- }
- get_magnitude_squared() {
- return Math.pow(this.x, 2) + Math.pow(this.y, 2);
- }
- get_inverted() {
- return new Vector2d(-this.x, -this.y);
- }
- clone() {
- return new Vector2d(this.x, this.y);
- }
- reflect(vector) {
- if (vector instanceof Vector2d) {
- var vec2 = vector.clone(); // Avoid modifying given vector
- vec2.multiply(vec2.get_dot_product(this));
- vec2.multiply(2);
- this.subtract(vec2);
- return this;
- }
- else throw "Invalid argument";
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement