From: Elijah Cohen Date: Thu, 25 Apr 2019 23:02:37 +0000 (-0500) Subject: refactored constants out to their own file X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=4516c024c1c3c11a376680d5425e484f84c4f650;p=pong_br refactored constants out to their own file --- diff --git a/.gitignore b/.gitignore index c6a1d47..5bb3fe9 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ server/node_modules/* /server/ball.js~ /server/coord.js~ /server/endpoints.js~ +/server/constants.js~ diff --git a/server/ball.js b/server/ball.js index 9efe6bf..e0daf4e 100644 --- a/server/ball.js +++ b/server/ball.js @@ -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 index 0000000..910d520 --- /dev/null +++ b/server/constants.js @@ -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 diff --git a/server/field.js b/server/field.js index 8b893c3..1fcae74 100644 --- a/server/field.js +++ b/server/field.js @@ -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(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(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)); } diff --git a/server/gamestate.js b/server/gamestate.js index 2e49170..84e5f6e 100644 --- a/server/gamestate.js +++ b/server/gamestate.js @@ -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) { diff --git a/server/matchmaker.js b/server/matchmaker.js index a7d4766..232df89 100644 --- a/server/matchmaker.js +++ b/server/matchmaker.js @@ -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); } } diff --git a/server/paddles.js b/server/paddles.js index dd26b92..b8d7f6b 100644 --- a/server/paddles.js +++ b/server/paddles.js @@ -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); }