aboutsummaryrefslogtreecommitdiff
path: root/src/main/Level.java
blob: 95d6b5db6c364c35ee8e1c8534b2d7132a895088 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main;

import java.io.File;
import java.util.ArrayList;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import gui.Driver;

public class Level 
{
    public File thisDir;
    private String title;
    private String artist;
    private ArrayList<Difficulty> diffList;

    public Image preview; //optional
    public String desc;
    public Image background; //optional
    public Color[] colors;//optional, have default colors

    public Level(File dir)
    {
        thisDir = dir;
        readData();
    }

    public void readData()
    {
        diffList = new ArrayList<Difficulty>();
        for(File cur: thisDir.listFiles()) //iterates through all files/folders in src/assets/levels/LEVEL
        {
            if (cur.isDirectory()) //all subfolders within a level folder are difficulties
            {
                Difficulty diff = new Difficulty(cur);
                diffList.add(diff);
            }

            if (cur.getName().equals("preview.png"))
            {
                preview = new Image(cur.toURI().toString());
            }

            if (cur.getName().equals("metadata.json"))
            {
                parseMetadata();
            }

            if (cur.getName().equals("background.png"))
            {
                background = new Image(cur.toURI().toString());
            }
        }
    }

    public void parseMetadata() 
    {
        JSONParser jsonParser = new JSONParser(); //parser to read the file
		
		try(FileReader reader = new FileReader(new File(thisDir, "metadata.json")))
		{
			Object obj = jsonParser.parse(reader); 
			JSONObject levelStuff = new JSONObject();
			levelStuff = (JSONObject)(obj); //converts read object to a JSONObject

            title = (String)(levelStuff.get("title"));
            artist = (String)(levelStuff.get("artist"));
            desc = (String)(levelStuff.get("desc"));

            if(( levelStuff).containsKey("color1")) //check for custom colors in a hexadecimal format
            {
                colors = new Color[5];

                colors[0] = Color.web((String)(levelStuff.get("color1"))); //read in all the custom colors
                colors[1] = Color.web((String)(levelStuff.get("color2")));
                colors[2] = Color.web((String)(levelStuff.get("color3")));
                colors[3] = Color.web((String)(levelStuff.get("color4")));
                colors[4] = Color.web((String)(levelStuff.get("color5")));
            }
            else
            {
                colors = new Color[]{Color.RED,Color.BLUE,Color.GREEN,Color.PURPLE,Color.YELLOW};
            }
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
    }

    public void writeMetadata()
    {
        FileWriter fileWriter;
        try 
        {
            fileWriter = new FileWriter(new File(thisDir, "metadata.json"));
            JSONObject obj = new JSONObject();
            obj.put("title", title);
            obj.put("artist", artist);
            obj.put("desc", desc);
            obj.put("color1",colors[0]);
            obj.put("color2",colors[1]);
            obj.put("color3",colors[2]);
            obj.put("color4",colors[3]);
            obj.put("color5",colors[4]);
            obj.writeJSONString(fileWriter);
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<Difficulty> getDiffList()
    {
        return diffList;
    }

    public void addDiff(String text) 
    {
        File diffDir = new File(thisDir, text);
        diffDir.mkdirs();
        File metadataDir = new File(diffDir, "metadata.json");
        try {
            metadataDir.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        readData();
    }

    public void removeDiff(Difficulty diff) 
    {
        //soon
    }

    public void renameDiff(Difficulty diff, String newName)
    {
        //soon
    }

    public String getTitle() 
    {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() 
    {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
}