Created
December 8, 2012 13:38
-
-
Save ghusse/4240299 to your computer and use it in GitHub Desktop.
Roborator
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 scans = 4, | |
scansDegrees = 10, | |
scanIncrements = 5; | |
//FightCode can only understand your robot | |
//if its class is called Robot | |
var Robot = function(robot) { | |
if (robot.availableClones > 0){ | |
robot.clone(); | |
robot. | |
} | |
robot.ennemyAngle = 0; | |
robot.ennemyScans = 0; | |
}; | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
if (robot.ennemyAngle !== null && robot.ennemyScans == 0){ | |
robot.ennemyAngle = null; | |
} | |
if (robot.ennemyAngle === null){ | |
robot.rotateCannon(360); | |
if (this.r()){ | |
robot.turn(13); | |
} else { | |
robot.turn(-17); | |
} | |
}else{ | |
this.rotateAndScan(robot); | |
robot.ennemyScans--; | |
robot.ahead(1); | |
} | |
}; | |
Robot.prototype.r = function(){ | |
return Math.random() > 0.5; | |
}; | |
Robot.prototype.rotateAndScan = function(robot){ | |
var angle = robot.ennemyAngle - robot.cannonAbsoluteAngle; | |
var increment = (scans - robot.ennemyScans) * scanIncrements; | |
angle = angle < 0 ? angle - scansDegrees - increment : angle + scansDegrees + increment; | |
robot.rotateCannon(angle); | |
}; | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot = ev.robot; | |
var angle = ev.bearing + robot.absoluteAngle; | |
robot.ennemyAngle = angle; | |
this.rotateAndScan(robot, angle); | |
this.turnAndPreserveCannon(robot, ev.bearing); | |
robot.ahead(100); | |
}; | |
Robot.prototype.onRobotCollision = function(ev){ | |
var robot = ev.robot; | |
if (this.canShootRobot(ev.collidedRobot, robot)){ | |
robot.rotateCannon(ev.collidedRobot.cannonAngle); | |
} | |
robot.back(10); | |
this.turnAndPreserveCannon(robot, ev.bearing + 90); | |
robot.ahead(100); | |
}; | |
Robot.prototype.turnAndPreserveCannon = function(robot, angle){ | |
if (this.r()){ | |
angle = -angle; | |
} | |
robot.turn(angle); | |
robot.rotateCannon(-angle); | |
if (robot.ennemyAngle !== null){ | |
robot.ennemyAngle = robot.ennemyAngle - angle; | |
} | |
}; | |
Robot.prototype.onWallCollision = function(ev) { | |
var robot = ev.robot, | |
angle; | |
this.turnAndPreserveCannon(robot, ev.bearing + 90); | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
if (this.canShootRobot(ev.scannedRobot, robot)){ | |
robot.stop(); | |
robot.fire(); | |
robot.ennemyAngle = robot.cannonAbsoluteAngle; | |
robot.ennemyScans = scans; | |
if (Math.random() > 0.5){ | |
robot.rotateCannon(scansDegrees); | |
}else{ | |
robot.rotateCannon(-scansDegrees); | |
} | |
} | |
}; | |
Robot.prototype.canShootRobot = function(testedRobot, robot){ | |
return (testedRobot.id != robot.parentId && testedRobot.parentId != robot.id); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment