PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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