From 76739987aa35444727932a4677286101e7ccc441 Mon Sep 17 00:00:00 2001 From: musilto9 Date: Wed, 11 Mar 2020 21:41:20 +0100 Subject: [PATCH] first step towards interactions --- src/main/java/InteractionCell.java | 1 + src/main/java/Map.java | 18 ++++++++++++++++++ src/main/java/Person.java | 29 +++++++++++++++++++++++++++-- src/main/java/TypeOfCell.java | 2 +- src/main/java/WindowOfFreeMove.java | 1 + 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/java/InteractionCell.java b/src/main/java/InteractionCell.java index 5b7ae74..96d6608 100644 --- a/src/main/java/InteractionCell.java +++ b/src/main/java/InteractionCell.java @@ -1,6 +1,7 @@ import java.awt.*; public class InteractionCell extends Cell { + InteractionCell(){ super(-1,-1, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); } diff --git a/src/main/java/Map.java b/src/main/java/Map.java index 942994d..9a104b9 100644 --- a/src/main/java/Map.java +++ b/src/main/java/Map.java @@ -13,6 +13,7 @@ public class Map { private Person person; private LinkedList moveToCells; private LinkedList block; + private LinkedList eventCells; private boolean isBlocked; private int idExit; private ImageCell imageCell; @@ -49,6 +50,7 @@ public class Map { idExit = 0; moveToCells = new LinkedList(); block = new LinkedList(); + eventCells = new LinkedList(); mapArray = new TypeOfCell[height][width]; this.isBlocked = isBlocked; imageCell = new ImageCell(); @@ -80,6 +82,11 @@ public class Map { moveToCells.add(moveToCell); } + public void addInteractionEvent(InteractionCell interactionCell) { + mapArray[interactionCell.getX()][interactionCell.getY()] = TypeOfCell.EVENT; + eventCells.add(interactionCell); + } + public void setName(String name) { this.name = name; } @@ -104,6 +111,10 @@ public class Map { return moveToCells; } + public LinkedList getInteractionEventCells() { + return eventCells; + } + public void setIdExit(int id) { this.idExit = id; } @@ -182,6 +193,13 @@ public class Map { return false; } + boolean isCellEvent(int x, int y) { + for (InteractionCell cell : eventCells) + if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("InteractionCell")) + return true; + return false; + } + EventMoveToCell findEventByXY(int x, int y) { for (EventMoveToCell cell : moveToCells) if (x == cell.getX() && y == cell.getY()) diff --git a/src/main/java/Person.java b/src/main/java/Person.java index 811e00d..2eba6f5 100644 --- a/src/main/java/Person.java +++ b/src/main/java/Person.java @@ -75,11 +75,13 @@ public class Person extends Cell { y = 0; break; } - if (directionMove != 0 && !map.isBusyCell(x, y)) { + if (directionMove != 0 && !map.isBusyCell(x, y) && !map.isCellEvent(x, y)) { directionMoveLast = directionMove; isMoving = true; } else if (directionMove != 0 && map.isBusyCell(x, y)) { directionMoveLast = 0; + } else if (directionMove != 0 && map.isCellEvent(x, y)) { + directionMoveLast = 0; } if (map.isCellMoveTo(x, y)) { set(x, y); @@ -88,13 +90,36 @@ public class Person extends Cell { map.findEventByXY(x, y).moveToMap(); } } else { - if (!map.isBusyCell(x, y)) { + if (!map.isBusyCell(x, y) && !map.isCellEvent(x,y)) { set(x, y); } } directionMove = 0; + + if (isEventInFront(x,y,directionSight)) { + System.out.println("showEvent"); + } } } + + private boolean isEventInFront(int x, int y, int directionSight) { + switch (directionSight) { + case 1: + x--; + break; + case 3: + x++; + break; + case 2: + y--; + break; + case 0: + y++; + break; + } + return map.isCellEvent(x, y); + } + public void stopPerson(){ directionMove = 0; directionMoveLast = 0; diff --git a/src/main/java/TypeOfCell.java b/src/main/java/TypeOfCell.java index 76d7231..c621655 100644 --- a/src/main/java/TypeOfCell.java +++ b/src/main/java/TypeOfCell.java @@ -1,3 +1,3 @@ public enum TypeOfCell { - BLOCK, EMPTY, MOVETO, SPAWNPOINT; + BLOCK, EMPTY, MOVETO, EVENT, SPAWNPOINT; } diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java index 7d1aec6..68f8e5c 100644 --- a/src/main/java/WindowOfFreeMove.java +++ b/src/main/java/WindowOfFreeMove.java @@ -149,6 +149,7 @@ public class WindowOfFreeMove extends JFrame { m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this)); m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this)); + m1.addInteractionEvent(new InteractionCell(5, 5)); m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this)); m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this));