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

/game/engine/actors/weapons/BottleWeapon.as

https://bitbucket.org/charliehoey/gatsby
ActionScript | 113 lines | 94 code | 17 blank | 2 comment | 14 complexity | 3f09272f6658f1f1003734545dedc53e MD5 | raw file
  1. package engine.actors.weapons {
  2. import engine.IObserver;
  3. import engine.actors.player.Hero;
  4. import engine.actors.enemies.Enemy;
  5. import engine.actors.geoms.*;
  6. import flash.events.Event;
  7. import engine.actors.Animatable;
  8. public class BottleWeapon extends Weapon implements IObserver {
  9. private var throwDistance:int = 120;
  10. private var throwHeight = -19;
  11. private var velx:Number = 0;
  12. private var vely:Number = 0;
  13. private var gravity = 1;
  14. private const MAX_VEL_Y = 8;
  15. private const MAX_VEL_X = 5;
  16. public function BottleWeapon(owner) {
  17. super(owner);
  18. }
  19. override public function setup() {
  20. flySpeed = 2;
  21. vely = throwHeight;
  22. damage = 1;
  23. myName = "BottleWeapon";
  24. mySkin = "BottleWeaponSkin";
  25. tilesWide = 1;
  26. tilesTall = 1;
  27. collide_left = 2; // what pixel do we collide on on the left
  28. collide_right = 14; // what pixel do we collide on on the right
  29. startFrame = 0; // the first frame to loop on
  30. endFrame = 7; // the final frame in the row
  31. nowFrame = 0; // current frame in row
  32. loopFrame = 0; // frame at which to loop
  33. loopType = 0; // 0 loops, 1 bounces
  34. loopRow = 0; // which row are we on
  35. loopDir = 1; // loop forward (to the right) by default
  36. speed = 2; // how many frames should go by before we advance
  37. }
  38. override public function notify(subject):void {
  39. if(subject is Hero) {
  40. if(checkCollision(subject)) {
  41. subject.receiveDamage(this);
  42. frameCount = frameDelay;
  43. }
  44. }
  45. }
  46. public function throwBottle(goingLeft) {
  47. this.vely = throwHeight;
  48. frameCount = 0;
  49. frameDelay = throwDistance;
  50. if(goingLeft) {
  51. velx = -flySpeed;
  52. } else {
  53. velx = flySpeed;
  54. }
  55. }
  56. public function applyPhysics():void {
  57. // velocitize y (gravity)
  58. if (this.vely < MAX_VEL_Y) {
  59. this.vely += this.gravity;
  60. }
  61. // check map bounds
  62. if(this.x < 0) {
  63. this.x = 0;
  64. }
  65. }
  66. override public function update():void {
  67. animate();
  68. if(frameCount >= frameDelay) {
  69. frameCount = 0;
  70. owner.catchBottle(this);
  71. } else {
  72. frameCount++;
  73. }
  74. applyPhysics();
  75. if(velx > MAX_VEL_X) {
  76. velx = MAX_VEL_X;
  77. }
  78. if(vely > MAX_VEL_Y) {
  79. vely = MAX_VEL_Y;
  80. }
  81. this.y += vely / 2; // update our y variable
  82. this.x += velx / 2; // update our x variable
  83. if(velx > 0) {
  84. this.x = Math.ceil(this.x);
  85. } else {
  86. this.x = Math.floor(this.x);
  87. }
  88. if(vely > 0) {
  89. this.y = Math.ceil(this.y);
  90. } else {
  91. this.y = Math.floor(this.y);
  92. }
  93. notifyObservers();
  94. }
  95. }
  96. }