Skip to content

Instantly share code, notes, and snippets.

@acelan
Created December 6, 2012 03:41
Show Gist options
  • Save acelan/4221617 to your computer and use it in GitHub Desktop.
Save acelan/4221617 to your computer and use it in GitHub Desktop.
Alpha01
var mainTank= {};
var cloneTank= {};
var curTank= {};
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(100);
};
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(20);
robot.ahead(20);
}
};
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.stop();
robot.fire();
if( target.angle < 10)
{
robot.turn(10);
robot.ahead(20);
robot.rotateCannon(-50);
}
else if( target.angle > 170)
{
robot.turn(10);
robot.ahead(20);
robot.rotateCannon(-50);
}
else
{
robot.rotateCannon(-20);
robot.ahead(20);
}
}
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.cannonRotate(robot.cannonAbsoluteAngle+target.angle);
robot.fire();
}
};
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
var turn = ev.bearing-90;
if( turn > 180)
turn= turn- 180;
robot.ignore('onHitByBullet');
robot.back(30);
robot.turn(turn);
robot.rotateCannon(-turn);
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.bearing < 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