PageRenderTime 65ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/tanks_No_Patterns/src/b2_ludus/b2_tank.as

https://github.com/lbonilla/Ludus_tank
ActionScript | 521 lines | 383 code | 63 blank | 75 comment | 49 complexity | daa230d41157966776b03be39c819117 MD5 | raw file
  1. package b2_ludus
  2. {
  3. import Box2D.Collision.Shapes.b2PolygonShape;
  4. import Box2D.Common.Math.b2Vec2;
  5. import Box2D.Dynamics.b2Body;
  6. import Box2D.Dynamics.b2BodyDef;
  7. import Box2D.Dynamics.b2FixtureDef;
  8. import Box2D.Dynamics.b2World;
  9. import flash.display.DisplayObject;
  10. import flash.display.MovieClip;
  11. import flash.display.Sprite;
  12. import flash.events.TimerEvent;
  13. import flash.filters.DropShadowFilter;
  14. import flash.geom.Point;
  15. import flash.utils.Timer;
  16. /**
  17. * @author Ludus Team
  18. */
  19. //this class is on charge of creating the tank.
  20. public class b2_tank extends Sprite
  21. {
  22. private var globals:b2_globals = new b2_globals();
  23. private var world:b2_world;
  24. private var xpos:Number;
  25. private var ypos:Number;
  26. private var track_xpos:Number;
  27. private var track_ypos:Number;
  28. //bodies
  29. private var _tank:b2Body;
  30. private var _rightWheel:b2Body;
  31. private var _leftWheel:b2Body;
  32. private var _bullet:b2Body;
  33. //mcs
  34. private var _tank_mc:MovieClip;
  35. private var _rightWheel_mc:Sprite;
  36. private var _leftWheel_mc:Sprite;
  37. private var _bullet_mc:Sprite;
  38. //shooting variables
  39. private var _shootCannonRight:Boolean = true;
  40. private var _shootCannonLeft:Boolean = false;
  41. private var bullet_ready:Boolean = true;
  42. private var bullet_on_stage:Boolean = false;
  43. private var bullet_damage:int = 25;
  44. private var cant_bullet_shoot:int = 0;
  45. private var myTimer:Timer;
  46. private var bulletforce:b2Vec2;
  47. private var bulletSpeed:Number = 20;
  48. private var charingTime:int = 5;
  49. private var _cant_bullet:int = 10;
  50. private var _shootRLRight:Boolean = true;
  51. private var _shootRLLeft:Boolean = false;
  52. private var launchPos:b2Vec2 = new b2Vec2(0, 0);
  53. private var launchForce:b2Vec2;
  54. private var rocket_ready:Boolean = true;
  55. private var rocket_on_stage:Boolean = false;
  56. private var rocket_damage:int = 15;
  57. private var cant_rocket_shoot:int = 0;
  58. private var cant_rocket:int = 5;
  59. //tank information
  60. private var controlPanel:b2_controlPanel;
  61. private var healthbar:b2_healthbar;
  62. private var type:String;
  63. private var _life:int = 100;
  64. public function b2_tank(type:int = 0){
  65. //hit_tank = new b2_contact();
  66. if(type == 1){
  67. //red tank
  68. this.type = "red";
  69. _tank_mc = new redTank_mc();
  70. _rightWheel_mc = new redWheel_mc();
  71. _leftWheel_mc = new redWheel_mc();
  72. _bullet_mc = new redBullet_mc();
  73. controlPanel = new b2_controlPanel(0xff3636);
  74. controlPanel.x = 200;
  75. controlPanel.y = 5;
  76. healthbar = new b2_healthbar();
  77. }else if(type == 2){
  78. //blue tank
  79. this.type = "blue";
  80. _tank_mc = new blueTank_mc();
  81. _rightWheel_mc = new blueWheel_mc();
  82. _leftWheel_mc = new blueWheel_mc();
  83. _bullet_mc = new blueBullet_mc();
  84. controlPanel = new b2_controlPanel(0x36bfff);
  85. controlPanel.x = 285;
  86. controlPanel.y = 5;
  87. healthbar = new b2_healthbar();
  88. }
  89. //drop shadow
  90. /*
  91. var dropShadow:DropShadowFilter = new DropShadowFilter();
  92. dropShadow.distance = 20;
  93. dropShadow.angle = 10;
  94. dropShadow.color = 0x000000;
  95. dropShadow.alpha = 1;
  96. dropShadow.blurX = 1.75;
  97. dropShadow.blurY = 1.75;
  98. dropShadow.strength = 0.35;
  99. dropShadow.quality = 15;
  100. dropShadow.inner = false;
  101. dropShadow.knockout = false;
  102. dropShadow.hideObject = false;
  103. tank_mc.filters = new Array(dropShadow);
  104. */
  105. //addChild(cannon_mc);
  106. //addChild(rl_mc);
  107. addChild(rightWheel_mc);
  108. addChild(leftWheel_mc);
  109. addChild(tank_mc);
  110. addChild(controlPanel);
  111. addChild(healthbar);
  112. }
  113. public function createTank(world:b2_world, xpos:Number, ypos:Number, track_xpos:Number, track_ypos:Number):void{
  114. this.world = world;
  115. this.xpos = xpos;
  116. this.ypos = ypos;
  117. this.track_xpos = track_xpos;
  118. this.track_ypos = track_ypos;
  119. //create the tank body
  120. create_tankBody();
  121. //create the right wheel
  122. create_rightWheel();
  123. //create the left wheel
  124. create_leftWheel();
  125. //create the tracks
  126. create_Track();
  127. }
  128. //create the tank body
  129. private function create_tankBody():void{
  130. //body definition
  131. var body_def:b2BodyDef = new b2BodyDef();
  132. body_def.position.Set(xpos / globals.WORLD_SCALE, ypos / globals.WORLD_SCALE);
  133. body_def.type = b2Body.b2_dynamicBody;
  134. //vertex upper body
  135. var p1:b2Vec2 = new b2Vec2(-16 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE);
  136. var p2:b2Vec2 = new b2Vec2(-13 / globals.WORLD_SCALE, -10 / globals.WORLD_SCALE);
  137. var p3:b2Vec2 = new b2Vec2(13 / globals.WORLD_SCALE, -10 / globals.WORLD_SCALE);
  138. var p4:b2Vec2 = new b2Vec2(16 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE);
  139. var vertices:Array = [p1, p2, p3, p4];
  140. //upper body shape
  141. var upperBody_shape:b2PolygonShape = new b2PolygonShape();
  142. upperBody_shape.SetAsArray(vertices);
  143. //upper body fixture
  144. var upperBody_fix:b2FixtureDef = new b2FixtureDef();
  145. upperBody_fix.shape = upperBody_shape;
  146. upperBody_fix.density = 1;
  147. //upper body shape coordinates
  148. var p5:b2Vec2 = new b2Vec2(-25 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE);
  149. var p6:b2Vec2 = new b2Vec2(25 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE);
  150. var p7:b2Vec2 = new b2Vec2(25 / globals.WORLD_SCALE, 6 / globals.WORLD_SCALE);
  151. var p8:b2Vec2 = new b2Vec2(-25 / globals.WORLD_SCALE, 6 / globals.WORLD_SCALE);
  152. var vertices2:Array = [p5, p6, p7, p8];
  153. //botton body shape
  154. var bottonBody_shape:b2PolygonShape = new b2PolygonShape();
  155. bottonBody_shape.SetAsArray(vertices2);
  156. //botton body fixture
  157. var bottonBody_fix:b2FixtureDef = new b2FixtureDef();
  158. bottonBody_fix.shape = bottonBody_shape;
  159. bottonBody_fix.density = 1;
  160. //finish tank body
  161. _tank = world.CreateBody(body_def);
  162. _tank.CreateFixture(upperBody_fix);
  163. _tank.CreateFixture(bottonBody_fix);
  164. _tank.SetUserData(type);
  165. }
  166. //create the right wheel
  167. private function create_rightWheel():void{
  168. _rightWheel = new b2_wheel().createWheel(world, tank,
  169. tank.GetPosition().x + (15 / globals.WORLD_SCALE),
  170. tank.GetPosition().y + (20 / globals.WORLD_SCALE),
  171. (7.5 / globals.WORLD_SCALE), type);
  172. }
  173. //create the left wheel
  174. private function create_leftWheel():void{
  175. _leftWheel = new b2_wheel().createWheel(world, tank,
  176. tank.GetPosition().x - (15 / globals.WORLD_SCALE),
  177. tank.GetPosition().y + (20 / globals.WORLD_SCALE),
  178. (7.5 / globals.WORLD_SCALE), type);
  179. }
  180. //create the tracks
  181. private function create_Track():void{
  182. var tracks:b2_tracks = new b2_tracks(world, tank, 5, 1.5, 25, 16, 1, 2, 0, track_xpos, track_ypos, type);
  183. }
  184. //update the tank, cannon and wheels movies clips
  185. public function update_mc():void{
  186. tank_mc.x = tank.GetPosition().x * globals.WORLD_SCALE;
  187. tank_mc.y = tank.GetPosition().y * globals.WORLD_SCALE;
  188. tank_mc.rotationZ = (tank.GetAngle() / (Math.PI / 180));
  189. rightWheel_mc.x = rightWheel.GetPosition().x * globals.WORLD_SCALE;
  190. rightWheel_mc.y = rightWheel.GetPosition().y * globals.WORLD_SCALE;
  191. rightWheel_mc.rotationZ = (rightWheel.GetAngle() / (Math.PI / 180));
  192. leftWheel_mc.x = leftWheel.GetPosition().x * globals.WORLD_SCALE;
  193. leftWheel_mc.y = leftWheel.GetPosition().y * globals.WORLD_SCALE;
  194. leftWheel_mc.rotationZ = (leftWheel.GetAngle() / (Math.PI / 180));
  195. healthbar.x = tank_mc.x - 30;
  196. healthbar.y = tank_mc.y - 25;
  197. if(bullet_on_stage){
  198. _bullet_mc.x = _bullet.GetPosition().x * globals.WORLD_SCALE;
  199. _bullet_mc.y = _bullet.GetPosition().y * globals.WORLD_SCALE;
  200. }
  201. }
  202. //rotate the tank cannon
  203. public function rotateCannon_Left():void{
  204. if(tank_mc.getChildByName("cannon_mc").rotation >= -170){
  205. tank_mc.getChildByName("cannon_mc").rotation --;
  206. if(tank_mc.getChildByName("cannon_mc").rotation < -90 && tank_mc.getChildByName("cannon_mc").rotation >= -170){
  207. _shootCannonLeft = true;
  208. _shootCannonRight = false;
  209. }
  210. }
  211. }
  212. public function rotateCannon_Right():void{
  213. if(tank_mc.getChildByName("cannon_mc").rotation <= -10){
  214. tank_mc.getChildByName("cannon_mc").rotation ++;
  215. if(tank_mc.getChildByName("cannon_mc").rotation > -90 && tank_mc.getChildByName("cannon_mc").rotation <= -10){
  216. _shootCannonLeft = false;
  217. _shootCannonRight = true;
  218. }
  219. }
  220. }
  221. //rotate the tanks rocket launcher
  222. public function rotateRL_Left():void{
  223. if(tank_mc.getChildByName("rl_mc").rotation >= -140){
  224. tank_mc.getChildByName("rl_mc").rotation --;
  225. if(tank_mc.getChildByName("rl_mc").rotation < -90 && tank_mc.getChildByName("rl_mc").rotation >= -140){
  226. _shootRLLeft = true;
  227. _shootRLRight = false;
  228. }
  229. }
  230. }
  231. public function rotateRL_Right():void{
  232. if(tank_mc.getChildByName("rl_mc").rotation <= -40){
  233. tank_mc.getChildByName("rl_mc").rotation ++;
  234. if(tank_mc.getChildByName("rl_mc").rotation > -90 && tank_mc.getChildByName("rl_mc").rotation <= -40){
  235. _shootRLLeft = false;
  236. _shootRLRight = true;
  237. }
  238. }
  239. }
  240. //moving tank
  241. public function moveRight():void{
  242. leftWheel.ApplyTorque(5);
  243. rightWheel.ApplyTorque(5);
  244. }
  245. public function moveLeft():void{
  246. leftWheel.ApplyTorque(-5);
  247. rightWheel.ApplyTorque(-5);
  248. }
  249. //Shooting bullets
  250. public function shootBullet():void{
  251. if(_shootCannonRight && bullet_ready){
  252. //add commnad to shoot cannon right
  253. if(cant_bullet_shoot == 0 && cant_bullet != 0){
  254. rightWheel.ApplyImpulse(new b2Vec2(0, -2), rightWheel.GetWorldCenter());
  255. //create bullet
  256. _bullet = new b2_bullet().createBullet(world, getShootingPos().x / globals.WORLD_SCALE, getShootingPos().y / globals.WORLD_SCALE, 4 / globals.WORLD_SCALE, true, true, false, 0.25, 0, 0);
  257. _bullet.ApplyImpulse(new b2Vec2(bulletSpeed, getCannon_AntiGravity()), _bullet.GetWorldCenter());
  258. cant_bullet_shoot++;
  259. cant_bullet --;
  260. addChild(_bullet_mc);
  261. bullet_on_stage = true;
  262. time();
  263. controlPanel.setBullets(cant_bullet);
  264. controlPanel.turn_red();
  265. }
  266. }else if(_shootCannonLeft && bullet_ready){
  267. //add command to shoot cannon left
  268. if(cant_bullet_shoot == 0 && cant_bullet != 0){
  269. leftWheel.ApplyImpulse(new b2Vec2(0, -2), leftWheel.GetWorldCenter());
  270. //create bullet
  271. _bullet = new b2_bullet().createBullet(world, getShootingPos().x / globals.WORLD_SCALE, getShootingPos().y / globals.WORLD_SCALE, 4 / globals.WORLD_SCALE, true, true, false, 0.25, 0, 0);
  272. _bullet.ApplyImpulse(new b2Vec2(-bulletSpeed, getCannon_AntiGravity()), _bullet.GetWorldCenter());
  273. cant_bullet_shoot++;
  274. cant_bullet --;
  275. addChild(_bullet_mc);
  276. bullet_on_stage = true;
  277. time();
  278. controlPanel.setBullets(cant_bullet);
  279. controlPanel.turn_red();
  280. }
  281. }
  282. }
  283. //shooting missils
  284. public function shootMissil():void{
  285. if(_shootRLRight && rocket_ready){
  286. if(cant_rocket_shoot == 0 && cant_rocket != 0){
  287. rightWheel.ApplyImpulse(new b2Vec2(0, -1), rightWheel.GetWorldCenter());
  288. var box1:b2Body = new b2_rocket().createRocket(world, getLaunchPos1().x / globals.WORLD_SCALE, getLaunchPos1().y / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, true, 1, 0, 0);
  289. var box2:b2Body = new b2_rocket().createRocket(world, getLaunchPos2().x / globals.WORLD_SCALE, getLaunchPos2().y / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, true, 1, 0, 0);
  290. var box3:b2Body = new b2_rocket().createRocket(world, getLaunchPos3().x / globals.WORLD_SCALE, getLaunchPos3().y / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, true, 1, 0, 0);
  291. //first number = rocket force ,, second number antigravity
  292. launchForce = new b2Vec2(20, (getRL_AntiGravity() - 5));
  293. box1.ApplyForce(launchForce, box1.GetWorldCenter());
  294. launchForce = new b2Vec2(19, (getRL_AntiGravity() - 3));
  295. box2.ApplyForce(launchForce, box2.GetWorldCenter());
  296. launchForce = new b2Vec2(18, (getRL_AntiGravity() - 1));
  297. box3.ApplyForce(launchForce, box3.GetWorldCenter());
  298. }
  299. }else if(_shootRLLeft && rocket_ready){
  300. if(cant_rocket_shoot == 0 && cant_rocket != 0){
  301. rightWheel.ApplyImpulse(new b2Vec2(0, 1), rightWheel.GetWorldCenter());
  302. var box1:b2Body = new b2_rocket().createRocket(world, getLaunchPos1().x / globals.WORLD_SCALE, getLaunchPos1().y / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, true, 1, 0, 0);
  303. var box2:b2Body = new b2_rocket().createRocket(world, getLaunchPos2().x / globals.WORLD_SCALE, getLaunchPos2().y / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, true, 1, 0, 0);
  304. var box3:b2Body = new b2_rocket().createRocket(world, getLaunchPos3().x / globals.WORLD_SCALE, getLaunchPos3().y / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, 3 / globals.WORLD_SCALE, true, 1, 0, 0);
  305. //first number = rocket force ,, second number antigravity
  306. launchForce = new b2Vec2(-18, (getRL_AntiGravity() - 1));
  307. box1.ApplyForce(launchForce, box1.GetWorldCenter());
  308. launchForce = new b2Vec2(-19, (getRL_AntiGravity() - 3));
  309. box2.ApplyForce(launchForce, box2.GetWorldCenter());
  310. launchForce = new b2Vec2(-20, (getRL_AntiGravity() - 5));
  311. box3.ApplyForce(launchForce, box3.GetWorldCenter());
  312. }
  313. }
  314. }
  315. //add damage to tank
  316. public function reduce_life():void{
  317. _life = life - bullet_damage;
  318. controlPanel.setHealth(life);
  319. healthbar.update(_life);
  320. }
  321. //charging timer
  322. private function time():void{
  323. myTimer = new Timer(1000, charingTime);
  324. myTimer.addEventListener(TimerEvent.TIMER, timerListener);
  325. myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
  326. myTimer.start();
  327. }
  328. private function timerListener (e:TimerEvent):void{
  329. bullet_ready = false;
  330. }
  331. private function timerDone(e:TimerEvent):void{
  332. myTimer.stop();
  333. bullet_ready = true;
  334. controlPanel.turn_green();
  335. cant_bullet_shoot = 0;
  336. }
  337. //this methods sets the anti-gravity for the bullet when shooting
  338. public function getCannon_AntiGravity():Number{
  339. var num:Number = 0.30;
  340. var force:Number;
  341. var angle:Number = tank_mc.getChildByName("cannon_mc").rotation + 180;
  342. force = (angle * -num);
  343. if(angle > 90){
  344. angle = tank_mc.getChildByName("cannon_mc").rotation;
  345. force = (angle * num);
  346. }
  347. return force;
  348. }
  349. //cannon shooting position
  350. public function getShootingPos():b2Vec2{
  351. var hip:Number= 30;
  352. var targetMc:DisplayObject = tank_mc.getChildByName("cannon_mc");
  353. var pt:Point = new Point(targetMc.x, targetMc.y);
  354. pt = targetMc.parent.localToGlobal(pt);
  355. launchPos.x = (Math.cos(getCannonRad()) * hip) + pt.x;
  356. launchPos.y = (Math.sin(getCannonRad()) * hip) + pt.y;
  357. return launchPos;
  358. }
  359. //this methods sets the anti-gravity for the rocket
  360. public function getRL_AntiGravity():Number{
  361. var num:Number = 0.30;
  362. var force:Number;
  363. var angle:Number = tank_mc.getChildByName("rl_mc").rotation + 180;
  364. force = (angle * -num);
  365. if(angle > 90){
  366. angle = tank_mc.getChildByName("rl_mc").rotation;
  367. force = (angle * num);
  368. }
  369. return force;
  370. }
  371. //getting the launching position 1
  372. public function getLaunchPos1():b2Vec2{
  373. var hip:Number= 30;
  374. var targetMc:DisplayObject = tank_mc.getChildByName("rl_mc");
  375. var pt:Point = new Point(targetMc.x, targetMc.y);
  376. pt = targetMc.parent.localToGlobal(pt);
  377. launchPos.x = (Math.cos(getRLRad() - 0.6) * hip) + pt.x;
  378. launchPos.y = (Math.sin(getRLRad() - 0.6) * hip) + pt.y;
  379. return launchPos;
  380. }
  381. //getting the launching position 2
  382. public function getLaunchPos2():b2Vec2{
  383. var hip:Number= 30;
  384. var targetMc:DisplayObject = tank_mc.getChildByName("rl_mc");
  385. var pt:Point = new Point(targetMc.x, targetMc.y);
  386. pt = targetMc.parent.localToGlobal(pt);
  387. launchPos.x = (Math.cos(getRLRad() - 0.2) * hip) + pt.x;
  388. launchPos.y = (Math.sin(getRLRad() - 0.2) * hip) + pt.y;
  389. return launchPos;
  390. }
  391. //getting the launching position 3
  392. public function getLaunchPos3():b2Vec2{
  393. var hip:Number= 30;
  394. var targetMc:DisplayObject = tank_mc.getChildByName("rl_mc");
  395. var pt:Point = new Point(targetMc.x, targetMc.y);
  396. pt = targetMc.parent.localToGlobal(pt);
  397. launchPos.x = (Math.cos(getRLRad() + 0.20) * hip) + pt.x;
  398. launchPos.y = (Math.sin(getRLRad() + 0.20) * hip) + pt.y;
  399. return launchPos;
  400. }
  401. //get the cannon angle on radians
  402. private function getCannonRad():Number{
  403. var rads:Number = getCannon360() * Math.PI / 180;
  404. return rads;
  405. }
  406. //get the cannon angle on 360 degrees
  407. private function getCannon360():Number{
  408. var num:Number;
  409. if( tank_mc.getChildByName("cannon_mc").rotation >= 0 && tank_mc.getChildByName("cannon_mc").rotation <= 180){
  410. num = tank_mc.getChildByName("cannon_mc").rotation;
  411. }else if ( tank_mc.getChildByName("cannon_mc").rotation < 0){
  412. num = 360 + tank_mc.getChildByName("cannon_mc").rotation;
  413. }
  414. return num;
  415. }
  416. //get the cannon rotation on angles
  417. private function getCannonDeg():Number{
  418. var num:Number = getCannonRad() * 180 / Math.PI;
  419. return num;
  420. }
  421. //get the RL angle on radians
  422. private function getRLRad():Number{
  423. var rads:Number = getRL360() * Math.PI / 180;
  424. return rads;
  425. }
  426. //get the RL angle on 360 degrees
  427. private function getRL360():Number{
  428. var num:Number;
  429. if( tank_mc.getChildByName("rl_mc").rotation >= 0 && tank_mc.getChildByName("rl_mc").rotation <= 180){
  430. num = tank_mc.getChildByName("rl_mc").rotation;
  431. }else if ( tank_mc.getChildByName("rl_mc").rotation < 0){
  432. num = 360 + tank_mc.getChildByName("rl_mc").rotation;
  433. }
  434. return num;
  435. }
  436. //get the RL rotation on angles
  437. private function getRLDeg():Number{
  438. var num:Number = getRLRad() * 180 / Math.PI;
  439. return num;
  440. }
  441. //getters b2bodies
  442. public function get tank():b2Body{return _tank;}
  443. public function get rightWheel():b2Body{return _rightWheel;}
  444. public function get leftWheel():b2Body{return _leftWheel;}
  445. public function get shootCannonRight():Boolean{return _shootCannonRight;}
  446. public function get shootCannonLeft():Boolean{return _shootCannonLeft;}
  447. //getters mcs
  448. public function get tank_mc():MovieClip{return _tank_mc;}
  449. public function get rightWheel_mc():Sprite{return _rightWheel_mc;}
  450. public function get leftWheel_mc():Sprite{return _leftWheel_mc;}
  451. //getters properties
  452. public function get life():int{return _life;}
  453. public function get cant_bullet():int{return _cant_bullet;}
  454. //setter properties
  455. public function set life(value:int):void{_life = value;}
  456. public function set cant_bullet(value:int):void{_cant_bullet = value;}
  457. }
  458. }