-
-
Save p7o/4250348 to your computer and use it in GitHub Desktop.
Destroyers
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 mainTank= {}; | |
var cloneTank= {}; | |
var curTank= {}; | |
var turnStep = 40; | |
var rotateStep = 15; | |
var goStep = 15; | |
function updateTank(robot) | |
{ | |
getCurTank( robot); | |
curTank.id= robot.id; | |
curTank.parentId= robot.parentId; | |
curTank.position= robot.position; | |
setCurTank( robot); | |
} | |
function getCurTank(robot) | |
{ | |
if ( robot.parentId) | |
curTank= mainTank; | |
else | |
curTank= cloneTank; | |
} | |
function setCurTank(robot) | |
{ | |
if ( robot.parentId) | |
mainTank= curTank; | |
else | |
cloneTank= curTank; | |
} | |
//FightCode can only understand your robot | |
//if its class is called Robot | |
var Robot = function(robot) { | |
mainTank = this; | |
robot.clone(); | |
robot.turn(90); | |
robot.back(50); | |
}; | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
if ( robot.gunCoolDownTime > 0 ) | |
{ | |
robot.turn(10); | |
robot.ahead(10); | |
} | |
else | |
{ | |
robot.listen('onRobotCollision'); | |
robot.listen('onHitByBullet'); | |
robot.listen('onWallCollision'); | |
getCurTank( robot); | |
curTank.is_firing= false; | |
robot.rotateCannon(rotateStep); | |
robot.ahead(10); | |
} | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
var target = ev.scannedRobot; | |
if( target.parentId != robot.id | |
&& target.id != robot.parentId) | |
{ | |
robot.ignore('onRobotCollision'); | |
robot.ignore('onHitByBullet'); | |
robot.ignore('onWallCollision'); | |
updateTank( robot); | |
curTank.is_firing= true; | |
setCurTank( robot); | |
robot.fire(); | |
if( target.angle < 10) | |
{ | |
robot.turn(30); | |
robot.rotateCannon(-50); | |
robot.ahead(); | |
} | |
else if( target.angle > 170) | |
{ | |
robot.turn(30); | |
robot.rotateCannon(-50); | |
robot.ahead(); | |
} | |
else | |
{ | |
robot.rotateCannon(-20); | |
robot.ahead(10); | |
} | |
} | |
else | |
{ | |
robot.cannonRotate(10); | |
getCurTank( robot); | |
if( !curTank.is_firing) | |
robot.cannonRotate(-10); | |
} | |
}; | |
Robot.prototype.onRobotCollision = function(ev) { | |
var robot = ev.robot; | |
var target = ev.collidedRobot; | |
if( target.parentId == robot.id) | |
{ | |
robot.back(50); | |
robot.turn(ev.bearing); | |
} | |
else if( target.id == robot.parentId) | |
{ | |
robot.back(100); | |
robot.turn(-ev.bearing); | |
} | |
else | |
{ | |
robot.rotateCannon(-target.cannonAngle-5); | |
robot.fire(); | |
} | |
}; | |
Robot.prototype.onWallCollision = function(ev) { | |
var robot = ev.robot; | |
robot.ignore('onHitByBullet'); | |
robot.back(30); | |
robot.turn(90-ev.bearing); | |
robot.rotateCannon(ev.bearing-90); | |
robot.listen('onHitByBullet'); | |
}; | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot = ev.robot; | |
var targetAngle= ev.bearing+robot.cannonRelativeAngle; | |
robot.ignore('onHitByBullet'); | |
getCurTank(robot); | |
if( curTank.is_firing) | |
{ | |
robot.listen('onHitByBullet'); | |
return; | |
} | |
robot.stop(); | |
if((ev.bulletBearing < 10) | |
|| (ev.bearing > 170)) | |
robot.turn(20); | |
robot.rotateCannon(targetAngle); | |
robot.ahead(50); | |
robot.listen('onHitByBullet'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment