From 37adfe322b52dee5f93308cfb4583aa37a9b62f7 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Tue, 30 Apr 2019 17:36:27 -0500 Subject: [PATCH] lotta changes, dunno what's going on really... --- server/field.js | 68 +++++++++++++---------------- server/gamestate.js | 16 +++---- web/draw.js | 43 +++++++++---------- web/field.js | 101 ++++++++++++-------------------------------- web/test.js | 6 +-- 5 files changed, 88 insertions(+), 146 deletions(-) diff --git a/server/field.js b/server/field.js index dbcf103..1b4d997 100644 --- a/server/field.js +++ b/server/field.js @@ -7,60 +7,52 @@ const Coord = require('./coord.js') 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(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 (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 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 @@ -90,7 +89,6 @@ GameState.prototype.update = function(inputs) { 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... @@ -126,12 +124,14 @@ GameState.prototype.update = function(inputs) { } // 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); diff --git a/web/draw.js b/web/draw.js index 72086f0..cbad3a7 100644 --- a/web/draw.js +++ b/web/draw.js @@ -16,41 +16,36 @@ var draw = function(state, ctx) { // 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; @@ -64,7 +59,9 @@ var getPaddlePoints = function(paddle, enclosing) { 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(); diff --git a/web/field.js b/web/field.js index 4316922..2744f84 100644 --- a/web/field.js +++ b/web/field.js @@ -2,90 +2,43 @@ // 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(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 (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(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}; diff --git a/web/test.js b/web/test.js index 311bfb4..5c37389 100644 --- a/web/test.js +++ b/web/test.js @@ -13,9 +13,9 @@ function start() { // 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); } } -- 2.39.2