Skip to content

Instantly share code, notes, and snippets.

@kjaku
Forked from jaskolek/robot.js
Created December 4, 2012 17:46
Show Gist options
  • Save kjaku/4206775 to your computer and use it in GitHub Desktop.
Save kjaku/4206775 to your computer and use it in GitHub Desktop.
jaskolek
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
this.stepFn = this.goToWall;
this.roundCount = 0;
};
Robot.prototype.moveCannonToCenter = function( ev ){
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;
}
var addon = this.roundCount % 66;
if( addon >= 33 ){
addon = 48 - addon;
}else{
addon = addon - 15;
}
angle += addon;
angle = ( angle + 360 )% 360;
var toRotate = -robot.cannonAbsoluteAngle + angle;
if( toRotate > 180 ) toRotate -= 360;
if( toRotate < -180 ) toRotate += 360;
//if( angle - robot.cannonAbsoluteAngle > 180 ) angle -= 360;
robot.rotateCannon( toRotate );
}
Robot.prototype.goAround = function( ev ){
var robot = ev.robot;
this.moveCannonToCenter( ev );
//this.autoRotateCannon( ev );
robot.ahead( 1 );
this.roundCount++;
//if( this.fireCount % 10 == 0 ) ev.robot.fire();
if(
( ev.robot.position.x > ev.robot.arenaWidth - 132 && ev.robot.position.y < 20 )
|| ( ev.robot.position.x > ev.robot.arenaWidth - 20 && ev.robot.position.y > robot.arenaHeight - 132 )
|| ( ev.robot.position.x < 132 && ev.robot.position.y > robot.arenaHeight - 20 )
|| ( ev.robot.position.x < 20 && ev.robot.position.y < 132 )
){
this.softTurn( ev, 90, 1, 2 );
}
}
Robot.prototype.goToWall = function( ev ){
var robot = ev.robot;
robot.turn( -robot.angle );
//robot.clone();
robot.ahead( robot.position.y );
robot.turn( 90 );
robot.rotateCannon( 90 );
this.stepFn = this.goAround;
}
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
this.stepFn(ev);
};
Robot.prototype.onScannedRobot = function(ev) {
ev.robot.fire();
};
Robot.prototype.softTurn = function( ev, angle, angleStep, moveSpeed ){
for( i = 0; i < angle; i += angleStep ){
ev.robot.turn( angleStep );
//ev.robot.rotateCannon( -angleStep );
ev.robot.ahead( moveSpeed );
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment