Created
December 4, 2012 11:52
-
-
Save Albitos/4203011 to your computer and use it in GitHub Desktop.
AlbioN
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 Angle = function() | |
{ | |
return { | |
points : function(from, to) | |
{ | |
deltaY = from.y - to.y; | |
deltaX = from.x - to.x; | |
var angle = Math.atan2(deltaY, deltaX) * 180 / Math.PI; | |
return angle; | |
}, | |
diff : function(a, b) | |
{ | |
} | |
}; | |
}; | |
ANGLE = new Angle(); | |
ROBOTS = {}; | |
$ = function(robot) | |
{ | |
extRobot = {}; | |
if(ROBOTS[robot.id] !== undefined) | |
extRobot = ROBOTS[robot.id]; | |
else | |
{ | |
extRobot.target = null; | |
} | |
for(key in robot) | |
extRobot[key] = robot[key]; | |
extRobot.turnSteady = function(angle) | |
{ | |
var a = 1; | |
if(angle < 0) | |
{ | |
a = -1; | |
angle = -angle; | |
} | |
for(i = angle; i >= 0; i--) | |
{ | |
this.turn(a*1); | |
this.rotateCannon(a*-1); | |
this.ahead(10); | |
} | |
return extRobot; | |
}; | |
extRobot.turnSteadyAround = function(angle) | |
{ | |
var a = 1; | |
if(angle < 0) | |
{ | |
a = -1; | |
angle = -angle; | |
} | |
for(i = angle; i >= 0; i--) | |
{ | |
this.turn(a*1); | |
this.rotateCannon(a*-1); | |
} | |
return extRobot; | |
}; | |
extRobot.face = function(angle) | |
{ | |
var a2 = (angle - this.angle - 90); | |
this.turnSteadyAround(a2); | |
}; | |
extRobot.turnTo = function(angle) | |
{ | |
this.turn(-this.angle+angle); | |
}; | |
ROBOTS[robot.id] = extRobot; | |
return extRobot; | |
}; | |
function Robot(robot) { | |
if(robot.parentId == null) | |
{ | |
robot.clone(); | |
robot.ahead(50); | |
} | |
} | |
// well, we need to do something... | |
// whenever our robot is idle, this method gets called. | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
if($(robot).target == null) | |
robot.rotateCannon(360); | |
else | |
{ | |
$(robot).ahead(50); | |
$(robot).rotateCannon(30); | |
$(robot).rotateCannon(-60); | |
$(robot).rotateCannon(30); | |
} | |
}; | |
// this method gets called whenever we hit another robot... | |
Robot.prototype.onRobotCollision = function(ev) {}; | |
// this method gets called whenever we hit a wall... | |
Robot.prototype.onWallCollision = function(ev) {}; | |
// yay we see another robot! time to wreak some havoc... | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
var enemy = ev.scannedRobot; | |
if(robot.id != enemy.parentId && robot.parentId != enemy.id) | |
{ | |
$(robot).fire(); | |
if($(robot).target == null) | |
{ | |
robot.log('Target found - facing it...'); | |
robot.stop(); | |
$(robot).fire(); | |
$(robot).target = enemy; | |
$(robot).face(robot.cannonAbsoluteAngle); | |
} | |
else | |
{ | |
robot.stop(); | |
$(robot).fire(); | |
$(robot).face(robot.cannonAbsoluteAngle); | |
} | |
} | |
}; | |
Robot.prototype.onRobotCollision = function(ev) { | |
var robot = ev.robot; | |
robot.stop(); | |
robot.back(50); // trying to run away | |
}; | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot = ev.robot; | |
robot.stop(); | |
robot.turn(ev.bearing); // Turn to wherever the bullet was fired | |
// so we can see who shot it | |
}; | |
Robot.prototype.onWallCollision = function(ev) { | |
var robot = ev.robot; | |
robot.stop(); | |
robot.back(10); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment