2023-05-16 22:09:53 -04:00
|
|
|
package funkin.ui.story;
|
|
|
|
|
|
|
|
import flixel.FlxSprite;
|
2023-05-17 16:42:58 -04:00
|
|
|
import flixel.util.FlxColor;
|
|
|
|
import funkin.play.song.Song;
|
2023-05-16 22:09:53 -04:00
|
|
|
import funkin.data.IRegistryEntry;
|
2023-09-08 17:46:44 -04:00
|
|
|
import funkin.data.song.SongRegistry;
|
2023-05-16 22:09:53 -04:00
|
|
|
import funkin.data.level.LevelRegistry;
|
|
|
|
import funkin.data.level.LevelData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object used to retrieve data about a story mode level (also known as "weeks").
|
|
|
|
* Can be scripted to override each function, for custom behavior.
|
|
|
|
*/
|
|
|
|
class Level implements IRegistryEntry<LevelData>
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The ID of the story mode level.
|
|
|
|
*/
|
|
|
|
public final id:String;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Level data as parsed from the JSON file.
|
|
|
|
*/
|
|
|
|
public final _data:LevelData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param id The ID of the JSON file to parse.
|
|
|
|
*/
|
|
|
|
public function new(id:String)
|
|
|
|
{
|
|
|
|
this.id = id;
|
|
|
|
_data = _fetchData(id);
|
|
|
|
|
|
|
|
if (_data == null)
|
|
|
|
{
|
|
|
|
throw 'Could not parse level data for id: $id';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of songs in this level, as an array of IDs.
|
|
|
|
* @return Array<String>
|
|
|
|
*/
|
|
|
|
public function getSongs():Array<String>
|
|
|
|
{
|
2023-05-30 16:21:12 -04:00
|
|
|
// Copy the array so that it can't be modified on accident
|
|
|
|
return _data.songs.copy();
|
2023-05-16 22:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the title of the level for display on the menu.
|
|
|
|
*/
|
|
|
|
public function getTitle():String
|
|
|
|
{
|
|
|
|
// TODO: Maybe add localization support?
|
|
|
|
return _data.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildTitleGraphic():FlxSprite
|
|
|
|
{
|
|
|
|
var result = new FlxSprite().loadGraphic(Paths.image(_data.titleAsset));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of songs in this level, as an array of names, for display on the menu.
|
|
|
|
* @return Array<String>
|
|
|
|
*/
|
|
|
|
public function getSongDisplayNames(difficulty:String):Array<String>
|
|
|
|
{
|
2023-05-17 16:42:58 -04:00
|
|
|
var songList:Array<String> = getSongs() ?? [];
|
2023-09-08 17:46:44 -04:00
|
|
|
var songNameList:Array<String> = songList.map(function(songId:String) {
|
|
|
|
return getSongDisplayName(songId, difficulty);
|
2023-05-16 22:09:53 -04:00
|
|
|
});
|
2023-05-17 16:42:58 -04:00
|
|
|
return songNameList;
|
2023-05-16 22:09:53 -04:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:46:44 -04:00
|
|
|
static function getSongDisplayName(songId:String, difficulty:String):String
|
|
|
|
{
|
|
|
|
var song:Null<Song> = SongRegistry.instance.fetchEntry(songId);
|
|
|
|
if (song == null) return 'Unknown';
|
|
|
|
|
|
|
|
return song.songName;
|
|
|
|
}
|
|
|
|
|
2023-05-16 22:09:53 -04:00
|
|
|
/**
|
|
|
|
* Whether this level is unlocked. If not, it will be greyed out on the menu and have a lock icon.
|
|
|
|
* TODO: Change this behavior in a later release.
|
|
|
|
*/
|
|
|
|
public function isUnlocked():Bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this level is visible. If not, it will not be shown on the menu at all.
|
|
|
|
*/
|
|
|
|
public function isVisible():Bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-27 15:21:09 -04:00
|
|
|
/**
|
|
|
|
* Build a sprite for the background of the level.
|
|
|
|
* Can be overriden by ScriptedLevel. Not used if `isBackgroundSimple` returns true.
|
|
|
|
*/
|
2023-05-16 22:09:53 -04:00
|
|
|
public function buildBackground():FlxSprite
|
|
|
|
{
|
2023-06-27 15:21:09 -04:00
|
|
|
if (!_data.background.startsWith('#'))
|
2023-05-16 22:09:53 -04:00
|
|
|
{
|
|
|
|
// Image specified
|
|
|
|
return new FlxSprite().loadGraphic(Paths.image(_data.background));
|
|
|
|
}
|
2023-06-27 15:21:09 -04:00
|
|
|
|
|
|
|
// Color specified
|
|
|
|
var result:FlxSprite = new FlxSprite().makeGraphic(FlxG.width, 400, FlxColor.WHITE);
|
|
|
|
result.color = getBackgroundColor();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the background is a solid color.
|
|
|
|
* If you have a ScriptedLevel with a fancy background, you may want to override this to false.
|
|
|
|
*/
|
|
|
|
public function isBackgroundSimple():Bool
|
|
|
|
{
|
|
|
|
return _data.background.startsWith('#');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the background is a solid color.
|
|
|
|
* If you have a ScriptedLevel with a fancy background, you may want to override this to false.
|
|
|
|
*/
|
|
|
|
public function getBackgroundColor():FlxColor
|
|
|
|
{
|
|
|
|
return FlxColor.fromString(_data.background);
|
2023-05-16 22:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDifficulties():Array<String>
|
|
|
|
{
|
|
|
|
var difficulties:Array<String> = [];
|
|
|
|
|
|
|
|
var songList = getSongs();
|
|
|
|
|
|
|
|
var firstSongId:String = songList[0];
|
2023-09-08 17:46:44 -04:00
|
|
|
var firstSong:Song = SongRegistry.instance.fetchEntry(firstSongId);
|
2023-05-16 22:09:53 -04:00
|
|
|
|
2023-05-22 20:55:53 -04:00
|
|
|
if (firstSong != null)
|
2023-05-16 22:09:53 -04:00
|
|
|
{
|
2023-05-22 20:55:53 -04:00
|
|
|
for (difficulty in firstSong.listDifficulties())
|
|
|
|
{
|
|
|
|
difficulties.push(difficulty);
|
|
|
|
}
|
2023-05-16 22:09:53 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:59 -04:00
|
|
|
// sort the difficulties, since they may be out of order in the chart JSON
|
2023-10-17 00:07:36 -04:00
|
|
|
// also copy/pasted to Song.listDifficulties()!
|
2023-10-16 23:43:59 -04:00
|
|
|
var diffMap:Map<String, Int> = new Map<String, Int>();
|
|
|
|
for (difficulty in difficulties)
|
|
|
|
{
|
|
|
|
var num:Int = 0;
|
|
|
|
switch (difficulty)
|
|
|
|
{
|
|
|
|
case 'easy':
|
|
|
|
num = 0;
|
|
|
|
case 'normal':
|
|
|
|
num = 1;
|
|
|
|
case 'hard':
|
|
|
|
num = 2;
|
|
|
|
case 'erect':
|
|
|
|
num = 3;
|
|
|
|
case 'nightmare':
|
|
|
|
num = 4;
|
|
|
|
}
|
|
|
|
diffMap.set(difficulty, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
difficulties.sort(function(a:String, b:String) {
|
|
|
|
return diffMap.get(a) - diffMap.get(b);
|
|
|
|
});
|
|
|
|
|
2023-05-16 22:09:53 -04:00
|
|
|
// Filter to only include difficulties that are present in all songs
|
2023-05-17 16:42:58 -04:00
|
|
|
for (songIndex in 1...songList.length)
|
2023-05-16 22:09:53 -04:00
|
|
|
{
|
2023-05-17 16:42:58 -04:00
|
|
|
var songId:String = songList[songIndex];
|
2023-09-08 17:46:44 -04:00
|
|
|
var song:Song = SongRegistry.instance.fetchEntry(songId);
|
2023-05-16 22:09:53 -04:00
|
|
|
|
2023-05-22 13:47:01 -04:00
|
|
|
if (song == null) continue;
|
|
|
|
|
2023-05-16 22:09:53 -04:00
|
|
|
for (difficulty in difficulties)
|
|
|
|
{
|
|
|
|
if (!song.hasDifficulty(difficulty))
|
|
|
|
{
|
|
|
|
difficulties.remove(difficulty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-22 20:55:53 -04:00
|
|
|
if (difficulties.length == 0) difficulties = ['normal'];
|
|
|
|
|
2023-05-16 22:09:53 -04:00
|
|
|
return difficulties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildProps():Array<LevelProp>
|
|
|
|
{
|
|
|
|
var props:Array<LevelProp> = [];
|
|
|
|
|
|
|
|
if (_data.props.length == 0) return props;
|
|
|
|
|
|
|
|
for (propIndex in 0..._data.props.length)
|
|
|
|
{
|
|
|
|
var propData = _data.props[propIndex];
|
2023-06-10 02:56:03 -04:00
|
|
|
|
|
|
|
var propSprite:Null<LevelProp> = LevelProp.build(propData);
|
|
|
|
if (propSprite == null) continue;
|
|
|
|
|
2023-05-16 22:09:53 -04:00
|
|
|
propSprite.x += FlxG.width * 0.25 * propIndex;
|
2023-05-17 16:42:58 -04:00
|
|
|
props.push(propSprite);
|
2023-05-16 22:09:53 -04:00
|
|
|
}
|
2023-05-17 16:42:58 -04:00
|
|
|
|
|
|
|
return props;
|
2023-05-16 22:09:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy():Void {}
|
|
|
|
|
|
|
|
public function toString():String
|
|
|
|
{
|
|
|
|
return 'Level($id)';
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:46:44 -04:00
|
|
|
static function _fetchData(id:String):Null<LevelData>
|
2023-05-16 22:09:53 -04:00
|
|
|
{
|
2023-08-22 04:27:30 -04:00
|
|
|
return LevelRegistry.instance.parseEntryDataWithMigration(id, LevelRegistry.instance.fetchEntryVersion(id));
|
2023-05-16 22:09:53 -04:00
|
|
|
}
|
|
|
|
}
|