102 lines
3.5 KiB
Java
102 lines
3.5 KiB
Java
import java.awt.*;
|
|
|
|
public class Person extends Cell {
|
|
private ImagePerson imagePerson;
|
|
private int directionMove;
|
|
private int directionSight;
|
|
|
|
public Person() {
|
|
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
|
this.directionMove = 0;
|
|
this.directionSight = 3;
|
|
}
|
|
|
|
public Person(int x, int y) {
|
|
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
|
this.directionMove = 0;
|
|
this.directionSight = 3;
|
|
}
|
|
|
|
public void setDirectionMove(int directionMove) {
|
|
if ((directionMove >= WindowOfFreeMove.KEY_LEFT) && (directionMove <= WindowOfFreeMove.KEY_DOWN))
|
|
this.directionMove = directionMove;
|
|
}
|
|
|
|
public void setImagePerson(String name, Rectangle[] rectangles) {
|
|
this.imagePerson = new ImagePerson(name, rectangles);
|
|
}
|
|
|
|
public void setAnimation(String[] names){
|
|
this.imagePerson.setAnimationOfMoving(names);
|
|
}
|
|
|
|
public void move() {
|
|
int x = this.getX();
|
|
int y = this.getY();
|
|
switch (directionMove) {
|
|
case WindowOfFreeMove.KEY_LEFT:
|
|
directionSight = 1;
|
|
x--;
|
|
if (x < 0)
|
|
x = WindowOfFreeMove.CANVAS_WIDTH - 1;
|
|
break;
|
|
case WindowOfFreeMove.KEY_RIGHT:
|
|
directionSight = 3;
|
|
x++;
|
|
if (x == WindowOfFreeMove.CANVAS_WIDTH)
|
|
x = 0;
|
|
break;
|
|
case WindowOfFreeMove.KEY_UP:
|
|
directionSight = 2;
|
|
y--;
|
|
if (y < 0)
|
|
y = WindowOfFreeMove.CANVAS_HEIGHT - 1;
|
|
break;
|
|
case WindowOfFreeMove.KEY_DOWN:
|
|
directionSight = 0;
|
|
y++;
|
|
if (y == WindowOfFreeMove.CANVAS_HEIGHT)
|
|
y = 0;
|
|
break;
|
|
}
|
|
this.directionMove = 0;
|
|
|
|
if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) {
|
|
WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap();
|
|
} else {
|
|
if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) {
|
|
set(x, y);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public boolean isInPerson(int x, int y) {
|
|
return this.getX() == x && this.getY() == y;
|
|
}
|
|
|
|
@Override
|
|
public void paint(Graphics2D g) {
|
|
super.paint(g);
|
|
if (directionMove == 0)
|
|
g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX()*WindowOfFreeMove.CELL_SIZE,
|
|
getY()*WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
|
|
else{
|
|
switch (directionMove){
|
|
case WindowOfFreeMove.KEY_LEFT:
|
|
g.drawImage(imagePerson.getAnimationOfMoving()[3], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
|
break;
|
|
case WindowOfFreeMove.KEY_RIGHT:
|
|
g.drawImage(imagePerson.getAnimationOfMoving()[1], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
|
break;
|
|
case WindowOfFreeMove.KEY_UP:
|
|
g.drawImage(imagePerson.getAnimationOfMoving()[0], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
|
break;
|
|
case WindowOfFreeMove.KEY_DOWN:
|
|
g.drawImage(imagePerson.getAnimationOfMoving()[2], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|