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;
public class Block extends Cell {
Random random;
// private Random random;
public Block(){
super(-1, - 1, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
this.random = new Random();
// this.random = new Random();
}
public Block(int x, int y){
super(x,y, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
this.random = new Random();
// this.random = new Random();
}
public boolean isInBlock(int x, int y) {
@ -21,12 +21,12 @@ public class Block extends Cell {
return false;
}
public void randomAppear() {
int x, y;
do {
x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT);
y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH);
} while (WindowOfFreeMove.map.isBusyCell(x, y));
set(x, y);
}
// public void randomAppear() {
// int x, y;
// do {
// x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT);
// y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH);
// } while (WindowOfFreeMove.getMap().isBusyCell(x, y));
// set(x, y);
// }
}

View File

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

View File

@ -1,21 +1,47 @@
import java.awt.*;
public class EventMoveToCell extends Cell {
Map map;
private Map map;
private int id;
private int directionOut;
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 int getId(){
return id;
}
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);
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);
}
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);
this.id = id;
this.directionOut = directionOut;
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;
public class Map {
private TypeOfCell[][] map;
private String name;
private TypeOfCell[][] mapArray;
private Person person;
private LinkedList<EventMoveToCell> moveToCells;
private LinkedList<Block> block;
private boolean isBlocked;
private int idExit;
Map(int height, int width) {
moveToCells = new LinkedList<>();
block = new LinkedList<>();
map = new TypeOfCell[height][width];
person = new Person(0,0);
name = "Spawn";
idExit = 0;
mapArray = new TypeOfCell[height][width];
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){
moveToCells = new LinkedList<>();
block = new LinkedList<>();
map = new TypeOfCell[height][width];
if ((height < xPerson || xPerson < 0) || (width < yPerson || yPerson < 0)){
map[0][0] = TypeOfCell.PERSON;
}
else map [xPerson][yPerson] = TypeOfCell.PERSON;
}
Map(int height, int width, Boolean isBlocked) {
name = "Spawn";
idExit = 0;
person = new Person();
moveToCells = new LinkedList<EventMoveToCell>();
block = new LinkedList<Block>();
mapArray = new TypeOfCell[height][width];
this.isBlocked = isBlocked;
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)){
map[1][1] = TypeOfCell.PERSON;
}
else map [xPerson][yPerson] = TypeOfCell.PERSON;
TypeOfCell type;
if (isBlocked)
type = TypeOfCell.BLOCK;
else
type = TypeOfCell.EMPTY;
for (int i = 0; i < map.length; i++){
for (int j = 0; j < map.length; j++){
map[0][j] = type;
map[map.length - 1][j] = type;
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;
}
map[i][0] = type;
map[i][map.length - 1] = type;
mapArray[i][0] = type;
mapArray[i][mapArray.length - 1] = type;
}
}
@ -51,61 +64,92 @@ public class Map {
}
public void addMoveToEvent(EventMoveToCell moveToCell) {
map[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO;
this.moveToCells.add(moveToCell);
mapArray[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO;
moveToCells.add(moveToCell);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Person getPerson() {
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() {
for (int i = 0; i < map.length; i++){
for (int j = 0; j < map.length; j++){
if (map[i][j] == TypeOfCell.BLOCK){
for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < mapArray.length; j++) {
if (mapArray[i][j] == TypeOfCell.BLOCK) {
block.add(new Block(i, j));
}
if (map[i][j] == TypeOfCell.MOVETO){
moveToCells.add(new EventMoveToCell(i,j));
if (mapArray[i][j] == TypeOfCell.SPAWNPOINT && idExit == 0) {
person.set(i, j);
}
}
}
setPersonByMoveToEvent();
}
if (map[i][j] == TypeOfCell.PERSON){
person = new Person(i,j);
private void setPersonByMoveToEvent() {
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) {
for (Block cell : block) {
if (cell.isInBlock(x, y))
return true;
}
return person.isInPerson(x,y);
return false;
}
boolean isCellMoveTo(int x, int y) {
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 false;
}
@ -114,6 +158,6 @@ public class Map {
for (EventMoveToCell cell : moveToCells)
if (x == cell.getX() && y == cell.getY())
return cell;
return new EventMoveToCell(WindowOfFreeMove.CANVAS_HEIGHT,WindowOfFreeMove.CANVAS_WIDTH, this);
return moveToCells.getFirst();
}
}

View File

@ -1,17 +1,16 @@
import java.awt.*;
public class Person {
private Cell person;
public class Person extends Cell {
private int direction;
public Person() {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.direction = 0;
this.person = new Cell(10,10, WindowOfFreeMove.CELL_SIZE,Color.RED);
}
public Person(int x, int y) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.direction = 0;
this.person = new Cell(x,y, WindowOfFreeMove.CELL_SIZE,Color.RED);
}
public void setDirection(int direction) {
@ -20,45 +19,51 @@ public class Person {
}
public void move() {
int x = person.getX();
int y = person.getY();
int x = this.getX();
int y = this.getY();
switch (direction) {
case WindowOfFreeMove.KEY_LEFT: x--;
case WindowOfFreeMove.KEY_LEFT:
x--;
if (x < 0)
x = WindowOfFreeMove.CANVAS_WIDTH - 1;
break;
case WindowOfFreeMove.KEY_RIGHT: x++;
case WindowOfFreeMove.KEY_RIGHT:
x++;
if (x == WindowOfFreeMove.CANVAS_WIDTH)
x = 0;
break;
case WindowOfFreeMove.KEY_UP: y--;
case WindowOfFreeMove.KEY_UP:
y--;
if (y < 0)
y = WindowOfFreeMove.CANVAS_HEIGHT - 1;
break;
case WindowOfFreeMove.KEY_DOWN: y++;
case WindowOfFreeMove.KEY_DOWN:
y++;
if (y == WindowOfFreeMove.CANVAS_HEIGHT)
y = 0;
break;
}
this.direction = 0;
if (!WindowOfFreeMove.map.isBusyCell(x, y)){
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();
if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) {
WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap();
} 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) {
if (this.person.getX() == x && this.person.getY() == y)
if (this.getX() == x && this.getY() == y)
return true;
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 {
BLOCK, EMPTY, MOVETO, PERSON;
BLOCK, EMPTY, MOVETO, SPAWNPOINT;
}

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 = 20;
static final int CELL_SIZE = 40;
static final int CANVAS_WIDTH = 20;
static final int CANVAS_HEIGHT = 20;
@ -14,7 +14,11 @@ public class WindowOfFreeMove extends JFrame {
static final int KEY_DOWN = 40;
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() {
setTitle("Ahoj");
@ -40,23 +44,42 @@ public class WindowOfFreeMove extends JFrame {
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();
while (true) {
map.getPerson().move();
canvas.repaint();
sleep();
//sleep();
}
}
class Canvas extends JPanel {
static class Canvas extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
@ -73,9 +96,9 @@ public class WindowOfFreeMove extends JFrame {
}
}
private void sleep() {
protected static void sleep() {
try {
Thread.sleep(50);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}