Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package yuriah.entities;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import javax.swing.ImageIcon;
- import yuriah.map.Map;
- public class Player {
- private float x;
- private float y;
- private float ang;
- private float size = 0.3f;
- public Image image;
- private Map map;
- public Player(Map map, float x, float y) {
- this.map = map;
- this.x = x;
- this.y = y;
- }
- public boolean move(float dx, float dy) {
- float nx = x + dx;
- float ny = y + dy;
- if (validLocation(nx, ny)) {
- x = nx;
- y = ny;
- ang = (float) (Math.atan2(dy, dx) - (Math.PI / 2));
- return true;
- }
- return false;
- }
- public boolean validLocation(float nx, float ny) {
- if (map.blocked(nx - size, ny - size)) {
- return false;
- }
- if (map.blocked(nx + size, ny - size)) {
- return false;
- }
- if (map.blocked(nx - size, ny + size)) {
- return false;
- }
- if (map.blocked(nx + size, ny + size)) {
- return false;
- }
- return true;
- }
- public void paint(Graphics2D g) {
- this.image = new ImageIcon(getClass().getResource("sprite.gif")).getImage();
- if (x <= .45)
- x = .45f;
- if (y <= .35)
- y = .35f;
- int xp = (int) (Map.TILE_SIZE * x);
- int yp = (int) (Map.TILE_SIZE * y);
- g.drawString(x + ":" + y, 100, 100);
- g.rotate(ang, xp, yp);
- g.drawImage(image, (int) (xp - 16), (int) (yp - 16), null);
- g.rotate(-ang, xp, yp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement