Adding walking animation
This commit is contained in:
parent
05f2ac726d
commit
7bdee2034d
@ -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);
|
||||||
|
|||||||
@ -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(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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,18 +25,18 @@ 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();
|
||||||
|
if (ready) {
|
||||||
switch (directionMove) {
|
switch (directionMove) {
|
||||||
case WindowOfFreeMove.KEY_LEFT:
|
case WindowOfFreeMove.KEY_LEFT:
|
||||||
directionSight = 1;
|
directionSight = 1;
|
||||||
@ -60,7 +63,19 @@ public class Person extends Cell {
|
|||||||
y = 0;
|
y = 0;
|
||||||
break;
|
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();
|
||||||
@ -69,7 +84,7 @@ public class Person extends Cell {
|
|||||||
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){
|
||||||
|
case 0:
|
||||||
g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE,
|
g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE,
|
||||||
getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
|
getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
|
||||||
else {
|
break;
|
||||||
switch (directionMove) {
|
|
||||||
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() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null);
|
g.drawImage(imagePerson.getAnimationOfMoving()[1], (getX()-1)*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() * WindowOfFreeMove.CELL_SIZE, null);
|
g.drawImage(imagePerson.getAnimationOfMoving()[2], getX()*WindowOfFreeMove.CELL_SIZE, (getY()-1)*WindowOfFreeMove.CELL_SIZE, null);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@ -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,11 +125,10 @@ 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);
|
// lp.remove(dialogBar);
|
||||||
// final String text = s1.substring(0, i);
|
//
|
||||||
|
//
|
||||||
// JPanel jf = new JPanel() {
|
// JPanel jf = new JPanel() {
|
||||||
// Font f = new Font("Times New Roman", Font.ITALIC, 50);
|
// Font f = new Font("Times New Roman", Font.ITALIC, 50);
|
||||||
//
|
//
|
||||||
@ -135,7 +137,7 @@ public class WindowOfFreeMove extends JFrame {
|
|||||||
// super.paint(g);
|
// super.paint(g);
|
||||||
// g.setColor(Color.green);
|
// g.setColor(Color.green);
|
||||||
// g.setFont(f);
|
// 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);
|
// dialogBar.setVisible(true);
|
||||||
//setCanPersonMove(false);
|
//setCanPersonMove(false);
|
||||||
// lp.add(jf, JLayeredPane.POPUP_LAYER);
|
// lp.add(jf, JLayeredPane.POPUP_LAYER);
|
||||||
//
|
|
||||||
// sleep(300);
|
|
||||||
// }
|
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
src/main/resources/main-walk-down-long.gif
Normal file
BIN
src/main/resources/main-walk-down-long.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
BIN
src/main/resources/main-walk-left-long.gif
Normal file
BIN
src/main/resources/main-walk-left-long.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
BIN
src/main/resources/main-walk-right-long.gif
Normal file
BIN
src/main/resources/main-walk-right-long.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
BIN
src/main/resources/main-walk-up-long.gif
Normal file
BIN
src/main/resources/main-walk-up-long.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
Loading…
x
Reference in New Issue
Block a user