Add MapPainter

This commit is contained in:
iryupnik 2020-03-13 23:15:33 +01:00
parent 3af9f48386
commit 555b70e2f0
5 changed files with 120 additions and 74 deletions

View File

@ -42,9 +42,9 @@ public class EventMoveToCell extends Cell {
public void moveToMap() { public void moveToMap() {
//window.getMap().getPerson().stopPerson(); //window.getMap().getPerson().stopPerson();
window.getMap().setIdExit(id); window.getMap().getActualMap().setIdExit(id);
window.setMap(this.map); window.setMap(this.map);
window.getMap().setIdExit(id); window.getMap().getActualMap().setIdExit(id);
window.paintMapOnWindow(); window.paintMapOnWindow();
window.repaintCanvas(); window.repaintCanvas();
window.setCanPersonMove(true); window.setCanPersonMove(true);
@ -53,7 +53,7 @@ public class EventMoveToCell extends Cell {
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(g); super.paint(g);
g.drawImage(window.getMap().getImageCell().getImagesOfTiles().get(277), getX() * WindowOfFreeMove.CELL_SIZE, g.drawImage(window.getMap().getActualMap().getImageCell().getImagesOfTiles().get(277), 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);
g.drawString(Integer.toString(getId()), getX()* WindowOfFreeMove.CELL_SIZE, getY()* WindowOfFreeMove.CELL_SIZE); g.drawString(Integer.toString(getId()), getX()* WindowOfFreeMove.CELL_SIZE, getY()* WindowOfFreeMove.CELL_SIZE);
} }

View File

@ -135,60 +135,6 @@ public class Map {
public TypeOfCell[][] getMapArray() { return mapArray; } public TypeOfCell[][] getMapArray() { return mapArray; }
public void createMap() {
for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < mapArray.length; j++) {
if (mapArray[i][j] == TypeOfCell.BLOCK && !isBusyCell(i,j)) {
block.add(new Block(i, j, imageCell));
}
if (mapArray[i][j] == TypeOfCell.SPAWNPOINT && idExit == 0) {
person.set(i, j);
}
}
}
setPersonByMoveToEvent();
}
private void setPersonByMoveToEvent() {
for (EventMoveToCell cell : moveToCells) {
if (this.idExit == cell.getId()) {
switch (cell.getDirectionOut()) {
case 1:
person.set(cell.getX(), cell.getY() - 1);
person.setIsMoving(true);
person.setDirectionMoveLast(WindowOfFreeMove.KEY_UP);
person.setSize(WindowOfFreeMove.CELL_SIZE);
person.setDirectionSight(2);
break;
case 2:
person.set(cell.getX() + 1, cell.getY());
person.setIsMoving(true);
person.setDirectionMoveLast(WindowOfFreeMove.KEY_RIGHT);
person.setSize(WindowOfFreeMove.CELL_SIZE);
person.setDirectionSight(3);
break;
case 3:
person.set(cell.getX(), cell.getY() + 1);
person.setIsMoving(true);
person.setDirectionMoveLast(WindowOfFreeMove.KEY_DOWN);
person.setSize(WindowOfFreeMove.CELL_SIZE);
person.setDirectionSight(0);
break;
case 4:
person.set(cell.getX() - 1, cell.getY());
person.setIsMoving(true);
person.setDirectionMoveLast(WindowOfFreeMove.KEY_LEFT);
person.setSize(WindowOfFreeMove.CELL_SIZE);
person.setDirectionSight(1);
break;
default:
person.set(-1, -1);
break;
}
}
}
}
public boolean isBusyCell(int x, int y) { public boolean isBusyCell(int x, int y) {
for (Block cell : block) { for (Block cell : block) {

View File

@ -0,0 +1,90 @@
package ruscz.game.gameOfProd.Map;
import ruscz.game.gameOfProd.Cells.Block;
import ruscz.game.gameOfProd.Cells.EventMoveToCell;
import ruscz.game.gameOfProd.Cells.TypeOfCell;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.util.LinkedList;
public class MapPainter {
private LinkedList<Map> maps;
private Map actualMap;
public MapPainter (LinkedList<Map> maps){
this.maps = maps;
}
public void setMap(Map map){
if (maps.contains(map)){
actualMap = map;
}
else {
maps.add(map);
actualMap = map;
}
}
public Map getActualMap(){
return actualMap;
}
public void createMap() {
for (int i = 0; i < actualMap.getMapArray().length; i++) {
for (int j = 0; j < actualMap.getMapArray().length; j++) {
if (actualMap.getMapArray()[i][j] == TypeOfCell.BLOCK && !actualMap.isBusyCell(i,j)) {
actualMap.getBlocks().add(new Block(i, j, actualMap.getImageCell()));
}
if (actualMap.getMapArray()[i][j] == TypeOfCell.SPAWNPOINT && actualMap.getIdExit() == 0) {
actualMap.getPerson().set(i, j);
}
}
}
setPersonByMoveToEvent();
}
private void setPersonByMoveToEvent() {
for (EventMoveToCell cell : actualMap.getMoveToCells()) {
if (this.actualMap.getIdExit() == cell.getId()) {
switch (cell.getDirectionOut()) {
case 1:
actualMap.getPerson().set(cell.getX(), cell.getY() - 1);
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_UP);
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
actualMap.getPerson().setDirectionSight(2);
break;
case 2:
actualMap.getPerson().set(cell.getX() + 1, cell.getY());
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_RIGHT);
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
actualMap.getPerson().setDirectionSight(3);
break;
case 3:
actualMap.getPerson().set(cell.getX(), cell.getY() + 1);
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_DOWN);
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
actualMap.getPerson().setDirectionSight(0);
break;
case 4:
actualMap.getPerson().set(cell.getX() - 1, cell.getY());
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_LEFT);
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
actualMap.getPerson().setDirectionSight(1);
break;
default:
actualMap.getPerson().set(-1, -1);
break;
}
}
}
}
}

View File

@ -6,6 +6,7 @@ import ruscz.game.gameOfProd.Cells.TriggerCell;
import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Dialog.Dialog;
import ruscz.game.gameOfProd.Dialog.DialogBar; import ruscz.game.gameOfProd.Dialog.DialogBar;
import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Map.MapPainter;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -13,6 +14,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.util.LinkedList;
public class WindowOfFreeMove extends JFrame { public class WindowOfFreeMove extends JFrame {
public static final int CELL_SIZE = 40; public static final int CELL_SIZE = 40;
@ -32,11 +34,11 @@ public class WindowOfFreeMove extends JFrame {
private Canvas canvas; private Canvas canvas;
private DialogBar dialogBar; private DialogBar dialogBar;
private JLayeredPane lp; private JLayeredPane lp;
private Map map; private MapPainter map;
private Timer timer; private Timer timer;
private Timer inputTimer; private Timer inputTimer;
public Map getMap() { public MapPainter getMap() {
return map; return map;
} }
@ -49,7 +51,7 @@ public class WindowOfFreeMove extends JFrame {
} }
public void setMap(Map map) { public void setMap(Map map) {
this.map = map; this.map.setMap(map);
} }
public void paintMapOnWindow() { public void paintMapOnWindow() {
@ -79,8 +81,8 @@ public class WindowOfFreeMove extends JFrame {
inputTimer = new Timer(10, new ActionListener() { inputTimer = new Timer(10, new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
if (!map.getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) { if (!map.getActualMap().getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) {
map.getPerson().setDirectionMove(nextMove); map.getActualMap().getPerson().setDirectionMove(nextMove);
} }
} }
}); });
@ -88,8 +90,8 @@ public class WindowOfFreeMove extends JFrame {
addKeyListener(new KeyAdapter() { addKeyListener(new KeyAdapter() {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
if (!map.getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) { if (!map.getActualMap().getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) {
map.getPerson().setDirectionMove(nextMove); map.getActualMap().getPerson().setDirectionMove(nextMove);
} }
} }
}); });
@ -156,7 +158,7 @@ public class WindowOfFreeMove extends JFrame {
m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this)); m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this));
m1.addInteractionEvent(new InteractionCell(3,5,"first-dialog", new Dialog(dialogBar), m1.getImageCell())); m1.addInteractionEvent(new InteractionCell(3,5,"first-dialog", new Dialog(dialogBar), m1.getImageCell()));
m1.addInteractionEvent(new InteractionCell(5, 5, "dialog-test", new Dialog(dialogBar), m1.getImageCell())); m1.addInteractionEvent(new InteractionCell(5, 5, "dialog-test", new Dialog(dialogBar), m1.getImageCell()));
m1.addInteractionEvent(new TriggerCell(7,5,"second-dialog", new Dialog(dialogBar), m1.getImageCell())); m1.addInteractionEvent(new TriggerCell(2,2,"second-dialog", new Dialog(dialogBar), m1.getImageCell()));
m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this)); m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this));
m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this)); m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this));
@ -172,6 +174,14 @@ public class WindowOfFreeMove extends JFrame {
m3.getImageCell().readMapFile("m1-tileset"); m3.getImageCell().readMapFile("m1-tileset");
m4.getImageCell().readMapFile("m1-tileset"); m4.getImageCell().readMapFile("m1-tileset");
LinkedList<Map> mapLinkedList = new LinkedList<>();
mapLinkedList.add(m1);
mapLinkedList.add(m2);
mapLinkedList.add(m3);
mapLinkedList.add(m4);
map = new MapPainter(mapLinkedList);
setMap(m1); setMap(m1);
map.createMap(); map.createMap();
isReadyToPaint = true; isReadyToPaint = true;
@ -180,7 +190,7 @@ public class WindowOfFreeMove extends JFrame {
timer = new Timer(10, new ActionListener() { timer = new Timer(10, new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
map.getPerson().move(); map.getActualMap().getPerson().move();
canvas.repaint(); canvas.repaint();
} }
}); });
@ -198,18 +208,18 @@ public class WindowOfFreeMove extends JFrame {
int count = 0; int count = 0;
for (int i = 0; i < CANVAS_WIDTH; i++) { for (int i = 0; i < CANVAS_WIDTH; i++) {
for (int j = 0; j < CANVAS_HEIGHT; j++) { for (int j = 0; j < CANVAS_HEIGHT; j++) {
map.getImageCell().paintBackground(g2D, i, j, count); map.getActualMap().getImageCell().paintBackground(g2D, i, j, count);
count++; count++;
} }
} }
for (Cell cell : map.getMoveToCells()) { for (Cell cell : map.getActualMap().getMoveToCells()) {
cell.paint(g2D); cell.paint(g2D);
} }
map.getPerson().paint(g2D); map.getActualMap().getPerson().paint(g2D);
for (Cell cell : map.getBlocks()) { for (Cell cell : map.getActualMap().getBlocks()) {
cell.paint(g2D); cell.paint(g2D);
} }
for (Cell cell: map.getInteractionEventCells()){ for (Cell cell: map.getActualMap().getInteractionEventCells()){
cell.paint(g2D); cell.paint(g2D);
} }
} }

View File

@ -1,7 +1,7 @@
Cop: Turn around! Cop: Turn around!
Me: every now and then I get a little bit lonely, Me: Living in material world
and you're never comming 'round And I am a material boy
Cop: Turn around! Cop: Turn around!
Me: every now and then... Me: You know that...
*gets tazed* *gets tazed*
<end> <end>