PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/game/engine/actors/weapons/HatProjectile.as

https://bitbucket.org/charliehoey/gatsby
ActionScript | 150 lines | 115 code | 22 blank | 13 comment | 26 complexity | 67bf7d77a4c4e2ef12c8668a6fe7cd19 MD5 | raw file
  1. package engine.actors.weapons {
  2. import engine.IObserver;
  3. import engine.actors.Actor;
  4. import engine.Scoreboard;
  5. import engine.actors.player.Hero;
  6. import engine.actors.enemies.*;
  7. import engine.actors.geoms.*;
  8. import flash.events.Event;
  9. import engine.actors.Animatable;
  10. import engine.actors.weapons.Projectile;
  11. public class HatProjectile extends Projectile implements IObserver {
  12. private var returning:Boolean = false;
  13. private var throwDistance:int = 15;
  14. protected var flySpeed:Number = 8;
  15. private var inertia:Number = 4; // amount of inertia to change velocity
  16. private var scoreboard:Scoreboard = Scoreboard.getInstance();
  17. private var successiveHits = 0;
  18. public function HatProjectile(owner) {
  19. super(owner);
  20. }
  21. override public function setup() {
  22. damage = 1;
  23. myName = "HatWeapon";
  24. mySkin = "HatWeaponSkin";
  25. tilesWide = 1;
  26. tilesTall = 1;
  27. collide_left = 0; // what pixel do we collide on on the left
  28. collide_right = 16; // what pixel do we collide on on the right
  29. startFrame = 0; // the first frame to loop on
  30. endFrame = 3; // the final frame in the row
  31. nowFrame = 0; // current frame in row
  32. loopFrame = 0; // frame at which to loop
  33. loopType = 1; // 0 loops, 1 bounces
  34. loopRow = 0; // which row are we on
  35. loopDir = 1; // loop forward (to the right) by default
  36. speed = 5; // how many frames should go by before we advance
  37. velx = 0;
  38. vely = 0;
  39. if(vecx > 0) {
  40. throwHat(false);
  41. } else {
  42. throwHat(true);
  43. }
  44. }
  45. override public function notify(subject):void {
  46. if(checkCollision(subject)) {
  47. if(subject is Enemy || subject is EnemyWalker) {
  48. if(subject.getHP() > 0) {
  49. successiveHits++;
  50. scoreboard.setMultiplier(successiveHits);
  51. subject.receiveDamage(this);
  52. if(!returning) {
  53. returning = true;
  54. velx = -velx;
  55. }
  56. }
  57. } else if(subject is Hero && returning) {
  58. successiveHits = 0;
  59. scoreboard.setMultiplier(1);
  60. this.isDead = true;
  61. }
  62. }
  63. }
  64. public function throwHat(goingLeft) {
  65. frameCount = 0;
  66. frameDelay = throwDistance;
  67. if(goingLeft) {
  68. velx = -flySpeed;
  69. } else {
  70. velx = flySpeed;
  71. }
  72. vely = 0;
  73. returning = false;
  74. }
  75. private function flyBack() {
  76. if(owner.x >= this.x) {
  77. velx += inertia;
  78. } else {
  79. velx -= inertia;
  80. }
  81. if(velx >= flySpeed) {
  82. velx = flySpeed;
  83. } else if(velx <= -flySpeed) {
  84. velx = -flySpeed;
  85. }
  86. if(owner.y >= this.y) {
  87. vely += inertia;
  88. } else {
  89. vely -= inertia;
  90. }
  91. if(vely >= flySpeed) {
  92. vely = flySpeed;
  93. } else if(vely <= -flySpeed) {
  94. vely = -flySpeed;
  95. }
  96. }
  97. override public function update():void {
  98. animate();
  99. if(frameCount >= frameDelay) {
  100. frameCount = 0;
  101. if(!returning) {
  102. returning = true;
  103. } else {
  104. flyBack();
  105. }
  106. frameDelay = 2;
  107. } else {
  108. frameCount++;
  109. }
  110. this.y += vely / 2; // update our y variable
  111. this.x += velx / 2; // update our x variable
  112. /*
  113. if(velx > 0) {
  114. this.x = Math.ceil(this.x);
  115. } else {
  116. this.x = Math.floor(this.x);
  117. }
  118. if(vely > 0) {
  119. this.y = Math.ceil(this.y);
  120. } else {
  121. this.y = Math.floor(this.y);
  122. }
  123. */
  124. notifyObservers();
  125. }
  126. }
  127. }