Skip to content

Commit 259642b

Browse files
authored
feat: (Day) Add FadeIn/FadeOut event commands (AscensionGameDev#2092)
* feat: (Day) Add FadeIn/FadeOut event commands * feat: (AVild) Code review; add fade speed/type/wait options in editor * chore: (Day) Code review * chore: (Day) Code review * chore: (Day) Code review
1 parent 9fc2eac commit 259642b

File tree

26 files changed

+973
-385
lines changed

26 files changed

+973
-385
lines changed

Intersect (Core)/Configuration/ClientConfiguration.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public static ClientConfiguration LoadAndSave(string? filePath = default)
5353

5454
public const int DEFAULT_CHAT_LINES = 100;
5555

56+
public const float DEFAULT_FADE_DURATION_MS = 3000f;
57+
5658
public const DisplayMode DEFAULT_MENU_BACKGROUND_DISPLAY_MODE = DisplayMode.Default;
5759

5860
public const long DEFAULT_MENU_BACKGROUND_FRAME_INTERVAL = 50;
@@ -206,6 +208,11 @@ public void Validate()
206208
/// </summary>
207209
public int MusicFadeTimer { get; set; } = 1500;
208210

211+
/// <summary>
212+
/// The default duration of fades. Controls duration of non-event fades like on login/logout
213+
/// </summary>
214+
public float FadeDurationMs { get; set; } = DEFAULT_FADE_DURATION_MS;
215+
209216
/// <summary>
210217
/// Configures whether or not the context menus are enabled upon right-clicking certain elements.
211218
/// </summary>

Intersect (Core)/GameObjects/Events/Commands/EventCommands.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,4 +1010,15 @@ public partial class CastSpellOn : EventCommand
10101010

10111011
public bool GuildMembers { get; set; }
10121012
}
1013+
1014+
public partial class ScreenFadeCommand : EventCommand
1015+
{
1016+
public override EventCommandType Type { get; } = EventCommandType.Fade;
1017+
1018+
public FadeType FadeType { get; set; }
1019+
1020+
public bool WaitForCompletion { get; set; }
1021+
1022+
public int DurationMs { get; set; }
1023+
}
10131024
}

Intersect (Core)/GameObjects/Events/Enums.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,14 @@ public enum EventCommandType
236236
ResetStatPointAllocations,
237237

238238
CastSpellOn,
239+
240+
Fade,
241+
}
242+
243+
public enum FadeType
244+
{
245+
None = 0,
246+
FadeIn,
247+
FadeOut,
239248
}
240249
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using MessagePack;
7+
8+
namespace Intersect.Network.Packets.Client;
9+
10+
[MessagePackObject]
11+
public class FadeCompletePacket : IntersectPacket
12+
{
13+
public FadeCompletePacket()
14+
{
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Intersect.GameObjects.Events;
2+
using MessagePack;
3+
4+
namespace Intersect.Network.Packets.Server;
5+
6+
[MessagePackObject]
7+
public class FadePacket : IntersectPacket
8+
{
9+
10+
public FadePacket()
11+
{
12+
}
13+
14+
public FadePacket(FadeType fadeType, bool waitForCompletion, int durationMs)
15+
{
16+
FadeType = fadeType;
17+
WaitForCompletion = waitForCompletion;
18+
DurationMs = durationMs;
19+
}
20+
21+
[Key(0)]
22+
public FadeType FadeType { get; set; }
23+
24+
[Key(1)]
25+
public bool WaitForCompletion { get; set; }
26+
27+
[Key(2)]
28+
public int DurationMs { get; set; }
29+
}

Intersect.Client/Core/Fade.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Intersect.Utilities;
1+
using Intersect.Client.Networking;
2+
using Intersect.Configuration;
3+
using Intersect.Utilities;
24

35
namespace Intersect.Client.Core
46
{
@@ -21,64 +23,80 @@ public enum FadeType
2123

2224
private static float sFadeAmt;
2325

24-
private static float sFadeRate = 3000f;
26+
private static float sFadeDurationMs;
2527

2628
private static long sLastUpdate;
2729

28-
public static void FadeIn()
30+
public static float Alpha => sFadeAmt * 255f;
31+
32+
private static bool InformServer { get; set; }
33+
34+
public static void FadeIn(float durationMs, bool informServer = false)
2935
{
36+
sFadeDurationMs = durationMs;
3037
sCurrentAction = FadeType.In;
31-
sFadeAmt = 255f;
38+
sFadeAmt = 1f;
3239
sLastUpdate = Timing.Global.MillisecondsUtc;
40+
InformServer = informServer;
3341
}
3442

35-
public static void FadeOut()
43+
public static void FadeOut(float durationMs, bool informServer = false)
3644
{
45+
sFadeDurationMs = durationMs;
3746
sCurrentAction = FadeType.Out;
3847
sFadeAmt = 0f;
3948
sLastUpdate = Timing.Global.MillisecondsUtc;
49+
InformServer = informServer;
4050
}
4151

42-
public static void Cancel()
52+
public static void Cancel(bool informServer = false)
4353
{
4454
sCurrentAction = FadeType.None;
4555
sFadeAmt = default;
56+
InformServerOfCompletion(true);
4657
}
4758

4859
public static bool DoneFading()
4960
{
5061
return sCurrentAction == FadeType.None;
5162
}
5263

53-
public static float GetFade()
54-
{
55-
return sFadeAmt;
56-
}
57-
5864
public static void Update()
5965
{
6066
if (sCurrentAction == FadeType.In)
6167
{
62-
sFadeAmt -= (Timing.Global.MillisecondsUtc - sLastUpdate) / sFadeRate * 255f;
68+
sFadeAmt -= (Timing.Global.MillisecondsUtc - sLastUpdate) / sFadeDurationMs;
6369
if (sFadeAmt <= 0f)
6470
{
6571
sCurrentAction = FadeType.None;
6672
sFadeAmt = 0f;
73+
74+
InformServerOfCompletion();
6775
}
6876
}
6977
else if (sCurrentAction == FadeType.Out)
7078
{
71-
sFadeAmt += (Timing.Global.MillisecondsUtc - sLastUpdate) / sFadeRate * 255f;
72-
if (sFadeAmt >= 255f)
79+
sFadeAmt += (Timing.Global.MillisecondsUtc - sLastUpdate) / sFadeDurationMs;
80+
if (sFadeAmt >= 1)
7381
{
7482
sCurrentAction = FadeType.None;
75-
sFadeAmt = 255f;
83+
sFadeAmt = 1f;
84+
85+
InformServerOfCompletion();
7686
}
7787
}
7888

7989
sLastUpdate = Timing.Global.MillisecondsUtc;
8090
}
8191

92+
private static void InformServerOfCompletion(bool force = false)
93+
{
94+
if (InformServer || force)
95+
{
96+
InformServer = false;
97+
PacketSender.SendFadeCompletePacket();
98+
}
99+
}
82100
}
83101

84102
}

Intersect.Client/Core/Graphics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ public static void Render(TimeSpan deltaTime, TimeSpan totalTime)
599599

600600
DrawGameTexture(
601601
Renderer.GetWhiteTexture(), new FloatRect(0, 0, 1, 1), CurrentView,
602-
new Color((int)Fade.GetFade(), 0, 0, 0), null, GameBlendModes.None
602+
new Color((int)Fade.Alpha, 0, 0, 0), null, GameBlendModes.None
603603
);
604604

605605
// Draw our mousecursor at the very end, but not when taking screenshots.

Intersect.Client/Core/Input.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Intersect.Client.Interface.Game;
1010
using Intersect.Client.Maps;
1111
using Intersect.Client.Networking;
12+
using Intersect.Configuration;
1213
using Intersect.Utilities;
1314

1415
namespace Intersect.Client.Core
@@ -63,7 +64,7 @@ public static void OnKeyPressed(Keys modifier, Keys key)
6364
break;
6465
}
6566

66-
Fade.FadeIn();
67+
Fade.FadeIn(ClientConfiguration.Instance.FadeDurationMs);
6768
Globals.GameState = GameStates.Menu;
6869

6970
return;

Intersect.Client/Core/Main.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static void Start(IClientContext context)
3838

3939
//Init Network
4040
Networking.Network.InitNetwork(context);
41-
Fade.FadeIn();
41+
Fade.FadeIn(ClientConfiguration.Instance.FadeDurationMs);
4242

4343
//Make Json.Net Familiar with Our Object Types
4444
var id = Guid.NewGuid();
@@ -131,7 +131,7 @@ private static void ProcessIntro()
131131
else
132132
{
133133
Globals.IntroIndex++;
134-
Fade.FadeIn();
134+
Fade.FadeIn(ClientConfiguration.Instance.FadeDurationMs);
135135
Globals.IntroComing = true;
136136
}
137137
}
@@ -141,7 +141,7 @@ private static void ProcessIntro()
141141
if (Timing.Global.MillisecondsUtc > Globals.IntroStartTime + Globals.IntroDelay)
142142
{
143143
//If we have shown an image long enough, fade to black -- keep track that the image is going
144-
Fade.FadeOut();
144+
Fade.FadeOut(ClientConfiguration.Instance.FadeDurationMs);
145145
Globals.IntroStartTime = -1;
146146
Globals.IntroComing = false;
147147
}
@@ -187,7 +187,7 @@ private static void ProcessLoading()
187187

188188
Audio.PlayMusic(MapInstance.Get(Globals.Me.MapId).Music, ClientConfiguration.Instance.MusicFadeTimer, ClientConfiguration.Instance.MusicFadeTimer, true);
189189
Globals.GameState = GameStates.InGame;
190-
Fade.FadeIn();
190+
Fade.FadeIn(ClientConfiguration.Instance.FadeDurationMs);
191191
}
192192

193193
private static void ProcessGame()
@@ -339,7 +339,7 @@ public static void Logout(bool characterSelect, bool skipFade = false)
339339
}
340340
else
341341
{
342-
Fade.FadeOut();
342+
Fade.FadeOut(ClientConfiguration.Instance.FadeDurationMs);
343343
}
344344

345345
if (!ClientContext.IsSinglePlayer)
@@ -400,7 +400,7 @@ public static void Logout(bool characterSelect, bool skipFade = false)
400400
}
401401
else
402402
{
403-
Fade.FadeIn();
403+
Fade.FadeIn(ClientConfiguration.Instance.FadeDurationMs);
404404
}
405405
}
406406

Intersect.Client/Networking/PacketHandler.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ public void HandlePacket(IPacketSender packetSender, InputVariablePacket packet)
11541154
//ErrorMessagePacket
11551155
public void HandlePacket(IPacketSender packetSender, ErrorMessagePacket packet)
11561156
{
1157-
Fade.FadeIn();
1157+
Fade.FadeIn(ClientConfiguration.Instance.FadeDurationMs);
11581158
Globals.WaitingOnServer = false;
11591159
Interface.Interface.MsgboxErrors.Add(new KeyValuePair<string, string>(packet.Header, packet.Error));
11601160
Interface.Interface.MenuUi?.Reset();
@@ -2084,7 +2084,7 @@ public void HandlePacket(IPacketSender packetSender, TargetOverridePacket packet
20842084
public void HandlePacket(IPacketSender packetSender, EnteringGamePacket packet)
20852085
{
20862086
//Fade out, we're finally loading the game world!
2087-
Fade.FadeOut();
2087+
Fade.FadeOut(ClientConfiguration.Instance.FadeDurationMs);
20882088
}
20892089

20902090
//CancelCastPacket
@@ -2121,6 +2121,22 @@ public void HandlePacket(IPacketSender packetSender, GuildInvitePacket packet)
21212121
);
21222122
}
21232123

2124+
public void HandlePacket(IPacketSender packetSender, FadePacket packet)
2125+
{
2126+
switch (packet.FadeType)
2127+
{
2128+
case GameObjects.Events.FadeType.None:
2129+
Fade.Cancel(packet.WaitForCompletion);
2130+
break;
2131+
case GameObjects.Events.FadeType.FadeIn:
2132+
Fade.FadeIn(packet.DurationMs, packet.WaitForCompletion);
2133+
break;
2134+
case GameObjects.Events.FadeType.FadeOut:
2135+
Fade.FadeOut(packet.DurationMs, packet.WaitForCompletion);
2136+
break;
2137+
}
2138+
}
2139+
21242140
}
21252141

21262142
}

0 commit comments

Comments
 (0)