PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/game/engine/actors/Walker.as

https://bitbucket.org/charliehoey/gatsby
ActionScript | 289 lines | 236 code | 44 blank | 9 comment | 89 complexity | 6fed176734ad5cf66c4127e8c90e8b52 MD5 | raw file
  1. package engine.actors {
  2. import engine.actors.Animatable;
  3. import engine.actors.geoms.*;
  4. import flash.geom.Point;
  5. import engine.Scoreboard;
  6. public class Walker extends Animatable {
  7. // MAX_VEL_Y has to be less than the height of most shallow platform.
  8. // otherwise you will fall through the ground
  9. protected const MAX_VEL_Y:Number = 6; // so min platform height should be 22.
  10. protected const MAX_VEL_X:Number = 3;
  11. protected var walkSpeed:Number = 1;
  12. //DON'T CHANGE THESE
  13. public var vely:Number = 0;
  14. public var velx:Number = walkSpeed;
  15. protected var scoreboard = Scoreboard.getInstance();
  16. //CHANGE THESE
  17. public var jumpVelocity:uint = 10; //exponential. 20 jumps 3x higher than 10
  18. public var Xspeed:Number = 2;
  19. //
  20. public var fric:Number = 1; //frictional coefficient of go
  21. public var gravity:Number = .5; //how much the velocity changes on each frameEvent
  22. protected var jumpCount:Number = 0;
  23. protected var jumpPressed:Boolean = false;
  24. protected var attackFlag:Boolean = false;
  25. protected var duckFlag:Boolean = false;
  26. protected var standFlag:Boolean = false;
  27. public var stuckTo = false; // what surface are we currently stuck to
  28. protected var hitDirection:String; // what direction were we hit from
  29. protected var frameStarted:Boolean = false;
  30. protected var statusSet:Boolean = false;
  31. protected var walkEnabled = true;
  32. protected var jumpEnabled = false;
  33. protected var deadFlag:Boolean = false;
  34. protected var newAction;
  35. protected var prevAction;
  36. protected var myAction:uint = 3;
  37. public function Walker() {
  38. trace("mover created");
  39. }
  40. public function land(observer):void {
  41. this.vely = 0;
  42. var observerHeight:Number;
  43. if(observer is FountainPlatform) {
  44. observerHeight = observer.convertedY;
  45. } else {
  46. observerHeight = observer.y;
  47. }
  48. if(observer is FountainPlatform) {
  49. this.y = (observerHeight + observer.velocity) - this.height;
  50. } else {
  51. this.y = observerHeight - this.height;
  52. }
  53. jumpCount = 0;
  54. standFlag = true;
  55. if(stuckTo != observer) {
  56. stuckTo = observer;
  57. }
  58. }
  59. public function depart(observer):void {
  60. standFlag = false;
  61. stuckTo = false;
  62. }
  63. public function checkRight(observer):Boolean {
  64. if((this.x + this.collide_left_ground) < observer.x + observer.width && this.x + this.collide_right_ground > observer.x + observer.width) { // if we're collided with the square's right side currently
  65. return true;
  66. }
  67. return false;
  68. }
  69. public function checkLeft(observer):Boolean {
  70. if((this.x + this.collide_right_ground) > observer.x && this.x + this.collide_left_ground < observer.x) { // if we're collided with the block's left side currently
  71. return true;
  72. }
  73. return false;
  74. }
  75. public function checkTop(observer):Boolean {
  76. var observerHeight:Number;
  77. var observerX:Number;
  78. if(observer is FountainPlatform) {
  79. observerHeight = observer.convertedY;
  80. observerX = observer.convertedX;
  81. } else {
  82. observerHeight = observer.y;
  83. observerX = observer.x;
  84. }
  85. if((this.x + this.collide_right_ground) > observerX && (this.x + this.collide_left_ground) < (observerX + observer.width)) {
  86. if((this.y + this.height) >= observerHeight) { // if we're collided with the top currently
  87. if((this.y + this.height) - this.vely <= observerHeight) { // and we hadn't collided in the previous frame
  88. return true; // then we've just collided with the top
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. public function checkWidth(observer):Boolean {
  95. var observerX:Number;
  96. if(observer is FountainPlatform) {
  97. observerX = observer.convertedX;
  98. } else {
  99. observerX = observer.x;
  100. }
  101. if((this.x + this.collide_right_ground) > observerX && (this.x + this.collide_left_ground) < (observerX + observer.width)) {
  102. return true;
  103. }
  104. return false;
  105. }
  106. public function flipCollide(collide) {
  107. return this.width - collide;
  108. }
  109. override public function collide(observer, ...args) {
  110. if(observer is Cloud || observer is FountainPlatform) { // if we're a cloud or a fountain
  111. if(checkTop(observer) || observer == stuckTo) { // if we just collided with the top of it, or we're already stuckTo it
  112. land(observer); // land on it again
  113. }
  114. } else if(observer is Block) { // otherwise, if it's a block
  115. if(observer == stuckTo) { // otherwise, if we're colliding with the thing we're stuck to
  116. land(observer); // continue to follow it
  117. } else if(myAction == FALL && checkTop(observer)) { // if we just fell and collided with the top
  118. land(observer); // land us on the top
  119. } else if(checkRight(observer)) { // if we hit the right edge of the block
  120. this.x = (observer.x + observer.width) - collide_left_ground; // set us to there
  121. } else if(checkLeft(observer)) { // if we hit the left edge of the block
  122. this.x = observer.x - collide_right_ground; // stop us there
  123. }
  124. } else if(observer is KillBlock) {
  125. receiveDamage(observer);
  126. }
  127. }
  128. public function updateStatus():void {
  129. newAction = STAND; // by default, we're standing
  130. if(standFlag) { // if we're standing on something
  131. if(!stuckTo.checkGroundCollision(this)) { // and we're not colliding with it anymore
  132. if(stuckTo is FountainPlatform && checkWidth(stuckTo) && jumpCount == 0) {
  133. land(stuckTo);
  134. } else {
  135. depart(stuckTo); // depart whatever platform we were on
  136. }
  137. } else if(velx == 0) { // otherwise, if we're not moving
  138. newAction = STAND; // we're standing
  139. } else { // otherwise, we're walking
  140. newAction = WALK;
  141. }
  142. } else if(vely < 0) { // if we're going up
  143. newAction = JUMP; // we're jumping
  144. } else if(vely > 0) { // if we're going down
  145. newAction = FALL; // we're falling
  146. } else { // otherwise, we just peaked in a jump
  147. newAction = FALL; // now we're falling
  148. }
  149. if(newAction != prevAction) {
  150. setAction(newAction);
  151. setAnimation(newAction);
  152. }
  153. prevAction = newAction;
  154. }
  155. public function applyPhysics():void {
  156. // velocitize y (gravity)
  157. if (this.vely < MAX_VEL_Y) {
  158. this.vely += this.gravity;
  159. }
  160. // apply friction
  161. if (this.velx > 0) {
  162. this.velx -= fric;
  163. } else if (this.velx < 0) {
  164. this.velx += fric;
  165. }
  166. // check map bounds
  167. if(this.x < 0) {
  168. this.x = 0;
  169. }
  170. }
  171. protected function customUpdate() {
  172. // this will get overwritten later
  173. }
  174. public function moveMe():void {
  175. if(frameCount >= frameDelay) {
  176. frameStarted = true;
  177. statusSet = false;
  178. this.y += vely / 2; // update our y variable
  179. this.x += velx / 2; // update our x variable
  180. if(velx > 0) {
  181. this.x = Math.ceil(this.x);
  182. } else {
  183. this.x = Math.floor(this.x);
  184. }
  185. if(vely > 0) {
  186. this.y = Math.ceil(this.y);
  187. } else {
  188. this.y = Math.floor(this.y);
  189. }
  190. notifyObservers(); // tell everybody where we are now
  191. applyPhysics(); // apply our enviromental variables
  192. updateStatus(); // update our status
  193. frameCount = 0;
  194. frameStarted = false;
  195. } else {
  196. frameCount++;
  197. }
  198. animate();
  199. customUpdate();
  200. }
  201. public function setAction(myAction) {
  202. if(this.myAction != myAction) { // if we're defining a new action
  203. this.myAction = myAction;
  204. if(myAction == JUMP || myAction == FALL) {
  205. walkEnabled = true;
  206. jumpEnabled = false;
  207. } else if(myAction == STAND || myAction == WALK) {
  208. walkEnabled = true;
  209. jumpEnabled = true;
  210. }
  211. }
  212. }
  213. public function setAnimation(status) {
  214. switch(myAction) {
  215. default:
  216. setLoop(loopRow, startFrame, endFrame, loopFrame, loopType);
  217. break;
  218. }
  219. }
  220. public function killMe():void {
  221. if(myStatus != 'DYING') {
  222. HP = 0;
  223. myStatus = 'DYING';
  224. if(hitDirection == 'LEFT') {
  225. this.velx = 3;
  226. } else if(hitDirection == 'RIGHT') {
  227. this.velx = -3;
  228. }
  229. notifyObservers();
  230. this.vely = -4;
  231. gravity = .25;
  232. }
  233. if(frameCount >= frameDelay) {
  234. applyPhysics();
  235. this.y += vely;
  236. animate();
  237. if(this.y > 240) {
  238. myMap.removeFromMap(this);
  239. }
  240. } else {
  241. frameCount++;
  242. }
  243. }
  244. }
  245. }