const Endpoints = require('./endpoints.js')
-var genEndpoints = function(n ,dead) {
- /**
- /* takes the number of players total (that the game started with, should be NUM_PLAYERS),
- /* and the array of the dead and dying
- /* does id's in a bad but expectable way
- */
- // so i'll start from a radius out a zero radians
+
+var genAllEndpoints = function(n, dead) {
+ // gives everything relevant: endpoints enclosing walls, endpoints enclosing players
+ // throw it all in the same array, sort it on demand. this might actually be better computationally
var endpoints = [];
- var players_length = n - dead.length;
+ var players_length = n -dead.length;
for(var d of dead) {
players_length += d.time/c.DYING_TIME_IN_FRAMES;
}
var theta = 0;
var dtheta = (2*Math.PI)/players_length;
- // so my dumb ass forgot that 'r' isn't a thing, so I'm setting it from a proper place here...
- var r = c.BOARD_RADIUS;
- for(var i=0; i<n; i++) {
- var deadStatus = dead.find(e=>(e.id==i));
- if((deadStatus !== undefined) && (deadStatus.time > 0)) {
- 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/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 r = c.BOARD_RADIUS; // for brevity
+ var multiplier = 1;
+ for(var i=0;i<n;i++) {
+ var deadOption = dead.find(d => (d.id==i));
+ if(deadOption !== undefined) {
+ if(deadOption.time >0) {
+ multiplier = (deadOption.time/c.DYING_TIME_IN_FRAMES)/2;
+ var point1 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += multiplier*dtheta;
+ var point2 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += multiplier*dtheta;
+ var point3 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ endpoints.push(new Endpoints(point1, point2, i));
+ endpoints.push(new Endpoints(point2, point3, -1));
+ }
}
- else {
- theta += dtheta/2;
- var pt1 = new Coord(r*Math.cos(theta), r*Math.sin(theta));
- theta += dtheta/2;
- var pt2 = new Coord(r*Math.cos(theta), r*Math.sin(theta));
- endpoints.push(new Endpoints(pt1, pt2, n));
+ else { // here it has to be alive, skips over dead and no time
+ var point1 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += dtheta;
+ var point2 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += dtheta;
+ var point3 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ endpoints.push(new Endpoints(point1, point2, i));
+ endpoints.push(new Endpoints(point2, point3, -1));
}
}
return endpoints;
}
-var endpointNegatives = function(endpoints) {
- // generates the opposite of genEndpoints
- // i.e. genEndpoints gives the spaces where the paddles live,
- // and this gives the endpoints enclosing walls
- var newpoints = [];
- newpoints.push(new Endpoints(endpoints[endpoints.length-1].s, endpoints[0].f, -1));
- for(var i=1;i<endpoints.length;i++) {
- newpoints.push(new Endpoints(endpoints[i-1].s, endpoints[i].f, -1));
- }
- return newpoints;
-}
-
var AnglePair = function(f,s,id) {
this.f = f;
this.s = s;
this.id = id;
}
+// THIS WHOLE ANGLE NONSENSE NEEDS TO BE REWORKED? yes, to match
var angles = function(n, dead, thresh) {
// gives angle pairs for the thresholds and whatnot.
var angs = [];
return angs;
}
-module.exports = {angles: angles, genEndpoints: genEndpoints, endpointNegatives: endpointNegatives};
+module.exports = {angles: angles, genAllEndpoints: genAllEndpoints};
ball.coord.y += ball.dy;
}
//
- var endpoints = field.genEndpoints(this.numPlayers, this.dead);
- //console.log(endpoints);
- var borders = field.endpointNegatives(endpoints);
- var deadzones = endpoints.filter(ep => ep.isDead);
- var walls = borders.concat(deadzones);
+ var endpoints = field.genAllEndpoints(this.numPlayers, this.dead);
+ var livingzones = endpoints.filter(e => (e.id != -1));
+ livingzones = livingzones.filter(e => this.dead.some(d => (d.id!=e.id))); // bad time complexity?
+ var walls = endpoints.filter(e => (e.id == -1) || this.dead.some(d => (d.id==e.id)));
// check for collisions
for(var ball of this.balls) {
// (check the fixt edges first, then the paddles I guess
ball.speed_up();
}
}
- var livingzones = endpoints.filter(ep => !ep.isDead);
for(var lz of livingzones) {
// get corresponding paddle
// should be guaranteed to find one.... so...
}
// shrink the existing dead zones for the future
for(var d of this.dead) {
- d.time--;
+ if(d.time > 0)
+ d.time--;
}
// check for deaths
- // 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 (±)
+
+ // JUST REWRITE HERE TO AVOID field.angles, I CAN JUST DO ARCTAN ON PLAYERPOINTS
var zero = new Coord(0,0);
var oobs = this.balls.filter(b => (b.coord.dist2(zero)>(c.BOARD_RADIUS+c.OOB_THRESH)**2));
var angs = field.angles(this.numPlayers, this.dead, c.ANGLE_THRESH);
// then draw the balls,
// then finally the hard part is the paddles
//console.log(state);
- var theEndpoints = genEndpoints(state.n, state.dead);
- var negatives = endpointNegatives(theEndpoints);
- console.log(theEndpoints);
- // dead walls
- for(var ep of theEndpoints) {
- var deadOption = state.dead.find(d => (d.id==ep.id));
- if((deadOption !== undefined) && (deadOption.time > 0)) {
- drawLine(ctx, dwallcolor, ep.f, ep.s);
+
+ var endpoints = genAllEndpoints(state.n, state.dead);
+ console.log(endpoints);
+ var livingzones = endpoints.filter(e => (e.id != -1) && (state.dead.some(d=>(d.id!=e.id))));
+ var walls = endpoints.filter(e => (e.id==-1) || state.dead.some(d=>(d.id==e.id)));
+ //draw walls
+ for(var eps of walls) {
+ var c = wallcolor;
+ if(eps.id != -1) {
+ c = dwallcolor;
}
- }
- // walls
- for(var ep of negatives) {
- drawLine(ctx, wallcolor, ep.f, ep.s);
+ drawLine(ctx, c, eps.f, eps.s);
}
// balls
for(var b of state.balls) {
drawBall(ctx, bcolor, b);
}
// finally the paddles...
- for(var ep of theEndpoints) {
- var paddleOption = state.paddles.find(p => (p.id == ep.id));
- if(paddleOption !== undefined) {
- // should probably make sure it's not dead?
- if(!state.dead.some(d => (d.id == ep.id))) {
- var pps = getPaddlePoints(paddleOption, ep);
- drawLine(ctx, pcolor, pps.f, pps.s);
- }
- }
+ for(var eps of livingzones) {
+ var paddle = state.paddles.find(p => (p.id==ep.id)); //should be guaranteed?
+ if(paddle === undefined) alert("UH OH, paddle somehow undefined");
+ // cool this is way simpler since we know these are all alive
+ var pps = getPaddlePoints(paddle, eps);
+ drawLine(ctx, pcolor, pps.f, pps.s);
}
}
var getPaddlePoints = function(paddle, enclosing) {
// returns an endpoints object for the paddle
// given the desired width of said paddle and the enclosing endpoints
- console.log(enclosing);
var encl_len = dist(enclosing.f, enclosing.s);
var pspace_len = encl_len - (2*c.WIDTH_RATIO*encl_len);
var place_on_pspace = pspace_len*(paddle.position+1)/2;
return new Endpoints(first, second, paddle.id);
}
-
+var dist = function(c1, c2) {
+ return Math.sqrt((c1.x-c2.x)**2 + (c1.y-c2.y)**2);
+}
var drawBall = function(ctx, color, coord) {
ctx.save();
// 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...
-
-var genEndpoints = function(n ,dead) {
- /**
- /* takes the number of players total (that the game started with, should be NUM_PLAYERS),
- /* and the array of the dead and dying
- /* does id's in a bad but expectable way
- */
- // so i'll start from a radius out a zero radians
+var genAllEndpoints = function(n, dead) {
+ // gives everything relevant: endpoints enclosing walls, endpoints enclosing players
+ // throw it all in the same array, sort it on demand. this might actually be better computationally
var endpoints = [];
- var players_length = n - dead.length;
+ var players_length = n -dead.length;
for(var d of dead) {
players_length += d.time/c.DYING_TIME_IN_FRAMES;
}
var theta = 0;
var dtheta = (2*Math.PI)/players_length;
- // same bs with the 'r'...
- var r = c.BOARD_RADIUS;
- for(var i=0; i<n; i++) {
- var deadStatus = dead.find(e=>(e.id==i));
- if((deadStatus !== undefined) && (deadStatus.time > 0)) {
- 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/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 r = c.BOARD_RADIUS; // for brevity
+ var multiplier = 1;
+ for(var i=0;i<n;i++) {
+ var deadOption = dead.find(d => (d.id==i));
+ if(deadOption !== undefined) {
+ if(deadOption.time >0) {
+ multiplier = (deadOption.time/c.DYING_TIME_IN_FRAMES)/2;
+ var point1 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += multiplier*dtheta;
+ var point2 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += multiplier*dtheta;
+ var point3 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ endpoints.push(new Endpoints(point1, point2, i));
+ endpoints.push(new Endpoints(point2, point3, -1));
+ }
}
- else {
- theta += dtheta/2;
- var pt1 = new Coord(r*Math.cos(theta), r*Math.sin(theta));
- theta += dtheta/2;
- var pt2 = new Coord(r*Math.cos(theta), r*Math.sin(theta));
- endpoints.push(new Endpoints(pt1, pt2, n));
+ else { // here it has to be alive, skips over dead and no time
+ var point1 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += dtheta;
+ var point2 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ theta += dtheta;
+ var point3 = new Coord(r*Math.cos(theta),r*Math.sin(theta));
+ endpoints.push(new Endpoints(point1, point2, i));
+ endpoints.push(new Endpoints(point2, point3, -1));
}
}
return endpoints;
}
-var endpointNegatives = function(endpoints) {
- // generates the opposite of genEndpoints
- // i.e. genEndpoints gives the spaces where the paddles live,
- // and this gives the endpoints enclosing walls
- var newpoints = [];
- newpoints.push(new Endpoints(endpoints[endpoints.length-1].s, endpoints[0].f, -1));
- for(var i=1;i<endpoints.length;i++) {
- newpoints.push(new Endpoints(endpoints[i-1].s, endpoints[i].f, -1));
- }
- return newpoints;
-}
-
-var AnglePair = function(f,s,id) {
- this.f = f;
- this.s = s;
- this.id = id;
-}
-var angles = function(n, dead, thresh) {
- // gives angle pairs for the thresholds and whatnot.
- var angs = [];
- var players_length = n - dead.length;
- for(var d of dead) {
- players_length += d.time/c.DYING_TIME_IN_FRAMES;
- }
- var theta = 0;
- var dtheta = players_length/(2*Math.PI);
- 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 = c.BOARD_RADIUS;
- theta += (deadStatus.time/c.DYING_TIME_IN_FRAMES)/2;
- var t1 = theta;
- theta += (deadStatus.time/c.DYING_TIME_IN_FRAMES)/2;
- var t2 = theta
- angs.push(new AnglePair(t1, t2, n));
- }
- else {
- theta += dtheta/2;
- var t1 = theta;
- theta += dtheta/2;
- var t2 = theta;
- angs.push(new AnglePair(t1, t2, n));
- }
- }
- return angs;
-}
-module.exports = {angles: angles, genEndpoints: genEndpoints, endpointNegatives: endpointNegatives};
// opens up a nice websocket
theSocket = new WebSocket(prefixurl);
theSocket.onmessage = function (e) {
- newdiv = document.createElement('div')
- newdiv.innerText = "data:" + e.data;
- document.getElementById('content').appendChild(newdiv);
+ // newdiv = document.createElement('div')
+ // newdiv.innerText = "data:" + e.data;
+ // document.getElementById('content').appendChild(newdiv);
}
}