Created
December 5, 2012 13:19
-
-
Save Adirelle/4215460 to your computer and use it in GitHub Desktop.
AdiBot
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 Robot = function(robot) { | |
}; | |
var areEnemies = function(robot, other) { | |
return other.id != robot.parentId && other.parentId != robot.id; | |
} | |
var normalizeAngle = function(a) { | |
return ((a % 360) + 360) % 360; | |
} | |
var calculateAngle = function(from, to) { | |
return normalizeAngle(Math.atan2(to.y - from.y, to.x - from.x) * 180 / Math.PI) | |
} | |
var calculateBearing = function(robot, to, currentAngle) { | |
currentAngle -= 90; | |
var angleToTarget = calculateAngle(robot.position, to); | |
var bearing = angleToTarget - currentAngle; | |
if(bearing > 180) { | |
bearing -= 360; | |
} else if(bearing < -180) { | |
bearing += 360; | |
} | |
return bearing; | |
} | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
if(robot.availableClones > 0) { | |
robot.clone(); | |
robot.back(50); | |
robot.turn(-90); | |
return; | |
} | |
var px = 100 * robot.position.x / robot.arenaWidth; | |
var py = 100 * robot.position.y / robot.arenaHeight; | |
if(px < 10 || px > 90 || py < 10 || py > 90) { | |
robot.turn(30); | |
robot.ahead(20); | |
} else { | |
robot.turn(60 * (Math.random() - 0.5)); | |
robot.ahead(100); | |
} | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
var other = ev.scannedRobot; | |
if(areEnemies(robot, other)) { | |
if(robot.gunCoolDownTime <= 1) { | |
robot.fire(); | |
} else{ | |
var cannonBearing = 90-robot.cannonRelativeAngle; | |
if(cannonBearing != 0) { | |
robot.rotateCannon(90-robot.cannonRelativeAngle); | |
} else { | |
var bearing = calculateBearing(robot, other.position, robot.angle); | |
if(bearing != 0) { | |
robot.turn(bearing); | |
} else { | |
robot.ahead(50); | |
} | |
} | |
} | |
} | |
}; | |
Robot.prototype.onWallCollision = function(ev) { | |
var robot = ev.robot; | |
robot.back(50); | |
robot.turn(60); | |
}; | |
Robot.prototype.onRobotCollision = function(ev) { | |
var robot = ev.robot; | |
var other = ev.collidedRobot; | |
if(areEnemies(robot, other)) { | |
var bearing = calculateBearing(robot, other.position, robot.cannonAbsoluteAngle); | |
robot.rotateCannon(bearing); | |
robot.fire(); | |
} | |
robot.back(50); | |
robot.turn(-30); | |
}; | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot; | |
robot = ev.robot; | |
robot.turn(90 - ev.bulletBearing); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment