Move to another map Event

This commit is contained in:
iryupnik 2020-02-26 12:08:58 +02:00
parent 5616d7196a
commit 45dbe318e2
5 changed files with 75 additions and 6 deletions

View File

@ -0,0 +1,21 @@
import java.awt.*;
public class EventMoveToCell extends Cell {
Map map;
public EventMoveToCell(int x, int y) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.green);
this.map = new Map(WindowOfFreeMove.CANVAS_HEIGHT, WindowOfFreeMove.CANVAS_WIDTH,x,y,TypeOfCell.EMPTY);
}
public EventMoveToCell(int x, int y, String nameOfMap){
super(x,y,WindowOfFreeMove.CELL_SIZE, Color.green);
this.map = Map.findMapByName(nameOfMap);
}
public EventMoveToCell(int x, int y, Map map){
super(x,y,WindowOfFreeMove.CELL_SIZE, Color.green);
this.map = map;
}
}

View File

@ -1,3 +1,5 @@
import javafx.scene.shape.MoveTo;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.LinkedList; import java.util.LinkedList;
@ -5,14 +7,18 @@ import java.util.LinkedList;
public class Map { public class Map {
private TypeOfCell[][] map; private TypeOfCell[][] map;
private Person person; private Person person;
private LinkedList<EventMoveToCell> moveToCells;
private LinkedList<Block> block; private LinkedList<Block> block;
Map(int height, int width){ Map(int height, int width){
moveToCells = new LinkedList<>();
block = new LinkedList<>(); block = new LinkedList<>();
map = new TypeOfCell[height][width]; map = new TypeOfCell[height][width];
person = new Person(0,0);
} }
Map(int height, int width, int xPerson, int yPerson){ Map(int height, int width, int xPerson, int yPerson){
moveToCells = new LinkedList<>();
block = new LinkedList<>(); block = new LinkedList<>();
map = new TypeOfCell[height][width]; map = new TypeOfCell[height][width];
if ((height < xPerson || xPerson < 0) || (width < yPerson || yPerson < 0)){ if ((height < xPerson || xPerson < 0) || (width < yPerson || yPerson < 0)){
@ -22,6 +28,7 @@ public class Map {
} }
Map(int height, int width,int xPerson, int yPerson, TypeOfCell type){ Map(int height, int width,int xPerson, int yPerson, TypeOfCell type){
moveToCells = new LinkedList<>();
block = new LinkedList<>(); block = new LinkedList<>();
map = new TypeOfCell[height][width]; map = new TypeOfCell[height][width];
if ((height - 1 < xPerson || xPerson <= 0) || (width - 1 < yPerson || yPerson <= 0)){ if ((height - 1 < xPerson || xPerson <= 0) || (width - 1 < yPerson || yPerson <= 0)){
@ -39,12 +46,23 @@ public class Map {
} }
} }
public static Map findMapByName(String nameOfMap) {
return new Map(0,0); //TODO finding map in resources and parsing
}
public void addMoveToEvent(EventMoveToCell moveToCell){
map[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO;
this.moveToCells.add(moveToCell);
}
public Person getPerson() { public Person getPerson() {
return person; return person;
} }
public LinkedList<Block> getBlocks() { return block; } public LinkedList<Block> getBlocks() { return block; }
public LinkedList<EventMoveToCell> getMoveToCells() { return moveToCells; }
public void createMap(){ public void createMap(){
for (int i = 0; i < map.length; i++){ for (int i = 0; i < map.length; i++){
for (int j = 0; j < map.length; j++){ for (int j = 0; j < map.length; j++){
@ -52,6 +70,10 @@ public class Map {
block.add(new Block(i,j)); block.add(new Block(i,j));
} }
if (map[i][j] == TypeOfCell.MOVETO){
moveToCells.add(new EventMoveToCell(i,j));
}
if (map[i][j] == TypeOfCell.PERSON){ if (map[i][j] == TypeOfCell.PERSON){
person = new Person(i,j); person = new Person(i,j);
} }
@ -59,7 +81,7 @@ public class Map {
} }
} }
class Canvas extends JPanel { // class for rendering (drawing) class Canvas extends JPanel {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
super.paint(g); super.paint(g);
@ -80,4 +102,18 @@ public class Map {
} }
return person.isInPerson(x,y); return person.isInPerson(x,y);
} }
boolean isCellMoveTo(int x, int y){
for (EventMoveToCell cell: moveToCells)
if (x == cell.getX() && y == cell.getY())
return true;
return false;
}
EventMoveToCell findEventByXY(int x, int y){
for (EventMoveToCell cell: moveToCells)
if (x == cell.getX() && y == cell.getY())
return cell;
return new EventMoveToCell(WindowOfFreeMove.CANVAS_HEIGHT,WindowOfFreeMove.CANVAS_WIDTH, this);
}
} }

View File

@ -45,6 +45,10 @@ public class Person {
person = new Cell(x , y,20,Color.RED); person = new Cell(x , y,20,Color.RED);
} }
if (WindowOfFreeMove.map.isCellMoveTo(x,y)){
WindowOfFreeMove.map = WindowOfFreeMove.map.findEventByXY(x,y).map;
WindowOfFreeMove.map.createMap();
}
} }
public void paint(Graphics2D g) { public void paint(Graphics2D g) {

View File

@ -1,3 +1,3 @@
public enum TypeOfCell { public enum TypeOfCell {
EMPTY, BLOCK, PERSON BLOCK, EMPTY, MOVETO, PERSON;
} }

View File

@ -8,13 +8,12 @@ public class WindowOfFreeMove extends JFrame {
static final int CANVAS_WIDTH = 20; static final int CANVAS_WIDTH = 20;
static final int CANVAS_HEIGHT = 20; static final int CANVAS_HEIGHT = 20;
static final int KEY_LEFT = 37; static final int KEY_LEFT = 37;
static final int KEY_UP = 38; static final int KEY_UP = 38;
static final int KEY_RIGHT = 39; static final int KEY_RIGHT = 39;
static final int KEY_DOWN = 40; static final int KEY_DOWN = 40;
Canvas canvas; private Canvas canvas;
static Map map; static Map map;
public WindowOfFreeMove() { public WindowOfFreeMove() {
@ -42,12 +41,18 @@ public class WindowOfFreeMove extends JFrame {
} }
public void start() { public void start() {
map = new Map(CANVAS_HEIGHT,CANVAS_WIDTH, 10, 10, TypeOfCell.BLOCK); Map m1 = new Map(CANVAS_HEIGHT,CANVAS_WIDTH, 10, 10, TypeOfCell.BLOCK);
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, 0,0, TypeOfCell.EMPTY);
m1.addMoveToEvent(new EventMoveToCell(15,15, m2));
m2.addMoveToEvent(new EventMoveToCell(15,15, m1));
map = m1;
map.createMap(); map.createMap();
while (true) { while (true) {
map.getPerson().move(); map.getPerson().move();
canvas.repaint(); canvas.repaint();
sleep();
} }
} }
@ -62,12 +67,15 @@ public class WindowOfFreeMove extends JFrame {
cell.paint(g2D); cell.paint(g2D);
} }
map.getPerson().paint(g2D); map.getPerson().paint(g2D);
for (Cell cell : map.getMoveToCells()) {
cell.paint(g2D);
}
} }
} }
private void sleep() { private void sleep() {
try { try {
Thread.sleep(100); Thread.sleep(50);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }