package ruscz.game.gameOfProd.Cells; import ruscz.game.gameOfProd.Image.ImagePerson; import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import java.awt.*; public class Person extends Cell { private Map map; private ImagePerson imagePerson; private int directionMove; private int directionSight; private int directionMoveLast; private boolean isMoving; private static boolean isInEvent; private int size; public Map getMap() { return map; } public void setDirectionMoveLast(int directionMoveLast) { this.directionMoveLast = directionMoveLast; } public void setDirectionSight(int directionSight) { this.directionSight = directionSight; } public Person(Map map) { super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); this.directionMove = 0; this.map = map; this.directionSight = 3; this.isMoving = false; this.size = WindowOfFreeMove.CELL_SIZE; } public Person(int x, int y, Map map) { super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED); this.directionMove = 0; this.directionSight = 3; this.isMoving = false; this.map = map; this.size = WindowOfFreeMove.CELL_SIZE; } public void setDirectionMove(int directionMove) { if ((directionMove >= WindowOfFreeMove.KEY_LEFT) && (directionMove <= WindowOfFreeMove.KEY_EVENT)) this.directionMove = directionMove; } public void setImagePerson(String name, Rectangle[] rectangles) { this.imagePerson = new ImagePerson(name, rectangles); } public void move() { if (!isMoving) { 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; case WindowOfFreeMove.KEY_EVENT: if (isEventInFront(x,y,directionSight) && !isInEvent) { isInEvent = true; map.findInteractionByXY(x, y, directionSight).startEvent(); } else if (isEventInFront(x,y,directionSight) && isInEvent) { map.findInteractionByXY(x, y, directionSight).readNextLine(); } else if (map.isActiveTriggerCell(x,y) && isInEvent) { map.findTriggerByXY(x,y).readNextLine(); } else if (isDoorInFront(x,y,directionSight)) { DoorCell door = map.findDoorByXY(x, y, directionSight); if (!door.isLocked()) { if (door.isOpen()) door.setOpen(false); else door.setOpen(true); } else if (!isInEvent) { isInEvent = true; door.startEvent(); } else { door.readNextLine(); } } break; } if (directionMove != WindowOfFreeMove.KEY_EVENT) { if (directionMove != 0 && !map.isBusyCell(x, y) && !map.isInteractionCell(x, y) && !map.isClosedDoor(x, y)) { directionMoveLast = directionMove; isMoving = true; } else if (directionMove != 0 && map.isBusyCell(x, y)) { directionMoveLast = 0; } else if (directionMove != 0 && map.isInteractionCell(x, y)) { directionMoveLast = 0; } else if (directionMove != 0 && map.isClosedDoor(x, y)) { directionMoveLast = 0; } if (map.isCellMoveTo(x, y) && !map.isClosedDoor(x, y)) { set(x, y); WindowOfFreeMove.setCanPersonMove(false); if (!isMoving) { map.findEventByXY(x, y).moveToMap(); } } else { if (!map.isInteractionCell(x, y) && !map.isBusyCell(x, y) && !map.isClosedDoor(x, y)) { set(x, y); } } } directionMove = 0; if (map.isActiveTriggerCell(x,y) && !isInEvent) { set(x, y); WindowOfFreeMove.setCanPersonMove(false); if (!isMoving) { isInEvent = true; map.findTriggerByXY(x,y).startEvent(); WindowOfFreeMove.setNextMove(WindowOfFreeMove.KEY_EVENT); } } } } private boolean isEventInFront(int x, int y, int directionSight) { switch (directionSight) { case 1: x--; break; case 3: x++; break; case 2: y--; break; case 0: y++; break; } return map.isInteractionCell(x, y); } private boolean isDoorInFront(int x, int y, int directionSight) { switch (directionSight) { case 1: x--; break; case 3: x++; break; case 2: y--; break; case 0: y++; break; } return map.isDoor(x, y); } public void stopPerson(){ directionMove = 0; directionMoveLast = 0; size = 0; isMoving = false; } public boolean isInPerson(int x, int y) { return this.getX() == x && this.getY() == y; } @Override public void paint(Graphics2D g) { super.paint(g); if (size == 0) { isMoving = false; } if (isMoving) { switch (directionMoveLast) { case WindowOfFreeMove.KEY_LEFT: animateMove(g, 1); break; case WindowOfFreeMove.KEY_RIGHT: animateMove(g, 3); break; case WindowOfFreeMove.KEY_UP: animateMove(g, 2); break; case WindowOfFreeMove.KEY_DOWN: animateMove(g, 0); break; } if (size > 0 && isMoving) { size -= 2; } else { isMoving = false; } } else { size = WindowOfFreeMove.CELL_SIZE; g.drawImage(imagePerson.getImagesOfDirection()[directionSight + 4], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } } public void animateMove(Graphics2D g, int direction){ int Xsize = 0; int Ysize = 0; switch (direction) { case 0: Ysize -= size; break; case 1: Xsize = size; break; case 2: Ysize = size; break; case 3: Xsize -= size; break; } if (size > WindowOfFreeMove.CELL_SIZE * 3 / 4) { g.drawImage(imagePerson.getImagesOfDirection()[direction], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } else if (size > WindowOfFreeMove.CELL_SIZE / 2) { g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } else if (size > WindowOfFreeMove.CELL_SIZE / 4) { g.drawImage(imagePerson.getImagesOfDirection()[direction + 8], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } else{ g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } } public boolean getIsMoving() { return isMoving; } public void setIsMoving(boolean isMoving) { this.isMoving = isMoving; } public void setSize(int size) { this.size = size; } public int getSize() { return size; } public int getDirectionMoveLast() {return directionMoveLast; } public static void setInEvent(boolean isInEvent) { Person.isInEvent = isInEvent; } }