diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java index 82714c5..26959b1 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java @@ -11,8 +11,8 @@ import java.io.FileNotFoundException; import java.util.Scanner; public class InteractionCell extends Cell { - private String message; - private ruscz.game.gameOfProd.Dialog.Dialog dialog; + protected String message; + protected ruscz.game.gameOfProd.Dialog.Dialog dialog; private Scanner scanner; private ImageCell imageCell; private WindowOfFreeMove window; diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Person.java b/src/main/java/ruscz/game/gameOfProd/Cells/Person.java index fc2361b..c1ee847 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Person.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Person.java @@ -110,11 +110,19 @@ public class Person extends Cell { else { door.readNextLine(); } + } else if (isItemInFront(x, y, directionSight)) { + if (!isInEvent) { + isInEvent = true; + map.findItemByXY(x, y, directionSight).startEvent(); + } + else { + map.findItemByXY(x, y, directionSight).endEvent(); + } } break; } if (directionMove != WindowOfFreeMove.KEY_EVENT) { - if (directionMove != 0 && !map.isBusyCell(x, y, true) && !map.isInteractionCell(x, y) && !map.isClosedDoor(x, y)) { + if (directionMove != 0 && !map.isBusyCell(x, y, true) && !map.isInteractionCell(x, y) && !map.isClosedDoor(x, y) && !map.isItem(x, y)) { directionMoveLast = directionMove; isMoving = true; } else if (directionMove != 0 && map.isBusyCell(x, y, true)) { @@ -123,6 +131,8 @@ public class Person extends Cell { directionMoveLast = 0; } else if (directionMove != 0 && map.isClosedDoor(x, y)) { directionMoveLast = 0; + } else if (directionMove != 0 && map.isItem(x, y)) { + directionMoveLast = 0; } if (map.isCellMoveTo(x, y) && !map.isClosedDoor(x, y)) { set(x, y); @@ -131,7 +141,7 @@ public class Person extends Cell { map.findEventByXY(x, y).moveToMap(); } } else { - if (!map.isInteractionCell(x, y) && !map.isBusyCell(x, y, true) && !map.isClosedDoor(x, y)) { + if (!map.isInteractionCell(x, y) && !map.isBusyCell(x, y, true) && !map.isClosedDoor(x, y) && !map.isItem(x, y)) { set(x, y); } } @@ -152,39 +162,18 @@ public class Person extends Cell { } 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.isInteractionCell(x, y); + int[] coords = map.directionSightSwitch(x, y, directionSight); + return map.isInteractionCell(coords[0], coords[1]); } private boolean isDoorInFront(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.isDoor(x, y); + int[] coords = map.directionSightSwitch(x, y, directionSight); + return map.isDoor(coords[0], coords[1]); + } + + private boolean isItemInFront(int x, int y, int directionSight) { + int[] coords = map.directionSightSwitch(x, y, directionSight); + return map.isActiveItem(coords[0], coords[1]); } public void stopPerson(){ diff --git a/src/main/java/ruscz/game/gameOfProd/Image/ImageCell.java b/src/main/java/ruscz/game/gameOfProd/Image/ImageCell.java index 9f6dc6d..12771d7 100644 --- a/src/main/java/ruscz/game/gameOfProd/Image/ImageCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Image/ImageCell.java @@ -1,6 +1,5 @@ package ruscz.game.gameOfProd.Image; -import ruscz.game.gameOfProd.Cells.Cell; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import javax.imageio.ImageIO; @@ -18,6 +17,7 @@ public class ImageCell { private LinkedList imagesOfTiles; private HashMap cellMap; private ImageDoor imageDoor; + private LinkedList imagesOfItems; private int[][] mapBackground; private int[][] mapForeground; @@ -34,6 +34,10 @@ public class ImageCell { String[] mapInfo = data.split(" "); imageTileset(mapInfo); } + else if (data.contains("")) { + String[] mapInfo = data.split(" "); + imageItem(mapInfo); + } else { String[] mapColumn = data.split(" "); for (int i = 0; i < mapColumn.length; i++) { @@ -127,6 +131,38 @@ public class ImageCell { } } + public void imageItem(String[] itemInfo) { + int imageWidth = 0; + int imageHeight = 0; + int size = 0; + try { + image = ImageIO.read(new File("src/main/resources/items/" + itemInfo[1])); + imageWidth = Integer.parseInt(itemInfo[2]); + imageHeight = Integer.parseInt(itemInfo[3]); + size = Integer.parseInt(itemInfo[4]); + } catch (IOException e) { + e.printStackTrace(); + } + + int Xtiles = imageWidth / size; + int Ytiles = imageHeight / size; + int tiles = Xtiles * Ytiles; + + if (imagesOfItems == null) + imagesOfItems = new LinkedList(); + + for (int i = 0; i < Xtiles; i++) { + for (int j = 0; j < Ytiles; j++) { + BufferedImage img = image.getSubimage(size * i, size * j, size, size); + imagesOfItems.add(img); + } + } + } + + public LinkedList getImagesOfItems() { + return imagesOfItems; + } + public int getImageFromCellMap(String cell) { return cellMap.get(cell); } diff --git a/src/main/java/ruscz/game/gameOfProd/Inventory/InventoryManager.java b/src/main/java/ruscz/game/gameOfProd/Inventory/InventoryManager.java new file mode 100644 index 0000000..de432fd --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Inventory/InventoryManager.java @@ -0,0 +1,36 @@ +package ruscz.game.gameOfProd.Inventory; + +import java.util.LinkedList; + +public class InventoryManager { + private static LinkedList inventory = new LinkedList(); + + + public static void addItem(Item item) { + inventory.add(item); + } + + public static void flushInventory() { + inventory = null; + } + + public static boolean isInInventory(Item item) { + for (Item inventoryItem : inventory) { + if (inventoryItem == item) return true; + } + return false; + } + + public static void removeItem(Item item) { + inventory.remove(item); + } + + public static LinkedList getInventory() { return inventory; } + + public static Item findItemByName(String name) { + for (Item item : inventory) { + if (item.getName().equals(name)) return item; + } + return null; + } +} diff --git a/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java b/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java new file mode 100644 index 0000000..48ccb48 --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java @@ -0,0 +1,102 @@ +package ruscz.game.gameOfProd.Inventory; + +import ruscz.game.gameOfProd.Cells.InteractionCell; +import ruscz.game.gameOfProd.Cells.Person; +import ruscz.game.gameOfProd.Dialog.Dialog; +import ruscz.game.gameOfProd.Image.ImageCell; +import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; + +import java.awt.*; + +public class Item extends InteractionCell { + private ItemAction itemAction; + private String name; + private int multiplicity; + private int image; + private boolean isActive; + + public Item(int x, int y, String name, Dialog dialog, ImageCell imageCell, WindowOfFreeMove window, int multiplicity, int image, String itemAction) { + super(x, y, null, dialog, imageCell, window, true); + message = parseNameToMessage(name); + this.image = image; + this.multiplicity = multiplicity; + setItemActionByName(itemAction); + this.isActive = true; + } + + public Item(int x, int y, String name, Dialog dialog, ImageCell imageCell, WindowOfFreeMove window, int image, String itemAction) { + super(x, y, null, dialog, imageCell, window, true); + message = parseNameToMessage(name); + this.image = image; + this.multiplicity = 1; + setItemActionByName(itemAction); + this.isActive = true; + } + + @Override + public void startEvent() { + WindowOfFreeMove.setCanPersonMove(false); + this.dialog.hideText(); + this.dialog.setText(message); + this.dialog.showText(); + InventoryManager.addItem(this); + } + + @Override + public void endEvent() { + this.isActive = false; + this.dialog.hideText(); + this.dialog.setImageDialog(null); + Person.setInEvent(false); + WindowOfFreeMove.setCanPersonMove(true); + } + + private void setItemActionByName(String action) { + switch (action) { + case "heal": + itemAction = ItemAction.HEAL; + break; + case "mana": + itemAction = ItemAction.MANA; + break; + case "def": + itemAction = ItemAction.DEFENCE; + break; + case "str": + itemAction = ItemAction.STRENGTH; + break; + case "key": + itemAction = ItemAction.KEY; + break; + } + } + + private String parseNameToMessage(String name) { + name = name.replace("-", " "); + this.name = name.substring(0,1).toUpperCase() + name.substring(1); + String message = this.name + " was added to your inventory."; + return message; + } + + public String getName() { return name; } + + public int getImage() { return image; } + + public int getMultiplicity() { return multiplicity; } + + public void hideItem() { + isActive = false; + } + + public boolean isActive() { + return isActive; + } + + @Override + public void paint(Graphics2D g) { + if (isActive) { + g.drawImage(super.getImageCell().getImagesOfItems().get(image), getX() * getSize(), + getY() * getSize(), getSize() / 2, getSize() / 2, null); + } + } +} diff --git a/src/main/java/ruscz/game/gameOfProd/Inventory/ItemAction.java b/src/main/java/ruscz/game/gameOfProd/Inventory/ItemAction.java new file mode 100644 index 0000000..78a9b03 --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Inventory/ItemAction.java @@ -0,0 +1,5 @@ +package ruscz.game.gameOfProd.Inventory; + +public enum ItemAction { + HEAL, MANA, DEFENCE, STRENGTH, KEY; +} diff --git a/src/main/java/ruscz/game/gameOfProd/Map/Map.java b/src/main/java/ruscz/game/gameOfProd/Map/Map.java index ed40a80..c70f339 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/Map.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/Map.java @@ -3,6 +3,8 @@ package ruscz.game.gameOfProd.Map; import ruscz.game.gameOfProd.Cells.*; import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Image.ImageCell; +import ruscz.game.gameOfProd.Inventory.Item; +import ruscz.game.gameOfProd.Inventory.ItemAction; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import java.io.File; @@ -19,6 +21,7 @@ public class Map { private LinkedList interactionCells; private LinkedList triggerCells; private LinkedList doorCells; + private LinkedList items; private LinkedList npcs; private int idExit; private ImageCell imageCell; @@ -34,6 +37,7 @@ public class Map { doorCells = new LinkedList<>(); block = new LinkedList<>(); imageCell = new ImageCell(); + items = new LinkedList<>(); npcs = new LinkedList<>(); this.mapArray = mapArray; person = new Person(this); @@ -113,6 +117,12 @@ public class Map { map.addInteractionEvent(new DoorCell(x, y, Boolean.parseBoolean(door[1]), map.getImageCell(), window)); else map.addInteractionEvent(new DoorCell(x, y, Boolean.parseBoolean(door[1]), door[2], new Dialog(window.getDialogBar()), map.getImageCell(), window, Boolean.parseBoolean(door[3]))); + } else if (line[x].contains("ITEM")) { + String[] item = line[x].split(","); + if (item.length == 5) + map.addInteractionEvent(new Item(x, y, item[1], new Dialog(window.getDialogBar()), map.getImageCell(), window, Integer.parseInt(item[2]), Integer.parseInt(item[3]), item[4])); + else + map.addInteractionEvent(new Item(x, y, item[1], new Dialog(window.getDialogBar()), map.getImageCell(), window, Integer.parseInt(item[2]), item[3])); } } } @@ -176,6 +186,11 @@ public class Map { doorCells.add(doorCell); } + public void addInteractionEvent(Item item) { + mapArray[item.getX()][item.getY()] = TypeOfCell.EVENT; + items.add(item); + } + public void setNpcs(Npc npc){ npcs.add(npc); } @@ -216,6 +231,8 @@ public class Map { public LinkedList getDoorCells() { return doorCells; } + public LinkedList getItems() { return items; } + public void setIdExit(int id) { this.idExit = id; } @@ -287,6 +304,20 @@ public class Map { return false; } + public boolean isActiveItem(int x, int y) { + for (Item item : items) + if (x == item.getX() && y == item.getY() && item.getClass().getName().equals("ruscz.game.gameOfProd.Inventory.Item") && item.isActive()) + return true; + return false; + } + + public boolean isItem(int x, int y) { + for (Item item : items) + if (x == item.getX() && y == item.getY() && item.getClass().getName().equals("ruscz.game.gameOfProd.Inventory.Item")) + return true; + return false; + } + public EventMoveToCell findEventByXY(int x, int y) { for (EventMoveToCell cell : moveToCells) if (x == cell.getX() && y == cell.getY()) @@ -295,22 +326,9 @@ public class Map { } public InteractionCell findInteractionByXY(int x, int y, int directionSight) { - switch (directionSight) { - case 1: - x--; - break; - case 3: - x++; - break; - case 2: - y--; - break; - case 0: - y++; - break; - } + int[] coords = directionSightSwitch(x, y, directionSight); for (InteractionCell cell : interactionCells) - if (x == cell.getX() && y == cell.getY()) + if (coords[0] == cell.getX() && coords[1] == cell.getY()) return cell; return null; } @@ -330,22 +348,9 @@ public class Map { } public DoorCell findDoorByXY(int x, int y, int directionSight) { - switch (directionSight) { - case 1: - x--; - break; - case 3: - x++; - break; - case 2: - y--; - break; - case 0: - y++; - break; - } + int[] coords = directionSightSwitch(x, y, directionSight); for (DoorCell cell : doorCells) - if (x == cell.getX() && y == cell.getY()) + if (coords[0] == cell.getX() && coords[1] == cell.getY()) return cell; return null; } @@ -363,4 +368,31 @@ public class Map { return cell; return null; } + + public Item findItemByXY(int x, int y, int directionSight) { + int[] coords = directionSightSwitch(x, y, directionSight); + for (Item item : items) + if (coords[0] == item.getX() && coords[1] == item.getY()) + return item; + return null; + } + + public int[] directionSightSwitch(int x, int y, int directionSight) { + switch (directionSight) { + case 1: + x--; + break; + case 3: + x++; + break; + case 2: + y--; + break; + case 0: + y++; + break; + } + int[] coords = {x, y}; + return coords; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index f5661b2..fc8bdc0 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -2,6 +2,7 @@ package ruscz.game.gameOfProd.Windows; import ruscz.game.gameOfProd.Cells.*; import ruscz.game.gameOfProd.Dialog.DialogBar; +import ruscz.game.gameOfProd.Inventory.Item; import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.MapManager; import ruscz.game.gameOfProd.Sounds.MusicManager; @@ -216,6 +217,9 @@ public class WindowOfFreeMove extends JLayeredPane { for (int j = 0; j < mapManager.getActualMap().getCanvasSize()[1]; j++) { mapManager.getActualMap().getImageCell().paintForeground(g2D, j, i); } + for (Item item : mapManager.getActualMap().getItems()) { + if (item.getY() == i) item.paint(g2D); + } } } } diff --git a/src/main/resources/items/TransparentIcons.png b/src/main/resources/items/TransparentIcons.png new file mode 100644 index 0000000..6617907 Binary files /dev/null and b/src/main/resources/items/TransparentIcons.png differ diff --git a/src/main/resources/maps/openspace-tileset.txt b/src/main/resources/maps/openspace-tileset.txt index 83604df..71f89a8 100644 --- a/src/main/resources/maps/openspace-tileset.txt +++ b/src/main/resources/maps/openspace-tileset.txt @@ -2,6 +2,7 @@ Openspace1.png 512 512 32 Openspace2.png 512 512 32 D_Inside_House.png 512 512 32 + TransparentIcons.png 512 640 32 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 117 117 117 117 117 117 117 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 117 117 117 117 117 117 117 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 diff --git a/src/main/resources/maps/openspace.txt b/src/main/resources/maps/openspace.txt index 5a5869e..a2e6859 100644 --- a/src/main/resources/maps/openspace.txt +++ b/src/main/resources/maps/openspace.txt @@ -6,7 +6,7 @@ BLOCK block EMPTY EMPTY EMPTY EMPTY EMPTY block BLOCK,u EMPTY EMPTY EMPTY BLOCK BLOCK block EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY DOOR,false EMPTY EMPTY EMPTY BLOCK EMPTY block block block block EMPTY block BLOCK BLOCK,d EMPTY BLOCK,d BLOCK,d BLOCK,d EMPTY BLOCK,d BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY block block block block EMPTY block BLOCK BLOCK DOOR,true,locked,false BLOCK BLOCK BLOCK DOOR,true BLOCK BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY block block block block EMPTY block -BLOCK block EMPTY block BLOCK block EMPTY block BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY block block block block EMPTY block +BLOCK block EMPTY block BLOCK block EMPTY block BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY block ITEM,white-key-card,19,key block block EMPTY block BLOCK block EMPTY block BLOCK block EMPTY block BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK block EMPTY block BLOCK block EMPTY block BLOCK EMPTY EMPTY EMPTY BLOCK BLOCK,d BLOCK,d BLOCK,d EMPTY BLOCK,d BLOCK,d BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY EMPTY EMPTY BLOCK BLOCK BLOCK BLOCK DOOR,true BLOCK BLOCK BLOCK diff --git a/src/main/resources/save/savedGame/events-DESKTOP-TL25DTN.txt b/src/main/resources/save/savedGame/events-DESKTOP-TL25DTN.txt new file mode 100644 index 0000000..245388a --- /dev/null +++ b/src/main/resources/save/savedGame/events-DESKTOP-TL25DTN.txt @@ -0,0 +1,70 @@ + + m1 + + + + + m2 + + + 7 + 2 + true + + + + + m3 + + + 2 + 5 + true + + + + + m4 + + + 3 + 3 + true + + + + + m5 + + + + + m6 + + + 11 + 11 + true + + + + + openspace + + + + + office + + + + + boss + + + 4 + 18 + true + + + diff --git a/src/main/resources/save/savedGame/events.txt b/src/main/resources/save/savedGame/events.txt index f79dde1..245388a 100644 --- a/src/main/resources/save/savedGame/events.txt +++ b/src/main/resources/save/savedGame/events.txt @@ -64,7 +64,7 @@ 4 18 - false + true diff --git a/src/main/resources/save/savedGame/person.txt b/src/main/resources/save/savedGame/person.txt index d826f54..3e8a7a4 100644 --- a/src/main/resources/save/savedGame/person.txt +++ b/src/main/resources/save/savedGame/person.txt @@ -1,5 +1,5 @@ openspace 8 - 15 + 16 \ No newline at end of file diff --git a/src/main/resources/save/savedGame/village-DESKTOP-TL25DTN.txt b/src/main/resources/save/savedGame/village-DESKTOP-TL25DTN.txt new file mode 100644 index 0000000..1b4bc7b --- /dev/null +++ b/src/main/resources/save/savedGame/village-DESKTOP-TL25DTN.txt @@ -0,0 +1 @@ +TODO: Village \ No newline at end of file