Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.ImageIcon;
- public class player
- {
- private int rx;
- private int ry;
- private Image PLAYER;
- private boolean moving;
- protected Rectangle playerRec;
- map m;
- private int direction;
- public player(map m)
- {
- this.m=m;
- PLAYER=new ImageIcon("sprites/adventurer.png").getImage();
- setx(m.findSpawnx());
- sety(m.findSpawny());
- playerRec=new Rectangle(rx,ry,48,48);
- }
- public void setx(int n)
- {
- rx=n*48;
- }
- public void sety(int n)
- {
- ry=n*48;
- }
- public void draw(Graphics g)
- {
- g.drawImage(PLAYER, playerRec.x, playerRec.y, null);
- }
- public void setLeft(boolean moving)
- {
- this.moving=moving;
- if (moving==true)
- {
- direction=0;
- }
- }
- public void setRight(boolean moving)
- {
- this.moving=moving;
- if (moving==true)
- {
- direction=1;
- }
- }
- public void setUp(boolean moving)
- {
- this.moving=moving;
- if (moving==true)
- {
- direction=2;
- }
- }
- public void setDown(boolean moving){
- this.moving=moving;
- if (moving==true)
- {
- direction=3;
- }
- }
- public void update()
- {
- move();
- }
- private void move()
- {
- if (direction==0 && moving==true && colCheck(0)==true)
- {
- playerRec.x=playerRec.x-48;
- moving = false;
- }
- if (direction==1&&moving==true&& colCheck(1)==true)
- {
- playerRec.x=playerRec.x+48;
- moving = false;
- }
- if (direction==2&&moving==true&& colCheck(2)==true)
- {
- playerRec.y=playerRec.y-48;
- moving = false;
- }
- if (direction==3&&moving==true&& colCheck(3)==true)
- {
- playerRec.y=playerRec.y+48;
- moving = false;
- }
- }
- private boolean colCheck(int n)
- {
- if(n==0)
- {
- if(m.tileMap[playerRec.y/48][(playerRec.x/48)-1]==1||m.tileMap[playerRec.y/48][(playerRec.x/48)-1]==4)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- if(n==1)
- {
- if(m.tileMap[playerRec.y/48][(playerRec.x/48)+1]==1||m.tileMap[playerRec.y/48][(playerRec.x/48)+1]==4)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- if(n==2)
- {
- if(m.tileMap[(playerRec.y/48)-1][(playerRec.x/48)]==1||m.tileMap[(playerRec.y/48)-1][(playerRec.x/48)]==4)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- if(n==3)
- {
- if(m.tileMap[(playerRec.y/48)+1][playerRec.x/48]==1||m.tileMap[(playerRec.y/48)+1][playerRec.x/48]==4)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement