/server/ball.js~
/server/coord.js~
/server/endpoints.js~
+/server/constants.js~
-
-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');
// 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);
}
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;
--- /dev/null
+
+
+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
-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')
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));
}
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));
}
// 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) {
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;
// 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) {
-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');
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);
}
}
-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')
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 {
// 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);
}