Created
December 7, 2012 19:46
-
-
Save ethernet8023/4235950 to your computer and use it in GitHub Desktop.
A.I.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //For finding and tracking other bots | |
| self.pos = [0,0,0,0,0]; | |
| var Robot = function(robot) { | |
| }; | |
| Robot.prototype.onIdle = function(ev) { | |
| var robot = ev.robot; | |
| var predictedPos = predictRobot(); | |
| robot.rotateCannon(20); | |
| robot.turn(pointDirection(robot.position,predictedPos)); | |
| robot.ahead(20); | |
| }; | |
| Robot.prototype.onWallCollision = function(ev) { | |
| var robot = ev.robot; | |
| robot.turn(90); | |
| }; | |
| Robot.onRobotCollision = function(ev) { | |
| var robot = ev.robot; | |
| }; | |
| Robot.prototype.onScannedRobot = function(ev) { | |
| var robot = ev.robot; | |
| addRobotToTable(robot); | |
| robot.fire(); | |
| }; | |
| Robot.prototype.onHitByBullet = function(ev) { | |
| var robot = ev.movie; | |
| }; | |
| function addRobotToTable(robot) { | |
| self.pos.shift(); | |
| self.pos.push(robot.position); | |
| } | |
| function predictRobot() { | |
| var predictedPos = {x:0, y:0}; | |
| predictedPos.x = 0; | |
| predictedPos.y = 0; | |
| for(i=0;i<4;i++) { | |
| predictedPos.x += self.pos[i].x; | |
| predictedPos.y += self.pos[i].y; | |
| } | |
| predictedPos.x /= 4; | |
| predictedPos.y /= 4; | |
| return predictedPos; | |
| } | |
| function pointDirection(point1, point2) { | |
| var xs = 0; | |
| var ys = 0; | |
| xs = point2.x - point1.x; | |
| xs = xs * xs; | |
| ys = point2.y - point1.y; | |
| ys = ys * ys; | |
| return Math.sqrt( xs + ys ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment