Skip to content

Commit

Permalink
Overhaul the difficultyData format
Browse files Browse the repository at this point in the history
We now store a dataVersion, which is enforced,
and store the type of each note, allowing for special notes to exist
  • Loading branch information
AnnoyingRain5 committed Nov 29, 2023
1 parent 60ebb5c commit 2e8cf81
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CrankItUp.Game/src/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public static class Constants

public static readonly double CRANK_DEFAULT_LENGTH = 337;
public static readonly double CRANK_DEFAULT_HEIGHT = 70;

public static readonly int MAP_DATAVERSION = 1;
}
}
28 changes: 20 additions & 8 deletions CrankItUp.Game/src/Elements/Beatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using osu.Framework.IO.Stores;
using osu.Framework.Platform;
using osu.Framework.Logging;
using osu.Framework.Extensions;

namespace CrankItUp.Game
{
Expand Down Expand Up @@ -82,14 +81,27 @@ public Queue<BaseNote> GetBaseNoteQueue()
{
Queue<BaseNote> noteQueue = new Queue<BaseNote>();

foreach (JToken noteobject in beatmap.GetValue("BaseNoteQueue"))
foreach (JToken noteobject in beatmap.GetValue("noteQueue"))
{
noteQueue.Enqueue(
new BaseNote(
noteobject.GetValue<float>("position"),
noteobject.GetValue<int>("spawnTime")
)
);
switch (noteobject.GetValue<string>("noteType"))
{
case "Standard":
noteQueue.Enqueue(
new BaseNote(
noteobject.GetValue<float>("position"),
noteobject.GetValue<int>("spawnTime")
)
);
break;
case "Special":
Logger.Log(
"Map tried to spawn a special note, which is not yet implemented"
);
break;
default:
Logger.Log("Unknown note type!");
break;
}
}
return noteQueue;
}
Expand Down
77 changes: 76 additions & 1 deletion CrankItUp.Game/src/Screens/DifficultySelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Platform;
using Newtonsoft.Json.Linq;
using System;
using osu.Framework.Logging;
using NuGet.ProjectModel;

namespace CrankItUp.Game
{
Expand Down Expand Up @@ -43,11 +47,64 @@ private void load(TextureStore textures, Storage storage)
};
var difficulties = storage.GetFiles(Path.Combine("maps", map));
Vector2 position = new Vector2(0, 0);
Storage mapStorage = storage.GetStorageForDirectory("maps").GetStorageForDirectory(map);
int invalidDifficultyCount = 0;
foreach (string difficulty in difficulties)
{
JObject beatmap;
if (difficulty.EndsWith(".json"))
{
string difficultyname = difficulty[
string difficultyname = difficulty[(difficulty.LastIndexOf("/") + 1)..];
using (
var sr = new StreamReader(
mapStorage.GetStream(difficultyname, mode: FileMode.Open)
)
)
{
// Read the stream as a string, and write the string to the console.
try
{
beatmap = JObject.Parse(sr.ReadToEnd());
}
catch (Exception e)
{
Logger.Error(
e,
"Error while loading difficuly JSON! Skipping difficulty"
);
invalidDifficultyCount += 1;
continue;
}
}
int dataVersion;
try
{
JToken meta = beatmap.GetValue("meta");
dataVersion = meta.GetValue<int>("dataVersion");
}
catch (Exception e)
{
Logger.Error(
e,
"Error while checking difficulty dataVersion! Skipping difficulty"
);
invalidDifficultyCount += 1;
continue;
}
if (dataVersion != Constants.MAP_DATAVERSION)
{
Logger.Log(
"Difficulty "
+ difficultyname
+ " has an invalid dataVersion for this version of the game!",
LoggingTarget.Runtime,
LogLevel.Important
);
invalidDifficultyCount += 1;
continue;
}

difficultyname = difficulty[
(difficulty.LastIndexOf("/") + 1)..(difficulty.Length - 5)
];
trackContainer.Add(
Expand All @@ -68,6 +125,23 @@ private void load(TextureStore textures, Storage storage)
position.X += 250;
}
}

SpriteText invalidDifficultyText;
if (invalidDifficultyCount > 0)
{
invalidDifficultyText = new SpriteText
{
X = 10,
Y = 10,
Text =
"Warning: Map contains " + invalidDifficultyCount + " invalid difficulties!"
};
}
else
{
// make an empty spritetext, as there is nothing to say
invalidDifficultyText = new SpriteText { };
}
InternalChildren = new Drawable[]
{
new DrawSizePreservingFillContainer
Expand All @@ -82,6 +156,7 @@ private void load(TextureStore textures, Storage storage)
},
backButton,
trackContainer,
invalidDifficultyText,
}
};
}
Expand Down
11 changes: 7 additions & 4 deletions CrankItUp.Game/src/Screens/MappingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class MappingScreen : Screen
string difficulty;
Track track;
JObject beatmap = new JObject();
JArray BaseNoteQueue = new JArray();
JArray NoteQueue = new JArray();
Storage storage;

public MappingScreen(string difficultyname)
Expand Down Expand Up @@ -71,25 +71,28 @@ protected override bool OnKeyDown(KeyDownEvent e)
{
JObject note = new JObject
{
{ "noteType", "Standard" },
{ "position", 0 },
{ "spawnTime", track.CurrentTime }
};
BaseNoteQueue.Add(note);
NoteQueue.Add(note);
}
// if the user wishes to exit
else if (e.Key == osuTK.Input.Key.Escape)
{
JObject meta = new JObject
{
// use default values for now
{ "dataVersion", "1" },
{ "radius", 50 },
{ "approachRate", 100 },
// set the current time as the endTime
{ "endTime", track.CurrentTime }
{ "endTime", track.CurrentTime },
{ "startTime", 0 }
};
// add everything to the beatmap
beatmap.Add("meta", meta);
beatmap.Add("BaseNoteQueue", BaseNoteQueue);
beatmap.Add("noteQueue", NoteQueue);
// save it to disk
StreamWriter mapfile = new StreamWriter(
storage.CreateFileSafely(Path.Combine("maps", "WIP", difficulty + ".json"))
Expand Down
10 changes: 8 additions & 2 deletions CrankItUp.Resources/Beatmaps/Example/easy.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
{
"meta": {
"radius": 50,
"approachRate": 100
"approachRate": 100,
"dataVersion": 1
},
"BaseNoteQueue": [
"noteQueue": [
{
"noteType": "Standard",
"position": 0,
"spawnTime": 0
},
{
"noteType": "Standard",
"position": 5,
"spawnTime": 200
},
{
"noteType": "Standard",
"position": 10,
"spawnTime": 400
},
{
"noteType": "Standard",
"position": 15,
"spawnTime": 600
},
{
"noteType": "Standard",
"position": 20,
"spawnTime": 800
}
Expand Down

0 comments on commit 2e8cf81

Please sign in to comment.