Advertisement
catsaremyreligion

player.java

Apr 26th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.ImageIcon;
  3.  
  4. public class player
  5. {
  6.  
  7. private int rx;
  8. private int ry;
  9. private Image PLAYER;
  10. private boolean moving;
  11. protected Rectangle playerRec;
  12. map m;
  13. private int direction;
  14. public player(map m)
  15.  
  16. {
  17. this.m=m;
  18. PLAYER=new ImageIcon("sprites/adventurer.png").getImage();
  19. setx(m.findSpawnx());
  20. sety(m.findSpawny());
  21. playerRec=new Rectangle(rx,ry,48,48);
  22. }
  23. public void setx(int n)
  24. {
  25. rx=n*48;
  26. }
  27. public void sety(int n)
  28. {
  29. ry=n*48;
  30. }
  31. public void draw(Graphics g)
  32. {
  33. g.drawImage(PLAYER, playerRec.x, playerRec.y, null);
  34.  
  35. }
  36. public void setLeft(boolean moving)
  37. {
  38. this.moving=moving;
  39. if (moving==true)
  40. {
  41. direction=0;
  42. }
  43.  
  44. }
  45. public void setRight(boolean moving)
  46. {
  47. this.moving=moving;
  48. if (moving==true)
  49. {
  50. direction=1;
  51. }
  52. }
  53. public void setUp(boolean moving)
  54. {
  55. this.moving=moving;
  56. if (moving==true)
  57. {
  58. direction=2;
  59. }
  60. }
  61. public void setDown(boolean moving){
  62. this.moving=moving;
  63. if (moving==true)
  64. {
  65. direction=3;
  66. }
  67. }
  68.  
  69. public void update()
  70. {
  71. move();
  72. }
  73.  
  74. private void move()
  75. {
  76. if (direction==0 && moving==true && colCheck(0)==true)
  77. {
  78. playerRec.x=playerRec.x-48;
  79. moving = false;
  80. }
  81. if (direction==1&&moving==true&& colCheck(1)==true)
  82. {
  83. playerRec.x=playerRec.x+48;
  84. moving = false;
  85. }
  86. if (direction==2&&moving==true&& colCheck(2)==true)
  87. {
  88. playerRec.y=playerRec.y-48;
  89. moving = false;
  90. }
  91. if (direction==3&&moving==true&& colCheck(3)==true)
  92. {
  93. playerRec.y=playerRec.y+48;
  94. moving = false;
  95. }
  96. }
  97. private boolean colCheck(int n)
  98. {
  99. if(n==0)
  100. {
  101. if(m.tileMap[playerRec.y/48][(playerRec.x/48)-1]==1||m.tileMap[playerRec.y/48][(playerRec.x/48)-1]==4)
  102. {
  103. return false;
  104. }
  105. else
  106. {
  107. return true;
  108. }
  109.  
  110. }
  111. if(n==1)
  112. {
  113. if(m.tileMap[playerRec.y/48][(playerRec.x/48)+1]==1||m.tileMap[playerRec.y/48][(playerRec.x/48)+1]==4)
  114. {
  115. return false;
  116. }
  117. else
  118. {
  119. return true;
  120. }
  121.  
  122. }
  123. if(n==2)
  124. {
  125. if(m.tileMap[(playerRec.y/48)-1][(playerRec.x/48)]==1||m.tileMap[(playerRec.y/48)-1][(playerRec.x/48)]==4)
  126. {
  127. return false;
  128. }
  129. else
  130. {
  131. return true;
  132. }
  133.  
  134. }
  135. if(n==3)
  136. {
  137. if(m.tileMap[(playerRec.y/48)+1][playerRec.x/48]==1||m.tileMap[(playerRec.y/48)+1][playerRec.x/48]==4)
  138. {
  139. return false;
  140. }
  141. else
  142. {
  143. return true;
  144. }
  145.  
  146. }
  147. else
  148. {
  149. return true;
  150. }
  151.  
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement