Advertisement
Guest User

Untitled

a guest
Jun 25th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package yuriah.entities;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5.  
  6. import javax.swing.ImageIcon;
  7.  
  8. import yuriah.map.Map;
  9.  
  10. public class Player {
  11. private float x;
  12. private float y;
  13. private float ang;
  14. private float size = 0.3f;
  15. public Image image;
  16. private Map map;
  17.  
  18. public Player(Map map, float x, float y) {
  19. this.map = map;
  20. this.x = x;
  21. this.y = y;
  22. }
  23.  
  24. public boolean move(float dx, float dy) {
  25. float nx = x + dx;
  26. float ny = y + dy;
  27.  
  28. if (validLocation(nx, ny)) {
  29. x = nx;
  30. y = ny;
  31. ang = (float) (Math.atan2(dy, dx) - (Math.PI / 2));
  32. return true;
  33. }
  34. return false;
  35. }
  36.  
  37. public boolean validLocation(float nx, float ny) {
  38. if (map.blocked(nx - size, ny - size)) {
  39. return false;
  40. }
  41. if (map.blocked(nx + size, ny - size)) {
  42. return false;
  43. }
  44. if (map.blocked(nx - size, ny + size)) {
  45. return false;
  46. }
  47. if (map.blocked(nx + size, ny + size)) {
  48. return false;
  49. }
  50. return true;
  51. }
  52.  
  53.  
  54. public void paint(Graphics2D g) {
  55. this.image = new ImageIcon(getClass().getResource("sprite.gif")).getImage();
  56.  
  57. if (x <= .45)
  58. x = .45f;
  59. if (y <= .35)
  60. y = .35f;
  61. int xp = (int) (Map.TILE_SIZE * x);
  62. int yp = (int) (Map.TILE_SIZE * y);
  63.  
  64. g.drawString(x + ":" + y, 100, 100);
  65.  
  66. g.rotate(ang, xp, yp);
  67. g.drawImage(image, (int) (xp - 16), (int) (yp - 16), null);
  68. g.rotate(-ang, xp, yp);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement