Created
December 3, 2012 19:36
-
-
Save loktar00/4197372 to your computer and use it in GitHub Desktop.
Rocky Balboa
This file contains 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
var Robot = function(robot) { | |
this.rotate = 360; | |
this.dist = 0; | |
this.lastAngle = 0; | |
}; | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
robot.turn(this.rotate); | |
this.lastAngle = this.rotate; | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot, | |
scannedRobot = ev.scannedRobot, | |
turnAngle = this.getAngle(robot.position, scannedRobot.position); | |
if (robot.id == scannedRobot.parentId || robot.parentId == scannedRobot.id) { | |
return; | |
} | |
if (turnAngle < 0){ | |
turnAngle += 360; | |
}else if(turnAngle > 180){ | |
turnAngle -= 360; | |
} | |
this.rotate = robot.cannonAbsoluteAngle - turnAngle | |
this.dist = this.getDist(robot.position, ev.scannedRobot.position); | |
robot.fire(); | |
robot.clone(); | |
robot.ahead(this.dist/2); | |
}; | |
Robot.prototype.onRobotCollision = function(ev) { | |
var robot = ev.robot; | |
robot.back(5); | |
}; | |
Robot.prototype.getAngle = function(a,b){ | |
var tx = b.x - a.x, | |
ty = b.y - a.y, | |
rad = Math.atan2(ty,tx), | |
angle = (rad/Math.PI * 180); | |
return angle; | |
}; | |
Robot.prototype.getDist = function(a,b){ | |
return Math.sqrt((b.x - a.x) *(b.x - a.x) + (b.y - a.y) * (b.y - a.y)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment