Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/com/codecool/dungeoncrawl/JSON/ActorJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.codecool.dungeoncrawl.JSON;

import com.codecool.dungeoncrawl.logic.actors.Actor;

public class ActorJson {
private String name;
private int hp;
private int x;
private int y;

public ActorJson(Actor actor) {
this.name = actor.getTileName();
this.x = actor.getX();
this.y = actor.getY();
this.hp = actor.getHealth();
}
/*public String convertPlayerJson(){
return ConvertToJSON.convertObjectToJson(this);
}*/


}
60 changes: 60 additions & 0 deletions src/main/java/com/codecool/dungeoncrawl/JSON/HandleJSON.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.codecool.dungeoncrawl.JSON;

import com.codecool.dungeoncrawl.logic.GameMap;
import com.codecool.dungeoncrawl.logic.actors.Actor;
import com.codecool.dungeoncrawl.logic.actors.Player;
import com.codecool.dungeoncrawl.logic.items.Item;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class HandleJSON {
public static String convertObjectToJson(Object obj) {
Gson JSONObject = new Gson();
return JSONObject.toJson(obj);
}

public static List<Object> getEveryMember(GameMap map){
List<Object> members = new ArrayList<>();
for (Actor actor: map.getActors()){
if(actor instanceof Player){
members.add( new PlayerJson((Player) actor));
}else{
members.add(new ActorJson(actor));
}
}
for (Item item : map.getItems()){
members.add(new ItemJson(item));
}
return members;
}

public static String createFinalJsonString(GameMap map){
List<String> list = mapConverter(map);
return createStringFromJSONList(list);
}

public static List<String> mapConverter(GameMap map) {
List<Object> mapObjects = getEveryMember(map);
List<String> JSONList = new ArrayList<>();
for (Object obj : mapObjects) {
JSONList.add(convertObjectToJson(obj));

}
for (String objectJson : JSONList) {
System.out.println(objectJson);
}
return JSONList;
}

public static String createStringFromJSONList(List<String> JSONList){
StringBuilder JSONbuilder = new StringBuilder();

for(String objectJson : JSONList){
JSONbuilder.append(objectJson);
}
return JSONbuilder.toString();
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/codecool/dungeoncrawl/JSON/ItemJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.codecool.dungeoncrawl.JSON;

import com.codecool.dungeoncrawl.logic.items.Item;

public class ItemJson {
private String name;
private int x;
private int y;

public ItemJson(Item item) {
this.name = item.getTileName();
this.x = item.getX();
this.y = item.getY();
}

/* public String convertPlayerJson(){
return ConvertToJSON.convertObjectToJson(this);
}*/

}
48 changes: 48 additions & 0 deletions src/main/java/com/codecool/dungeoncrawl/JSON/PlayerJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.codecool.dungeoncrawl.JSON;

import com.codecool.dungeoncrawl.logic.actors.Player;
import com.codecool.dungeoncrawl.logic.items.Inventory;

public class PlayerJson {

private String playerName;
private int hp;
private int x;
private int y;

public PlayerJson(Player player) {
this.playerName = player.getName();
this.inventory = player.getItemInventory();
this.x = player.getX();
this.y = player.getY();

this.hp = player.getHealth();

}
/*
public String convertPlayerJson(){
return ConvertToJSON.convertObjectToJson(this);
}*/

public Inventory getInventory() {
return inventory;
}

private Inventory inventory;

public String getPlayerName() {
return playerName;
}

public int getHp() {
return hp;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
66 changes: 45 additions & 21 deletions src/main/java/com/codecool/dungeoncrawl/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codecool.dungeoncrawl;

import com.codecool.dungeoncrawl.JSON.HandleJSON;
import com.codecool.dungeoncrawl.dao.GameDatabaseManager;
import com.codecool.dungeoncrawl.logic.Cell;
import com.codecool.dungeoncrawl.logic.GameMap;
Expand All @@ -8,7 +9,6 @@
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
Expand All @@ -23,6 +23,7 @@
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.text.Font;

Expand All @@ -33,9 +34,11 @@

import java.io.File;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;
import java.util.List;

public class Main extends Application {
static final int CONST_10 = 10;
Expand All @@ -58,15 +61,16 @@ public class Main extends Application {
Media backgroundMedia;
MediaPlayer backgroundMediaPlayer;
GameDatabaseManager dbManager;
Timer timer = new Timer();

Timeline fiveSecondsWonder;
private Stage myStage;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage){
public void start(Stage primaryStage) {
myStage = primaryStage;
setupDbManager();
backgroundMedia = new Media(new File("src/main/resources/background-music.wav").toURI().toString());
backgroundMediaPlayer = new MediaPlayer(backgroundMedia);
Expand Down Expand Up @@ -99,7 +103,7 @@ public void start(Stage primaryStage){

primaryStage.setTitle("Dungeon Crawl");
primaryStage.show();
moveMonsters();
//moveMonsters();
}

private void onKeyReleased(KeyEvent keyEvent) {
Expand All @@ -110,8 +114,7 @@ private void onKeyReleased(KeyEvent keyEvent) {
|| exitCombinationWin.match(keyEvent)
|| keyEvent.getCode() == KeyCode.ESCAPE) {
exit();
}
else if(saveCombination.match(keyEvent)){
} else if (saveCombination.match(keyEvent)) {
showSaveOption();
}
}
Expand Down Expand Up @@ -146,16 +149,15 @@ private void onKeyPressed(KeyEvent keyEvent) {
}
}

private void moveMonsters(){
Timeline fiveSecondsWonder = new Timeline(
private void moveMonsters() {
fiveSecondsWonder = new Timeline(
new KeyFrame(Duration.seconds(5),
event -> {
map.actAllMapCreature();
refresh();
}));
fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
fiveSecondsWonder.play();

}


Expand Down Expand Up @@ -195,17 +197,16 @@ private void setupDbManager() {
}


public void gameOverDisplay(){
timer.cancel();
public void gameOverDisplay() {
isGameOver = true;
Font myFont = new Font("Serif", 36);
gameOver.setText("Game\nOver!");
gameOver.setFont(myFont);
refresh();

}
public void gameWonDisplay(){
timer.cancel();

public void gameWonDisplay() {
isGameOver = true;
Font myFont = new Font("Serif", 22);
gameOver.setText("Congratulation!");
Expand All @@ -228,10 +229,6 @@ private void exit() {
System.exit(0);
}

/*public void saveName(Label nameLabel) {

}*/

public void showSaveOption() {

Label nameLabel = new Label("Please enter your name");
Expand All @@ -251,7 +248,34 @@ public void showSaveOption() {
Label playerName = new Label(name);
ui.add(playerName, 0, 0);
playerName.setFont(myFont);
System.out.println(map.getPlayer().getName());
String finalJson = HandleJSON.createFinalJsonString(map);
saveTextToFile(finalJson, saveToFile());

});
}


public File saveToFile() {
FileChooser file = new FileChooser();
file.setTitle("Save Image");
//System.out.println(pic.getId());
File file1 = file.showSaveDialog(myStage);

System.out.println(file1);
return file1;
}

private void saveTextToFile(String content, File file) {
try {
PrintWriter writer;
writer = new PrintWriter(file);
writer.println(content);
writer.close();
} catch (IOException ex) {
System.out.println("file not found");
}
}



}
2 changes: 0 additions & 2 deletions src/main/java/com/codecool/dungeoncrawl/logic/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public boolean isClosedDoor(){

public boolean isWater(){return this.type == CellType.WATER;}

public boolean isCrossedWater(){return this.type == CellType.CROSS_WATER;}

@Override
public String getTileName() {
return type.getTileName();
Expand Down
40 changes: 34 additions & 6 deletions src/main/java/com/codecool/dungeoncrawl/logic/GameMap.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.codecool.dungeoncrawl.logic;

import com.codecool.dungeoncrawl.logic.actors.Actor;
import com.codecool.dungeoncrawl.logic.actors.Monsters;
import com.codecool.dungeoncrawl.logic.actors.Player;
import com.codecool.dungeoncrawl.logic.items.ActableItem;
import com.codecool.dungeoncrawl.logic.items.Campfire;
import com.codecool.dungeoncrawl.logic.items.Item;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


Expand Down Expand Up @@ -64,7 +68,6 @@ public void repositionCenter() {
} else nextY = Math.min(player.getCell().getY(), height - 11);

centerCell = cells[nextX][nextY];

}


Expand All @@ -82,6 +85,35 @@ public void actAllMapCreature() {
}
}

public List<Item> getItems() {

List<Item> items = new ArrayList<>();

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (cells[x][y].getItem() != null){
items.add(cells[x][y].getItem());
}
}
}
return items;
}

public List<Actor> getActors() {

List<Actor> actors = new ArrayList<>();

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (cells[x][y].getActor() != null){
actors.add(cells[x][y].getActor());
}
}
}
return actors;
}


public void openDoor(int x, int y) {
cells[x][y].setType(CellType.ENTRANCE);
}
Expand All @@ -90,11 +122,7 @@ public void crossWater(int x, int y) {
cells[x][y].setType(CellType.CROSS_WATER);
}

public void remakeWater(int x, int y) {
cells[x][y].setType(CellType.WATER);
}

@Override
@Override
public String toString(){
StringBuilder map = new StringBuilder();
for(int i = 0; i < height; i++){
Expand Down
Loading