Skip to content

Commit c8296e6

Browse files
committed
Story Mode Handling
1 parent a235d48 commit c8296e6

4 files changed

Lines changed: 133 additions & 3 deletions

File tree

src/funkin/Constants.hx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class Constants
5454
*/
5555
public static final DEFAULT_DIFFICULTIES:Array<String> = ['easy', 'normal', 'hard', 'erect', 'nightmare'];
5656

57+
/**
58+
* Default Difficulty
59+
*/
60+
public static final DEFAULT_DIFFICULTY:String = 'normal';
61+
5762
static function get_TECHNOTDRIP_VERSION():String
5863
{
5964
return FlxG.stage.application.meta.get('version');

src/funkin/states/gameplay/PlayState.hx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package funkin.states.gameplay;
22

3+
import flixel.util.FlxSort;
34
import funkin.data.StrumlineData;
45
import funkin.data.song.Song;
56
import funkin.objects.gameplay.strumline.Strumline;
67
import funkin.structures.SongStructure;
8+
import funkin.util.StoryModeHandler;
79

810
class PlayState extends FunkinState
911
{
@@ -13,6 +15,11 @@ class PlayState extends FunkinState
1315
*/
1416
public static var instance:PlayState;
1517

18+
/**
19+
* Story Mode Handler.
20+
*/
21+
public static var storyMode:StoryModeHandler;
22+
1623
/**
1724
* The parameters used when initializing this state.
1825
*/
@@ -152,6 +159,13 @@ class PlayState extends FunkinState
152159
getPlayerSound()?.play();
153160
getOpponentSound()?.play();
154161

162+
var soundsAvailable:Array<FlxSound> = [FlxG.sound.music, getPlayerSound(), getOpponentSound()];
163+
soundsAvailable.sort((a:FlxSound, b:FlxSound) ->
164+
{
165+
return FlxSort.byValues(FlxSort.DESCENDING, a?.length ?? 0, b?.length ?? 0);
166+
});
167+
soundsAvailable[0].onComplete = finishSong;
168+
155169
conductor.setupBPMChanges(metadata.bpmChanges);
156170
}
157171

@@ -165,10 +179,27 @@ class PlayState extends FunkinState
165179
super.update(elapsed);
166180

167181
if (controls.justPressed.BACK)
182+
{
183+
finishSong();
184+
}
185+
}
186+
187+
/**
188+
* Finish the song.
189+
*/
190+
public function finishSong():Void
191+
{
192+
if (storyMode != null)
193+
{
194+
FlxG.switchState(storyMode.nextState);
195+
}
196+
else
168197
{
169198
conductor.changeBPM(102);
199+
FlxG.sound.music.onComplete = null;
170200
FlxG.sound.playMusic(Paths.content.audio('ui/menu/freakyMenu'));
171-
FlxG.switchState(funkin.states.ui.MenuState.new);
201+
202+
FlxG.switchState(funkin.states.ui.FreeplayState.new);
172203
}
173204
}
174205

src/funkin/states/ui/StoryModeState.hx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package funkin.states.ui;
22

33
import funkin.objects.ui.WeekItem;
4+
import funkin.states.gameplay.PlayState;
5+
import funkin.util.StoryModeHandler;
46
import funkin.util.Week;
57

68
class StoryModeState extends FunkinState
@@ -40,6 +42,8 @@ class StoryModeState extends FunkinState
4042
DiscordRPC.details = 'Story Mode Menu';
4143
#end
4244

45+
PlayState.storyMode = null;
46+
4347
grpWeekItems = new FlxTypedGroup<WeekItem>();
4448
add(grpWeekItems);
4549

@@ -142,10 +146,11 @@ class StoryModeState extends FunkinState
142146

143147
grpWeekItems.members[curSelected].startFlashing();
144148

149+
PlayState.storyMode = new StoryModeHandler(loadedWeeks[curSelected], _difficulties[curDifficulty]);
150+
145151
new FlxTimer().start(1, (tmr:FlxTimer) ->
146152
{
147-
// TODO: Change this to PlayState
148-
FlxG.switchState(MenuState.new);
153+
FlxG.switchState(PlayState.storyMode.nextState);
149154
});
150155
}
151156

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package funkin.util;
2+
3+
import flixel.FlxState;
4+
import funkin.data.song.Song;
5+
import funkin.states.gameplay.PlayState;
6+
import funkin.states.ui.StoryModeState;
7+
8+
/**
9+
* Story Mode Handler for `PlayState`.
10+
*/
11+
class StoryModeHandler
12+
{
13+
/**
14+
* The current week for this Story Mode session.
15+
*/
16+
public var week:Week;
17+
18+
/**
19+
* The song queue for this Story Mode session.
20+
* Based off of `week.songs`.
21+
*/
22+
public var songQueue:Array<Song> = [];
23+
24+
/**
25+
* The current difficulty for this Story Mode session.
26+
* If the current difficulty doesn't exist or is `null`, `Constants.DEFAULT_DIFFICULTY` is used.
27+
* If `Constants.DEFAULT_DIFFICULTY` isn't available, songs will be skipped until a song with this difficulty is available.
28+
*/
29+
public var difficulty:Null<String> = null;
30+
31+
/**
32+
* Creates a Story Mode Handler.
33+
* @param week The week to use for this Story Mode session.
34+
* @param difficulty The difficulty to use for this Story Mode Session. See `difficulty` for more information.
35+
*/
36+
public function new(week:Week, ?difficulty:Null<String> = null)
37+
{
38+
this.week = week;
39+
this.difficulty = difficulty;
40+
songQueue = week.songs;
41+
}
42+
43+
/**
44+
* Generates a `PlayState` based on the next song in queue.
45+
* To be used as the parameter for `FlxG.switchState`
46+
* @return `FlxState`
47+
*/
48+
public function nextState():FlxState
49+
{
50+
var foundSong:Song = null;
51+
var difficultyToUse:String = difficulty;
52+
53+
while (foundSong == null)
54+
{
55+
foundSong = songQueue.shift();
56+
if (foundSong == null)
57+
break;
58+
59+
var difficulties:Array<String> = foundSong.getDifficulties();
60+
61+
if (difficulties.contains(difficulty))
62+
{
63+
difficultyToUse = difficulty;
64+
break;
65+
}
66+
67+
if (difficulties.contains(Constants.DEFAULT_DIFFICULTY))
68+
{
69+
difficultyToUse = Constants.DEFAULT_DIFFICULTY;
70+
break;
71+
}
72+
73+
foundSong = null;
74+
}
75+
76+
if (foundSong == null)
77+
{
78+
FlxG.sound.music.onComplete = null;
79+
FlxG.sound.playMusic(Paths.content.audio('ui/menu/freakyMenu'));
80+
return new StoryModeState();
81+
}
82+
83+
return new PlayState({
84+
variation: 'default',
85+
song: foundSong,
86+
difficulty: difficultyToUse
87+
});
88+
}
89+
}

0 commit comments

Comments
 (0)