From 02d382a696f1f7233c4a3a33a65933a0aef0402a Mon Sep 17 00:00:00 2001 From: musilto9 Date: Tue, 3 Mar 2020 13:40:23 +0100 Subject: [PATCH] added an actual walking animation --- src/main/java/ImagePerson.java | 20 +------ src/main/java/Person.java | 87 ++++++++++++++++++++--------- src/main/java/WindowOfFreeMove.java | 21 ++++--- 3 files changed, 73 insertions(+), 55 deletions(-) diff --git a/src/main/java/ImagePerson.java b/src/main/java/ImagePerson.java index ce11011..21e243d 100644 --- a/src/main/java/ImagePerson.java +++ b/src/main/java/ImagePerson.java @@ -7,12 +7,11 @@ import java.io.IOException; public class ImagePerson { private BufferedImage image; private BufferedImage[] imagesOfDirection; - private Image[] animationOfMoving; public void setAllImagesOfDirection(Rectangle[] rectangles){ - imagesOfDirection = new BufferedImage[4]; + imagesOfDirection = new BufferedImage[12]; - if (rectangles.length < 4) + if (rectangles.length < 12) throw new IllegalArgumentException(); for (int i = 0; i < imagesOfDirection.length; i++) { @@ -20,17 +19,6 @@ public class ImagePerson { } } - public void setAnimationOfMoving(String[] animationOfMovingNames) { - animationOfMoving = new Image[4]; - - if (animationOfMovingNames.length < 4) - throw new IllegalArgumentException(); - - for (int i = 0; i < animationOfMoving.length; i++){ - animationOfMoving[i] = new ImageIcon(getClass().getResource(animationOfMovingNames[i])).getImage(); - } - } - public ImagePerson(String name, Rectangle[] rectangles) { try { image = ImageIO.read(getClass().getResource(name)); @@ -45,8 +33,4 @@ public class ImagePerson { public BufferedImage[] getImagesOfDirection() { return imagesOfDirection; } - - public Image[] getAnimationOfMoving() { - return animationOfMoving; - } } diff --git a/src/main/java/Person.java b/src/main/java/Person.java index fd39390..ffc751c 100644 --- a/src/main/java/Person.java +++ b/src/main/java/Person.java @@ -6,7 +6,8 @@ public class Person extends Cell { private int directionMove; private int directionSight; private int directionMoveLast; - private boolean ready; + private boolean isMoving; + private int size; public void setDirectionMoveLast(int directionMoveLast) { this.directionMoveLast = directionMoveLast; @@ -20,14 +21,16 @@ public class Person extends Cell { super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); this.directionMove = 0; this.directionSight = 3; - this.ready = true; + this.isMoving = false; + this.size = WindowOfFreeMove.CELL_SIZE; } public Person(int x, int y) { super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED); this.directionMove = 0; this.directionSight = 3; - this.ready = true; + this.isMoving = false; + this.size = WindowOfFreeMove.CELL_SIZE; } public void setDirectionMove(int directionMove) { @@ -39,14 +42,10 @@ public class Person extends Cell { 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(); - if (ready) { + if (!isMoving) { switch (directionMove) { case WindowOfFreeMove.KEY_LEFT: directionSight = 1; @@ -74,13 +73,8 @@ public class Person extends Cell { break; } if (directionMove != 0 && !WindowOfFreeMove.getMap().isBusyCell(x, y)) { - ready = false; directionMoveLast = directionMove; - imagePerson.getAnimationOfMoving()[0].flush(); - imagePerson.getAnimationOfMoving()[1].flush(); - imagePerson.getAnimationOfMoving()[2].flush(); - imagePerson.getAnimationOfMoving()[3].flush(); - ready = true; + isMoving = true; } else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) { directionMoveLast = 0; } @@ -103,23 +97,64 @@ public class Person extends Cell { @Override public void paint(Graphics2D g) { super.paint(g); - switch (directionMoveLast) { + 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--; + } + if (size == 0 && isMoving) { + 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); + } + } + + private void animateMove(Graphics2D g, int direction){ + int Xsize = 0; + int Ysize = 0; + switch (direction) { case 0: - g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE, - getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); + Ysize -= size; break; - case WindowOfFreeMove.KEY_LEFT: - g.drawImage(imagePerson.getAnimationOfMoving()[3], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); + case 1: + Xsize = size; break; - case WindowOfFreeMove.KEY_RIGHT: - g.drawImage(imagePerson.getAnimationOfMoving()[1], (getX() - 1) * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); + case 2: + Ysize = size; 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() - 1) * WindowOfFreeMove.CELL_SIZE, null); + 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 * 1 / 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 * 1 / 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); + } } } diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java index 5d46028..81454b9 100644 --- a/src/main/java/WindowOfFreeMove.java +++ b/src/main/java/WindowOfFreeMove.java @@ -87,28 +87,27 @@ public class WindowOfFreeMove extends JFrame { public void start() { - String[] strings = {"main-walk-up-long.gif", "main-walk-right-long.gif", "main-walk-down-long.gif", "main-walk-left-long.gif"}; - Rectangle[] rec = new Rectangle[4]; - for (int i = 0; i < rec.length; i++){ - rec[i] = new Rectangle(40, 40*i, 40, 40); + Rectangle[] rec = new Rectangle[12]; + int count = 0; + for (int i = 0; i < rec.length/4; i++){ + for (int j = 0; j