Created
December 3, 2012 22:45
-
-
Save matchu/4198827 to your computer and use it in GitHub Desktop.
Sweetie.Hunt
This file contains hidden or 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 LEFT = -1; | |
var RIGHT = 1; | |
//FightCode can only understand your robot | |
//if its class is called Robot | |
var Robot = function(robot) { | |
}; | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
this.initialize(robot); | |
if(this.hasTarget) { | |
this.clearTarget(); | |
robot.log("TARGET LOST"); | |
} | |
if(this.getIdleDirection(robot) == RIGHT) { | |
robot.turn(90); | |
} else { | |
robot.turn(-90); | |
} | |
robot.ahead(100); | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
var other = ev.scannedRobot; | |
if(other.parentId == robot.id || other.id == robot.parentId) { | |
return false; | |
} | |
var intent = this.getOthersIntent(robot, other); | |
var seekAngle = this.getAngleToOther(robot, other) - robot.angle; | |
if(this.hasTarget) { | |
this.updateTarget(other); | |
} else { | |
this.initTarget(other); | |
robot.log("TARGET ACQUIRED"); | |
} | |
if(this.targetMoving) { | |
robot.log("TARGET IN MOTION"); | |
} else { | |
robot.log("TARGET IS STABLE"); | |
} | |
robot.fire(); | |
robot.turn(seekAngle); | |
robot.ahead(60); | |
this.lastIntent = intent; | |
}; | |
Robot.prototype.onRobotCollision = function (ev) { | |
var robot = ev.robot; | |
if(ev.myFault) { | |
robot.back(20); | |
} | |
} | |
Robot.prototype.onWallCollision = function (ev) { | |
var robot = ev.robot; | |
robot.back(50); | |
} | |
Robot.prototype.getDistanceToOther = function (self, other) { | |
return Math.sqrt(Math.pow(Math.abs(self.position.x - other.position.x), 2) + | |
Math.pow(Math.abs(self.position.y - other.position.y), 2)); | |
} | |
Robot.prototype.getAngleToOther = function (self, other) { | |
var rawAngle = Math.atan2(-(other.position.y - self.position.y), | |
other.position.x - self.position.x) / Math.PI * 180; | |
//self.log("Deltas:", other.position.y - self.position.y, other.position.x - self.position.x); | |
//self.log("Raw angle:", rawAngle); | |
var transformedAngle = -(rawAngle - 90); | |
//self.log("Transformed angle:", transformedAngle); | |
return normalizeAngle(transformedAngle); | |
} | |
Robot.prototype.getOthersIntent = function (self, other) { | |
return normalizeAngle(other.angle - self.angle); | |
} | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot = ev.robot; | |
//robot.turn(ev.bearing); // Turn to wherever the bullet was fired | |
// so we can see who shot it | |
}; | |
Robot.prototype.clearTarget = function () { | |
this.hasTarget = false; | |
this.targetMoving = false; | |
this.targetLastPosition = null; | |
} | |
Robot.prototype.initTarget = function (other) { | |
this.hasTarget = true; | |
this.targetMoving = false; | |
this.targetLastPosition = other.position; | |
} | |
Robot.prototype.updateTarget = function (other) { | |
this.targetMoving = (this.targetLastPosition.x != other.position.x || | |
this.targetLastPosition.y != other.position.y); | |
this.targetLastPosition = other.position; | |
} | |
Robot.prototype.initialize = function (robot) { | |
if(typeof this.initialized === "undefined") { | |
console.log("INITIALIZE", robot.id); | |
robot.clone(); | |
this.lastIntent = null; | |
this.clearTarget(); | |
this.initialized = true; | |
} | |
} | |
Robot.prototype.getIdleDirection = function (robot) { | |
if(this.lastIntent === null) { | |
return (robot.parentId === null) ? RIGHT : LEFT; | |
} else { | |
return (this.lastIntent < 180) ? RIGHT : LEFT; | |
} | |
} | |
function normalizeAngle(angle) { | |
if(angle < 0) angle += 360; | |
if(angle >= 360) angle -= 360; | |
return angle; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment