-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathStrumLine.hx
More file actions
481 lines (409 loc) · 16 KB
/
StrumLine.hx
File metadata and controls
481 lines (409 loc) · 16 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package funkin.game;
import flixel.math.FlxPoint;
import flixel.sound.FlxSound;
import flixel.tweens.FlxTween;
import flixel.util.FlxSignal.FlxTypedSignal;
import funkin.backend.chart.ChartData;
import funkin.backend.scripting.events.note.*;
import funkin.backend.system.Conductor;
import funkin.backend.system.Controls;
/**
* Group of strums, that contains the strums and notes.
* Used in PlayState.
**/
class StrumLine extends FlxTypedGroup<Strum> {
/**
* Signal that triggers whenever a note is hit. Similar to onPlayerHit and onDadHit, except strumline specific.
* To add a listener, do
* `strumLine.onHit.add(function(e:NoteHitEvent) {});`
*/
public var onHit:FlxTypedSignal<NoteHitEvent->Void> = new FlxTypedSignal<NoteHitEvent->Void>();
/**
* Signal that triggers whenever a note is missed. Similar to onPlayerMiss, except strumline specific.
* To add a listener, do
* `strumLine.onMiss.add(function(e:NoteMissEvent) {});`
*/
public var onMiss:FlxTypedSignal<NoteMissEvent->Void> = new FlxTypedSignal<NoteMissEvent->Void>();
/**
* Signal that triggers whenever a note is being updated. Similar to onNoteUpdate, except strumline specific.
* To add a listener, do
* `strumLine.onNoteUpdate.add(function(e:NoteUpdateEvent) {});`
*/
public var onNoteUpdate:FlxTypedSignal<NoteUpdateEvent->Void> = new FlxTypedSignal<NoteUpdateEvent->Void>();
/**
* Signal that triggers whenever a note is being deleted. Similar to onNoteDelete, except strumline specific.
* To add a listener, do
* `strumLine.onNoteDelete.add(function(e:SimpleNoteEvent) {});`
*/
public var onNoteDelete:FlxTypedSignal<SimpleNoteEvent->Void> = new FlxTypedSignal<SimpleNoteEvent->Void>();
/**
* Array containing all of the characters "attached" to those strums.
*/
public var characters:Array<Character>;
/**
* Whenever this strumline is controlled by cpu or not.
*/
public var cpu(default, set):Bool = false;
/**
* Whenever this strumline is from the opponent side or the player side.
*/
public var opponentSide:Bool = false;
/**
* Controls assigned to this strumline.
*/
public var controls:Controls = null;
/**
* Chart JSON data assigned to this StrumLine (Codename format)
*/
public var data:ChartStrumLine = null;
/**
* Whenever Ghost Tapping is enabled.
*/
@:isVar public var ghostTapping(get, set):Null<Bool> = null;
/**
* Group of all of the notes in this strumline. Using `forEach` on this group will only loop through the first notes for performance reasons.
*/
public var notes:NoteGroup;
/**
* Whenever alt animation is enabled on this strumline.
*/
public var altAnim(get, set):Bool;
/**
* Which animation suffix on characters that should be used when hitting notes.
*/
public var animSuffix(default, set):String = "";
/**
* The current animation suffix the strumline should use. Note that setting this will only take effect upon the alt. animation being reset.
*/
public var defaultAnimSuffix:String = Flags.DEFAULT_ALT_ANIM_SUFFIX;
/**
* TODO: Write documentation about this being a variable that can help when making multi key
*/
public var strumAnimPrefix = ["left", "down", "up", "right"];
/**
* Vocals sound (Vocals.ogg). Used for individual vocals per strumline.
*/
public var vocals:FlxSound;
/**
* Extra data that can be added to the strum line.
**/
public var extra:Map<String, Dynamic> = [];
private function get_ghostTapping() {
if (this.ghostTapping != null) return this.ghostTapping;
if (PlayState.instance != null) return PlayState.instance.ghostTapping;
return false;
}
private inline function set_ghostTapping(b:Bool):Bool
return this.ghostTapping = b;
private var startingPos:FlxPoint = FlxPoint.get(0,0);
/**
* The scale of the strums.
* If called after generate, the strums wont be scaled.
* You can only change it in the Charter.
**/
public var strumScale:Float = 1;
public function new(characters:Array<Character>, startingPos:FlxPoint, strumScale:Float, cpu:Bool = false, opponentSide:Bool = true, ?controls:Controls, ?vocalPrefix:String = "") {
super();
this.characters = characters;
this.startingPos = startingPos;
this.strumScale = strumScale;
this.cpu = cpu;
this.opponentSide = opponentSide;
this.controls = controls;
this.notes = new NoteGroup();
var v = Paths.voices(PlayState.SONG.meta.name, PlayState.difficulty, vocalPrefix);
vocals = vocalPrefix != "" ? FlxG.sound.load(Options.streamedVocals ? Assets.getMusic(v) : v) : new FlxSound();
vocals.persist = false;
}
/**
* Generates the notes for the strumline.
**/
public function generate(strumLine:ChartStrumLine, ?startTime:Float) {
// TODO: implement double generate call support if needed
var total = 0;
if (strumLine.notes != null) for(note in strumLine.notes) {
if (startTime != null && startTime > note.time)
continue;
total++;
if (note.sLen > Conductor.stepCrochet * 0.75) {
var len:Float = note.sLen;
while(len > 10) {
total++;
len -= Math.min(len, Conductor.stepCrochet);
}
}
}
notes.preallocate(total);
var il = 0;
var prev:Note = null;
if (strumLine.notes != null) for(note in strumLine.notes) {
if (startTime != null && startTime > note.time)
continue;
notes.members[total-(il++)-1] = prev = new Note(this, note, false, prev);
if (note.sLen > Conductor.stepCrochet * 0.75) {
var len:Float = note.sLen;
var curLen:Float = 0;
while(len > 10) {
curLen = Math.min(len, Conductor.stepCrochet);
notes.members[total-(il++)-1] = prev = new Note(this, note, true, curLen, note.sLen - len, prev);
len -= curLen;
if (prev != null && prev.sustainParent != null)
prev.sustainParent.tailCount++;
}
}
}
notes.sortNotes();
var scrollSpeed = strumLine.scrollSpeed;
if(scrollSpeed == null) if (PlayState.instance != null) scrollSpeed = PlayState.instance.scrollSpeed;
if(scrollSpeed == null) scrollSpeed = 1;
// TODO: Make this work by accounting zoom and scroll speed changes - Nex
/*var limit = FlxG.height / 0.45;
notes.limit = limit / scrollSpeed;
OR
notes.limit = Flags.DEFAULT_NOTE_MS_LIMIT / scrollSpeed;*/
}
public override function update(elapsed:Float) {
super.update(elapsed);
notes.update(elapsed);
}
public override function draw() {
super.draw();
notes.cameras = cameras;
notes.draw();
}
/**
* Updates the notes.
**/
public inline function updateNotes() {
__updateNote_songPos = Conductor.songPosition;
if(__updateNote_event == null) __updateNote_event = PlayState.instance.__updateNote_event;
notes.forEach(updateNote);
}
var __updateNote_strum:Strum;
var __updateNote_songPos:Float;
var __updateNote_event:NoteUpdateEvent;
/**
* Updates a note.
* This updates the position, state, and handles the input.
**/
public function updateNote(daNote:Note) {
__updateNote_strum = members[daNote.noteData];
if (__updateNote_strum == null) return;
__updateNote_event.recycle(daNote, FlxG.elapsed, __updateNote_strum);
onNoteUpdate.dispatch(__updateNote_event);
if (__updateNote_event.cancelled) return;
if (__updateNote_event.__updateHitWindow) {
var hitWindow = Flags.USE_LEGACY_TIMING ? PlayState.instance.hitWindow : PlayState.instance.ratingManager.lastHitWindow;
daNote.canBeHit = (daNote.strumTime > __updateNote_songPos - (hitWindow * daNote.latePressWindow)
&& daNote.strumTime < __updateNote_songPos + (hitWindow * daNote.earlyPressWindow));
if (daNote.strumTime < __updateNote_songPos - hitWindow && !daNote.wasGoodHit)
daNote.tooLate = true;
}
if (cpu && __updateNote_event.__autoCPUHit && !daNote.avoid && !daNote.wasGoodHit && daNote.strumTime < __updateNote_songPos)
PlayState.instance.goodNoteHit(this, daNote);
if (daNote.wasGoodHit && daNote.isSustainNote && daNote.strumTime + daNote.sustainLength < __updateNote_songPos && !daNote.noSustainClip) {
deleteNote(daNote);
return;
}
if (daNote.tooLate) {
if (!cpu) PlayState.instance.noteMiss(this, daNote);
else deleteNote(daNote);
return;
}
if (__updateNote_event.strum == null) return;
if (__updateNote_event.__reposNote) __updateNote_event.strum.updateNotePosition(daNote);
if (daNote.isSustainNote) {
daNote.updateSustain(__updateNote_event.strum);
}
}
var __pressed:Array<Bool> = [];
var __justPressed:Array<Bool> = [];
var __justReleased:Array<Bool> = [];
var __notePerStrum:Array<Note> = [];
function __inputProcessPressed(note:Note) {
if (__pressed[note.strumID] && note.isSustainNote && note.strumTime < __updateNote_songPos && !note.wasGoodHit && note.sustainParent.wasGoodHit) {
PlayState.instance.goodNoteHit(this, note);
}
}
function __inputProcessJustPressed(note:Note) {
var strumID = note.strumID;
if (!__justPressed[strumID] || note.isSustainNote || note.wasGoodHit || !note.canBeHit) return;
var cur = __notePerStrum[strumID];
if (cur == null) {
__notePerStrum[strumID] = note;
return;
}
var songPos = __updateNote_songPos;
var noteDist = Math.abs(note.strumTime - songPos);
var noteShouldAvoid = note.avoid;
var curShouldAvoid = cur.avoid;
if (!noteShouldAvoid && curShouldAvoid) {
__notePerStrum[strumID] = note;
return;
}
var curDist = Math.abs(cur.strumTime - songPos);
if (noteShouldAvoid == curShouldAvoid && noteDist < curDist)
__notePerStrum[strumID] = note;
}
/**
* Updates the input for the strumline, and handles the input.
* @param id The ID of the strum
**/
public function updateInput(id:Int = 0) {
updateNotes();
if (cpu) return;
final membersLength = members.length;
if (__pressed.length != membersLength) {
__pressed.resize(membersLength);
__justPressed.resize(membersLength);
__justReleased.resize(membersLength);
}
if (__notePerStrum.length != membersLength)
__notePerStrum = cast new haxe.ds.Vector(membersLength); // [for(_ in 0...members.length) null];
for (i in 0...membersLength) {
__pressed[i] = members[i].__getPressed(this);
__justPressed[i] = members[i].__getJustPressed(this);
__justReleased[i] = members[i].__getJustReleased(this);
}
var event = EventManager.get(InputSystemEvent).recycle(__pressed, __justPressed, __justReleased, this, id);
if (PlayState.instance != null) PlayState.instance.gameAndCharsEvent("onInputUpdate", event);
if (event.cancelled) return;
__pressed = CoolUtil.getDefault(event.pressed, []);
__justPressed = CoolUtil.getDefault(event.justPressed, []);
__justReleased = CoolUtil.getDefault(event.justReleased, []);
if (__pressed.contains(true)) {
if (__justPressed.contains(true)) {
notes.forEachAlive(__inputProcessJustPressed);
for (k => pr in __justPressed)
{
var note = __notePerStrum[k];
if (note != null) {
PlayState.instance.goodNoteHit(this, note);
__notePerStrum[k] = null;
} else if (pr && !ghostTapping)
PlayState.instance.noteMiss(this, null, k, ID);
}
}
for (c in characters)
if (c.lastAnimContext != DANCE)
c.__lockAnimThisFrame = true;
notes.forEachAlive(__inputProcessPressed);
}
for (i => str in members)
str.updatePlayerInput(__pressed[i], __justPressed[i], __justReleased[i]);
PlayState.instance.gameAndCharsCall("onPostInputUpdate");
}
/**
* Adds/removes health to/from the strumline.
* If the strumline is an opponent strumline, it will subtract the health, otherwise it will add the health.
* @param health The amount of health to add/remove
**/
public inline function addHealth(health:Float)
PlayState.instance.health += health * (opponentSide ? -1 : 1);
/**
* Generates strums, and adds them to the strumline.
* @param amount The amount of strums to generate (-1 for the default amount)
**/
public inline function generateStrums(amount:Int = -1) {
if(amount == -1) amount = Flags.DEFAULT_STRUM_AMOUNT;
for (i in 0...amount)
add(createStrum(i));
}
override function destroy() {
super.destroy();
if(startingPos != null)
startingPos.put();
notes = FlxDestroyUtil.destroy(notes);
}
/**
* Creates a strum and returns the created strum (needs to be added manually).
* @param i Index of the strum
* @param animPrefix (Optional) Animation prefix (`left` = `arrowLEFT`, `left press`, `left confirm`).
* @param spritePath (Optional) The sprite's graphic path if you don't want the default one.
* @param playIntroAnimation (Optional) Whenever the intro animation should be played, by default might be `true` under certain conditions.
*/
public function createStrum(i:Int, ?animPrefix:String, ?spritePath:String, ?playIntroAnimation:Bool) {
if (animPrefix == null)
animPrefix = strumAnimPrefix[i % strumAnimPrefix.length];
var babyArrow:Strum = new Strum(startingPos.x + (Note.swagWidth * strumScale * (data.strumSpacing != null ? data.strumSpacing : 1) * i), startingPos.y + (Note.swagWidth*0.5) - (Note.swagWidth * strumScale * 0.5));
babyArrow.ID = i;
babyArrow.strumLine = this;
if(data.scrollSpeed != null)
babyArrow.scrollSpeed = data.scrollSpeed;
var event = EventManager.get(StrumCreationEvent).recycle(babyArrow, PlayState.instance.strumLines.members.indexOf(this), i, animPrefix);
event.__doAnimation = playIntroAnimation == null ? (!MusicBeatState.skipTransIn && (PlayState.instance != null ? PlayState.instance.introLength > 0 : true)) : playIntroAnimation;
if (spritePath != null) event.sprite = spritePath;
if (PlayState.instance != null) event = PlayState.instance.gameAndCharsEvent("onStrumCreation", event);
if (!event.cancelled) {
babyArrow.frames = Paths.getFrames(event.sprite);
babyArrow.animation.addByPrefix('green', 'arrowUP');
babyArrow.animation.addByPrefix('blue', 'arrowDOWN');
babyArrow.animation.addByPrefix('purple', 'arrowLEFT');
babyArrow.animation.addByPrefix('red', 'arrowRIGHT');
babyArrow.antialiasing = true;
babyArrow.setGraphicSize(Std.int((babyArrow.width * Flags.DEFAULT_NOTE_SCALE) * strumScale));
babyArrow.animation.addByPrefix('static', 'arrow${event.animPrefix.toUpperCase()}');
babyArrow.animation.addByPrefix('pressed', '${event.animPrefix} press', 24, false);
babyArrow.animation.addByPrefix('confirm', '${event.animPrefix} confirm', 24, false);
}
babyArrow.cpu = cpu;
babyArrow.updateHitbox();
babyArrow.scrollFactor.set();
if (event.__doAnimation)
{
babyArrow.y -= 10;
babyArrow.alpha = 0;
FlxTween.tween(babyArrow, {y: babyArrow.y + 10, alpha: 1}, 1, {ease: FlxEase.circOut, startDelay: 0.5 + (0.2 * i * (4 / (data.keyCount != null ? data.keyCount : 4)))});
}
babyArrow.playAnim('static');
insert(i, babyArrow);
if (PlayState.instance != null) PlayState.instance.gameAndCharsEvent("onPostStrumCreation", event);
return babyArrow;
}
/**
* Deletes a note from this strumline.
* @param note Note to delete
*/
public function deleteNote(note:Note) {
if (note == null) return;
var event:SimpleNoteEvent = EventManager.get(SimpleNoteEvent).recycle(note);
onNoteDelete.dispatch(event);
if (!event.cancelled) {
if (note.isSustainNote && note.sustainParent != null && note.sustainParent.tailCount > 0)
note.sustainParent.tailCount--;
note.kill();
notes.remove(note, true);
note.destroy();
}
}
public static inline function calculateStartingXPos(hudXRatio:Float, scale:Float, spacing:Float, keyCount:Int) {
return (FlxG.width * hudXRatio) - ((Note.swagWidth * scale * ((keyCount/2)-0.5) * spacing) + Note.swagWidth * 0.5 * scale);
}
public static inline function calculateStartingXPosFromInitialWidth(hudXRatio:Float, scale:Float, spacing:Float, keyCount:Int) {
return (FlxG.initialWidth * hudXRatio) - ((Note.swagWidth * scale * ((keyCount/2)-0.5) * spacing) + Note.swagWidth * 0.5 * scale);
}
/**
* SETTERS & GETTERS
*/
#if REGION
private inline function set_cpu(b:Bool):Bool {
for(s in members)
if (s != null)
s.cpu = b;
return cpu = b;
}
private inline function set_animSuffix(str:String):String {
for(s in members)
if (s != null)
s.animSuffix = str;
return animSuffix = str;
}
private inline function set_altAnim(b:Bool):Bool {
animSuffix = b ? defaultAnimSuffix : "";
return b;
}
private inline function get_altAnim():Bool {
return animSuffix == defaultAnimSuffix;
}
#end
}