Skip to content

Instantly share code, notes, and snippets.

@michal-gil
Forked from nargil/robot.js
Created December 4, 2012 15:14
Show Gist options
  • Save michal-gil/4205043 to your computer and use it in GitHub Desktop.
Save michal-gil/4205043 to your computer and use it in GitHub Desktop.
Cylonica
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
var Robot = function(robot) {
robot.clone();
};
var globalEnemy=null;
var RobotData = new Object();
RobotData.TurnSpeed = 1;
RobotData.Vel = 2;
RobotData.CanonRot = 10;
var CloneData = clone(RobotData);
CloneData.TurnSpeed= -1;
function walk(robot, data)
{
robot.ahead(data.Vel);
robot.turn(data.TurnSpeed);
}
function aim(robot, data)
{
var angle = data.TurnSpeed;
if(globalEnemy!=null)
{
var y = globalEnemy.position.y - robot.position.y;
var x = globalEnemy.position.x - robot.position.x;
var enemyAngle = Math.atan2(y,x);
enemyAngle = enemyAngle * 180 / 3.14159 + 180;
angle = enemyAngle - robot.cannonAbsoluteAngle;
if(angle>180)
angle=-360 + angle;
}
robot.rotateCannon(angle);
}
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
var data;
if(robot.parentId==null)
{
data=RobotData;
}
else
{
data=CloneData;
}
walk(robot,data);
aim(robot,data);
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
var enemy = ev.scannedRobot;
if(enemy.parentId==null && (enemy.id!=robot.parentId || enemy.parentId==null && robot.parentId==null))
{
globalEnemy = enemy;
robot.stop();
robot.fire();
}
};
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
var data;
if(robot.parentId==null)
{
data=RobotData;
}
else
{
data=CloneData;
}
data.TurnSpeed = -data.TurnSpeed;
//robot.stop();
robot.back(10);
robot.turn(-ev.bearing); // turn enought to be in a straight
// angle with the wall.
};
Robot.prototype.onRobotCollision = function(ev) {
var robot = ev.robot;
robot.back(20); // trying to run away
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment