From b0a34f7633d73fc8c340e83014c1fd912e26e615 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Wed, 15 May 2019 15:01:50 -0500 Subject: [PATCH] fixed the robots (they're p good) --- NOTES.org | 2 ++ TODO.org | 2 +- server/gamestate.js | 2 ++ server/robot.js | 28 +++++++++++++--------------- web/draw.js | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/NOTES.org b/NOTES.org index 4c7c08b..0bca85c 100644 --- a/NOTES.org +++ b/NOTES.org @@ -1,4 +1,6 @@ +so i'm just realising the bug in the ai code was not translating it by the negative. check the diff in this next commit and the gone line with =translate(center.x, center.y)= should have both =center= pieces negative + OKAY have an intro page, and then let the 'play' page I already have up be the whole thing. diff --git a/TODO.org b/TODO.org index c630992..35943c5 100644 --- a/TODO.org +++ b/TODO.org @@ -1,4 +1,4 @@ -important: +todo: - fix the robots - paddle movement bump - fix collisions diff --git a/server/gamestate.js b/server/gamestate.js index b574394..6c4f1b1 100644 --- a/server/gamestate.js +++ b/server/gamestate.js @@ -130,6 +130,8 @@ GameState.prototype.getState = function() { var newpads = this.paddles.map(p => p.reduce()); var thedead = this.dead; var totnum = this.numPlayers; + + var theobject = {n: totnum, dead: thedead, paddles: newpads, balls: newballs}; // maybe a proper constructor would be good here... return theobject; diff --git a/server/robot.js b/server/robot.js index cba2dd2..f7bb316 100644 --- a/server/robot.js +++ b/server/robot.js @@ -54,21 +54,19 @@ Robot.prototype.standard_ai = function(data) { var theta = Math.atan2(my_ep.s.y-my_ep.f.y, my_ep.s.x-my_ep.f.x); // not totally sure if i need this copy, dunno where else the object is used... var nearest_mod = new Coord(nearest.x, nearest.y) - nearest_mod.translate(center.x, center.y); - nearest_mod.rotate(-theta); - // now i get the paddle's relative position along the line, and use that to compare with - if(!data.paddles.some(p => p.id==data.id)) + var paddle = data.paddles.find(p => p.id == data.id); + if(typeof paddle == 'undefined') return; - var my_paddle = data.paddles.find(p => p.id == data.id); - // largely stolen from getPaddlePoints - var ep_len = my_ep.getLength(); - var pspace_len = ep_len-c.WIDTH_RATIO*ep_len; - var idk_len = (ep_len-pspace_len)/2; - var paddle_ratio = (my_paddle.pos+1)/2; - var center_pos = idk_len+paddle_ratio*pspace_len; - if(data.id==2) - console.log(nearest_mod.x < center_pos, nearest_mod.x, center_pos); - this.status = (nearest_mod.x < center_pos) ? 'd' : 'u'; + var pobj = new Paddle(data.id); + pobj.position = paddle.pos; + var pps = pobj.getPaddlePoints(my_ep); + var paddle_center = ep_midpoint(pps); + nearest_mod.translate(-paddle_center.x, -paddle_center.y); + nearest_mod.rotate(-theta); + + this.status = (nearest_mod.x < 0) ? 'u' : 'd'; + // if it's close enough just stay still though + if(Math.abs(nearest_mod.x) < 0.2) this.status = 'x'; } @@ -111,7 +109,7 @@ Robot.prototype.bad_ai = function(data) { } var midpoint = function(a,b) { - return new Coord((a.x+b.x/2), (a.y+b.y)/2); + return new Coord((a.x+b.x)/2, (a.y+b.y)/2); } var ep_midpoint = function(e) { return midpoint(e.f, e.s); diff --git a/web/draw.js b/web/draw.js index e7110d3..93d11f5 100644 --- a/web/draw.js +++ b/web/draw.js @@ -90,7 +90,7 @@ var dist = function(c1, c2) { var drawBall = function(ctx, color, coord) { ctx.save(); - ctx.fillStyle = bcolor; + ctx.fillStyle = color; ctx.beginPath(); ctx.arc(coord.x, coord.y, c.BALL_RADIUS, 0, 2*Math.PI, false); ctx.fill(); -- 2.39.2