Created
December 4, 2012 19:02
-
-
Save jaskolek/4207534 to your computer and use it in GitHub Desktop.
jaskolek3
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.roundCount = 0; | |
this.asyncFns = new Array(); | |
this.pauses = new Array(); | |
this.toCannonAngle = 270; | |
this.toAngle = 90; | |
this.speed = 2; | |
this.margin = 150; | |
this.scanV = 15; | |
this.shots = 0; | |
this.round = 0; | |
//robot.clone(); | |
this.addAsyncFn( "goForward", this.goForward); | |
this.addAsyncFn( "aimCenter", this.aimCenter ); | |
this.addAsyncFn( "scan", this.scan ); | |
this.addAsyncFn( "asyncTurn", this.asyncTurn ); | |
this.addAsyncFn( "asyncCannon", this.asyncCannon ); | |
this.addAsyncFn( "goAround", this.goAround ); | |
this.addAsyncFn( "autoShot", this.autoShot ); | |
}; | |
Robot.prototype.autoShot = function( ev, obj ){ | |
if( obj.shots > 0 && ev.robot.gunCoolDownTime == 0 ){ | |
ev.robot.fire(); | |
obj.shots --; | |
} | |
} | |
Robot.prototype.goForward = function( ev, obj ){ | |
if( obj.speed > 0 ){ | |
ev.robot.ahead( obj.speed ); | |
} | |
if( obj.speed < 0 ){ | |
ev.robot.back( -obj.speed ); | |
} | |
} | |
Robot.prototype.scan = function( ev, obj ){ | |
var addon = obj.round % (obj.scanV * 4 + 2); | |
if( addon > obj.scanV * 2 ){ | |
addon = obj.scanV * 3 - addon + 1; | |
}else{ | |
addon = addon - obj.scanV; | |
} | |
obj.toCannonAngle += addon; | |
} | |
Robot.prototype.aimCenter = function( ev, obj ){ | |
var robot = ev.robot; | |
var angle = Math.atan( ( ( robot.position.y - robot.arenaHeight / 2 ) / ( Math.abs( robot.position.x - robot.arenaWidth / 2 ) ) ) ) * 180 / Math.PI; | |
if( robot.position.x < robot.arenaWidth / 2 ){ | |
angle = 180 - angle; | |
} | |
obj.toCannonAngle = angle; | |
} | |
Robot.prototype.goAround = function( ev, obj ){ | |
var robot = ev.robot; | |
pause = true; | |
if( robot.position.x > robot.arenaWidth - obj.margin && obj.toAngle != 180 ){ | |
obj.toAngle = 180; | |
}else if( robot.position.x < obj.margin && obj.toAngle != 0 ){ | |
obj.toAngle = 0; | |
}else if( robot.position.y > robot.arenaHeight - obj.margin && obj.toAngle != 270 ){ | |
obj.toAngle = 270; | |
}else if( robot.position.y < obj.margin && obj.toAngle != 90 ){ | |
obj.toAngle = 90; | |
}else{ | |
pause = false; | |
} | |
if( pause == true ){ | |
obj.pauseAsyncFn( "goAround", Math.round( obj.margin / obj.speed ) * 1.4 ); | |
} | |
} | |
Robot.prototype.asyncCannon = function( ev, obj ){ | |
var robot = ev.robot; | |
var diff = robot.cannonAbsoluteAngle - ( obj.toCannonAngle % 360 ); | |
if( diff < -180 ) diff += 360; | |
if( diff > 180 ) diff -= 360; | |
if( diff < 0 ) robot.rotateCannon( 1 ); | |
if( diff > 0 ) robot.rotateCannon( -1 ); | |
} | |
Robot.prototype.asyncTurn = function( ev, obj ){ | |
var robot = ev.robot; | |
var diff = robot.angle - ( obj.toAngle % 360 ); | |
if( diff < -180 ) diff += 360; | |
if( diff > 180 ) diff -= 360; | |
if( diff < 0 ) robot.turn( 1 ); | |
if( diff > 0 ) robot.turn( -1 ); | |
} | |
Robot.prototype.pauseAsyncFn = function( name, rounds ){ | |
this.pauses[name] += rounds; | |
} | |
Robot.prototype.addAsyncFn = function( name, fn ){ | |
this.asyncFns[name] = fn; | |
this.pauses[name] = 0; | |
} | |
Robot.prototype.removeAsyncFn = function( name ){ | |
} | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
for( i in this.asyncFns ){ | |
if( this.pauses[i] == 0 ){ | |
this.asyncFns[i](ev, this); | |
}else{ | |
this.pauses[i]--; | |
} | |
} | |
this.round++; | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
this.shots = 3; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment