]> git.eli173.com Git - pong_br/commitdiff
fixed the robots (they're p good)
authorElijah Cohen <eli@eli173.com>
Wed, 15 May 2019 20:01:50 +0000 (15:01 -0500)
committerElijah Cohen <eli@eli173.com>
Wed, 15 May 2019 20:01:50 +0000 (15:01 -0500)
NOTES.org
TODO.org
server/gamestate.js
server/robot.js
web/draw.js

index 4c7c08bba905ece874969f1ba56d374ad50e14bc..0bca85c714d4b84065212e9f20a1f730a705dc41 100644 (file)
--- 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.
index c630992fbca345aaefa643e5c51d6eb1fe710b0c..35943c507af30e547d671975c70f8dbff8488566 100644 (file)
--- a/TODO.org
+++ b/TODO.org
@@ -1,4 +1,4 @@
-important:
+todo:
 - fix the robots
 - paddle movement bump
 - fix collisions
index b574394a2df7f0fc0428da81cd2fd45c046ed5c9..6c4f1b109e84225b383d26a0ace064cdfd1a15bc 100644 (file)
@@ -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;
index cba2dd27522093af6945a74b77f3d587862b5a15..f7bb31676da8e23e098883d1c03df0ba93b58a47 100644 (file)
@@ -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);
index e7110d3f05bd75771a5ded9947adede2f45097fb..93d11f5f2774b86b6e63938e6636b6cda0e2692f 100644 (file)
@@ -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();