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 final int WALKING_SPEED = 3; private final Map map; private ImagePerson imagePerson; private int directionMove; private int directionSight; private int directionMoveLast; private boolean isMoving; private static boolean isInEvent; private int distanceOfAnimation; 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.distanceOfAnimation = 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.distanceOfAnimation = WindowOfFreeMove.CELL_SIZE; } public void setDirectionMove(int directionMove) { if (directionMove == WindowOfFreeMove.KEY_LEFT || directionMove == WindowOfFreeMove.KEY_RIGHT || directionMove == WindowOfFreeMove.KEY_UP || directionMove == WindowOfFreeMove.KEY_DOWN || 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() && !isInEvent) { if (door.isOpen()) { door.setOpen(false); map.getSoundManager().playSound(door.getSound(), door.getVolume()); } else { door.setOpen(true); map.getSoundManager().playSound(door.getSound(), door.getVolume()); } } else if (!isInEvent) { isInEvent = true; door.startEvent(); map.getSoundManager().playSound(door.getSound(), door.getVolume()); } else { door.readNextLine(); } } else if (isChestInFront(x, y, directionSight)) { if (!isInEvent) { isInEvent = true; map.findChestByXY(x, y, directionSight).startEvent(); } else { map.findChestByXY(x, y, directionSight).readNextLine(); } } break; } if (directionMove != WindowOfFreeMove.KEY_EVENT && !isInEvent) { if (directionMove != 0 && !map.isBusyCell(x, y, true) && !map.isInteractionCell(x, y) && !map.isClosedDoor(x, y) && !map.isChest(x, y)) { directionMoveLast = directionMove; isMoving = true; } else if (directionMove != 0 && map.isBusyCell(x, y, true)) { directionMoveLast = 0; } else if (directionMove != 0 && map.isInteractionCell(x, y)) { directionMoveLast = 0; } else if (directionMove != 0 && map.isClosedDoor(x, y)) { directionMoveLast = 0; } else if (directionMove != 0 && map.isChest(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, true) && !map.isClosedDoor(x, y) && !map.isChest(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) { int[] coords = map.directionSightSwitch(x, y, directionSight); return map.isInteractionCell(coords[0], coords[1]); } private boolean isDoorInFront(int x, int y, int directionSight) { int[] coords = map.directionSightSwitch(x, y, directionSight); return map.isDoor(coords[0], coords[1]); } private boolean isChestInFront(int x, int y, int directionSight) { int[] coords = map.directionSightSwitch(x, y, directionSight); return map.isActiveChest(coords[0], coords[1]); } @Override public void paint(Graphics2D g) { super.paint(g); if (distanceOfAnimation <= WALKING_SPEED - 1) { 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 (distanceOfAnimation > 0 && isMoving) { distanceOfAnimation -= WALKING_SPEED; } else { isMoving = false; } if (!map.getName().equals("tutorial")){ map.getSoundManager().playSoundRepeatedly("wood01.wav", 20, -7.0f); } } else { distanceOfAnimation = getSize(); if (map.getName().equals("tutorial")) g.drawImage(imagePerson.getImagesOfDirection()[directionSight + 4], getX() * getSize() + (getSize() / 10), getY() * getSize(), getSize() * 8/10, getSize(), null); else g.drawImage(imagePerson.getImagesOfDirection()[directionSight + 4], getX() * getSize(), getY() * getSize() - (getSize() * 3/5), getSize(), getSize() * 3/2, null); } } public void animateMove(Graphics2D g, int direction){ int Xsize = 0; int Ysize = 0; switch (direction) { case 0: Ysize -= distanceOfAnimation; break; case 1: Xsize = distanceOfAnimation; break; case 2: Ysize = distanceOfAnimation; break; case 3: Xsize -= distanceOfAnimation; break; } if (map.getName().equals("tutorial")) { if (distanceOfAnimation > getSize() * 3 / 4) { g.drawImage(imagePerson.getImagesOfDirection()[direction], getX() * getSize() + (getSize() / 10), getY() * getSize(), getSize() * 8/10, getSize(), null); } else if (distanceOfAnimation > getSize() / 2) { g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * getSize() + (getSize() / 10), getY() * getSize(), getSize() * 8/10, getSize(), null); } else if (distanceOfAnimation > getSize() / 4) { g.drawImage(imagePerson.getImagesOfDirection()[direction + 8], getX() * getSize() + (getSize() / 10), getY() * getSize(), getSize() * 8/10, getSize(), null); } else { g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * getSize() + (getSize() / 10), getY() * getSize(), getSize() * 8/10, getSize(), null); } } else { if (distanceOfAnimation > getSize() * 3 / 4) { g.drawImage(imagePerson.getImagesOfDirection()[direction], getX() * getSize() + Xsize, getY() * getSize() + Ysize - (getSize() * 3 / 5), getSize(), getSize() * 3 / 2, null); } else if (distanceOfAnimation > getSize() / 2) { g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * getSize() + Xsize, getY() * getSize() + Ysize - (getSize() * 3 / 5), getSize(), getSize() * 3 / 2, null); } else if (distanceOfAnimation > getSize() / 4) { g.drawImage(imagePerson.getImagesOfDirection()[direction + 8], getX() * getSize() + Xsize, getY() * getSize() + Ysize - (getSize() * 3 / 5), getSize(), getSize() * 3 / 2, null); } else { g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * getSize() + Xsize, getY() * getSize() + Ysize - (getSize() * 3 / 5), getSize(), getSize() * 3 / 2, null); } } } public boolean getIsMoving() { return isMoving; } public void setIsMoving(boolean isMoving) { this.isMoving = isMoving; } public void setDistance(int distance) { this.distanceOfAnimation = distance; } public int getDistance() { return distanceOfAnimation; } public int getDirectionMoveLast() {return directionMoveLast; } public static void setInEvent(boolean isInEvent) { Person.isInEvent = isInEvent; } public int getDirectionSight() { return directionSight; } public static boolean isInEvent() { return isInEvent; } }