PageRenderTime 50ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/game/engine/actors/enemies/EnemyDrunk.as

https://bitbucket.org/charliehoey/gatsby
ActionScript | 124 lines | 104 code | 20 blank | 0 comment | 20 complexity | 28c16c70b5bdcb785228d235b85b4c2d MD5 | raw file
  1. package engine.actors.enemies {
  2. import engine.actors.weapons.BottleWeapon;
  3. import flash.media.Sound;
  4. import flash.media.SoundChannel;
  5. public class EnemyDrunk extends EnemyWalker {
  6. protected var stumbleDurationMax:Number = 100;
  7. protected var stumbleDurationMin:Number = 10;
  8. protected var stumbleDuration:Number = 8;
  9. protected var actionDuration:Number = 5;
  10. protected var actionCounter:Number = 0;
  11. protected var drinkDuration:Number = 100;
  12. protected const STUMBLING = 1;
  13. protected const DRINKING = 2;
  14. protected var currentStatus = STUMBLING;
  15. private var bottleSound = new bottle_sound();
  16. private var bottleWeapon:BottleWeapon;
  17. override public function setup() {
  18. points = 250;
  19. collide_left = 10; // what pixel do we collide on on the left
  20. collide_right = 22; // what pixel do we collide on on the right
  21. myName = "EnemyDrunk"; // the generic name of our enemy
  22. mySkin = "DrunkSkin"; // the name of the skin for this enemy
  23. bottleWeapon = new BottleWeapon(this);
  24. startFrame = 0; // the first frame to loop on
  25. endFrame = 1; // the final frame in the row
  26. nowFrame = 0; // current frame in row
  27. loopFrame = 0; // frame at which to loop
  28. loopType = 0; // 0 loops, 1 bounces
  29. loopRow = 0; // which row are we on
  30. loopDir = 1; // loop forward (to the right) by default
  31. speed = 10; // 5 replaced // how many frames should go by before we advance
  32. walkSpeed = 1;
  33. velx = 0;
  34. frameDelay = 2;
  35. }
  36. public override function moveMe():void {
  37. if(frameCount >= frameDelay) {
  38. frameStarted = true;
  39. statusSet = false;
  40. this.y += vely / 2; // update our y variable
  41. this.x += velx / 2; // update our x variable
  42. if(velx > 0) {
  43. this.x = Math.ceil(this.x);
  44. } else {
  45. this.x = Math.floor(this.x);
  46. }
  47. if(vely > 0) {
  48. this.y = Math.ceil(this.y);
  49. } else {
  50. this.y = Math.floor(this.y);
  51. }
  52. notifyObservers(); // tell everybody where we are now
  53. applyPhysics(); // apply our enviromental variables
  54. updateStatus(); // update our status
  55. frameCount = 0;
  56. frameStarted = false;
  57. }
  58. if(currentStatus == STUMBLING) {
  59. if(actionCounter >= actionDuration) {
  60. if(Math.ceil(Math.random() * 3) == 3) {
  61. actionDuration = drinkDuration;
  62. currentStatus = DRINKING;
  63. setLoop(2, 0, 1, 0, 0);
  64. velx = 0;
  65. } else {
  66. currentStatus = STUMBLING;
  67. stumbleDuration = Math.ceil((Math.random() * stumbleDurationMax) + stumbleDurationMin);
  68. goingLeft = goingLeft == 0;
  69. if(Math.round(Math.random())) {
  70. velx = walkSpeed;
  71. } else {
  72. velx = -walkSpeed;
  73. }
  74. }
  75. actionCounter = 0;
  76. }
  77. } else if(currentStatus == DRINKING) {
  78. if(actionCounter >= actionDuration) {
  79. actionDuration = stumbleDuration;
  80. actionCounter = 0;
  81. if(Math.round(Math.random())) {
  82. velx = walkSpeed;
  83. } else {
  84. velx = -walkSpeed;
  85. }
  86. currentStatus = STUMBLING;
  87. setLoop(0, 0, 1, 0, 0);
  88. throwBottle();
  89. }
  90. }
  91. frameCount++;
  92. actionCounter++;
  93. animate();
  94. }
  95. private function throwBottle() {
  96. myMap.spawnActor(bottleWeapon, this.x, this.y + 5);
  97. bottleWeapon.throwBottle(goingLeft);
  98. var soundChannel = bottleSound.play(0);
  99. }
  100. public function catchBottle(bottle) {
  101. myMap.removeFromMap(bottle);
  102. }
  103. }
  104. }