Move to another map Event
This commit is contained in:
parent
5616d7196a
commit
45dbe318e2
21
src/main/java/EventMoveToCell.java
Normal file
21
src/main/java/EventMoveToCell.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,3 +1,5 @@
|
||||
import javafx.scene.shape.MoveTo;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.LinkedList;
|
||||
@ -5,14 +7,18 @@ import java.util.LinkedList;
|
||||
public class Map {
|
||||
private TypeOfCell[][] map;
|
||||
private Person person;
|
||||
private LinkedList<EventMoveToCell> moveToCells;
|
||||
private LinkedList<Block> block;
|
||||
|
||||
Map(int height, int width){
|
||||
moveToCells = new LinkedList<>();
|
||||
block = new LinkedList<>();
|
||||
map = new TypeOfCell[height][width];
|
||||
person = new Person(0,0);
|
||||
}
|
||||
|
||||
Map(int height, int width, int xPerson, int yPerson){
|
||||
moveToCells = new LinkedList<>();
|
||||
block = new LinkedList<>();
|
||||
map = new TypeOfCell[height][width];
|
||||
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){
|
||||
moveToCells = new LinkedList<>();
|
||||
block = new LinkedList<>();
|
||||
map = new TypeOfCell[height][width];
|
||||
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() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public LinkedList<Block> getBlocks() { return block; }
|
||||
|
||||
public LinkedList<EventMoveToCell> getMoveToCells() { return moveToCells; }
|
||||
|
||||
public void createMap(){
|
||||
for (int i = 0; i < map.length; i++){
|
||||
for (int j = 0; j < map.length; j++){
|
||||
@ -52,6 +70,10 @@ public class Map {
|
||||
block.add(new Block(i,j));
|
||||
}
|
||||
|
||||
if (map[i][j] == TypeOfCell.MOVETO){
|
||||
moveToCells.add(new EventMoveToCell(i,j));
|
||||
}
|
||||
|
||||
if (map[i][j] == TypeOfCell.PERSON){
|
||||
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
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
@ -80,4 +102,18 @@ public class Map {
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,6 +45,10 @@ public class Person {
|
||||
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) {
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
public enum TypeOfCell {
|
||||
EMPTY, BLOCK, PERSON
|
||||
BLOCK, EMPTY, MOVETO, PERSON;
|
||||
}
|
||||
|
||||
@ -8,13 +8,12 @@ public class WindowOfFreeMove extends JFrame {
|
||||
static final int CANVAS_WIDTH = 20;
|
||||
static final int CANVAS_HEIGHT = 20;
|
||||
|
||||
|
||||
static final int KEY_LEFT = 37;
|
||||
static final int KEY_UP = 38;
|
||||
static final int KEY_RIGHT = 39;
|
||||
static final int KEY_DOWN = 40;
|
||||
|
||||
Canvas canvas;
|
||||
private Canvas canvas;
|
||||
static Map map;
|
||||
|
||||
public WindowOfFreeMove() {
|
||||
@ -42,12 +41,18 @@ public class WindowOfFreeMove extends JFrame {
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
while (true) {
|
||||
map.getPerson().move();
|
||||
canvas.repaint();
|
||||
sleep();
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,12 +67,15 @@ public class WindowOfFreeMove extends JFrame {
|
||||
cell.paint(g2D);
|
||||
}
|
||||
map.getPerson().paint(g2D);
|
||||
for (Cell cell : map.getMoveToCells()) {
|
||||
cell.paint(g2D);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sleep() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user