PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/game/engine/actors/enemies/EnemyGangster.as

https://bitbucket.org/charliehoey/gatsby
ActionScript | 131 lines | 108 code | 23 blank | 0 comment | 23 complexity | bf1eda9ac76e8f963eab74fe374b2734 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.weapons.BulletWeapon;
  7. import engine.actors.player.Hero;
  8. public class EnemyGangster extends EnemyWalker {
  9. protected const WALKING = 1;
  10. protected const SHOOTING = 2;
  11. protected var currentStatus = WALKING;
  12. protected var actionCounter:Number = 0; // holder var for how many frames go by between actions
  13. protected const walkDuration = 60; // how long we spend walking
  14. protected const shootDelay = 3; // how long we wait between bullets
  15. protected const bulletsMax = 3; // how many bullets we're allowed to fire
  16. protected var bulletCounter:Number = 0; // how many bullets have we fired
  17. override public function setup() {
  18. collide_left = 10; // what pixel do we collide on on the left
  19. collide_right = 22; // what pixel do we collide on on the right
  20. myName = "EnemyGangster"; // the generic name of our enemy
  21. mySkin = "GangsterSkin"; // the name of the skin for this enemy
  22. deathFrame = 4;
  23. startFrame = 0; // the first frame to loop on
  24. endFrame = 1; // the final frame in the row
  25. nowFrame = 0; // current frame in row
  26. loopFrame = 0; // frame at which to loop
  27. loopType = 0; // 0 loops, 1 bounces
  28. loopRow = 0; // which row are we on
  29. loopDir = 1; // loop forward (to the right) by default
  30. speed = 7; // 5 replaced // how many frames should go by before we advance
  31. walkSpeed = 2;
  32. goingLeft = 1;
  33. points = 500;
  34. frameDelay = 2;
  35. }
  36. private function fireGun() {
  37. var multiplier = 0;
  38. if(!goingLeft) {
  39. multiplier = 1;
  40. }
  41. myMap.spawnActor(new BulletWeapon(this), this.x + (multiplier * this.width), this.y + 4);
  42. }
  43. override public function notify(subject:*):void {
  44. if(checkCollision(subject)) { // if we're colliding with the subject
  45. subject.collide(this); // then collide with them
  46. }
  47. if(subject is Hero) {
  48. if(subject.x < this.x) {
  49. goingLeft = 1;
  50. } else {
  51. goingLeft = 0;
  52. }
  53. }
  54. }
  55. override public function moveMe():void {
  56. if(goingLeft) {
  57. velx = -walkSpeed;
  58. } else {
  59. velx = walkSpeed;
  60. }
  61. frameCount++;
  62. if(frameCount >= frameDelay) {
  63. if(currentStatus == WALKING) { // if we're walking
  64. if(actionCounter >= walkDuration) { // and we've been walking too long
  65. currentStatus = SHOOTING; // start shooting
  66. setLoop(0, 2, 3, 2, 0, 3); // set our shooting animation
  67. actionCounter = 0; // and reset our timer
  68. } else { // otherwise
  69. this.x += velx / 2; // update our x variable
  70. if(velx > 0) {
  71. this.x = Math.ceil(this.x);
  72. } else {
  73. this.x = Math.floor(this.x);
  74. }
  75. if(vely > 0) {
  76. this.y = Math.ceil(this.y);
  77. } else {
  78. this.y = Math.floor(this.y);
  79. }
  80. }
  81. } else if(currentStatus == SHOOTING) { // otherwise, if we're shooting
  82. if(bulletCounter < bulletsMax) { // and we're not out of bullets
  83. if(actionCounter >= shootDelay) { // and we've waiting enough time between shots
  84. fireGun(); // then shoot the gun
  85. bulletCounter++; // increment our bullet counter
  86. actionCounter = 0; // and reset our action counter
  87. } else {
  88. }
  89. } else { // if we're out of bullets
  90. currentStatus = WALKING; // make us start walking
  91. setLoop(0, 0, 1, 0, 0, 10); // and set up our walking animation
  92. bulletCounter = 0; // and reset our bullet counter
  93. }
  94. }
  95. frameStarted = true;
  96. statusSet = false;
  97. this.y += vely / 2; // update our y variable
  98. notifyObservers(); // tell everybody where we are now
  99. applyPhysics(); // apply our enviromental variables
  100. updateStatus(); // update our status
  101. frameCount = 0;
  102. frameStarted = false;
  103. actionCounter++;
  104. }
  105. animate();
  106. }
  107. }
  108. }