Created
December 3, 2012 17:47
-
-
Save loktar00/4196687 to your computer and use it in GitHub Desktop.
Grunt
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
//FightCode can only understand your robot | |
//if its class is called Robot | |
var Robot = function(robot) { | |
this.rotate = 1; | |
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; | |
var turnAngle = this.getAngle(robot.position, ev.scannedRobot.position); | |
if (turnAngle < -180){ | |
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.ahead(this.dist/2); | |
}; | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot = ev.robot; | |
this.rotate = ev.bearing; | |
robot.turn(ev.bearing/2); | |
robot.ahead(15); | |
}; | |
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