added inventory and items

This commit is contained in:
musilto9 2020-04-14 01:46:25 +02:00
parent beedda8b62
commit 20c77ace10
15 changed files with 344 additions and 68 deletions

View File

@ -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;

View File

@ -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(){

View File

@ -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<BufferedImage> imagesOfTiles;
private HashMap<String, Integer> cellMap;
private ImageDoor imageDoor;
private LinkedList<BufferedImage> imagesOfItems;
private int[][] mapBackground;
private int[][] mapForeground;
@ -34,6 +34,10 @@ public class ImageCell {
String[] mapInfo = data.split(" ");
imageTileset(mapInfo);
}
else if (data.contains("<items>")) {
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<BufferedImage>();
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<BufferedImage> getImagesOfItems() {
return imagesOfItems;
}
public int getImageFromCellMap(String cell) {
return cellMap.get(cell);
}

View File

@ -0,0 +1,36 @@
package ruscz.game.gameOfProd.Inventory;
import java.util.LinkedList;
public class InventoryManager {
private static LinkedList<Item> inventory = new LinkedList<Item>();
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<Item> getInventory() { return inventory; }
public static Item findItemByName(String name) {
for (Item item : inventory) {
if (item.getName().equals(name)) return item;
}
return null;
}
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,5 @@
package ruscz.game.gameOfProd.Inventory;
public enum ItemAction {
HEAL, MANA, DEFENCE, STRENGTH, KEY;
}

View File

@ -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<InteractionCell> interactionCells;
private LinkedList<TriggerCell> triggerCells;
private LinkedList<DoorCell> doorCells;
private LinkedList<Item> items;
private LinkedList<Npc> 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<DoorCell> getDoorCells() { return doorCells; }
public LinkedList<Item> 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;
}
}

View File

@ -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);
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

View File

@ -2,6 +2,7 @@
<tileset> Openspace1.png 512 512 32
<tileset> Openspace2.png 512 512 32
<tileset> D_Inside_House.png 512 512 32
<items> 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

View File

@ -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

View File

@ -0,0 +1,70 @@
<map>
<mapName>m1</mapName>
<events>
</events>
</map>
<map>
<mapName>m2</mapName>
<events>
<event>
<x>7</x>
<y>2</y>
<active>true</active>
</event>
</events>
</map>
<map>
<mapName>m3</mapName>
<events>
<event>
<x>2</x>
<y>5</y>
<active>true</active>
</event>
</events>
</map>
<map>
<mapName>m4</mapName>
<events>
<event>
<x>3</x>
<y>3</y>
<active>true</active>
</event>
</events>
</map>
<map>
<mapName>m5</mapName>
<events>
</events>
</map>
<map>
<mapName>m6</mapName>
<events>
<event>
<x>11</x>
<y>11</y>
<active>true</active>
</event>
</events>
</map>
<map>
<mapName>openspace</mapName>
<events>
</events>
</map>
<map>
<mapName>office</mapName>
<events>
</events>
</map>
<map>
<mapName>boss</mapName>
<events>
<event>
<x>4</x>
<y>18</y>
<active>true</active>
</event>
</events>
</map>

View File

@ -64,7 +64,7 @@
<event>
<x>4</x>
<y>18</y>
<active>false</active>
<active>true</active>
</event>
</events>
</map>

View File

@ -1,5 +1,5 @@
<location>
<map>openspace</map>
<x>8</x>
<y>15</y>
<y>16</y>
</location>

View File

@ -0,0 +1 @@
TODO: Village