]> git.eli173.com Git - pong_br/commitdiff
refactored constants out to their own file
authorElijah Cohen <eli@eli173.com>
Thu, 25 Apr 2019 23:02:37 +0000 (18:02 -0500)
committerElijah Cohen <eli@eli173.com>
Thu, 25 Apr 2019 23:02:37 +0000 (18:02 -0500)
.gitignore
server/ball.js
server/constants.js [new file with mode: 0644]
server/field.js
server/gamestate.js
server/matchmaker.js
server/paddles.js

index c6a1d474c2853bea1d6a0042ed7d649f247d5341..5bb3fe9dfbd80d6162240eb141dcc1094c4d969c 100644 (file)
@@ -28,3 +28,4 @@ server/node_modules/*
 /server/ball.js~
 /server/coord.js~
 /server/endpoints.js~
+/server/constants.js~
index 9efe6bfc998ddb72a45e0b1d06742879bc74612d..e0daf4edd6bfe46dee213476a811d44272c71443 100644 (file)
@@ -1,13 +1,5 @@
 
-
-const MIN_INIT = 3;
-const MAX_INIT = 5; // initial speed constraints
-
-const RADIUS = 0.5;
-
-const MAX_SPEED = 15;
-
-const SPEED_BUMP = 0.2;
+const c = require('./constants.js');
 
 const Coord = require('./coord.js');
 
@@ -15,7 +7,7 @@ function Ball() {
     // generates a new ball with random direction and speed (within limits)
     var coord = new Coord(0,0);
     var angle = Math.random()*2*Math.PI;
-    var speed = Math.random()*(MAX_INIT-MIN_INIT) + MIN_INIT;
+    var speed = Math.random()*(c.MAX_INIT-c.MIN_INIT) + c.MIN_INIT;
     this.dx = speed*Math.cos(angle);
     this.dy = speed*Math.sin(angle);
 }
@@ -23,16 +15,16 @@ function Ball() {
 Ball.prototype.speed_up = function() {
     // this is ez with proportions
     var speed = Math.sqrt(this.dx*this.dx + this.dy*this.dy);
-    if (speed > MAX_SPEED)
-       speed = MAX_SPEED;
-    this.dx = (speed*this.dx)/(speed+SPEED_BUMP);
-    this.dy = (speed*this.dy)/(speed+SPEED_BUMP);
+    if (speed > c.MAX_SPEED)
+       speed = c.MAX_SPEED;
+    this.dx = (speed*this.dx)/(speed+c.SPEED_BUMP);
+    this.dy = (speed*this.dy)/(speed+c.SPEED_BUMP);
 }
 
 Ball.prototype.get_angle = function() {
     // gets angle from the origin
     return Math.atan2(this.coord.y, this.coord.x);
 }
-Ball.prototype.radius = RADIUS;
+Ball.prototype.radius = c.BALL_RADIUS;
 
 module.exports = Ball;
diff --git a/server/constants.js b/server/constants.js
new file mode 100644 (file)
index 0000000..910d520
--- /dev/null
@@ -0,0 +1,27 @@
+
+
+var c = {
+    // matchmaker
+    WS_PORT: 6789,
+    NUM_PLAYERS: 3,
+    MS_PER_FRAME: 500,
+    // gamestate
+    DYING_TIME_IN_FRAMES: 100,
+    BOARD_RADIUS: 10,
+    OOB_THRESH: 1, // out-of-bounds threshold
+    ANGLE_THRESH: 0.2, //radians, needs to acct for various rotatings going on... can prolly wing it
+    PADDLE_MVT_BONUS: 0.1, // why this value? who knows. the extra speed from paddles in motion
+    // ball
+    MIN_INIT: 3,
+    MAX_INIT: 5, // initial speed constraints
+    BALL_RADIUS: 0.5,
+    MAX_SPEED: 15,
+    SPEED_BUMP: 0.2,
+    // field
+    BOARD_RADIUS: 10, // completely arbitrary actually...
+    // paddle
+    DPADDLE: 0.1,
+    WIDTH_RATIO: 0.1, // paddle is 1/10th of gap rn
+}
+
+module.exports = c
index 8b893c337539685aafcb5e6a2e036ac22373d3b3..1fcae74d8ca7217debf3208210ff8a2cde2a2f7f 100644 (file)
@@ -1,8 +1,7 @@
 
-const BOARD_RADIUS = 10; // completely arbitrary actually...
-
 // for generating/using the actual playing field of the game, I guess it'll be newly generated on each frame? the math is simple enough...
 
+const c = require('./constants.js');
 
 const Coord = require('./coord.js')
 const Endpoints = require('./endpoints.js')
@@ -18,18 +17,18 @@ var genEndpoints = function(n ,dead) {
     var endpoints = [];
     var players_length = n - dead.length;
     for(var d in dead) {
-       players_length += d.time/DYING_TIME_IN_FRAMES;
+       players_length += d.time/c.DYING_TIME_IN_FRAMES;
     }
     var theta = 0;
     var dtheta = players_length/(2*Math.PI);
-    var coord = new Coord(BOARD_RADIUS,0);
+    var coord = new Coord(c.BOARD_RADIUS,0);
     for(var i=0; i<n; i++) {
        var deadStatus = dead.find(e=>(e.id==i));
        if((deadStatus !== undefined) && (deadStatus.time > 0)) {
-           var r = BOARD_RADIUS;
-           theta += (deadStatus.time/DYING_TIME_IN_FRAMES)/2;
+           var r = c.BOARD_RADIUS;
+           theta += (deadStatus.time/c.DYING_TIME_IN_FRAMES)/2;
            var pt1 = new Coord(r*Math.cos(theta), r*Math.sin(theta));
-           theta += (deadStatus.time/DYING_TIME_IN_FRAMES)/2;
+           theta += (deadStatus.time/c.DYING_TIME_IN_FRAMES)/2;
            var pt2 = new Coord(r*Math.cos(theta), r*Math.sin(theta));
            endpoints.push(new Endpoints(pt1, pt2, n));
        }
@@ -66,18 +65,18 @@ var angles = function(n, dead, thresh) {
     var angs = [];
     var players_length = n - dead.length;
     for(var d in dead) {
-       players_length += d.time/DYING_TIME_IN_FRAMES;
+       players_length += d.time/c.DYING_TIME_IN_FRAMES;
     }
     var theta = 0;
     var dtheta = players_length/(2*Math.PI);
-    var coord = new Coord(BOARD_RADIUS,0);
+    var coord = new Coord(c.BOARD_RADIUS,0);
     for(var i=0; i<n; i++) {
        var deadStatus = dead.find(e=>(e.id==i));
        if((deadStatus !== undefined) && (deadStatus.time > 0)) {
-           var r = BOARD_RADIUS;
-           theta += (deadStatus.time/DYING_TIME_IN_FRAMES)/2;
+           var r = c.BOARD_RADIUS;
+           theta += (deadStatus.time/c.DYING_TIME_IN_FRAMES)/2;
            var t1 = theta;
-           theta += (deadStatus.time/DYING_TIME_IN_FRAMES)/2;
+           theta += (deadStatus.time/c.DYING_TIME_IN_FRAMES)/2;
            var t2 = theta
            angs.push(new AnglePair(t1, t2, n));
        }
index 2e49170968bfa66f82aa83f92df97eed037f2d5d..84e5f6ec39be4930f3467c97454c2a530ab3fd55 100644 (file)
@@ -1,23 +1,13 @@
 // NOTE: inputs all get updated, forever will be length n, should work fine as-is
 
-
-const DYING_TIME_IN_FRAMES = 100;
-
-const BOARD_RADIUS = 10; /// ERROR! DUPLICATE!
-const OOB_THRESH = 1; // out-of-bounds threshold
-const ANGLE_THRESH = 0.2 //radians, needs to acct for various rotatings going on... can prolly wing it
-
-const PADDLE_MVT_BONUS = 0.1; // why this value? who knows. the extra speed from paddles in motion
-
-// const BOUNCE_DISTANCE = 0.1; // probably needs to be lowered...
-// actually not needed, this data is in ball.radius...
+const c = require('./constants.js');
 
 const Coord = require('./coord.js');
 const Ball = require('./ball.js');
 
 function Dead(id) {
     this.id = id;
-    this.time = DYING_TIME_IN_FRAMES;
+    this.time = c.DYING_TIME_IN_FRAMES;
 }
 
 function GameState(n) {
@@ -117,10 +107,10 @@ GameState.prototype.update = function(inputs) {
                    vel_coord.x = -vel_coord.x;
                    // note: any 'too fast' errors will be solved in the 'speed_up' method, so i need not worry here
                    if(paddle.direction == 'u') { 
-                       vel_coord.x += PADDLE_MVT_BONUS;
+                       vel_coord.x += c.PADDLE_MVT_BONUS;
                    }
                    else if(paddle.direction == 'd') {
-                       vel_coord.x -= PADDLE_MVT_BONUS;
+                       vel_coord.x -= c.PADDLE_MVT_BONUS;
                    }
                    vel_coord.rotate(slope_angle);
                    ball.dx = vel_coord.x;
@@ -135,8 +125,8 @@ GameState.prototype.update = function(inputs) {
     // OK I'M HERE AFAICT
     // i don't need to check where any of the paddles are, I just need to check the spaces behind the paddles (±)
     var zero = new Coord(0,0);
-    var oobs = this.balls.filter(b => (b.dist2(zero)>(BOARD_RADIUS+OOB_THRESH)));
-    var angs = angles(this.numPlayers, this.dead, OOB_THRESH);
+    var oobs = this.balls.filter(b => (b.dist2(zero)>(c.BOARD_RADIUS+c.OOB_THRESH)));
+    var angs = angles(this.numPlayers, this.dead, c.ANGLE_THRESH);
     for(var oob in oobs) {
        var oobth = oob.get_angle();
        for(var ang in angs) {
index a7d4766c4f440f394e1f0f88ac4e33538e385c04..232df89b3f71ab6f8676f3303da920eb4c99cb3e 100644 (file)
@@ -1,11 +1,10 @@
 
-const WS_PORT = 6789;
-const NUM_PLAYERS = 3;
-const MS_PER_FRAME = 500;
+
+const c = require('./constants.js')
 
 const WebSocket = require('ws');
 
-const wss = new WebSocket.Server({port: WS_PORT});
+const wss = new WebSocket.Server({port: c.WS_PORT});
 
 const Player = require('./player.js');
 const Game = require('./game.js');
@@ -17,10 +16,10 @@ players = [];
 var manage_incoming = function(ws) {
     var new_player = new Player(ws);
     players.push(new_player);
-    if(players.length >= NUM_PLAYERS) {
-       game = new Game(players.slice(0, NUM_PLAYERS));
-       game.start(MS_PER_FRAME);
-       players = players.slice(NUM_PLAYERS);
+    if(players.length >= c.NUM_PLAYERS) {
+       game = new Game(players.slice(0, c.NUM_PLAYERS));
+       game.start(c.MS_PER_FRAME);
+       players = players.slice(c.NUM_PLAYERS);
     }
 }
 
index dd26b9205bc37487b8444a23b2e8f5e9d8f812c1..b8d7f6bae9e0d9b0d4532ad015691c9a4288d90e 100644 (file)
@@ -1,7 +1,5 @@
 
-const DPADDLE = 0.1;
-
-const WIDTH_RATIO = 0.1; // paddle is 1/10th of gap rn
+const c = require('./constants.js')
 
 const Coord = require('./coord.js')
 const Endpoints = require('./endpoints.js')
@@ -16,11 +14,11 @@ var Paddle = function(id) {
 Paddle.prototype.move(direction) {
     // direction is either 'u', 'd', or 'x', passed along from the inputs gathered elsewhere
     if((direction=='u') && (this.position < 1)) {
-       this.position += DPADDLE;
+       this.position += c.DPADDLE;
        this.direction = 'u';
     }
     else if((direction=='d') && (this.position > -1)) {
-       this.position -= DPADDLE;
+       this.position -= c.DPADDLE;
        this.direction = 'd'
     }
     else {
@@ -39,14 +37,14 @@ Paddle.prototype.getEndpoints(enclosing) {
     // returns an endpoints object for the paddle
     // given the desired width of said paddle and the enclosing endpoints
     var encl_len = dist(enclosing.f, enclosing.s);
-    var pspace_len = encl_len - (2*WIDTH_RATIO*encl_len);
+    var pspace_len = encl_len - (2*c.WIDTH_RATIO*encl_len);
     var place_on_pspace = pspace_len*(this.position+1)/2;
-    var above_f = pspace_len + WIDTH_RATIO;  // distance above the first enclosing point (takes advantage of the increasing in angle thing to determine orientations)
+    var above_f = pspace_len + c.WIDTH_RATIO;  // distance above the first enclosing point (takes advantage of the increasing in angle thing to determine orientations)
     var overall_proportion = above_f/encl_len;
     var vector = [enclosing.s.x-enclosing.f.x, enclosing.s.y-enclosing.f.y];
-    var d = overall_proportion-WIDTH_RATIO;
+    var d = overall_proportion-c.WIDTH_RATIO;
     var first = new Coord(vector[0]*d, vector[1]*d);
-    d = overall_proportion+WIDTH_RATIO;
+    d = overall_proportion+c.WIDTH_RATIO;
     var second = new Coord(vector[0]*d, vector[1]*d);
     return new Endpoints(first, second, this.id);
 }