]> git.eli173.com Git - pong_br/commitdiff
lotta changes, dunno what's going on really...
authorElijah Cohen <eli@eli173.com>
Tue, 30 Apr 2019 22:36:27 +0000 (17:36 -0500)
committerElijah Cohen <eli@eli173.com>
Tue, 30 Apr 2019 22:36:27 +0000 (17:36 -0500)
server/field.js
server/gamestate.js
web/draw.js
web/field.js
web/test.js

index dbcf103d2f10e34c05f2c7100072d41e18428c46..1b4d997f22dcaec6aef9a63ae3001e8fefceaf27 100644 (file)
@@ -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<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 = [];
@@ -92,4 +84,4 @@ var angles = function(n, dead, thresh) {
     return angs;
 }
 
-module.exports = {angles: angles, genEndpoints: genEndpoints, endpointNegatives: endpointNegatives};
+module.exports = {angles: angles, genAllEndpoints: genAllEndpoints};
index e4d6ae0b4d9a6e4d963438e59baf0f0f68454bd7..eba3a8b1b05a8ae77d1973c5d62fb608a72ce15f 100644 (file)
@@ -62,11 +62,10 @@ GameState.prototype.update = function(inputs) {
        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
@@ -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);
index 72086f0f1e2cd8018c0430041851b8ded1977fa9..cbad3a72bb6e7cea092838fb2891e35bb13f101f 100644 (file)
@@ -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();
index 4316922b9b9206cfe8e1e93bf47d8bb298c4924e..2744f84e461f772af6b1ba49175194ac9f99b88d 100644 (file)
@@ -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<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};
index 311bfb465f89f799b94abecd48578f517a253bce..5c37389ba566384f9b827b86b865f9b0367d7a0f 100644 (file)
@@ -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);
     }
 }