PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/game/engine/actors/enemies/EnemyPitcher.as

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