//originally compiled with this online compiler: http://hossy.net/sssc.php //thanks to hossy.net class BattleTanks { var tanks; var map; var count; var max_speed; var friction; var braking; var accel; var turnspeed; var bounce_factor; var tankscale; var num_enemies; var bullet_speed; var damage; var explosions; var num_particles; function BattleTanks(path) { //define global variable defineGlobals(path); //create map // 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 map = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], //1 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], //2 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], //3 [1,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1], //4 [1,0,0,0,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1], //5 [1,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1], //6 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], //7 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], //8 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], //9 [1,0,0,0,0,0,0,0,1,1,1,1,1,1,1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,0,0,0,0,0,0,0,0,1], //4 [1,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1], //5 [1,0,0,0,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1], //6 [1,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,1], //7 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], //8 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], //9 [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]];//0 //draw map drawMap(map,path); //create tanks tanks.push(newTank(path,true,map,3,3,90)); tanks.push(newTank(path,false,map,37,3,180)); tanks.push(newTank(path,false,map,37,37,-90)); tanks.push(newTank(path,false,map,3,37,0)); /*tanks.push(newTank(path,false,map,14,15,90)); tanks.push(newTank(path,false,map,24,15,180)); tanks.push(newTank(path,false,map,24,25,-90)); tanks.push(newTank(path,false,map,14,25,0));*/ } function elevateTankDepth() { for (var i = 0; i0) { this._alpha -= 7; } else { this._x = this.sx*10; this._y = this.sy*10; this._rotation = this.mov_angle = this.sr this.turret._rotation = 0; this.vel = 0; ref.calculateTreads(tank); this.mine_count = 0; this.mine = false; this.max_speed = ref.max_speed; this.turnspeed = ref.turnspeed; this.mine_rate = ref.mine_rate; this.shoot_rate = ref.shoot_rate; //apply key controls if tank is player controlled if (player) { this.shot = false; this.onEnterFrame = function() { if (this._alpha<100) { this._alpha += 5; } else { this.alive = true; } ref.calculateTreads(this); ref.drawTreads(this); this.vel *= ref.friction; //get rid of errors caused by decimal anomalies this.vel = Math.floor(this.vel*10)/10; if (Key.isDown(Key.UP)) { //acceleration style 2 this.vel = Math.min(this.max_speed,this.vel+ref.accel); } if (Key.isDown(Key.DOWN)) { //acceleration style 2 this.vel = Math.max(-this.max_speed,this.vel-ref.accel); } if (Key.isDown(Key.LEFT)) { this._rotation -= this.turnspeed; } if (Key.isDown(Key.RIGHT)) { this._rotation += this.turnspeed; } //turret rotation if (Key.isDown(65)) { this.turret._rotation -= this.turnspeed; } if (Key.isDown(68)) { this.turret._rotation += this.turnspeed; } //shooting if (Key.isDown(Key.SPACE)) { if (this.shot == false) { this.shoot(); this.shot = true; } } else { this.shot = false; } //reverse direction if (Key.isDown(83)) { if (this.reverse == false) { this.reverseDirection(); this.reverse = true; } } else { this.reverse = false; } //place a mine if(this.mine_count > this.mine_rate) { if(Key.isDown(87)) { if(this.mine == false) { this.placeMine(); this.mine_count = 0; this.mine = true; } } else { this.mine = false; } } else { this.mine_count++; } //movement calculations var xmov = Math.cos(this._rotation*(Math.PI/180))*this.vel; var ymov = Math.sin(this._rotation*(Math.PI/180))*this.vel; //wall collision ob = new Object(); ob.clip = this; ob.height = this.body._height/2; ob.width = this.body._width/2; ob.x = this._x; ob.y = this._y; ob.xtile = Math.floor(this._x/10); ob.ytile = Math.floor(this._y/10); //movement ref.getCorners(this._x,this._y+ymov,ob); if (ymov<0) { if (ob.upleft && ob.upright) { ob.y += ymov; } else { ob.y = ob.ytile*10+ob.height; } } if (ymov>0) { if (ob.downleft && ob.downright) { ob.y += ymov; } else { ob.y = (ob.ytile+1)*10-ob.height; } } ref.getCorners(this._x+xmov,this._y,ob); if (xmov<0) { if (ob.upleft && ob.downleft) { ob.x += xmov; } else { ob.x = ob.xtile*10+ob.width; } } else if (xmov>0) { if (ob.upright && ob.downright) { ob.x += xmov; } else { ob.x = (ob.xtile+1)*10-ob.width; } } this._x = ob.x; this._y = ob.y; } ; } else { this.count = Math.floor(Math.random()*15); this.shoot_count = 0; this.los = -1; this.decel = 0; this.mov_angle = this._rotation; this.chase = 0; this.onEnterFrame = function() { var xdist,ydist,dist,angle,xam,yam,ca,sa,tx,ty,dif,rot; if (this._alpha<100) { this._alpha += 5; } else { this.alive = true; } this.count++; this.mine_count++; if(this.count > 2999) { this.count = 0; } //draw tread marks ref.calculateTreads(this); ref.drawTreads(this); //calculate current position this.scx = Math.floor(this._x/10); this.scy = Math.floor(this._y/10); ca = Math.cos(this._rotation*(Math.PI/180)); sa = Math.sin(this._rotation*(Math.PI/180)); // //rotate turret if line of site // if (ref.tanks[this.los].alive == false) { this.los = -1; } this.olos = this.los; if (this.los > -1 && this.count % 15 == 0) { this.los_angle = ref.lineOfSight(this,ref.tanks[this.los]); if(this.los_angle == false) { this.los = -1; } } else if(this.los == -2 && (this.count-1) % 15 == 0) { if(ref.powerups[this.pwup]._visible == true) { this.los_angle = ref.lineOfSight(this,ref.powerups[this.pwup]); if(this.los_angle == false) { this.los = -1; } } } if (this.los < 0) { for (var k = 0; k -1 && this.chase == 0) { dif = Math.abs(this.los_angle-this._rotation); if(dif > 90 && dif < 270) { this.chase = -1; //run } else { this.chase = 1; //chase } } else if(this.los == -2) { this.chase = 1; } else if(this.los == -1) { this.chase = 0; } if(this.chase == -1) { this.mov_angle = this.los_angle+180; } else if(this.chase == 1) { this.mov_angle = this.los_angle; } if(this.los == -1) { this.mov_angle += Math.random()*15-7.5; } // //follow random path // if (Math.abs(this.mov_angle-this._rotation)>1) { if (this.mov_angle<0) { this.mov_angle += 360; } else if (this.mov_angle>360) { this.mov_angle -= 360; } this.rot = (this._rotation); if (this.rot<0) { this.rot += 360; } else if (rot>360) { this.rot -= 360; } dif = Math.abs(this.mov_angle-this.rot) if(this.rot > 0 && this.rot <= 90) { if(this.mov_angle > 180+this.rot || this.mov_angle < this.rot) { this._rotation -= Math.min(dif,this.turnspeed); } else { this._rotation += Math.min(dif,this.turnspeed); } } else if(this.rot > 90 && this.rot <= 180) { this.rot -= 90; if(this.mov_angle < this.rot+90 || this.mov_angle > 360-this.rot) { this._rotation -= Math.min(dif,this.turnspeed); } else { this._rotation += Math.min(dif,this.turnspeed); } } else if(this.rot > 180 && this.rot <= 270) { this.rot -= 180; if(this.mov_angle < this.rot+180 && this.mov_angle > this.rot) { this._rotation -= Math.min(dif,this.turnspeed); } else { this._rotation += Math.min(dif,this.turnspeed); } } else { this.rot -= 270; if(this.mov_angle > 270+this.rot || this.mov_angle < 90+this.rot) { this._rotation += Math.min(dif,this.turnspeed); } else { this._rotation -= Math.min(dif,this.turnspeed); } } if(this._rotation < 0) { this._rotation += 360; } else if(this._rotation > 360) { this._rotation -= 360; } } dif = Math.abs(this.los_angle-this.turret._rotation+this._rotation); if (dif>3) { if (this.los_angle<0) { this.los_angle += 360; } else if (this.los_angle>360) { this.los_angle -= 360; } this.rot = (this.turret._rotation+this._rotation); if (this.rot<0) { this.rot += 360; } else if (rot>360) { this.rot -= 360; } dif = Math.abs(this.los_angle-this.rot); if(this.rot > 0 && this.rot <= 90) { if(this.los_angle > 180+this.rot || this.los_angle < this.rot) { this.turret._rotation -= Math.min(dif,this.turnspeed); } else { this.turret._rotation += Math.min(dif,this.turnspeed); } } else if(this.rot > 90 && this.rot <= 180) { this.rot -= 90; if(this.los_angle < this.rot+90 || this.los_angle > 360-this.rot) { this.turret._rotation -= Math.min(dif,this.turnspeed); } else { this.turret._rotation += Math.min(dif,this.turnspeed); } } else if(this.rot > 180 && this.rot <= 270) { this.rot -= 180; if(this.los_angle < this.rot+180 && this.los_angle > this.rot) { this.turret._rotation -= Math.min(dif,this.turnspeed); } else { this.turret._rotation += Math.min(dif,this.turnspeed); } } else { this.rot -= 270; if(this.los_angle > 270+this.rot || this.los_angle < 90+this.rot) { this.turret._rotation += Math.min(dif,this.turnspeed); } else { this.turret._rotation -= Math.min(dif,this.turnspeed); } } if(this.turret._rotation < 0) { this.turret._rotation += 360; } else if(this.turret._rotation > 360) { this.turret._rotation -= 360; } } this.shoot_count++; if (dif<15 && this.shoot_count % 5 == 0 && ref.tanks[this.los].alive == true) { this.shoot(); } //obstacle avoidance x = this.rbx+ca*5; y = this.rby+sa*5; this.rcol = 0; this.lcol = 0; for (var k = 6; k>0; k--) { if (ref.map[Math.floor(y/10)][Math.floor(x/10)] == 1) { this.rcol = k; break; } x += ca*3; y += sa*3; } x = this.lbx+ca*5; y = this.lby+sa*5; for (var k = 6; k>0; k--) { if (ref.map[Math.floor(y/10)][Math.floor(x/10)] == 1) { this.lcol = k; break; } x += ca*4; y += sa*4; } var mult,mult2; if(this.rcol > this.lcol) { mult = this.rcol*1.3; mult2 = this.lcol/-1.5; } else if(this.lcol > this.rcol) { mult = this.lcol*-1.3; mult2 = this.rcol/1.5; } this._rotation += this.turnspeed*mult; //this._rotation += this.turnspeed*mult2; this.mov_angle = this._rotation; if(this.rcol > 0 && this.lcol > 0) { if(this.col_count++ > 30) { this.decel = 0; this.reverseDirection(); this.col_count = 0; } } else { this.col_count = 0; } this.vel = Math.min(this.max_speed,this.vel+ref.accel); // //rotation/movement // //movement //movement calculations this.vel *= ref.friction; this.vel = Math.floor(this.vel*10)/10; var xmov = ca*this.vel; var ymov = sa*this.vel; //wall collision ob = new Object(); ob.clip = this; ob.height = this.body._height/2; ob.width = this.body._width/2; ob.x = this._x; ob.y = this._y; ob.xtile = Math.floor(this._x/10); ob.ytile = Math.floor(this._y/10); //movement ref.getCorners(this._x,this._y+ymov,ob); if (ymov<0) { if (ob.upleft && ob.upright) { ob.y += ymov; } else { ob.y = ob.ytile*10+ob.height+1; this.collide(); } } if (ymov>0) { if (ob.downleft && ob.downright) { ob.y += ymov; } else { ob.y = (ob.ytile+1)*10-ob.height-1; this.collide(); } } ref.getCorners(this._x+xmov,this._y,ob); if (xmov<0) { if (ob.upleft && ob.downleft) { ob.x += xmov; } else { ob.x = ob.xtile*10+ob.width+1; this.collide(); } } else if (xmov>0) { if (ob.upright && ob.downright) { ob.x += xmov; } else { ob.x = (ob.xtile+1)*10-ob.width-1; this.collide(); } } this._x = ob.x; this._y = ob.y; } tank.collide = function() { //this.reverseDirection(); } } tank.reverseDirection = function() { this._rotation += 180; this.turret._rotation += 180; this.mov_angle += 180+Math.random()*90-45; this.vel *= -1; ref.calculateTreads(this); } tank.shoot = function() { var bullet = this._parent.createEmptyMovieClip("bullet"+(ref.count++),ref.count); ref.elevateTankDepth(); var angle = this.turret._rotation+this._rotation; var ca = Math.cos(angle*(Math.PI/180)); var sa = Math.sin(angle*(Math.PI/180)); bullet._x = ca*9+this._x; bullet._y = sa*9+this._y; bullet.xspeed = ca*ref.bullet_speed; bullet.yspeed = sa*ref.bullet_speed; bullet._rotation = angle; bullet.parent = this; ref.drawBox(bullet,1,1,0,0,0x000000); bullet.onEnterFrame = function() { this._x += this.xspeed; this._y += this.yspeed; if (ref.map[Math.floor(this._y/10)][Math.floor(this._x/10)] == 1 || this._x>400 || this._x<0 || this._y>400 || this._y<0) { this.removeMovieClip(); } else { for (var i in ref.tanks) { if (ref.tanks[i].hitTest(this) && ref.tanks[i] != this.parent && ref.tanks[i].alive) { ref.tanks[i].die(); this.removeMovieClip(); } else { if(ref.lineOfSight(this,ref.tanks[i],this.xpeed,this.yspeed) != false) { ref.tanks[i].bullet_alert = this; } } } } } } } } } tank.die(); return tank; } public static function main () { var b = new BattleTanks(_root); } }