Character.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.darkdimension.ritle_run;
  2. import android.graphics.Canvas;
  3. import android.graphics.Paint;
  4. public class Character extends DiPoint
  5. {
  6. private enum State
  7. {
  8. RUNNING,
  9. FALLING,
  10. JUMPING
  11. }
  12. //Animation
  13. private DiAnimation animation = new DiAnimation(24);
  14. private int aJump = 15;
  15. //AirJump
  16. private DiPoint pAirJump = new DiPoint(40);
  17. private DiAnimation aAirJump = new DiAnimation(15);
  18. private Paint invisJump = new Paint();
  19. private boolean canAirJump = true;
  20. //Character State
  21. private State state = State.FALLING;
  22. //Frame
  23. private int stateFrame = 6;
  24. private int currentFrame = 0;
  25. //Shield & land
  26. private boolean isShielded = false;
  27. private boolean justLanded = false;
  28. public int walkingOn = 0;
  29. //Magic Circle
  30. private int circleSize = 0, visualCircleSize = 0;
  31. private Paint circlePaint = new Paint();
  32. public Character()
  33. {
  34. super(28);
  35. pAirJump.setRight(0);
  36. if (DiPoint.firstChar) circlePaint.setARGB(50, 10, 10, 10);
  37. else circlePaint.setARGB(75, 113, 40, 167);
  38. circlePaint.setAntiAlias(true);
  39. animation.start();
  40. }
  41. public boolean update(int groundLevel, int jumpHeight)
  42. {
  43. if (justLanded)
  44. justLanded = false;
  45. //Update state
  46. switch (state)
  47. {
  48. case RUNNING:
  49. //If running off edge, fall down
  50. if (y +image[frame].getHeight() != groundLevel)
  51. {
  52. state = State.FALLING;
  53. stateFrame = 8;
  54. currentFrame = 0;
  55. }
  56. break;
  57. case FALLING:
  58. //Apply gravity
  59. y += jumpHeight /12;
  60. //If fell on ground
  61. if (y +image[frame].getHeight() >= groundLevel
  62. && y +image[frame].getHeight() < groundLevel +jumpHeight /12)
  63. {
  64. //Set character on top of ground
  65. y = groundLevel -image[frame].getHeight();
  66. //Start Walking
  67. state = State.RUNNING;
  68. stateFrame = 0;
  69. currentFrame = 0;
  70. justLanded = true;
  71. canAirJump = true;
  72. }
  73. break;
  74. case JUMPING:
  75. //Keep jumping
  76. y -= jumpHeight /15;
  77. //If animation reaches end
  78. if (aJump > 15)
  79. {
  80. //Start Falling
  81. state = State.FALLING;
  82. stateFrame = 8;
  83. currentFrame = 0;
  84. }
  85. //Advance animation
  86. aJump++;
  87. break;
  88. }
  89. //Animation
  90. if (animation.update())
  91. if (animation.getCounter() %4 == 0)
  92. {
  93. currentFrame++;
  94. if (currentFrame >= 8
  95. || (stateFrame != 0 && currentFrame >= 1)) currentFrame = 0;
  96. if (animation.isAtEnd())
  97. animation.startFromBeginning();
  98. if ((currentFrame == 1 || currentFrame == 5)
  99. && stateFrame == 0)
  100. GameSurface.playSound(0);
  101. }
  102. if (visualCircleSize > circleSize) visualCircleSize -= 4 *GameSurface.density;
  103. else
  104. if (visualCircleSize < circleSize) visualCircleSize += 4 *GameSurface.density;
  105. if (aAirJump.update())
  106. invisJump.setAlpha(aAirJump.getDistance(255));
  107. pAirJump.changeX(-(int)(4 *GameSurface.density));
  108. return true;
  109. }
  110. @Override
  111. public void draw(Canvas canvas, Paint paint)
  112. {
  113. pAirJump.draw(canvas, invisJump);
  114. canvas.drawCircle(this.getCenterX(), this.getCenterY(), visualCircleSize, circlePaint);
  115. if (isShielded)
  116. canvas.drawBitmap(image[39], x, y, paint);
  117. canvas.drawBitmap(image[frame +stateFrame +currentFrame], x, y, paint);
  118. if (isShielded)
  119. canvas.drawBitmap(image[38], x, y, paint);
  120. }
  121. public void drawSimple(Canvas canvas, Paint paint) {canvas.drawBitmap(image[frame +currentFrame], x, y, paint);}
  122. //Actions
  123. public void jump()
  124. {
  125. if (aJump > 5)
  126. if (canAirJump || circleSize > 0)
  127. {
  128. //Use Mana
  129. if (!canAirJump) circleSize -= 36 *GameSurface.density;
  130. else
  131. //Waste Air Jump
  132. if (state == State.FALLING || state == State.JUMPING)
  133. {
  134. canAirJump = false;
  135. pAirJump.setCenterY(getBottom());
  136. pAirJump.setCenterX(getCenterX());
  137. aAirJump.startFromEnd();
  138. }
  139. //Jump Animation
  140. state = State.JUMPING;
  141. stateFrame = 9;
  142. currentFrame = 0;
  143. aJump = 0;
  144. //Sound
  145. GameSurface.playSound(1);
  146. }
  147. }
  148. public boolean animate()
  149. {
  150. //Animation
  151. if (animation.update())
  152. if (animation.getCounter() %4 == 0)
  153. {
  154. currentFrame++;
  155. if (currentFrame >= 8) currentFrame = 0;
  156. if (animation.isAtEnd())
  157. animation.startFromBeginning();
  158. return true;
  159. }
  160. return false;
  161. }
  162. public void giveShield() {isShielded = true;}
  163. public void loseShield() {isShielded = false;}
  164. public boolean hasShield() {return isShielded;}
  165. public void addManaLevel()
  166. {
  167. //If circleSize smaller than max size
  168. if (circleSize < 108 *GameSurface.density)
  169. {
  170. //Make circle bigger
  171. circleSize += 36 *GameSurface.density;
  172. GameSurface.playSound(3);
  173. }
  174. //circleSize greater or equal to max size
  175. else
  176. {
  177. //Remove circle
  178. circleSize = 0;
  179. GameSurface.playSound(4);
  180. }
  181. }
  182. public void loseMana() {circleSize = 0;}
  183. public boolean hasMana() {return circleSize > 0;}
  184. public boolean justLanded() {return justLanded;}
  185. public int[] getSquare()
  186. {
  187. int[] returner = new int[4];
  188. returner[0] = x +image[frame].getWidth () *3/6;
  189. returner[1] = y +image[frame].getHeight() *3/6;
  190. returner[2] = x +image[frame].getWidth () *4/6;
  191. returner[3] = y +image[frame].getHeight() *4/6;
  192. return returner;
  193. }
  194. }