added an actual walking animation
This commit is contained in:
parent
5269320951
commit
02d382a696
@ -7,12 +7,11 @@ import java.io.IOException;
|
|||||||
public class ImagePerson {
|
public class ImagePerson {
|
||||||
private BufferedImage image;
|
private BufferedImage image;
|
||||||
private BufferedImage[] imagesOfDirection;
|
private BufferedImage[] imagesOfDirection;
|
||||||
private Image[] animationOfMoving;
|
|
||||||
|
|
||||||
public void setAllImagesOfDirection(Rectangle[] rectangles){
|
public void setAllImagesOfDirection(Rectangle[] rectangles){
|
||||||
imagesOfDirection = new BufferedImage[4];
|
imagesOfDirection = new BufferedImage[12];
|
||||||
|
|
||||||
if (rectangles.length < 4)
|
if (rectangles.length < 12)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|
||||||
for (int i = 0; i < imagesOfDirection.length; i++) {
|
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) {
|
public ImagePerson(String name, Rectangle[] rectangles) {
|
||||||
try {
|
try {
|
||||||
image = ImageIO.read(getClass().getResource(name));
|
image = ImageIO.read(getClass().getResource(name));
|
||||||
@ -45,8 +33,4 @@ public class ImagePerson {
|
|||||||
public BufferedImage[] getImagesOfDirection() {
|
public BufferedImage[] getImagesOfDirection() {
|
||||||
return imagesOfDirection;
|
return imagesOfDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Image[] getAnimationOfMoving() {
|
|
||||||
return animationOfMoving;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,8 @@ public class Person extends Cell {
|
|||||||
private int directionMove;
|
private int directionMove;
|
||||||
private int directionSight;
|
private int directionSight;
|
||||||
private int directionMoveLast;
|
private int directionMoveLast;
|
||||||
private boolean ready;
|
private boolean isMoving;
|
||||||
|
private int size;
|
||||||
|
|
||||||
public void setDirectionMoveLast(int directionMoveLast) {
|
public void setDirectionMoveLast(int directionMoveLast) {
|
||||||
this.directionMoveLast = directionMoveLast;
|
this.directionMoveLast = directionMoveLast;
|
||||||
@ -20,14 +21,16 @@ public class Person extends Cell {
|
|||||||
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
||||||
this.directionMove = 0;
|
this.directionMove = 0;
|
||||||
this.directionSight = 3;
|
this.directionSight = 3;
|
||||||
this.ready = true;
|
this.isMoving = false;
|
||||||
|
this.size = WindowOfFreeMove.CELL_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(int x, int y) {
|
public Person(int x, int y) {
|
||||||
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
|
||||||
this.directionMove = 0;
|
this.directionMove = 0;
|
||||||
this.directionSight = 3;
|
this.directionSight = 3;
|
||||||
this.ready = true;
|
this.isMoving = false;
|
||||||
|
this.size = WindowOfFreeMove.CELL_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDirectionMove(int directionMove) {
|
public void setDirectionMove(int directionMove) {
|
||||||
@ -39,14 +42,10 @@ public class Person extends Cell {
|
|||||||
this.imagePerson = new ImagePerson(name, rectangles);
|
this.imagePerson = new ImagePerson(name, rectangles);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAnimation(String[] names) {
|
|
||||||
this.imagePerson.setAnimationOfMoving(names);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void move() {
|
public void move() {
|
||||||
int x = this.getX();
|
int x = this.getX();
|
||||||
int y = this.getY();
|
int y = this.getY();
|
||||||
if (ready) {
|
if (!isMoving) {
|
||||||
switch (directionMove) {
|
switch (directionMove) {
|
||||||
case WindowOfFreeMove.KEY_LEFT:
|
case WindowOfFreeMove.KEY_LEFT:
|
||||||
directionSight = 1;
|
directionSight = 1;
|
||||||
@ -74,13 +73,8 @@ public class Person extends Cell {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (directionMove != 0 && !WindowOfFreeMove.getMap().isBusyCell(x, y)) {
|
if (directionMove != 0 && !WindowOfFreeMove.getMap().isBusyCell(x, y)) {
|
||||||
ready = false;
|
|
||||||
directionMoveLast = directionMove;
|
directionMoveLast = directionMove;
|
||||||
imagePerson.getAnimationOfMoving()[0].flush();
|
isMoving = true;
|
||||||
imagePerson.getAnimationOfMoving()[1].flush();
|
|
||||||
imagePerson.getAnimationOfMoving()[2].flush();
|
|
||||||
imagePerson.getAnimationOfMoving()[3].flush();
|
|
||||||
ready = true;
|
|
||||||
} else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) {
|
} else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) {
|
||||||
directionMoveLast = 0;
|
directionMoveLast = 0;
|
||||||
}
|
}
|
||||||
@ -103,23 +97,64 @@ public class Person extends Cell {
|
|||||||
@Override
|
@Override
|
||||||
public void paint(Graphics2D g) {
|
public void paint(Graphics2D g) {
|
||||||
super.paint(g);
|
super.paint(g);
|
||||||
|
if (isMoving) {
|
||||||
switch (directionMoveLast) {
|
switch (directionMoveLast) {
|
||||||
case 0:
|
|
||||||
g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE,
|
|
||||||
getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
|
|
||||||
break;
|
|
||||||
case WindowOfFreeMove.KEY_LEFT:
|
case WindowOfFreeMove.KEY_LEFT:
|
||||||
g.drawImage(imagePerson.getAnimationOfMoving()[3], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null);
|
animateMove(g, 1);
|
||||||
break;
|
break;
|
||||||
case WindowOfFreeMove.KEY_RIGHT:
|
case WindowOfFreeMove.KEY_RIGHT:
|
||||||
g.drawImage(imagePerson.getAnimationOfMoving()[1], (getX() - 1) * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null);
|
animateMove(g, 3);
|
||||||
break;
|
break;
|
||||||
case WindowOfFreeMove.KEY_UP:
|
case WindowOfFreeMove.KEY_UP:
|
||||||
g.drawImage(imagePerson.getAnimationOfMoving()[0], getX() * WindowOfFreeMove.CELL_SIZE, (getY()) * WindowOfFreeMove.CELL_SIZE, null);
|
animateMove(g, 2);
|
||||||
break;
|
break;
|
||||||
case WindowOfFreeMove.KEY_DOWN:
|
case WindowOfFreeMove.KEY_DOWN:
|
||||||
g.drawImage(imagePerson.getAnimationOfMoving()[2], getX() * WindowOfFreeMove.CELL_SIZE, (getY() - 1) * WindowOfFreeMove.CELL_SIZE, null);
|
animateMove(g, 0);
|
||||||
break;
|
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:
|
||||||
|
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 * 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -87,28 +87,27 @@ public class WindowOfFreeMove extends JFrame {
|
|||||||
|
|
||||||
|
|
||||||
public void start() {
|
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[12];
|
||||||
Rectangle[] rec = new Rectangle[4];
|
int count = 0;
|
||||||
for (int i = 0; i < rec.length; i++){
|
for (int i = 0; i < rec.length/4; i++){
|
||||||
rec[i] = new Rectangle(40, 40*i, 40, 40);
|
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);
|
Map m1 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
|
||||||
m1.getPerson().setImagePerson("mainpng.png", rec);
|
m1.getPerson().setImagePerson("mainpng.png", rec);
|
||||||
m1.getPerson().setAnimation(strings);
|
|
||||||
m1.setName("1");
|
m1.setName("1");
|
||||||
m1.setSpawnPoint(1, 1);
|
m1.setSpawnPoint(1, 1);
|
||||||
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
|
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
|
||||||
m2.getPerson().setImagePerson("mainpng.png", rec);
|
m2.getPerson().setImagePerson("mainpng.png", rec);
|
||||||
m2.getPerson().setAnimation(strings);
|
|
||||||
m2.setName("2");
|
m2.setName("2");
|
||||||
Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
|
Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
|
||||||
m3.getPerson().setImagePerson("mainpng.png", rec);
|
m3.getPerson().setImagePerson("mainpng.png", rec);
|
||||||
m3.getPerson().setAnimation(strings);
|
|
||||||
m3.setName("3");
|
m3.setName("3");
|
||||||
Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
|
Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
|
||||||
m4.getPerson().setImagePerson("mainpng.png", rec);
|
m4.getPerson().setImagePerson("mainpng.png", rec);
|
||||||
m4.getPerson().setAnimation(strings);
|
|
||||||
m4.setName("4");
|
m4.setName("4");
|
||||||
|
|
||||||
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2));
|
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2));
|
||||||
@ -151,7 +150,7 @@ public class WindowOfFreeMove extends JFrame {
|
|||||||
// lp.add(jf, JLayeredPane.POPUP_LAYER);
|
// lp.add(jf, JLayeredPane.POPUP_LAYER);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
//Thread.sleep();
|
sleep(1);
|
||||||
map.getPerson().move();
|
map.getPerson().move();
|
||||||
canvas.repaint();
|
canvas.repaint();
|
||||||
}
|
}
|
||||||
@ -174,9 +173,9 @@ public class WindowOfFreeMove extends JFrame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void sleep() {
|
protected static void sleep(long tm) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(tm);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user