-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTutorial.cs
More file actions
436 lines (390 loc) · 16.3 KB
/
Copy pathTutorial.cs
File metadata and controls
436 lines (390 loc) · 16.3 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
using RhythmBase.Global.Components.Easing;
using RhythmBase.Global.Settings;
using RhythmBase.RhythmDoctor.Components;
using RhythmBase.RhythmDoctor.Events;
using RhythmBase.RhythmDoctor.Extensions;
using RhythmBase.RhythmDoctor.Utils;
using System.Text.Json;
using RhythmBase.RhythmDoctor;
using RhythmBase.Global.Components;
using RhythmBase.Global.Converters;
using RhythmBase.RhythmDoctor.Converters;
using RhythmBase.Global.Components.RichText;
using RhythmBase.Global.Components.Vector;
namespace RhythmBase.Test;
[TestClass]
public sealed class Tutorial
{
static Tutorial()
{
_rdlevel = Level.Default;
}
private static Level _rdlevel;
[TestMethod]
public void CreateAnEmptyLevel()
{
using Level emptyLevel = [];
Console.WriteLine(emptyLevel); // "" Count = 0
}
[TestMethod]
public void CreateADefaultLevel()
{
using Level defaultLevel = Level.Default;
Console.WriteLine(defaultLevel); // "" Count = 3
}
[TestMethod]
public void ReadOrWriteLevel()
{
lock (this)
{
// Directly read a level file
using Level rdlevel1 = Level.FromFile(@"your\level.rdlevel");
// Read a level pack file
using Level rdlevel2 = Level.FromZip(@"your\level.rdzip");
// Read a compressed level pack
using Level rdlevel3 = Level.FromZip(@"your\level.zip");
// Write a level file
rdlevel1.SaveToFile(@"your\outLevel.rdlevel");
}
}
[TestMethod]
public void ReadOrWriteLevelWithSettings()
{
// Create custom read settings
LevelReadSettings settings = new()
{
// Handling of inactive events
InactiveEventsHandling = InactiveEventsHandling.Store,
// Handling of unreadable events
// Common when sprite events are not bound to sprite tracks, etc.
UnreadableEventsHandling = UnreadableEventHandling.Store,
// Unzip all files in a zip level pack into the cache path below
// This is usually the faster option
ZipProcessingMode = ZipProcessingMode.AllEntries,
// Load asset paths when reading or writing the level
LoadAssets = true,
};
// The cache path is required when reading zip level packs
// Default is the Temp path.
GlobalSettings.CachePath = @"your\cache\path";
lock (this)
{ using Level _rdlevel1 = Level.FromFile(@"your\level.rdlevel", settings); }
}
[TestMethod]
public void ConvertLevelToJsonDocument()
{
LevelWriteSettings settings = new();
JsonDocument jdoc = _rdlevel.ToJsonDocument();
string json = _rdlevel.ToJsonString(settings);
Console.WriteLine(jdoc);
Console.WriteLine(json);
}
[TestMethod]
public void FindEventsInLevel()
{
// Find MoveRow events between bar 3 and 5, and in event rows 0 to 2
IEnumerable<MoveRow> list = _rdlevel
.OfEvent<MoveRow>()
.InRange(new(3, 1), new(5, 1))// From Bar 3 to 5
.Where(i => 0 <= i.Y && i.Y < 3); // In event rows 0 to 2
}
[TestMethod]
public void FindEventsInDecoration()
{
_rdlevel.Decorations.Add([]);
// Find the AddClassicBeat event in the row decoration between beat (11,1) and (13,1)
IEnumerable<AddClassicBeat> list = _rdlevel.Decorations[0]
.OfEvent<AddClassicBeat>()
.InRange(
new TickTime(11, 1), // Start searching from bar 11, beat 1
new TickTime(13, 1) // End searching at bar 13, beat 1
);
}
[TestMethod]
public void CreateBeatWithoutBinding()
{
// Create a beat not associated with a level
TickTime beat1 = new(11);
TickTime beat2 = new(2, 3);
TickTime beat3 = (2, 3);
TickTime beat4 = new(TimeSpan.FromSeconds(11.45));
Console.WriteLine(beat1); // [10,?,?]
Console.WriteLine(beat2); // [?,(2, 3),?]
Console.WriteLine(beat3); // [?,(2, 3),?]
Console.WriteLine(beat4); // [?,?,00:00:11.4500000]
(int bar, float beat) = beat2;
}
[TestMethod]
public void CreateBeatWithBinding()
{
// Create a beat associated with a level
TickTime beat1 = _rdlevel.BeatOf(11);
TickTime beat2 = _rdlevel.Calculator.BeatOf(2, 3);
TickTime beat3 = beat1 - 10 + TimeSpan.FromSeconds(11.45);
Console.WriteLine(beat1); // [2,3]
Console.WriteLine(beat2); // [2,3]
Console.WriteLine(beat3); // [3,4.083334]
}
[TestMethod]
public void LinkBeats()
{
TickTime beat1 = _rdlevel.BeatOf(1);
TickTime beat2 = beat1.WithoutLink();
Console.WriteLine(beat1.FromSameLevel(beat2)); // False
Console.WriteLine(beat1.FromSameLevelOrNull(beat2)); // True
TickTime beat3 = beat2.WithLink(_rdlevel.Calculator);
(int bar, float beat) = beat3; // (1, 1)
}
[TestMethod]
public void ConvertTimeUnit()
{
(float, float) barbeat = _rdlevel.Calculator.TimeSpanToBarBeat(TimeSpan.FromSeconds(19.19));
Console.WriteLine(barbeat); // (4, 8.983334)
}
[TestMethod]
public void RangeUsage()
{
IEnumerable<IBaseEvent> result = _rdlevel.InRange(new RhythmDoctor.Components.Range(_rdlevel.DefaultBeat + 10, null));
}
[TestMethod]
public void ExpressionUsage()
{
Expression exp1 = new("i2+1");
Expression exp2 = new(30);
Expression exp3 = new("25.5");
Expression result = exp1 - exp2 * exp3;
Console.WriteLine(result.ExpressionValue); // i2+1-765
}
[TestMethod]
public void AddAndRemoveEvent()
{
Comment comment = new() { TickTime = new(12), Text = "My_comment." };
Console.WriteLine(comment); // [11,?,?] Comment My_comment.
_rdlevel.Add(comment);
Console.WriteLine(comment); // [2,4] Comment My_comment.
_rdlevel.Remove(comment);
Console.WriteLine(comment); // [11,?,?] Comment My_comment.
}
[TestMethod]
public void AddAndRemoveCpbEvent()
{
Level level = [];
Comment temp = new() { TickTime = (5, 3) };
level.Add(temp);
Console.WriteLine(temp.TickTime); // [5,3]
Console.WriteLine(temp.TickTime.Tick); // 35
SetCrotchetsPerBar cpb = new() { TickTime = (3, 1), CrotchetsPerBar = 6 };
level.Add(cpb, BeatChangeStrategy.Default);
Console.WriteLine(temp.TickTime); // [6,1]
Console.WriteLine(temp.TickTime.Tick); // 35 (unchanged due to Default strategy)
level.Remove(cpb, BeatChangeStrategy.Default);
level.Add(cpb, BeatChangeStrategy.RDLE);
Console.WriteLine(temp.TickTime); // [5,3] (unchanged due to RDLE strategy)
Console.WriteLine(temp.TickTime.Tick); // 31
level.Remove(cpb, BeatChangeStrategy.RDLE);
}
[TestMethod]
public void AddForwardEvent()
{
MyEvent myEvent = new();
myEvent.MyProperty = (2, 1);
_rdlevel.Add(myEvent);
myEvent.TickTime = new(8);
Console.WriteLine(myEvent.Type); // ForwardEvent
Console.WriteLine(myEvent.ActualType); // MyEvent
}
[TestMethod]
public void EventTypeUtilsConvert()
{
Console.WriteLine(EventType.Tint.ToType()); // RhythmBase.Events.Tint
Console.WriteLine(EventTypeRegistry.ToEnum(typeof(Tint))); // Tint
Console.WriteLine(EventTypeRegistry.ToEnum<Tint>()); // Tint
Console.WriteLine(string.Join(", ", EventTypeRegistry.ToEnums(typeof(IBarBeginningEvent)))); // PlaySong,SetCrotchetsPerBar, SetHeartExplodeVolume
Console.WriteLine(string.Join(", ", EventTypeRegistry.ToEnums<IBarBeginningEvent>())); // PlaySong,SetCrotchetsPerBar, SetHeartExplodeVolume
}
[TestMethod]
public void EventTypeUtilsStatic()
{
Console.WriteLine(string.Join(",\n", EventTypeRegistry.DecorationTypes));
// Comment,
// CustomDecorationEvent,
// Move,
// PlayAnimation,
// SetVisible,
// Tile,
// Tint
Console.WriteLine(string.Join(",\n", EventTypeRegistry.EventTypeEnumsForCameraFX));
// MoveCamera,
// ShakeScreen,
// FlipScreen,
// PulseCamera
Console.WriteLine(string.Join(",\n", EventTypeRegistry.EventTypeEnumsForUtility));
// Comment,
// TagAction,
// CallCustomMethod
}
[TestMethod]
public void RichTextUsage()
{
RichLine<RichStringStyle> line = RichLine<RichStringStyle>.Deserialize("Hel<color=#00FF00>lo");
Console.WriteLine(line.ToString()); // Hello
Console.WriteLine(line.Serialize()); // Hel<color=lime>lo</color>
line +=
new Phrase<RichStringStyle>(" Rhythm")
{
Style = new()
{
Color = Color.Lime
}
};
line += " Doctor!";
Console.WriteLine(line.ToString()); // Hello Rhythm Doctor!
Console.WriteLine(line.Serialize()); // Hel<color=lime>lo Rhythm</color> Doctor!
}
[TestMethod]
public void RichTextModify()
{
RichLine<RichStringStyle> line = RichLine<RichStringStyle>.Deserialize("Hel<color=#00FF00>lo Rhythm</color> Doctor!");
#if NETCOREAPP
Console.WriteLine(line[6..].ToString()); // Rhythm Doctor!
Console.WriteLine(line[6..].Serialize()); // <color=lime>Rhythm</color> Doctor!
line[5] = " and Welcome to ";
#endif
Console.WriteLine(line.ToString()); // Hello and Welcome to Rhythm Doctor!
Console.WriteLine(line.Serialize()); // Hel<color=lime>lo</color> and Welcome to <color=lime>Rhythm</color> Doctor!
return;
}
[TestMethod]
public void RichTextBuild()
{
DialogueExchange exchange =
[
new DialogueBlock()
{
Character = "Paige",
Expression = "neutral",
Content = RichLine<DialoguePhraseStyle>.Deserialize("Hel<color=#00FF00>lo [2]<shake>Rhythm</color> Doctor</shake>!"),
},
new DialogueBlock()
{
Character = "Ian",
Content = "Hello Paige!",
},
new DialogueBlock()
{
Character = "Paige",
Expression = "happy",
Content = new Phrase<DialoguePhraseStyle>("What a good day!")
{
Events =
[
new DialogueTone(DialogueToneType.VerySlow,6),
new DialogueTone(DialogueToneType.Static,11),
],
Style = new DialoguePhraseStyle()
{
Volume = 0.5f,
Bold = true,
},
}
}
];
Console.WriteLine(exchange.Serialize());
// Paige_neutral:Hel<color=lime>lo [2]<shake>Rhythm</color> Doctor</shake>!
// Ian:Hello Paige!
// Paige_happy:<volume=0.5><bold>What a[vslow] good[static] day!</volume></bold>
}
[TestMethod]
public void EasingCalculate()
{
double var1 = EaseType.InSine.Calculate(0.25);
double var2 = EaseType.Linear.Calculate(0.5, 4, 9);
Console.WriteLine(var1); // 0.07612046748871326
Console.WriteLine(var2); // 6.5
}
//[TestMethod]
//public void RDCode()
//{
// RDLang.Variables.i[1] = 9;
// RDLang.TryRun("numMistakesP2 = 3", out float result); // 3
// Console.WriteLine(result);
// RDLang.TryRun("numMistakesP2+i1", out result); // 12
// Console.WriteLine(result);
// RDLang.TryRun("atLeastRank(A)", out result); // 1
// Console.WriteLine(result);
//}
public void Example_01()
{
// Read the visual effects level file
using Level vfxLevel = Level.FromFile(@"vfx.rdlevel");
// Read the audio level file
using Level audioLevel = Level.FromFile(@"beat.rdlevel");
// Remove all rows from the visual effects level
Row[] vfxrows = [.. vfxLevel.Rows];
foreach (Row row in vfxrows)
vfxLevel.Rows.Remove(row);
// Copy all rows from the audio level into the new level
foreach (Row row in audioLevel.Rows)
{
// Copy row information
Row row2 = new()
{
Room = row.Room,
Character = row.Character,
Sound = row.Sound,
RowType = row.RowType
};
vfxLevel.Rows.Add(row2);
// Copy events within the row
BaseBeat[] evts = [.. row.OfEvent<BaseBeat>()];
foreach (BaseBeat evt in evts)
row2.Add(evt);
}
// Copy necessary sound events
foreach (IBaseEvent sound in audioLevel.Where(e =>
e.Tab == Tab.Sounds && // Event is in the Sounds tab
e is not BaseRowAction && // Sound events contain row events; adding row events here would cause reference errors
e is not PlaySong && // No need to copy PlaySong if the music is the same
e is not SetCrotchetsPerBar)) // The timing of these events is independent of the number of crotchets per bar, so they don't need to be added
{
vfxLevel.Add(sound);
}
// Write to a new level file
vfxLevel.SaveToFile(@"result.rdlevel");
}
// Create a MyEvent type
// Inherit from CustomEvent
public record class MyEvent : ForwardEvent
{
// Override property
public override Tab Tab => Tab.Actions;
// All implemented properties need to be bound to and checked for null in the ForwardEvent.ExtraData field.
// Implement an PointN type property
public PointN? MyProperty
{
get
{
// Get the required content from the Data field and check for null
return ExtraData.TryGetValue("myProperty", out JsonElement jsonElement)
&& jsonElement.GetProperty("x").GetSingle() is float x
&& jsonElement.GetProperty("y").GetSingle() is float y
? (x, y)
: null;
}
set
{
// Save the content in the Data field
ExtraData["myProperty"] =
value.HasValue ?
JsonElement.Parse($$"""{"x": {{value.Value.X}}, "y": {{value.Value.Y}}}""") :
default;
}
}
// Initialize the type in the constructor
public MyEvent()
{
// Initialize the ActualType property.
ActualType = nameof(MyEvent);
}
}
}