PageRenderTime 64ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/game/engine/actors/enemies/EnemyBossTJEckleberg.as

https://bitbucket.org/charliehoey/gatsby
ActionScript | 343 lines | 273 code | 61 blank | 9 comment | 78 complexity | 68539e01c6cab4ab4169336cca6bc42d MD5 | raw file
  1. package engine.actors.enemies {
  2. import engine.actors.enemies.EnemyWalker;
  3. import engine.ISubject;
  4. import engine.IObserver;
  5. import engine.actors.geoms.*;
  6. import engine.actors.player.Hero;
  7. import engine.Scoreboard;
  8. import engine.actors.Explosion;
  9. import flash.media.Sound;
  10. import flash.media.SoundChannel;
  11. import flash.geom.Point;
  12. import engine.actors.weapons.LaserWeapon;
  13. public class EnemyBossTJEckleberg extends EnemyWalker {
  14. private var laserSound = new laser_sound();
  15. private var roarSound = new tjeckleberg_roar();
  16. private var explodeSound = new bullet_sound();
  17. protected const flyingDuration = 60;
  18. protected const damageDuration = 60;
  19. protected const shootWarningDuration = 15;
  20. protected const shootDuration = 0;
  21. protected var actionDelay = flyingDuration;
  22. protected var actionCounter = 0;
  23. protected var currentRow = 0;
  24. protected const FLYING = 1;
  25. protected const SHOOTING = 2;
  26. protected const SHOOT_WARNING = 3;
  27. protected var damagedFlag = 0;
  28. protected var damagedCounter = 0;
  29. protected const damagedDuration = 60;
  30. protected var destinationPoint:Point = new Point(296, 60);
  31. protected var destArray = new Array();
  32. protected var currentPoint:Point = new Point(0, 0);
  33. protected var pointCounter = 0;
  34. protected const pointProximity = 25;
  35. protected var prevVelx = 0;
  36. protected var prevVely = 0;
  37. protected var myHero = null;
  38. protected var maxVel = 4; // fastest we're allowed to go by the universe
  39. protected var INERTIA = .17; // the fastest we're allowed to increase in velocity per frame
  40. protected var currentAction = FLYING;
  41. protected var deathRow = 4;
  42. protected var explosionCounter = 0;
  43. protected var explosionMax = 8;
  44. override public function setup() {
  45. collide_left = 0; // what pixel do we collide on on the left
  46. collide_right = 112; // what pixel do we collide on on the right
  47. walkSpeed = 0;
  48. velx = walkSpeed;
  49. HP = 5;
  50. deathFrame = 0;
  51. points = 2500;
  52. myName = "TJ ECKLEBERG"; // the generic name of our enemy
  53. mySkin = "TJEcklebergSkinBig"; // the name of the skin for this enemy
  54. startFrame = 0; // the first frame to loop on
  55. endFrame = 0; // the final frame in the row
  56. nowFrame = 0; // current frame in row
  57. loopFrame = 0; // frame at which to loop
  58. loopType = 0; // 0 loops, 1 bounces
  59. loopRow = 0; // which row are we on
  60. loopDir = 1; // loop forward (to the right) by default
  61. speed = 10; // 5 replaced // how many frames should go by before we advance
  62. destArray = new Array( (new Point(80, 55)), (new Point(196, 55)) );
  63. // place our explosions
  64. tilesWide = 7;
  65. tilesTall = 3;
  66. }
  67. private function getCurrentFrame() {
  68. return getRectangle(currentRow, nowFrame);
  69. }
  70. override public function killMe():void {
  71. HP = 0;
  72. if(myStatus != 'DYING') {
  73. setLoop(deathRow, 0, 1, 0, 0, 2); // make us die
  74. myStatus = 'DYING';
  75. this.vely = -4;
  76. if(hitDirection == 'LEFT') {
  77. this.velx = 3;
  78. } else if(hitDirection == 'RIGHT') {
  79. this.velx = -3;
  80. }
  81. }
  82. if(frameCount >= frameDelay) {
  83. applyPhysics();
  84. this.y += vely;
  85. if(vely >= 1) {
  86. vely = 1;
  87. }
  88. animate();
  89. explosionCounter++;
  90. if(explosionCounter >= explosionMax) {
  91. var myExplosion = new Explosion();
  92. myMap.spawnActor(myExplosion, (Math.floor(Math.random() * this.width) + this.x - 16), (Math.floor(Math.random() * this.height) + this.y - 16));
  93. explosionCounter = 0;
  94. explosionMax = Math.floor(Math.random() * 3) + 8;
  95. }
  96. if(this.y > 240) {
  97. myStatus = 'DEAD';
  98. myMap.updateStatus(COMPLETE);
  99. myMap.removeFromMap(this);
  100. }
  101. } else {
  102. frameCount++;
  103. }
  104. }
  105. private function moveEyes(hero) {
  106. if(currentAction == SHOOT_WARNING) {
  107. // var damagedFlag = 1;
  108. }
  109. var heroDelta = (this.x + (this.width / 2)) - (hero.x + (this.height / 2)); // subtract Eckleberg's center from the hero's and see what's what
  110. if(heroDelta > 64) {
  111. if(currentRow != 0) {
  112. currentRow = 0;
  113. setLoop(currentRow, 0, damagedFlag, 0, 0, 2);
  114. }
  115. } else if(heroDelta > 0 && heroDelta < 64) {
  116. if(currentRow != 1) {
  117. currentRow = 1;
  118. setLoop(currentRow, 0, damagedFlag, 0, 0, 2);
  119. }
  120. } else if(heroDelta > -64 && heroDelta < 0) {
  121. if(currentRow != 2) {
  122. currentRow = 2;
  123. setLoop(currentRow, 0, damagedFlag, 0, 0, 2);
  124. }
  125. } else if(heroDelta < -64) {
  126. if(currentRow != 3) {
  127. currentRow = 3;
  128. setLoop(currentRow, 0, damagedFlag, 0, 0, 2);
  129. }
  130. }
  131. }
  132. override public function notify(subject:*):void {
  133. if(subject is Hero) {
  134. if(!deadFlag && INERTIA != 0) {
  135. moveEyes(subject);
  136. }
  137. if(myHero == null) {
  138. myHero = subject;
  139. }
  140. }
  141. }
  142. // the hero will use this to deal damage
  143. override public function receiveDamage(attacker):void {
  144. if(damagedFlag == 0) {
  145. var soundChannel = roarSound.play(0);
  146. HP -= attacker.damage;
  147. if(HP <= 0) {
  148. HP = 0;
  149. if(attacker.velx > 0) {
  150. this.velx = 1;
  151. } else {
  152. this.velx = -1;
  153. }
  154. }
  155. scoreboard.setBossHP(HP);
  156. damagedFlag = 1;
  157. }
  158. }
  159. private function shootLaser(hero) {
  160. // calculate the center of each of us
  161. var thisCenter = new Point(this.x + (this.width / 2), this.y + (this.height / 2));
  162. var heroCenter = new Point(hero.x + (hero.width / 2), hero.y + (hero.height / 2));
  163. // get a vector to the hero's position
  164. var lenX = (heroCenter.x - thisCenter.x);
  165. var lenY = (heroCenter.y - thisCenter.y);
  166. // calculate the length of the vector
  167. var vectorLength = Math.sqrt((lenX * lenX) + (lenY * lenY));
  168. // normalize it
  169. var heroVX = (lenX / vectorLength);
  170. var heroVY = (lenY / vectorLength);
  171. var laserLeft = new LaserWeapon(this, heroVX, heroVY);
  172. var laserRight = new LaserWeapon(this, heroVX, heroVY);
  173. myMap.spawnActor(laserLeft, thisCenter.x - 25, thisCenter.y);
  174. myMap.spawnActor(laserRight, thisCenter.x + 25, thisCenter.y);
  175. var soundChannel = laserSound.play(0);
  176. }
  177. private function updatePoints() {
  178. // see how close we are to our destination point
  179. var currentPoint = new Point(this.x + (this.width / 2), this.y + (this.height / 2));
  180. var destPoint = destArray[pointCounter];
  181. if(Math.abs(destPoint.x - currentPoint.x) <= pointProximity && Math.abs(destPoint.y - currentPoint.y) <= pointProximity) {
  182. pointCounter++;
  183. }
  184. if(pointCounter > (destArray.length - 1)) {
  185. pointCounter = 0;
  186. }
  187. }
  188. private function flyToPoint(destinationPoint) {
  189. currentPoint = new Point(this.x + (this.width / 2), this.y + (this.height / 2));
  190. if(currentPoint.x < destinationPoint.x) {
  191. velx += INERTIA;
  192. } else {
  193. velx -= INERTIA;
  194. }
  195. if(currentPoint.y < destinationPoint.y) {
  196. vely += INERTIA;
  197. } else {
  198. vely -= INERTIA;
  199. }
  200. if(Math.abs(velx) > maxVel) {
  201. if(velx < 0) {
  202. velx = -maxVel;
  203. } else {
  204. velx = maxVel;
  205. }
  206. }
  207. if(Math.abs(vely) > maxVel) {
  208. if(vely < 0) {
  209. vely = -maxVel;
  210. } else {
  211. vely = maxVel;
  212. }
  213. }
  214. }
  215. override public function updateStatus():void {
  216. }
  217. override public function moveMe():void {
  218. updatePoints();
  219. flyToPoint(destArray[pointCounter]);
  220. frameCount++;
  221. if(frameCount >= frameDelay) {
  222. if(actionCounter >= actionDelay) { // if we've waited long enough for our current action
  223. if(currentAction == FLYING) { // and we're flying
  224. actionDelay = shootWarningDuration; // set our delay value
  225. currentAction = SHOOT_WARNING; // switch us to shoot warning mode
  226. setLoop(currentRow, 0, 1, 0, 0, 3); // set our animation to do what it does
  227. } else if(currentAction == SHOOT_WARNING) { // otherwise, if we're shoot warning, then we're done
  228. setLoop(currentRow, 0, 0, 0, 0, 10); // set us back to normal animation
  229. actionDelay = shootDuration; // set our pause duration to shoot
  230. currentAction = SHOOTING; // and set us to ready to shoot
  231. } else if(currentAction == SHOOTING) { // otherwise, if we're ready to shoot
  232. this.velx = walkSpeed;
  233. if(myHero != null) { // if we've got a hero
  234. shootLaser(myHero); // shoot a laser beam at him
  235. }
  236. actionDelay = flyingDuration; // get us prepped to fly
  237. currentAction = FLYING; // and start just flying
  238. }
  239. actionCounter = 0;
  240. }
  241. if(damagedFlag != 0) {
  242. if(damagedCounter == 0) {
  243. setLoop(currentRow, 0, damagedFlag, 0, 0, 0);
  244. }
  245. if(damagedCounter >= damagedDuration) {
  246. damagedFlag = 0;
  247. damagedCounter = 0;
  248. setLoop(currentRow, 0, damagedFlag, 0, 0, 0);
  249. } else {
  250. damagedCounter++;
  251. }
  252. }
  253. frameStarted = true;
  254. statusSet = false;
  255. this.y += vely / 2; // update our y variable
  256. this.x += velx / 2; // update our x variable
  257. if(velx > 0) {
  258. this.x = Math.ceil(this.x);
  259. } else {
  260. this.x = Math.floor(this.x);
  261. }
  262. if(vely > 0) {
  263. this.y = Math.ceil(this.y);
  264. } else {
  265. this.y = Math.floor(this.y);
  266. }
  267. notifyObservers(); // tell everybody where we are now
  268. //applyPhysics(); // apply our enviromental variables
  269. updateStatus(); // update our status
  270. frameCount = 0;
  271. frameStarted = false;
  272. actionCounter++;
  273. if(HP <= 1) {
  274. setLoop(currentRow, 1, 1, 1, 0, 5);
  275. maxVel = 5;
  276. INERTIA = .2;
  277. }
  278. }
  279. animate();
  280. }
  281. }
  282. }