PageRenderTime 63ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/MTRG/Src/CMinion.hx

http://game4web.googlecode.com/
Haxe | 916 lines | 644 code | 169 blank | 103 comment | 41 complexity | e9dae6ba1b8c9b16df0e0c4bbdd1bcda MD5 | raw file
  1. /****************************************************
  2. * MTRG : Motion-Twin recruitment game
  3. * A game by David Elahee
  4. *
  5. * MTRG is a Space Invader RTS, the goal is to protect your mothership from
  6. * the random AI that shoots on it.
  7. *
  8. * Powered by Game4Web a cross-platform engine by David Elahee & Benjamin Dubois.
  9. *
  10. Copyright (C) 2011 David Elahee
  11. This program is free software: you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation, either version 3 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses
  21. * @author de
  22. ****************************************************/
  23. /**
  24. * Gathers the minions base, and their variations, this is the short term motive, crush asteroids, produce mass of space invader
  25. * and crush the space ship
  26. */
  27. package ;
  28. import algorithms.CPool;
  29. import flash.display.Bitmap;
  30. import flash.display.BitmapData;
  31. import flash.display.BlendMode;
  32. import flash.display.DisplayObject;
  33. import flash.display.GradientType;
  34. import flash.display.Shape;
  35. import flash.display.Sprite;
  36. import CCollManager;
  37. import flash.geom.ColorTransform;
  38. import flash.geom.Matrix;
  39. import flash.Vector;
  40. import math.Interp;
  41. import math.RandomEx;
  42. import math.Registers;
  43. import math.Utils;
  44. import CDebug;
  45. import kernel.Glb;
  46. import math.CV2D;
  47. import CProjectile;
  48. import algorithms.CPool;
  49. import renderer.CDrawObject;
  50. ////////////////////////////////////
  51. enum EMinions
  52. {
  53. SpaceInvaders;
  54. Shielders;
  55. Crossars;
  56. Perforators;
  57. Count;
  58. }
  59. //since i cannot store my constants in pooled object let's do different
  60. //this is the root of dance
  61. class Constants
  62. {
  63. ////////////////////////////////////
  64. public static var CUR_SV_DANCE : CV2D = new CV2D(0, 0);
  65. public static inline var CIRCLE_SPEED : Float = 0.005;
  66. public static inline var SV_SPEED : Float = 0.03;
  67. public static inline var PERFORATOR_SPEED : Float = 0.1;
  68. public static inline var CROSS_SPEED : Float = 0.3;
  69. public static inline var BASE_SV_BOULETTE_SPEED : Float = 0.15;
  70. ////////////////////////////////////
  71. static var SV_STATE_DURATION : Float = 1.0;
  72. static var m_SvState : Int = 0;
  73. static var m_SvTimer : Float = 0;
  74. ////////////////////////////////////
  75. public static function Update()
  76. {
  77. m_SvTimer += Glb.GetSystem().GetGameDeltaTime();
  78. if (m_SvTimer>SV_STATE_DURATION )
  79. {
  80. m_SvState = (m_SvState + 1) % 3;
  81. m_SvTimer = 0;
  82. }
  83. switch(m_SvState)
  84. {
  85. case 0: CUR_SV_DANCE.Set(1, 0);
  86. case 1: CUR_SV_DANCE.Set(0, 1);
  87. case 2: CUR_SV_DANCE.Set(-1, 0);
  88. }
  89. }
  90. }
  91. ////////////////////////////////////
  92. class CMinion extends Sprite , implements Updatable, implements BSphered
  93. {
  94. ////////////////////////////////////
  95. public var m_Center : CV2D;
  96. public var m_Radius : Float;
  97. public var m_CollClass : COLL_CLASS;
  98. public var m_CollShape : COLL_SHAPE;
  99. public var m_CollMask : Int;
  100. public var m_Hp(GetHp,SetHp) : Int;
  101. private var _Hp : Int;
  102. private var m_ImgNormal : DisplayObject;
  103. private var m_ImgHit : DisplayObject;
  104. private var m_ShootDelay : Float;
  105. private var m_ShootTimer : Float ;
  106. private var m_Level: Int;
  107. public var m_HasAI(default, SetHasAi) : Bool;
  108. public var m_CollDmg : Int;
  109. public var m_BaseHp : Int;
  110. public var me : DO<CMinion>;
  111. ////////////////////////////////////
  112. public function new()
  113. {
  114. super();
  115. m_ShootTimer = 0;
  116. m_ShootDelay = 0;
  117. m_ImgNormal = null;
  118. m_ImgHit = null;
  119. m_CollClass = Aliens;
  120. m_CollShape = Sphere;
  121. m_Center = new CV2D(0,0);
  122. m_Radius = 8 / MTRG.HEIGHT;
  123. m_Level = 0;
  124. m_HasAI = false;
  125. m_CollDmg = 50;
  126. m_Hp = 10;
  127. m_BaseHp = 10;
  128. m_CollMask = ( (1 << Type.enumIndex(Asteroids))
  129. | (1 << Type.enumIndex(SpaceShip))
  130. | (1 << Type.enumIndex(SpaceShipShoots)));
  131. me = new DO( this );
  132. }
  133. //////////////////////////////////
  134. private function GetHp() : Int
  135. {
  136. return _Hp;
  137. }
  138. //////////////////////////////////
  139. private function SetHp(v : Int) : Int
  140. {
  141. var l_OldHp = _Hp;
  142. _Hp = v;
  143. if( _Hp <= 0 )
  144. {
  145. OnDestroy();
  146. }
  147. else if( v <= l_OldHp )
  148. {
  149. OnHit();
  150. }
  151. return _Hp;
  152. }
  153. ////////////////////////////////////
  154. public function OnHit()
  155. {
  156. var l_This = this;
  157. //CDebug.CONSOLEMSG("onhit mn:" + _Hp);
  158. MTRG.s_Instance.m_Gameplay.m_Tasks.push( new CTimedTask(function(ratio)
  159. {
  160. l_This.m_ImgNormal.blendMode = ADD;
  161. if (ratio >= 1.0)
  162. {
  163. l_This.m_ImgNormal.blendMode = NORMAL;
  164. }
  165. }
  166. ,0.1,0) );
  167. }
  168. ////////////////////////////////////
  169. private function SetHasAi( _OnOff )
  170. {
  171. if (!m_HasAI&&_OnOff)
  172. {
  173. OnEnable();
  174. Think();
  175. }
  176. m_HasAI = _OnOff;
  177. return m_HasAI;
  178. }
  179. //////////////////////////////////
  180. private function OnDestroy()
  181. {
  182. //CDebug.CONSOLEMSG("Min dtryd " + this);
  183. MTRG.s_Instance.m_Gameplay.m_MinionHelper.Delete(this);
  184. MTRG.s_Instance.m_Gameplay.m_CollMan.Remove(this);
  185. var l_This = this;
  186. MTRG.s_Instance.m_Gameplay.m_Tasks.push( new CTimedTask(function(ratio)
  187. {
  188. l_This.m_ImgNormal.visible = false;
  189. l_This.m_ImgHit.visible = true;
  190. l_This.alpha = 1.0 - ratio;
  191. if (l_This.alpha == 0)
  192. {
  193. l_This.visible = false;
  194. }
  195. }
  196. ,0.15,0) );
  197. }
  198. //////////////////////////////////
  199. public function OnCollision( _Collider : BSphered )
  200. {
  201. switch( _Collider.m_CollClass )
  202. {
  203. case SpaceShip:
  204. var l_Ship : CSpaceShip = cast _Collider;
  205. l_Ship.m_Hp -= m_CollDmg;
  206. OnDestroy();//i am always destroyed
  207. //CDebug.CONSOLEMSG("collided ship");
  208. case Asteroids:
  209. var l_Aster : CAsteroid = cast _Collider;
  210. l_Aster.m_Hp -= m_CollDmg;
  211. OnDestroy();
  212. case SpaceShipShoots:
  213. //handled by projectile
  214. default: CDebug.BREAK("Minion collided something not expected");
  215. }
  216. }
  217. //////////////////////////////////
  218. public function Initialize()
  219. {
  220. CDebug.BREAK("Override me");
  221. }
  222. //////////////////////////////////
  223. public function ProcessNextPosition()
  224. {
  225. CDebug.BREAK("Override me");
  226. }
  227. //////////////////////////////////
  228. public function IsLoaded()
  229. {
  230. return m_ImgNormal != null && m_ImgHit != null;
  231. }
  232. //////////////////////////////////
  233. //processes ai and calls update and generic shoot procedures
  234. public function Update()
  235. {
  236. if ( m_HasAI )
  237. {
  238. ProcessNextPosition();
  239. }
  240. x = m_Center.x * MTRG.HEIGHT; // aka (m_Img.x / MTRG.WIDTH) * (MTRG.WIDTH / MTRG.HEIGHT);
  241. y = m_Center.y * MTRG.HEIGHT;
  242. if ( m_HasAI )
  243. {
  244. m_ShootTimer += Glb.GetSystem().GetGameDeltaTime();
  245. if ( m_ShootTimer >= m_ShootDelay )
  246. {
  247. Shoot();
  248. m_ShootTimer = 0;
  249. }
  250. }
  251. if ((m_Center.x > 1.2 * Glb.g_System.m_Display.GetAspectRatio()) || (m_Center.x < -0.1)
  252. || (m_Center.y > 1.1) || (m_Center.y < -0.1))
  253. {
  254. OnDestroy();
  255. }
  256. }
  257. //////////////////////////////////
  258. public function Shut()
  259. {
  260. me.Shut();
  261. me = null;
  262. m_ImgNormal = null;
  263. m_ImgHit = null;
  264. }
  265. //////////////////////////////////
  266. public function Shoot()
  267. {
  268. CDebug.BREAK("Override me");
  269. }
  270. //////////////////////////////////
  271. public function Think()
  272. {
  273. }
  274. //good idea is to turn on hp, colls
  275. public function OnEnable()
  276. {
  277. MTRG.s_Instance.m_Gameplay.m_CollMan.Add(this);
  278. m_Hp = m_BaseHp;
  279. }
  280. }
  281. //////////////////////////////////
  282. class CSpaceInvaderMinion extends CMinion
  283. {
  284. //////////////////////////////////
  285. public function new()
  286. {
  287. super();
  288. m_BaseHp = 10;
  289. }
  290. //putting inline causes compiler issue when bulding pools...oh my gosh
  291. public var SI_SIZE : Int;
  292. //////////////////////////////////
  293. public override function OnEnable()
  294. {
  295. super.OnEnable();
  296. }
  297. //////////////////////////////////
  298. public override function Initialize()
  299. {
  300. SI_SIZE = 32;
  301. var l_BmpData : BitmapData = cast MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage();
  302. CDebug.ASSERT( null != MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage() );
  303. var l_Bmp = new Sprite();
  304. CDebug.ASSERT( l_Bmp != null );
  305. m_ImgNormal = l_Bmp;
  306. l_Bmp.graphics.beginBitmapFill( cast MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage());
  307. l_Bmp.graphics.drawRect(0, 0, 64, 64);
  308. l_Bmp.graphics.endFill();
  309. l_Bmp.x = - SI_SIZE / 2;
  310. l_Bmp.y = - SI_SIZE / 2 ;
  311. l_Bmp.scaleX = SI_SIZE / 64;
  312. l_Bmp.scaleY = SI_SIZE / 64;
  313. l_Bmp.transform.colorTransform = new ColorTransform(1,0,0);
  314. var l_BmpHit = new Sprite( );
  315. m_ImgHit = l_BmpHit;
  316. l_BmpHit.visible = false;
  317. l_BmpHit.graphics.beginBitmapFill( cast MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage());
  318. l_BmpHit.graphics.drawRect(0, 0, 64, 64);
  319. l_BmpHit.graphics.endFill();
  320. l_BmpHit.x = - SI_SIZE / 2;
  321. l_BmpHit.y = - SI_SIZE / 2 ;
  322. l_BmpHit.scaleX = SI_SIZE / 64;
  323. l_BmpHit.scaleY = SI_SIZE / 64;
  324. addChild( m_ImgHit );
  325. addChild( m_ImgNormal);
  326. visible = false;
  327. me.Activate();
  328. m_ShootDelay = 3;
  329. }
  330. //////////////////////////////////
  331. public override function Update()
  332. {
  333. super.Update();
  334. }
  335. //////////////////////////////////
  336. public override function Shoot()
  337. {
  338. var l_NewOne = MTRG.s_Instance.m_Gameplay.m_ProjectileHelper.m_CBoulettePool.Create();
  339. if ( l_NewOne == null ) return;
  340. l_NewOne.visible = true;
  341. var l_To: CV2D = Registers.V2DPool.Create();
  342. l_To.Set( 0, 1 );
  343. CV2D.Add( l_To , m_Center , l_To );
  344. l_NewOne.Fire(m_Center, l_To, Constants.BASE_SV_BOULETTE_SPEED );
  345. MTRG.s_Instance.m_SoundBank.PlayBouletteSound();
  346. Registers.V2DPool.Destroy( l_To );
  347. }
  348. //////////////////////////////////
  349. public override function ProcessNextPosition()
  350. {
  351. CV2D.Incr(m_Center, CV2D.Scale( Registers.V2_0, Glb.GetSystem().GetGameDeltaTime() * Constants.SV_SPEED, Constants.CUR_SV_DANCE ));
  352. }
  353. }
  354. //////////////////////////////////
  355. enum CircleBHV
  356. {
  357. WanderMother;
  358. Oscillate( _PosX : Float,_PosY : Float);
  359. }
  360. class CSpaceCircleMinion extends CMinion
  361. {
  362. //////////////////////////////////
  363. var m_ThinkTimer : Float;
  364. var m_BHV : CircleBHV;
  365. var m_Dir : CV2D;
  366. //////////////////////////////////
  367. public function new()
  368. {
  369. super();
  370. m_BaseHp = 50;
  371. }
  372. //////////////////////////////////
  373. public override function OnEnable()
  374. {
  375. super.OnEnable();
  376. }
  377. //////////////////////////////////
  378. public override function Initialize()
  379. {
  380. var l_BmpData : BitmapData = cast MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage();
  381. CDebug.ASSERT( null != MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage() );
  382. //build standard shape
  383. {
  384. var l_PrimaryShape : Shape = new Shape();
  385. var l_GradientMatrix : Matrix = new Matrix();
  386. l_GradientMatrix.createGradientBox( 12,12, 0, -12,-12 );
  387. l_PrimaryShape.graphics.beginGradientFill( GradientType.RADIAL, [0xCCC5BE , 0x302B1D], [1, 1], [0, 255],l_GradientMatrix, flash.display.SpreadMethod.PAD );
  388. l_PrimaryShape.graphics.drawCircle( 0, 0,12);
  389. l_PrimaryShape.graphics.endFill();
  390. l_PrimaryShape.graphics.lineStyle(2, 0xCCC5BE);
  391. l_PrimaryShape.graphics.drawCircle( 0, 0, 12);
  392. l_PrimaryShape.blendMode = BlendMode.NORMAL;
  393. l_PrimaryShape.cacheAsBitmap = true;
  394. l_PrimaryShape.visible = true;
  395. m_ImgNormal = l_PrimaryShape;
  396. }
  397. {
  398. var l_PrimaryShape : Shape = new Shape();
  399. l_PrimaryShape.graphics.beginFill( 0xEEEEEE );
  400. l_PrimaryShape.graphics.drawCircle( 0, 0, 21);
  401. l_PrimaryShape.graphics.endFill();
  402. l_PrimaryShape.blendMode = BlendMode.ADD;
  403. l_PrimaryShape.cacheAsBitmap = true;
  404. l_PrimaryShape.visible = false;
  405. m_ImgHit = l_PrimaryShape;
  406. }
  407. addChild( m_ImgHit );
  408. addChild( m_ImgNormal);
  409. visible = false;
  410. me.Activate();
  411. m_Dir = new CV2D(0, 0);
  412. m_BHV = WanderMother;
  413. m_ThinkTimer = 0.5;
  414. }
  415. //////////////////////////////////
  416. public override function Update()
  417. {
  418. super.Update();
  419. if ( IsLoaded() )
  420. {
  421. m_ImgNormal.rotationZ += 30 * Glb.g_System.GetGameDeltaTime();
  422. }
  423. }
  424. //////////////////////////////////
  425. //does not shoot but rather turn aroun the mothership of go down oscilating
  426. public override function Shoot()
  427. {
  428. }
  429. public override function Think()
  430. {
  431. var l_Wander = RandomEx.DiceF(0,0.5) > 0.05;
  432. //get a shot if far
  433. if ( CV2D.GetDistance( m_Center, MTRG.s_Instance.m_Gameplay.m_Mothership.m_Center ) > 0.5 )
  434. {
  435. l_Wander = false;
  436. }
  437. //when starts tracking always pursue
  438. if ( WanderMother != m_BHV )
  439. {
  440. l_Wander = false;
  441. }
  442. //find a target near ms
  443. if ( l_Wander )
  444. {
  445. var l_R0 = Registers.V2DPool.Create();
  446. var l_Ms = MTRG.s_Instance.m_Gameplay.m_Mothership;
  447. RandomEx.RandBox( l_R0,
  448. l_Ms.m_Center.x - l_Ms.MS_SIZE_X / MTRG.HEIGHT * 0.6,
  449. l_Ms.m_Center.y - l_Ms.MS_SIZE_Y / MTRG.HEIGHT * 0.6,
  450. l_Ms.m_Center.x + l_Ms.MS_SIZE_X / MTRG.HEIGHT * 0.6,
  451. l_Ms.m_Center.y + l_Ms.MS_SIZE_Y / MTRG.HEIGHT * 0.6);
  452. CV2D.Sub( m_Dir, l_R0, m_Center);
  453. CV2D.SafeNormalize( m_Dir , CV2D.ZERO );
  454. m_BHV = WanderMother;
  455. m_ThinkTimer = 3.0;
  456. Registers.V2DPool.Destroy(l_R0);
  457. }
  458. else
  459. {
  460. m_BHV = Oscillate( MTRG.s_Instance.m_Gameplay.m_Ship.m_Center.x, MTRG.s_Instance.m_Gameplay.m_Ship.m_Center.y );
  461. //CDebug.CONSOLEMSG( MTRG.s_Instance.m_Gameplay.m_Ship.m_Center);
  462. m_ThinkTimer = 0.75;
  463. }
  464. }
  465. //////////////////////////////////
  466. public override function ProcessNextPosition()
  467. {
  468. m_ThinkTimer -= Glb.g_System.GetGameDeltaTime();
  469. if (m_ThinkTimer<0)
  470. {
  471. Think();
  472. }
  473. switch(m_BHV)
  474. {
  475. case WanderMother:
  476. {
  477. //wander freely always same winding
  478. var l_Temp = Registers.V2DPool.Create();
  479. l_Temp.Set(0, 0);
  480. var l_Dir = RandomEx.DiceF( 0, 0.5);
  481. l_Temp.Set(l_Dir, Math.sqrt(1 - Math.sqrt(l_Dir * l_Dir)));
  482. CV2D.Incr(m_Dir, CV2D.Scale( l_Temp, RandomEx.DiceF(0, 0.5) * Glb.g_System.GetGameDeltaTime() , l_Temp));
  483. CV2D.SafeNormalize( m_Dir, CV2D.ZERO );
  484. Registers.V2DPool.Destroy(l_Temp);
  485. }
  486. //oscillate with target, it is particuliarly efficient to protect MS
  487. case Oscillate(px, py):
  488. {
  489. var l_Temp = Registers.V2DPool.Create();
  490. l_Temp.Set( px, py);
  491. m_Dir.Copy( CV2D.Sub(l_Temp, l_Temp , m_Center ) ) ;
  492. CV2D.Incr(m_Dir, CV2D.Scale( l_Temp, RandomEx.DiceF(-0.1, 0.1) * Glb.g_System.GetGameDeltaTime() , l_Temp));
  493. Registers.V2DPool.Destroy(l_Temp);
  494. }
  495. }
  496. CV2D.SafeNormalize( m_Dir , CV2D.ZERO );
  497. var l_R0 = Registers.V2DPool.Create();
  498. CV2D.Incr(m_Center, CV2D.Scale( l_R0, Glb.g_System.GetGameDeltaTime() * Constants.CIRCLE_SPEED, m_Dir ));
  499. Registers.V2DPool.Destroy(l_R0);
  500. }
  501. }
  502. class CPerforatingMinion extends CMinion
  503. {
  504. public var m_Dir : CV2D;
  505. public var m_MatchingAngle : Float;
  506. public var m_Speed : Float;
  507. public function new()
  508. {
  509. super();
  510. m_Radius = 4 / MTRG.HEIGHT;
  511. m_Dir = new CV2D(0, 0);
  512. m_MatchingAngle = 0;
  513. m_Speed = 0;
  514. m_CollDmg = 80;
  515. m_BaseHp = 10;
  516. }
  517. //////////////////////////////////
  518. public override function OnEnable()
  519. {
  520. super.OnEnable();
  521. m_MatchingAngle = 0;
  522. m_Speed = 0;
  523. }
  524. //////////////////////////////////
  525. public override function Initialize()
  526. {
  527. var l_BmpData : BitmapData = cast MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage();
  528. CDebug.ASSERT( null != MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage() );
  529. var l_Vec : Vector<Float> = new Vector<Float>();
  530. var i = 0;
  531. l_Vec[i++] = 4; l_Vec[i++] = 0;
  532. l_Vec[i++] = 0; l_Vec[i++] = 32;
  533. l_Vec[i++] = -4; l_Vec[i++] = 0;
  534. //build standard shape
  535. {
  536. var l_PrimaryShape : Shape = new Shape();
  537. l_PrimaryShape.graphics.lineStyle(3, 0x888888, 1.0);
  538. l_PrimaryShape.graphics.beginFill( 0x3F522B );
  539. l_PrimaryShape.graphics.drawTriangles( l_Vec );
  540. l_PrimaryShape.graphics.endFill();
  541. l_PrimaryShape.blendMode = BlendMode.NORMAL;
  542. l_PrimaryShape.cacheAsBitmap = true;
  543. l_PrimaryShape.visible = true;
  544. m_ImgNormal = l_PrimaryShape;
  545. }
  546. {
  547. var l_PrimaryShape : Shape = new Shape();
  548. l_PrimaryShape.graphics.beginFill( 0xFFFFFF );
  549. l_PrimaryShape.graphics.drawTriangles( l_Vec );
  550. l_PrimaryShape.graphics.endFill();
  551. l_PrimaryShape.blendMode = BlendMode.NORMAL;
  552. l_PrimaryShape.cacheAsBitmap = true;
  553. l_PrimaryShape.visible = false;
  554. m_ImgHit = l_PrimaryShape;
  555. }
  556. addChild( m_ImgHit );
  557. addChild( m_ImgNormal);
  558. visible = false;
  559. me.Activate();
  560. }
  561. //////////////////////////////////
  562. public override function Update()
  563. {
  564. super.Update();
  565. }
  566. //////////////////////////////////
  567. public override function Shoot()
  568. {
  569. }
  570. //////////////////////////////////
  571. public override function ProcessNextPosition()
  572. {
  573. var l_R0 = Registers.V2DPool.Create();
  574. CV2D.Sub( m_Dir, MTRG.s_Instance.m_Gameplay.m_Ship.m_Center , m_Center );
  575. var l_Dist = m_Dir.Norm();
  576. CV2D.SafeNormalize( m_Dir, CV2D.ZERO);
  577. if( l_Dist > 0.35 )
  578. {
  579. //do a slight accel
  580. m_Speed = Interp.SInterp( Math.pow( m_MatchingAngle, 0.5), 0, Constants.PERFORATOR_SPEED);
  581. }
  582. else
  583. {
  584. //go for the throat !
  585. m_Speed += Constants.PERFORATOR_SPEED * 3 * Glb.g_System.GetGameDeltaTime();
  586. }
  587. var l_Delta = m_Speed * Glb.g_System.GetGameDeltaTime();
  588. if (l_Delta > l_Dist )
  589. {
  590. //CDebug.CONSOLEMSG("warp d:" + l_Delta + " dis" + l_Dist);
  591. m_Center = MTRG.s_Instance.m_Gameplay.m_Ship.m_Center;
  592. }
  593. else
  594. {
  595. //we inject lerping to give this lagy sensation for AI
  596. CV2D.Incr( m_Center, CV2D.Scale( l_R0, l_Delta , m_Dir ));
  597. }
  598. var l_Angle = Math.atan2(m_Dir.x, m_Dir.y);
  599. m_ImgNormal.rotationZ = Interp.Lerp( m_MatchingAngle, m_ImgNormal.rotationZ , (- l_Angle * math.Constants.RAD_TO_DEG) );
  600. //CDebug.CONSOLEMSG(l_Angle);
  601. Registers.V2DPool.Destroy(l_R0);
  602. m_MatchingAngle += Glb.g_System.GetGameDeltaTime() * 0.85;
  603. m_MatchingAngle = Utils.Clamp( m_MatchingAngle, 0, 1);
  604. }
  605. }
  606. enum CrossState
  607. {
  608. CS_IDLE;
  609. CS_SALVA(n:Int, dly : Float);
  610. }
  611. class CCrossMinion extends CMinion
  612. {
  613. public var m_State: CrossState;
  614. public var m_Target : CV2D;
  615. //////////////////////////////////
  616. public function new()
  617. {
  618. super();
  619. m_ShootDelay = 5;
  620. m_Target = new CV2D(0, 0);
  621. m_BaseHp = 20;
  622. }
  623. public override function OnEnable()
  624. {
  625. super.OnEnable();
  626. RandomEx.RandBox( m_Target,
  627. 0, 0,
  628. Glb.g_System.m_Display.GetAspectRatio(), 0.8
  629. );
  630. m_State = CS_IDLE;
  631. }
  632. //////////////////////////////////
  633. public override function Initialize()
  634. {
  635. var l_BmpData : BitmapData = cast MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage();
  636. CDebug.ASSERT( null != MTRG.s_Instance.m_Gameplay.m_RscSpaceInvader.GetDriverImage() );
  637. //build standard shape
  638. {
  639. var l_PrimaryShape : Shape = new Shape();
  640. var l_GradientMatrix : Matrix = new Matrix();
  641. l_PrimaryShape.graphics.lineStyle(3, 0xAAAAAA);
  642. l_PrimaryShape.graphics.moveTo( -8, -8);
  643. l_PrimaryShape.graphics.lineTo( 8, 8);
  644. l_PrimaryShape.graphics.lineStyle(5, 0xBBBBBB);
  645. l_PrimaryShape.graphics.moveTo( -8, 8);
  646. l_PrimaryShape.graphics.lineTo( 8, -8);
  647. l_PrimaryShape.blendMode = BlendMode.NORMAL;
  648. l_PrimaryShape.cacheAsBitmap = true;
  649. l_PrimaryShape.visible = true;
  650. m_ImgNormal = l_PrimaryShape;
  651. }
  652. {
  653. var l_PrimaryShape : Shape = new Shape();
  654. l_PrimaryShape.graphics.lineStyle(3, 0xFFFFFF);
  655. l_PrimaryShape.graphics.moveTo( -8, -8);
  656. l_PrimaryShape.graphics.lineTo( 8, 8);
  657. l_PrimaryShape.graphics.lineStyle(5, 0xFFFFFF);
  658. l_PrimaryShape.graphics.moveTo( -8, 8);
  659. l_PrimaryShape.graphics.lineTo( 8, -8);
  660. l_PrimaryShape.blendMode = BlendMode.ADD;
  661. l_PrimaryShape.cacheAsBitmap = true;
  662. l_PrimaryShape.visible = false;
  663. m_ImgHit = l_PrimaryShape;
  664. }
  665. addChild( m_ImgHit );
  666. addChild( m_ImgNormal);
  667. visible = false;
  668. me.Activate();
  669. }
  670. //////////////////////////////////
  671. public override function Update()
  672. {
  673. super.Update();
  674. if ( IsLoaded() )
  675. {
  676. if (m_State == CS_IDLE )
  677. {
  678. m_ImgNormal.rotationZ += 120 * Glb.g_System.GetGameDeltaTime();
  679. }
  680. else
  681. {
  682. m_ImgNormal.rotationZ += 720* Glb.g_System.GetGameDeltaTime();
  683. }
  684. }
  685. }
  686. //////////////////////////////////
  687. public override function Shoot()
  688. {
  689. m_State = CS_SALVA(5,0);
  690. }
  691. //////////////////////////////////
  692. public override function ProcessNextPosition()
  693. {
  694. switch(m_State)
  695. {
  696. //do nothing
  697. case CS_IDLE:
  698. //process current salva
  699. case CS_SALVA(n, dly):
  700. if(dly - Glb.g_System.GetGameDeltaTime() >= 0)
  701. {
  702. m_State = CS_SALVA(n, dly - Glb.g_System.GetGameDeltaTime());
  703. }
  704. else
  705. {
  706. if (n==0)
  707. {
  708. RandomEx.RandBox( m_Target,
  709. 0, 0,
  710. Glb.g_System.m_Display.GetAspectRatio(), 0.8
  711. );
  712. //CDebug.CONSOLESMG()("heading to " + m_Target);
  713. m_State = CS_IDLE;
  714. }
  715. else
  716. {
  717. var l_NewOne = MTRG.s_Instance.m_Gameplay.m_ProjectileHelper.m_CBoulettePool.Create();
  718. if (l_NewOne != null)
  719. {
  720. l_NewOne.visible = true;
  721. l_NewOne.Fire(m_Center, MTRG.s_Instance.m_Gameplay.m_Ship.m_Center, Constants.BASE_SV_BOULETTE_SPEED );
  722. MTRG.s_Instance.m_SoundBank.PlayBouletteSound();
  723. }
  724. m_State = CS_SALVA(n - 1, 0.1 );
  725. }
  726. }
  727. }
  728. //move out
  729. CV2D.Sub( Registers.V2_1, m_Target, m_Center );
  730. var l_Dist2 = Registers.V2_1.Norm2();
  731. var l_Delta = Glb.g_System.GetGameDeltaTime() * Constants.CROSS_SPEED;
  732. if (l_Delta*l_Delta > l_Dist2)
  733. {
  734. m_Center.Copy(m_Target);
  735. m_Target.Copy(m_Center);
  736. }
  737. else
  738. {
  739. CV2D.SafeNormalize(Registers.V2_1, CV2D.ZERO);
  740. CV2D.Incr( m_Center, CV2D.Scale( Registers.V2_0, l_Delta,Registers.V2_1));
  741. }
  742. }
  743. }