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() {
WindowOfFreeMove.getMap().setIdExit(id);
WindowOfFreeMove.setMap(this.map);
WindowOfFreeMove.getMap().getPerson().setDirectionSight(idToDirectionSight());
WindowOfFreeMove.getMap().setIdExit(id);
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
public void paint(Graphics2D g) {
super.paint(g);

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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_HEIGHT = 10;
@ -86,8 +86,8 @@ public class WindowOfFreeMove extends JFrame {
}
public void start() {
String[] strings = {"main-walk-up.gif", "main-walk-right.gif", "main-walk-down.gif", "main-walk-left.gif"};
public void start() throws InterruptedException {
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);
@ -100,12 +100,15 @@ public class WindowOfFreeMove extends JFrame {
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));
@ -122,11 +125,10 @@ public class WindowOfFreeMove extends JFrame {
setMap(m1);
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);
//
@ -135,7 +137,7 @@ public class WindowOfFreeMove extends JFrame {
// super.paint(g);
// g.setColor(Color.green);
// g.setFont(f);
// g.drawString(text, 10, 50);
// g.drawString("> Test", 10, 50);
// }
// };
//
@ -147,11 +149,9 @@ public class WindowOfFreeMove extends JFrame {
// dialogBar.setVisible(true);
//setCanPersonMove(false);
// lp.add(jf, JLayeredPane.POPUP_LAYER);
//
// sleep(300);
// }
while (true) {
Thread.sleep(1);
map.getPerson().move();
canvas.repaint();
}
@ -174,9 +174,9 @@ public class WindowOfFreeMove extends JFrame {
}
}
protected static void sleep(int i) {
protected static void sleep() {
try {
Thread.sleep(i);
Thread.sleep(1000);
} catch (InterruptedException e) {
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