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

/com.project.flickhockey.Flick_Hockey/src/com/project/flickhockey/Player.java

https://github.com/1002064/BDH-FlickHockey
Java | 82 lines | 57 code | 18 blank | 7 comment | 6 complexity | 4cb36e114e4c0037500266c6f96be5b9 MD5 | raw file
  1. package com.project.flickhockey;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Rect;
  4. import android.graphics.RectF;
  5. import android.media.SoundPool;
  6. public class Player extends AnimatedGraphic {
  7. public enum AnimationState {
  8. STATIC, MOVING, SHOOTING, CHEERING
  9. };
  10. private SoundPool soundPool;
  11. public Player(Bitmap bitmap) {
  12. super(bitmap);
  13. this.frameTimer = 0;
  14. this.frameNumber = 1;
  15. }
  16. public void setSoundPool(SoundPool soundPool) {
  17. this.soundPool = soundPool;
  18. }
  19. public void setFps(int fps) {
  20. if (fps < 1) {
  21. fps = 1;
  22. }
  23. frameDelay = 1000 / fps;
  24. }
  25. public void setFrames(int frames) {
  26. frameTotal = frames - 1;
  27. super.setWidth(super.getWidth() / frames);
  28. }
  29. public void Animate(AnimationState animState) {
  30. if (animState == AnimationState.MOVING) {
  31. // if it's time to change frame
  32. if (frameTimer >= frameDelay) {
  33. // if the frame is not the final frame of the animation
  34. if (frameNumber < frameTotal) {
  35. // move to the next frame
  36. frameNumber++;
  37. }
  38. // if the frame is the final frame of the animation
  39. else {
  40. // move back to the start of the animation
  41. frameNumber = 0;
  42. }
  43. // reset the timer
  44. frameTimer = 0;
  45. }
  46. // increment the timer
  47. frameTimer++;
  48. }
  49. }
  50. public Rect getVisibleRect() {
  51. Rect visiblePlayerRect = super.getVisibleRect();
  52. visiblePlayerRect.left = frameNumber * super.getWidth();
  53. visiblePlayerRect.right = (frameNumber + 1) * super.getWidth();
  54. visiblePlayerRect.top = 0;
  55. return visiblePlayerRect;
  56. }
  57. @Override
  58. public RectF getCollisionRect() {
  59. RectF playerCollisionRect = super.getCollisionRect();
  60. playerCollisionRect.top = playerCollisionRect.bottom - super.getHeight()
  61. / 2;
  62. return playerCollisionRect;
  63. }
  64. }