Event MoveTo fixed, SPAWNPOINT appeared

This commit is contained in:
iryupnik 2020-02-29 14:15:41 +01:00
parent 45dbe318e2
commit 7af14c4637
7 changed files with 232 additions and 135 deletions

View File

@ -2,16 +2,16 @@ import java.awt.*;
import java.util.Random; import java.util.Random;
public class Block extends Cell { public class Block extends Cell {
Random random; // private Random random;
public Block(){ public Block(){
super(-1, - 1, WindowOfFreeMove.CELL_SIZE, Color.BLUE); super(-1, - 1, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
this.random = new Random(); // this.random = new Random();
} }
public Block(int x, int y){ public Block(int x, int y){
super(x,y, WindowOfFreeMove.CELL_SIZE, Color.BLUE); super(x,y, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
this.random = new Random(); // this.random = new Random();
} }
public boolean isInBlock(int x, int y) { public boolean isInBlock(int x, int y) {
@ -21,12 +21,12 @@ public class Block extends Cell {
return false; return false;
} }
public void randomAppear() { // public void randomAppear() {
int x, y; // int x, y;
do { // do {
x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT); // x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT);
y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH); // y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH);
} while (WindowOfFreeMove.map.isBusyCell(x, y)); // } while (WindowOfFreeMove.getMap().isBusyCell(x, y));
set(x, y); // set(x, y);
} // }
} }

View File

@ -1,9 +1,9 @@
import java.awt.*; import java.awt.*;
public class Cell { public class Cell {
protected int x, y; // object coordinates private int x, y;
protected int size; // object size in px private int size;
protected Color color; // object color private Color color;
public Cell(int x, int y, int size, Color color) { public Cell(int x, int y, int size, Color color) {
set(x, y); set(x, y);
@ -24,9 +24,8 @@ public class Cell {
return y; return y;
} }
public void paint(Graphics2D g) {// object rendering public void paint(Graphics2D g) {
g.setColor(color); g.setColor(color);
g.fillOval(x * size, y * size, size, size); g.fillOval(x * size, y * size, size, size);
g.drawString("ahoj123", 0, 0);
} }
} }

View File

@ -1,21 +1,47 @@
import java.awt.*; import java.awt.*;
public class EventMoveToCell extends Cell { public class EventMoveToCell extends Cell {
Map map; private Map map;
private int id;
private int directionOut;
public EventMoveToCell(int x, int y) { public int getId(){
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.green); return id;
this.map = new Map(WindowOfFreeMove.CANVAS_HEIGHT, WindowOfFreeMove.CANVAS_WIDTH,x,y,TypeOfCell.EMPTY);
} }
public EventMoveToCell(int x, int y, String nameOfMap){ public EventMoveToCell(int id, int directionOut, int x, int y) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.green); super(x, y, WindowOfFreeMove.CELL_SIZE, Color.green);
this.id = id;
this.directionOut = directionOut;
this.map = new Map(WindowOfFreeMove.CANVAS_HEIGHT, WindowOfFreeMove.CANVAS_WIDTH,false);
}
public EventMoveToCell(int id, int directionOut, int x, int y, String nameOfMap){
super(x,y,WindowOfFreeMove.CELL_SIZE, Color.green);
this.id = id;
this.directionOut = directionOut;
this.map = Map.findMapByName(nameOfMap); this.map = Map.findMapByName(nameOfMap);
} }
public EventMoveToCell(int x, int y, Map map){ public EventMoveToCell(int id, int directionOut, int x, int y, Map map){
super(x,y,WindowOfFreeMove.CELL_SIZE, Color.green); super(x,y,WindowOfFreeMove.CELL_SIZE, Color.green);
this.id = id;
this.directionOut = directionOut;
this.map = map; this.map = map;
} }
public int getDirectionOut() { return directionOut; }
public void moveToMap() {
WindowOfFreeMove.getMap().setIdExit(id);
WindowOfFreeMove.setMap(this.map);
WindowOfFreeMove.getMap().setIdExit(id);
WindowOfFreeMove.paintMapOnWindow();
}
@Override
public void paint(Graphics2D g) {
super.paint(g);
g.drawString(Integer.toString(getId()), getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE);
}
} }

View File

@ -1,48 +1,61 @@
import javafx.scene.shape.MoveTo;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList; import java.util.LinkedList;
public class Map { public class Map {
private TypeOfCell[][] map; private String name;
private TypeOfCell[][] mapArray;
private Person person; private Person person;
private LinkedList<EventMoveToCell> moveToCells; private LinkedList<EventMoveToCell> moveToCells;
private LinkedList<Block> block; private LinkedList<Block> block;
private boolean isBlocked;
private int idExit;
Map(int height, int width) { Map(int height, int width) {
moveToCells = new LinkedList<>(); name = "Spawn";
block = new LinkedList<>(); idExit = 0;
map = new TypeOfCell[height][width]; mapArray = new TypeOfCell[height][width];
person = new Person(0,0); person = new Person();
moveToCells = new LinkedList<EventMoveToCell>();
block = new LinkedList<Block>();
isBlocked = false;
TypeOfCell type;
if (isBlocked)
type = TypeOfCell.BLOCK;
else
type = TypeOfCell.EMPTY;
for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < mapArray.length; j++) {
mapArray[0][j] = type;
mapArray[mapArray.length - 1][j] = type;
}
mapArray[i][0] = type;
mapArray[i][mapArray.length - 1] = type;
}
} }
Map(int height, int width, int xPerson, int yPerson){ Map(int height, int width, Boolean isBlocked) {
moveToCells = new LinkedList<>(); name = "Spawn";
block = new LinkedList<>(); idExit = 0;
map = new TypeOfCell[height][width]; person = new Person();
if ((height < xPerson || xPerson < 0) || (width < yPerson || yPerson < 0)){ moveToCells = new LinkedList<EventMoveToCell>();
map[0][0] = TypeOfCell.PERSON; block = new LinkedList<Block>();
} mapArray = new TypeOfCell[height][width];
else map [xPerson][yPerson] = TypeOfCell.PERSON; this.isBlocked = isBlocked;
}
Map(int height, int width,int xPerson, int yPerson, TypeOfCell type){ TypeOfCell type;
moveToCells = new LinkedList<>(); if (isBlocked)
block = new LinkedList<>(); type = TypeOfCell.BLOCK;
map = new TypeOfCell[height][width]; else
if ((height - 1 < xPerson || xPerson <= 0) || (width - 1 < yPerson || yPerson <= 0)){ type = TypeOfCell.EMPTY;
map[1][1] = TypeOfCell.PERSON;
}
else map [xPerson][yPerson] = TypeOfCell.PERSON;
for (int i = 0; i < map.length; i++){ for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < map.length; j++){ for (int j = 0; j < mapArray.length; j++) {
map[0][j] = type; mapArray[0][j] = type;
map[map.length - 1][j] = type; mapArray[mapArray.length - 1][j] = type;
} }
map[i][0] = type; mapArray[i][0] = type;
map[i][map.length - 1] = type; mapArray[i][mapArray.length - 1] = type;
} }
} }
@ -51,61 +64,92 @@ public class Map {
} }
public void addMoveToEvent(EventMoveToCell moveToCell) { public void addMoveToEvent(EventMoveToCell moveToCell) {
map[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO; mapArray[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO;
this.moveToCells.add(moveToCell); moveToCells.add(moveToCell);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
} }
public Person getPerson() { public Person getPerson() {
return person; return person;
} }
public LinkedList<Block> getBlocks() { return block; } public int getIdExit() {
return idExit;
}
public LinkedList<EventMoveToCell> getMoveToCells() { return moveToCells; } public LinkedList<Block> getBlocks() {
return block;
}
public LinkedList<EventMoveToCell> getMoveToCells() {
return moveToCells;
}
public void setIdExit(int id) {
this.idExit = id;
}
public void setSpawnPoint(int x, int y) {
mapArray[x][y] = TypeOfCell.SPAWNPOINT;
}
public void createMap() { public void createMap() {
for (int i = 0; i < map.length; i++){ for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < map.length; j++){ for (int j = 0; j < mapArray.length; j++) {
if (map[i][j] == TypeOfCell.BLOCK){ if (mapArray[i][j] == TypeOfCell.BLOCK) {
block.add(new Block(i, j)); block.add(new Block(i, j));
} }
if (map[i][j] == TypeOfCell.MOVETO){ if (mapArray[i][j] == TypeOfCell.SPAWNPOINT && idExit == 0) {
moveToCells.add(new EventMoveToCell(i,j)); person.set(i, j);
}
}
}
setPersonByMoveToEvent();
} }
if (map[i][j] == TypeOfCell.PERSON){ private void setPersonByMoveToEvent() {
person = new Person(i,j); for (EventMoveToCell cell : moveToCells) {
if (this.idExit == cell.getId()) {
switch (cell.getDirectionOut()) {
case 1:
person.set(cell.getX(), cell.getY() - 1);
break;
case 2:
person.set(cell.getX() + 1, cell.getY());
break;
case 3:
person.set(cell.getX(), cell.getY() + 1);
break;
case 4:
person.set(cell.getX() - 1, cell.getY());
break;
default:
person.set(-1, -1);
break;
} }
} }
} }
} }
class Canvas extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Cell cell: block){
cell.paint(g2D);
}
person.paint(g2D);
}
}
boolean isBusyCell(int x, int y) { boolean isBusyCell(int x, int y) {
for (Block cell : block) { for (Block cell : block) {
if (cell.isInBlock(x, y)) if (cell.isInBlock(x, y))
return true; return true;
} }
return person.isInPerson(x,y); return false;
} }
boolean isCellMoveTo(int x, int y) { boolean isCellMoveTo(int x, int y) {
for (EventMoveToCell cell : moveToCells) for (EventMoveToCell cell : moveToCells)
if (x == cell.getX() && y == cell.getY()) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("EventMoveToCell"))
return true; return true;
return false; return false;
} }
@ -114,6 +158,6 @@ public class Map {
for (EventMoveToCell cell : moveToCells) for (EventMoveToCell cell : moveToCells)
if (x == cell.getX() && y == cell.getY()) if (x == cell.getX() && y == cell.getY())
return cell; return cell;
return new EventMoveToCell(WindowOfFreeMove.CANVAS_HEIGHT,WindowOfFreeMove.CANVAS_WIDTH, this); return moveToCells.getFirst();
} }
} }

View File

@ -1,17 +1,16 @@
import java.awt.*; import java.awt.*;
public class Person { public class Person extends Cell {
private Cell person;
private int direction; private int direction;
public Person() { public Person() {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.direction = 0; this.direction = 0;
this.person = new Cell(10,10, WindowOfFreeMove.CELL_SIZE,Color.RED);
} }
public Person(int x, int y) { public Person(int x, int y) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.direction = 0; this.direction = 0;
this.person = new Cell(x,y, WindowOfFreeMove.CELL_SIZE,Color.RED);
} }
public void setDirection(int direction) { public void setDirection(int direction) {
@ -20,45 +19,51 @@ public class Person {
} }
public void move() { public void move() {
int x = person.getX(); int x = this.getX();
int y = person.getY(); int y = this.getY();
switch (direction) { switch (direction) {
case WindowOfFreeMove.KEY_LEFT: x--; case WindowOfFreeMove.KEY_LEFT:
x--;
if (x < 0) if (x < 0)
x = WindowOfFreeMove.CANVAS_WIDTH - 1; x = WindowOfFreeMove.CANVAS_WIDTH - 1;
break; break;
case WindowOfFreeMove.KEY_RIGHT: x++; case WindowOfFreeMove.KEY_RIGHT:
x++;
if (x == WindowOfFreeMove.CANVAS_WIDTH) if (x == WindowOfFreeMove.CANVAS_WIDTH)
x = 0; x = 0;
break; break;
case WindowOfFreeMove.KEY_UP: y--; case WindowOfFreeMove.KEY_UP:
y--;
if (y < 0) if (y < 0)
y = WindowOfFreeMove.CANVAS_HEIGHT - 1; y = WindowOfFreeMove.CANVAS_HEIGHT - 1;
break; break;
case WindowOfFreeMove.KEY_DOWN: y++; case WindowOfFreeMove.KEY_DOWN:
y++;
if (y == WindowOfFreeMove.CANVAS_HEIGHT) if (y == WindowOfFreeMove.CANVAS_HEIGHT)
y = 0; y = 0;
break; break;
} }
this.direction = 0; this.direction = 0;
if (!WindowOfFreeMove.map.isBusyCell(x, y)){
person = new Cell(x , y,20,Color.RED);
}
if (WindowOfFreeMove.map.isCellMoveTo(x,y)){ if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) {
WindowOfFreeMove.map = WindowOfFreeMove.map.findEventByXY(x,y).map; WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap();
WindowOfFreeMove.map.createMap(); } else {
if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) {
set(x, y);
} }
} }
public void paint(Graphics2D g) {
person.paint(g);
} }
public boolean isInPerson(int x, int y) { public boolean isInPerson(int x, int y) {
if (this.person.getX() == x && this.person.getY() == y) if (this.getX() == x && this.getY() == y)
return true; return true;
return false; return false;
} }
@Override
public void paint(Graphics2D g) {
super.paint(g);
g.drawString(Integer.toString(WindowOfFreeMove.getMap().getIdExit()), getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE);
}
} }

View File

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

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 = 20; static final int CELL_SIZE = 40;
static final int CANVAS_WIDTH = 20; static final int CANVAS_WIDTH = 20;
static final int CANVAS_HEIGHT = 20; static final int CANVAS_HEIGHT = 20;
@ -14,7 +14,11 @@ public class WindowOfFreeMove extends JFrame {
static final int KEY_DOWN = 40; static final int KEY_DOWN = 40;
private Canvas canvas; private Canvas canvas;
static Map map; private static Map map;
public static Map getMap() { return map; }
public static void setMap(Map map){ WindowOfFreeMove.map = map; }
public static void paintMapOnWindow() {WindowOfFreeMove.map.createMap();}
public WindowOfFreeMove() { public WindowOfFreeMove() {
setTitle("Ahoj"); setTitle("Ahoj");
@ -40,23 +44,42 @@ public class WindowOfFreeMove extends JFrame {
setVisible(true); setVisible(true);
} }
public void start() {
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; public void start() {
Map m1 = new Map(CANVAS_HEIGHT,CANVAS_WIDTH, true);
m1.setName("1");
m1.setSpawnPoint(12,12);
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m2.setName("2");
Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
m3.setName("3");
Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m4.setName("4");
m1.addMoveToEvent(new EventMoveToCell(1,1,18,18, m2));
m1.addMoveToEvent(new EventMoveToCell(4, 3,18,1, m4));
m2.addMoveToEvent(new EventMoveToCell(2,1, 18, 18, m3));
m2.addMoveToEvent(new EventMoveToCell(1, 3,18, 1, m1));
m3.addMoveToEvent(new EventMoveToCell(3, 1,18, 18, m4));
m3.addMoveToEvent(new EventMoveToCell(2, 3,18, 1, m2));
m4.addMoveToEvent(new EventMoveToCell(4, 1,18, 18, m1));
m4.addMoveToEvent(new EventMoveToCell(3,3,18,1, m3));
setMap(m1);
map.createMap(); map.createMap();
while (true) { while (true) {
map.getPerson().move(); map.getPerson().move();
canvas.repaint(); canvas.repaint();
sleep(); //sleep();
} }
} }
class Canvas extends JPanel { static class Canvas extends JPanel {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
super.paint(g); super.paint(g);
@ -73,9 +96,9 @@ public class WindowOfFreeMove extends JFrame {
} }
} }
private void sleep() { protected static void sleep() {
try { try {
Thread.sleep(50); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }