Adding walking animation

This commit is contained in:
iryupnik 2020-03-03 11:47:22 +02:00
parent 05f2ac726d
commit 7bdee2034d
9 changed files with 110 additions and 104 deletions

View File

@ -35,25 +35,10 @@ public class EventMoveToCell extends Cell {
public void moveToMap() { public void moveToMap() {
WindowOfFreeMove.getMap().setIdExit(id); WindowOfFreeMove.getMap().setIdExit(id);
WindowOfFreeMove.setMap(this.map); WindowOfFreeMove.setMap(this.map);
WindowOfFreeMove.getMap().getPerson().setDirectionSight(idToDirectionSight());
WindowOfFreeMove.getMap().setIdExit(id); WindowOfFreeMove.getMap().setIdExit(id);
WindowOfFreeMove.paintMapOnWindow(); WindowOfFreeMove.paintMapOnWindow();
} }
private int idToDirectionSight(){
switch (directionOut){
case 1:
return 0;
case 2:
return 1;
case 3:
return 2;
case 4:
return 3;
}
return 0;
}
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(g); super.paint(g);

View File

@ -1,5 +1,5 @@
public class Game { public class Game {
public static void main(String[] args) { public static void main(String[] args) throws InterruptedException {
new WindowOfFreeMove().start(); } new WindowOfFreeMove().start(); }
} }

View File

@ -121,15 +121,23 @@ public class Map {
switch (cell.getDirectionOut()) { switch (cell.getDirectionOut()) {
case 1: case 1:
person.set(cell.getX(), cell.getY() - 1); person.set(cell.getX(), cell.getY() - 1);
person.directionMoveLast = 0;
person.directionSight = 2;
break; break;
case 2: case 2:
person.set(cell.getX() + 1, cell.getY()); person.set(cell.getX() + 1, cell.getY());
person.directionMoveLast = 0;
person.directionSight = 3;
break; break;
case 3: case 3:
person.set(cell.getX(), cell.getY() + 1); person.set(cell.getX(), cell.getY() + 1);
person.directionMoveLast = 0;
person.directionSight = 0;
break; break;
case 4: case 4:
person.set(cell.getX() - 1, cell.getY()); person.set(cell.getX() - 1, cell.getY());
person.directionMoveLast = 0;
person.directionSight = 1;
break; break;
default: default:
person.set(-1, -1); person.set(-1, -1);

View File

@ -1,9 +1,12 @@
import javax.swing.*;
import java.awt.*; import java.awt.*;
public class Person extends Cell { public class Person extends Cell {
private ImagePerson imagePerson; private ImagePerson imagePerson;
private int directionMove; private int directionMove;
private int directionSight; public int directionSight;
public int directionMoveLast;
boolean ready = true;
public Person() { public Person() {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
@ -22,54 +25,66 @@ public class Person extends Cell {
this.directionMove = directionMove; this.directionMove = directionMove;
} }
public void setDirectionSight(int directionSight) { public void setImagePerson(String name, Rectangle[] rectangles) {
if ((directionSight >= 0) && (directionSight <= 3)) this.imagePerson = new ImagePerson(name, rectangles);
this.directionSight = directionSight;
} }
public void setImagePerson(String name, Rectangle[] rectangles) { this.imagePerson = new ImagePerson(name, rectangles); } public void setAnimation(String[] names){
this.imagePerson.setAnimationOfMoving(names);
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();
switch (directionMove) { if (ready) {
case WindowOfFreeMove.KEY_LEFT: switch (directionMove) {
directionSight = 1; case WindowOfFreeMove.KEY_LEFT:
x--; directionSight = 1;
if (x < 0) x--;
x = WindowOfFreeMove.CANVAS_WIDTH - 1; if (x < 0)
break; x = WindowOfFreeMove.CANVAS_WIDTH - 1;
case WindowOfFreeMove.KEY_RIGHT: break;
directionSight = 3; case WindowOfFreeMove.KEY_RIGHT:
x++; directionSight = 3;
if (x == WindowOfFreeMove.CANVAS_WIDTH) x++;
x = 0; if (x == WindowOfFreeMove.CANVAS_WIDTH)
break; x = 0;
case WindowOfFreeMove.KEY_UP: break;
directionSight = 2; case WindowOfFreeMove.KEY_UP:
y--; directionSight = 2;
if (y < 0) y--;
y = WindowOfFreeMove.CANVAS_HEIGHT - 1; if (y < 0)
break; y = WindowOfFreeMove.CANVAS_HEIGHT - 1;
case WindowOfFreeMove.KEY_DOWN: break;
directionSight = 0; case WindowOfFreeMove.KEY_DOWN:
y++; directionSight = 0;
if (y == WindowOfFreeMove.CANVAS_HEIGHT) y++;
y = 0; if (y == WindowOfFreeMove.CANVAS_HEIGHT)
break; y = 0;
} break;
this.directionMove = 0; }
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;
}
else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) {
directionMoveLast = 0;
}
directionMove = 0;
if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) { if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) {
WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap(); WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap();
} else { } else {
if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) { if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) {
set(x, y); set(x, y);
}
} }
} }
} }
public boolean isInPerson(int x, int y) { public boolean isInPerson(int x, int y) {
@ -79,25 +94,23 @@ public class Person extends Cell {
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(g); super.paint(g);
if (directionMove == 0) switch (directionMoveLast){
g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE, case 0:
getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE,
else { getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
switch (directionMove) { break;
case WindowOfFreeMove.KEY_LEFT: case WindowOfFreeMove.KEY_LEFT:
g.drawImage(imagePerson.getAnimationOfMoving()[3], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, g.drawImage(imagePerson.getAnimationOfMoving()[3], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null);
WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); 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);
g.drawImage(imagePerson.getAnimationOfMoving()[1], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); break;
break; case WindowOfFreeMove.KEY_UP:
case WindowOfFreeMove.KEY_UP: g.drawImage(imagePerson.getAnimationOfMoving()[0], getX()*WindowOfFreeMove.CELL_SIZE, (getY())*WindowOfFreeMove.CELL_SIZE, null);
g.drawImage(imagePerson.getAnimationOfMoving()[0], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); 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);
g.drawImage(imagePerson.getAnimationOfMoving()[2], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); break;
break;
}
} }
} }
} }

View File

@ -4,7 +4,7 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
public class WindowOfFreeMove extends JFrame { public class WindowOfFreeMove extends JFrame {
static final int CELL_SIZE = 50; static final int CELL_SIZE = 40;
static final int CANVAS_WIDTH = 10; static final int CANVAS_WIDTH = 10;
static final int CANVAS_HEIGHT = 10; static final int CANVAS_HEIGHT = 10;
@ -86,8 +86,8 @@ public class WindowOfFreeMove extends JFrame {
} }
public void start() { public void start() throws InterruptedException {
String[] strings = {"main-walk-up.gif", "main-walk-right.gif", "main-walk-down.gif", "main-walk-left.gif"}; 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]; Rectangle[] rec = new Rectangle[4];
for (int i = 0; i < rec.length; i++){ for (int i = 0; i < rec.length; i++){
rec[i] = new Rectangle(40, 40*i, 40, 40); rec[i] = new Rectangle(40, 40*i, 40, 40);
@ -100,12 +100,15 @@ public class WindowOfFreeMove extends JFrame {
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));
@ -122,36 +125,33 @@ public class WindowOfFreeMove extends JFrame {
setMap(m1); setMap(m1);
map.createMap(); map.createMap();
// String s1 = "> Test";
// for (int i = 0; i < s1.length() + 1; i++) {
// lp.remove(dialogBar);
// final String text = s1.substring(0, i);
// JPanel jf = new JPanel() {
// Font f = new Font("Times New Roman", Font.ITALIC, 50);
// //
// @Override // lp.remove(dialogBar);
// public void paint(Graphics g) {
// super.paint(g);
// g.setColor(Color.green);
// g.setFont(f);
// g.drawString(text, 10, 50);
// }
// };
// //
// jf.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4);
// jf.setBackground(Color.BLACK);
// jf.setVisible(false);
// //
// setDialogBar(jf); // JPanel jf = new JPanel() {
// dialogBar.setVisible(true); // Font f = new Font("Times New Roman", Font.ITALIC, 50);
//setCanPersonMove(false);
// lp.add(jf, JLayeredPane.POPUP_LAYER);
// //
// sleep(300); // @Override
// } // public void paint(Graphics g) {
// super.paint(g);
// g.setColor(Color.green);
// g.setFont(f);
// g.drawString("> Test", 10, 50);
// }
// };
//
// jf.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4);
// jf.setBackground(Color.BLACK);
// jf.setVisible(false);
//
// setDialogBar(jf);
// dialogBar.setVisible(true);
//setCanPersonMove(false);
// lp.add(jf, JLayeredPane.POPUP_LAYER);
while (true) { while (true) {
Thread.sleep(1);
map.getPerson().move(); map.getPerson().move();
canvas.repaint(); canvas.repaint();
} }
@ -174,9 +174,9 @@ public class WindowOfFreeMove extends JFrame {
} }
} }
protected static void sleep(int i) { protected static void sleep() {
try { try {
Thread.sleep(i); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB