added an actual walking animation

This commit is contained in:
musilto9 2020-03-03 13:40:23 +01:00
parent 5269320951
commit 02d382a696
3 changed files with 73 additions and 55 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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 <rec.length/3; j++) {
rec[count] = new Rectangle(40 * i, 40 * j, 40, 40);
count++;
}
}
Map m1 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
m1.getPerson().setImagePerson("mainpng.png", rec);
m1.getPerson().setAnimation(strings);
m1.setName("1");
m1.setSpawnPoint(1, 1);
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m2.getPerson().setImagePerson("mainpng.png", rec);
m2.getPerson().setAnimation(strings);
m2.setName("2");
Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
m3.getPerson().setImagePerson("mainpng.png", rec);
m3.getPerson().setAnimation(strings);
m3.setName("3");
Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m4.getPerson().setImagePerson("mainpng.png", rec);
m4.getPerson().setAnimation(strings);
m4.setName("4");
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2));
@ -151,7 +150,7 @@ public class WindowOfFreeMove extends JFrame {
// lp.add(jf, JLayeredPane.POPUP_LAYER);
while (true) {
//Thread.sleep();
sleep(1);
map.getPerson().move();
canvas.repaint();
}
@ -174,9 +173,9 @@ public class WindowOfFreeMove extends JFrame {
}
}
protected static void sleep() {
protected static void sleep(long tm) {
try {
Thread.sleep(1000);
Thread.sleep(tm);
} catch (InterruptedException e) {
e.printStackTrace();
}